2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
19 extern cvar_t prvm_backtraceforwarnings;
21 extern dllhandle_t ode_dll;
24 // LadyHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
25 void VM_Warning(prvm_prog_t *prog, const char *fmt, ...)
28 char msg[MAX_INPUTLINE];
29 static double recursive = -1;
32 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
35 Con_Printf(CON_WARN "%s VM warning: %s", prog->name, msg);
37 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
38 if(prvm_backtraceforwarnings.integer && recursive != host.realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
40 recursive = host.realtime;
41 PRVM_PrintState(prog, 0);
47 //============================================================================
50 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
51 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
52 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LadyHavoc]
53 // TODO: will this war ever end? [2007-01-23 LadyHavoc]
55 void VM_CheckEmptyString(prvm_prog_t *prog, const char *s)
57 if (ISWHITESPACE(s[0]))
58 prog->error_cmd("%s: Bad string", prog->name);
61 qbool PRVM_ConsoleCommand (prvm_prog_t *prog, const char *text, int *func, qbool preserve_self, int curself, double ptime, qbool prog_loaded, const char *error_message)
63 int restorevm_tempstringsbuf_cursize;
64 int save_self = 0; // hush compiler warning
73 save_self = PRVM_gameglobaledict(self);
75 PRVM_gameglobalfloat(time) = ptime;
76 PRVM_gameglobaledict(self) = curself;
77 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
78 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, text);
79 prog->ExecuteProgram(prog, *func, error_message);
80 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
82 PRVM_gameglobaledict(self) = save_self;
83 r = (int) PRVM_G_FLOAT(OFS_RETURN) != 0;
89 void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
91 // self.frame is the interpolation target (new frame)
92 // self.frame1time is the animation base time for the interpolation target
93 // self.frame2 is the interpolation start (previous frame)
94 // self.frame2time is the animation base time for the interpolation start
95 // self.lerpfrac is the interpolation strength for self.frame2
96 // self.lerpfrac3 is the interpolation strength for self.frame3
97 // self.lerpfrac4 is the interpolation strength for self.frame4
98 // pitch angle on a player model where the animator set up 5 sets of
99 // animations and the csqc simply lerps between sets)
100 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
101 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
102 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
103 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
104 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
105 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
106 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
107 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
108 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
109 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
110 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
111 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
112 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
115 // LadyHavoc: quite tempting to break apart this function to reuse the
116 // duplicated code, but I suspect it is better for performance
118 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const model_t *model, double curtime)
120 int sub2, numframes, f, i, k;
121 int isfirstframegroup = true;
123 double sublerp, lerp, d;
124 const animscene_t *scene;
125 const framegroupblend_t *g;
126 frameblend_t *blend = frameblend;
128 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
130 // rpolzer: Not testing isanimated here - a model might have
131 // "animations" that move no vertices (but only bones), thus rendering
132 // may assume it's not animated while processing can't.
139 nolerp = ((model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer) || (model->nolerp == true);
140 numframes = model->numframes;
141 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
144 if ((unsigned int)f >= (unsigned int)numframes)
146 if (developer_extra.integer)
147 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
155 if (isfirstframegroup)
158 isfirstframegroup = false;
163 if (model->animscenes)
165 scene = model->animscenes + f;
166 f = scene->firstframe;
167 if (scene->framecount > 1)
169 // this code path is only used on .zym models and torches
170 sublerp = scene->framerate * (curtime - g->start);
171 f = (int) floor(sublerp);
174 if (sublerp < (1.0 / 65536.0f))
176 if (sublerp > (65535.0f / 65536.0f))
182 f = (f % scene->framecount);
183 sub2 = (sub2 % scene->framecount);
185 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
186 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
188 // two framelerps produced from one animation
191 for (i = 0;i < MAX_FRAMEBLENDS;i++)
193 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
195 blend[i].subframe = sub2;
201 d = (1 - sublerp) * lerp;
206 for (i = 0;i < MAX_FRAMEBLENDS;i++)
208 if (blend[i].lerp <= 0 || blend[i].subframe == f)
210 blend[i].subframe = f;
219 void VM_UpdateEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed, const model_t *edmodel, const frameblend_t *frameblend)
221 if (ed->priv.server->skeleton.model != edmodel)
223 VM_RemoveEdictSkeleton(prog, ed);
224 ed->priv.server->skeleton.model = edmodel;
226 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
228 if(ed->priv.server->skeleton.relativetransforms)
229 Mem_Free(ed->priv.server->skeleton.relativetransforms);
230 ed->priv.server->skeleton.relativetransforms = NULL;
235 int skeletonindex = -1;
236 skeleton_t *skeleton;
237 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
238 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
240 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
241 if (!ed->priv.server->skeleton.relativetransforms)
242 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
243 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
247 if(ed->priv.server->skeleton.relativetransforms)
248 Mem_Free(ed->priv.server->skeleton.relativetransforms);
249 ed->priv.server->skeleton.relativetransforms = NULL;
254 void VM_RemoveEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed)
256 if (ed->priv.server->skeleton.relativetransforms)
257 Mem_Free(ed->priv.server->skeleton.relativetransforms);
258 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
264 //============================================================================
267 void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength)
273 outend = out + outlength - 1;
274 for (i = first;i < prog->argc && out < outend;i++)
276 s = PRVM_G_STRING((OFS_PARM0+i*3));
277 while (out < outend && *s)
287 returns true if the extension is supported by the server
289 checkextension(extensionname)
293 // kind of helper function
294 static qbool checkextension(prvm_prog_t *prog, const char *name)
298 for (e = prog->extensionstring;*e;e++)
300 if(!strcasecmp(*e, name))
303 // special sheck for ODE
304 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
306 #ifndef LINK_TO_LIBODE
307 return ode_dll ? true : false;
309 #ifdef LINK_TO_LIBODE
318 // special sheck for d0_blind_id
319 if (!strcasecmp("DP_CRYPTO", name))
320 return Crypto_Available();
321 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
322 return Crypto_Available();
324 // special shreck for libcurl
325 if (!strcasecmp("DP_QC_URI_GET", name) || !strcasecmp("DP_QC_URI_POST", name))
326 return Curl_Available();
334 void VM_checkextension(prvm_prog_t *prog)
336 VM_SAFEPARMCOUNT(1,VM_checkextension);
338 PRVM_G_FLOAT(OFS_RETURN) = checkextension(prog, PRVM_G_STRING(OFS_PARM0));
345 This is a TERMINAL error, which will kill off the entire prog.
351 void VM_error(prvm_prog_t *prog)
354 char string[VM_STRINGTEMP_LENGTH];
356 VM_VarString(prog, 0, string, sizeof(string));
357 Con_Printf(CON_ERROR "======%s ERROR in %s:\n%s\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
358 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
359 PRVM_ED_Print(prog, ed, NULL);
361 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);
368 Dumps out self, then an error message. The program is aborted and self is
369 removed, but the level can continue.
374 void VM_objerror(prvm_prog_t *prog)
377 char string[VM_STRINGTEMP_LENGTH];
379 VM_VarString(prog, 0, string, sizeof(string));
380 Con_Printf(CON_ERROR "======OBJECT ERROR======\n"); // , prog->name, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
381 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
382 PRVM_ED_Print(prog, ed, NULL);
383 PRVM_ED_Free (prog, ed);
384 Con_Printf(CON_ERROR "%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
396 void VM_print(prvm_prog_t *prog)
398 char string[VM_STRINGTEMP_LENGTH];
400 VM_VarString(prog, 0, string, sizeof(string));
408 broadcast print to everyone on server
413 void VM_bprint(prvm_prog_t *prog)
415 char string[VM_STRINGTEMP_LENGTH];
419 VM_Warning(prog, "VM_bprint: game is not server(%s) !\n", prog->name);
423 VM_VarString(prog, 0, string, sizeof(string));
424 SV_BroadcastPrint(string);
429 VM_sprint (menu & client but only if server.active == true)
431 single print to a specific client
433 sprint(float clientnum,...[string])
436 void VM_sprint(prvm_prog_t *prog)
440 char string[VM_STRINGTEMP_LENGTH];
442 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
444 //find client for this entity
445 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
446 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
448 VM_Warning(prog, "VM_sprint: %s: invalid client or server is not active !\n", prog->name);
452 client = svs.clients + clientnum;
453 if (!client->netconnection)
456 VM_VarString(prog, 1, string, sizeof(string));
457 MSG_WriteChar(&client->netconnection->message,svc_print);
458 MSG_WriteString(&client->netconnection->message, string);
465 single print to the screen
470 void VM_centerprint(prvm_prog_t *prog)
472 char string[VM_STRINGTEMP_LENGTH];
474 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
475 VM_VarString(prog, 0, string, sizeof(string));
476 SCR_CenterPrint(string);
483 vector normalize(vector)
486 void VM_normalize(prvm_prog_t *prog)
492 VM_SAFEPARMCOUNT(1,VM_normalize);
494 value1 = PRVM_G_VECTOR(OFS_PARM0);
496 f = VectorLength2(value1);
500 VectorScale(value1, f, newvalue);
503 VectorClear(newvalue);
505 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
515 void VM_vlen(prvm_prog_t *prog)
517 VM_SAFEPARMCOUNT(1,VM_vlen);
518 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
525 float vectoyaw(vector)
528 void VM_vectoyaw(prvm_prog_t *prog)
533 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
535 value1 = PRVM_G_VECTOR(OFS_PARM0);
537 if (value1[1] == 0 && value1[0] == 0)
541 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
546 PRVM_G_FLOAT(OFS_RETURN) = yaw;
554 vector vectoangles(vector[, vector])
557 void VM_vectoangles(prvm_prog_t *prog)
559 vec3_t result, forward, up;
560 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
562 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), forward);
565 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), up);
566 AnglesFromVectors(result, forward, up, true);
569 AnglesFromVectors(result, forward, NULL, true);
570 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
577 Returns a random number > 0 and < 1
582 void VM_random(prvm_prog_t *prog)
584 VM_SAFEPARMCOUNT(0,VM_random);
586 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
593 localsound(string sample, float chan, float vol)
596 void VM_localsound(prvm_prog_t *prog)
601 VM_SAFEPARMCOUNTRANGE(1, 3,VM_localsound);
603 s = PRVM_G_STRING(OFS_PARM0);
606 chan = PRVM_G_FLOAT(OFS_PARM1);
607 vol = PRVM_G_FLOAT(OFS_PARM2) == 0 ? 1 : PRVM_G_FLOAT(OFS_PARM2);
608 if(!S_LocalSoundEx(s, chan, vol))
610 PRVM_G_FLOAT(OFS_RETURN) = -4;
611 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
615 else if(!S_LocalSound (s))
617 PRVM_G_FLOAT(OFS_RETURN) = -4;
618 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
622 PRVM_G_FLOAT(OFS_RETURN) = 1;
632 void VM_break(prvm_prog_t *prog)
634 prog->error_cmd("%s: break statement", prog->name);
637 //============================================================================
643 Sends text over to the client's execution buffer
645 [localcmd (string, ...) or]
649 void VM_localcmd_local(prvm_prog_t *prog)
651 char string[VM_STRINGTEMP_LENGTH];
652 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd_local);
653 VM_VarString(prog, 0, string, sizeof(string));
654 Cbuf_AddText(cmd_local, string);
661 Sends text over to the server's execution buffer
663 [localcmd (string, ...) or]
667 void VM_localcmd_server(prvm_prog_t *prog)
669 char string[VM_STRINGTEMP_LENGTH];
670 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd_server);
671 VM_VarString(prog, 0, string, sizeof(string));
672 Cbuf_AddText(cmd_local, string);
675 static qbool PRVM_Cvar_ReadOk(prvm_prog_t *prog, const char *string)
678 cvar = Cvar_FindVar(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask);
679 return ((cvar) && ((cvar->flags & CF_PRIVATE) == 0));
689 void VM_cvar(prvm_prog_t *prog)
691 char string[VM_STRINGTEMP_LENGTH];
692 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
693 VM_VarString(prog, 0, string, sizeof(string));
694 VM_CheckEmptyString(prog, string);
695 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(prog, string) ? Cvar_VariableValue(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask) : 0;
702 float cvar_type (string)
703 float CVAR_TYPEFLAG_EXISTS = 1;
704 float CVAR_TYPEFLAG_SAVED = 2;
705 float CVAR_TYPEFLAG_PRIVATE = 4;
706 float CVAR_TYPEFLAG_ENGINE = 8;
707 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
708 float CVAR_TYPEFLAG_READONLY = 32;
711 void VM_cvar_type(prvm_prog_t *prog)
713 char string[VM_STRINGTEMP_LENGTH];
717 VM_SAFEPARMCOUNTRANGE(1, 8, VM_cvar_type);
718 VM_VarString(prog, 0, string, sizeof(string));
719 VM_CheckEmptyString(prog, string);
720 cvar = Cvar_FindVar(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask);
725 PRVM_G_FLOAT(OFS_RETURN) = 0;
726 return; // CVAR_TYPE_NONE
729 ret = 1; // CVAR_EXISTS
730 if(cvar->flags & CF_ARCHIVE)
731 ret |= 2; // CVAR_TYPE_SAVED
732 if(cvar->flags & CF_PRIVATE)
733 ret |= 4; // CVAR_TYPE_PRIVATE
734 if(!(cvar->flags & CF_ALLOCATED))
735 ret |= 8; // CVAR_TYPE_ENGINE
736 if(cvar->description != cvar_dummy_description)
737 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
738 if(cvar->flags & CF_READONLY)
739 ret |= 32; // CVAR_TYPE_READONLY
741 PRVM_G_FLOAT(OFS_RETURN) = ret;
748 const string VM_cvar_string (string, ...)
751 void VM_cvar_string(prvm_prog_t *prog)
753 char string[VM_STRINGTEMP_LENGTH];
754 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
755 VM_VarString(prog, 0, string, sizeof(string));
756 VM_CheckEmptyString(prog, string);
757 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_Cvar_ReadOk(prog, string) ? Cvar_VariableString(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask) : "");
762 ========================
765 const string VM_cvar_defstring (string, ...)
766 ========================
768 void VM_cvar_defstring(prvm_prog_t *prog)
770 char string[VM_STRINGTEMP_LENGTH];
771 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
772 VM_VarString(prog, 0, string, sizeof(string));
773 VM_CheckEmptyString(prog, string);
774 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDefString(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask));
778 ========================
781 const string VM_cvar_description (string, ...)
782 ========================
784 void VM_cvar_description(prvm_prog_t *prog)
786 char string[VM_STRINGTEMP_LENGTH];
787 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
788 VM_VarString(prog, 0, string, sizeof(string));
789 VM_CheckEmptyString(prog, string);
790 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDescription(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask));
796 void cvar_set (string,string, ...)
799 void VM_cvar_set(prvm_prog_t *prog)
802 char string[VM_STRINGTEMP_LENGTH];
803 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
804 VM_VarString(prog, 1, string, sizeof(string));
805 name = PRVM_G_STRING(OFS_PARM0);
806 VM_CheckEmptyString(prog, name);
807 Cvar_Set(prog->console_cmd->cvars, name, string);
817 void VM_dprint(prvm_prog_t *prog)
819 char string[VM_STRINGTEMP_LENGTH];
820 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
821 VM_VarString(prog, 0, string, sizeof(string));
823 Con_DPrintf("%s", string);
825 Con_DPrintf("%s: %s", prog->name, string);
837 void VM_ftos(prvm_prog_t *prog)
842 VM_SAFEPARMCOUNT(1, VM_ftos);
844 v = PRVM_G_FLOAT(OFS_PARM0);
846 if ((prvm_vec_t)((prvm_int_t)v) == v)
847 dpsnprintf(s, sizeof(s), "%.0f", v);
849 dpsnprintf(s, sizeof(s), "%f", v);
850 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
861 void VM_fabs(prvm_prog_t *prog)
865 VM_SAFEPARMCOUNT(1,VM_fabs);
867 v = PRVM_G_FLOAT(OFS_PARM0);
868 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
879 void VM_vtos(prvm_prog_t *prog)
883 VM_SAFEPARMCOUNT(1,VM_vtos);
885 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]);
886 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
897 void VM_etos(prvm_prog_t *prog)
901 VM_SAFEPARMCOUNT(1, VM_etos);
903 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
904 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
911 float stof(...[string])
914 void VM_stof(prvm_prog_t *prog)
916 char string[VM_STRINGTEMP_LENGTH];
917 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
918 VM_VarString(prog, 0, string, sizeof(string));
919 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
923 ========================
927 ========================
929 void VM_itof(prvm_prog_t *prog)
931 VM_SAFEPARMCOUNT(1, VM_itof);
932 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
936 ========================
939 entity ftoe(float num)
940 ========================
942 void VM_ftoe(prvm_prog_t *prog)
945 VM_SAFEPARMCOUNT(1, VM_ftoe);
947 ent = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM0);
948 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->free)
949 ent = 0; // return world instead of a free or invalid entity
951 PRVM_G_INT(OFS_RETURN) = ent;
955 ========================
958 float etof(entity ent)
959 ========================
961 void VM_etof(prvm_prog_t *prog)
963 VM_SAFEPARMCOUNT(1, VM_etof);
964 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
971 string strftime(float uselocaltime, string[, string ...])
974 void VM_strftime(prvm_prog_t *prog)
983 char fmt[VM_STRINGTEMP_LENGTH];
984 char result[VM_STRINGTEMP_LENGTH];
985 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
986 VM_VarString(prog, 1, fmt, sizeof(fmt));
989 if (PRVM_G_FLOAT(OFS_PARM0))
990 tmresult = localtime_s(&tm, &t);
992 tmresult = gmtime_s(&tm, &t);
995 if (PRVM_G_FLOAT(OFS_PARM0))
1002 PRVM_G_INT(OFS_RETURN) = 0;
1005 #if _MSC_VER >= 1400
1006 strftime(result, sizeof(result), fmt, &tm);
1008 strftime(result, sizeof(result), fmt, tm);
1010 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, result);
1021 void VM_spawn(prvm_prog_t *prog)
1024 VM_SAFEPARMCOUNT(0, VM_spawn);
1025 prog->xfunction->builtinsprofile += 20;
1026 ed = PRVM_ED_Alloc(prog);
1027 VM_RETURN_EDICT(ed);
1038 void VM_remove(prvm_prog_t *prog)
1041 prog->xfunction->builtinsprofile += 20;
1043 VM_SAFEPARMCOUNT(1, VM_remove);
1045 ed = PRVM_G_EDICT(OFS_PARM0);
1046 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
1048 if (developer.integer > 0)
1049 VM_Warning(prog, "VM_remove: tried to remove the null entity or a reserved entity!\n" );
1053 if (developer.integer > 0)
1054 VM_Warning(prog, "VM_remove: tried to remove an already freed entity!\n" );
1057 PRVM_ED_Free (prog, ed);
1064 entity find(entity start, .string field, string match)
1068 void VM_find(prvm_prog_t *prog)
1075 VM_SAFEPARMCOUNT(3,VM_find);
1077 e = PRVM_G_EDICTNUM(OFS_PARM0);
1078 f = PRVM_G_INT(OFS_PARM1);
1079 s = PRVM_G_STRING(OFS_PARM2);
1081 // LadyHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1082 // expects it to find all the monsters, so we must be careful to support
1085 for (e++ ; e < prog->num_edicts ; e++)
1087 prog->xfunction->builtinsprofile++;
1088 ed = PRVM_EDICT_NUM(e);
1091 t = PRVM_E_STRING(ed,f);
1096 VM_RETURN_EDICT(ed);
1101 VM_RETURN_EDICT(prog->edicts);
1108 entity findfloat(entity start, .float field, float match)
1109 entity findentity(entity start, .entity field, entity match)
1112 // LadyHavoc: added this for searching float, int, and entity reference fields
1113 void VM_findfloat(prvm_prog_t *prog)
1120 VM_SAFEPARMCOUNT(3,VM_findfloat);
1122 e = PRVM_G_EDICTNUM(OFS_PARM0);
1123 f = PRVM_G_INT(OFS_PARM1);
1124 s = PRVM_G_FLOAT(OFS_PARM2);
1126 for (e++ ; e < prog->num_edicts ; e++)
1128 prog->xfunction->builtinsprofile++;
1129 ed = PRVM_EDICT_NUM(e);
1132 if (PRVM_E_FLOAT(ed,f) == s)
1134 VM_RETURN_EDICT(ed);
1139 VM_RETURN_EDICT(prog->edicts);
1146 entity findchain(.string field, string match)
1149 // chained search for strings in entity fields
1150 // entity(.string field, string match) findchain = #402;
1151 void VM_findchain(prvm_prog_t *prog)
1156 prvm_edict_t *ent, *chain;
1159 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1162 chainfield = PRVM_G_INT(OFS_PARM2);
1164 chainfield = prog->fieldoffsets.chain;
1166 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1168 chain = prog->edicts;
1170 f = PRVM_G_INT(OFS_PARM0);
1171 s = PRVM_G_STRING(OFS_PARM1);
1173 // LadyHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1174 // expects it to find all the monsters, so we must be careful to support
1177 ent = PRVM_NEXT_EDICT(prog->edicts);
1178 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1180 prog->xfunction->builtinsprofile++;
1183 t = PRVM_E_STRING(ent,f);
1189 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1193 VM_RETURN_EDICT(chain);
1200 entity findchainfloat(.string field, float match)
1201 entity findchainentity(.string field, entity match)
1204 // LadyHavoc: chained search for float, int, and entity reference fields
1205 // entity(.string field, float match) findchainfloat = #403;
1206 void VM_findchainfloat(prvm_prog_t *prog)
1211 prvm_edict_t *ent, *chain;
1214 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1217 chainfield = PRVM_G_INT(OFS_PARM2);
1219 chainfield = prog->fieldoffsets.chain;
1221 prog->error_cmd("VM_findchainfloat: %s doesnt have the specified chain field !", prog->name);
1223 chain = (prvm_edict_t *)prog->edicts;
1225 f = PRVM_G_INT(OFS_PARM0);
1226 s = PRVM_G_FLOAT(OFS_PARM1);
1228 ent = PRVM_NEXT_EDICT(prog->edicts);
1229 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1231 prog->xfunction->builtinsprofile++;
1234 if (PRVM_E_FLOAT(ent,f) != s)
1237 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1241 VM_RETURN_EDICT(chain);
1245 ========================
1248 entity findflags(entity start, .float field, float match)
1249 ========================
1251 // LadyHavoc: search for flags in float fields
1252 void VM_findflags(prvm_prog_t *prog)
1259 VM_SAFEPARMCOUNT(3, VM_findflags);
1262 e = PRVM_G_EDICTNUM(OFS_PARM0);
1263 f = PRVM_G_INT(OFS_PARM1);
1264 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM2);
1266 for (e++ ; e < prog->num_edicts ; e++)
1268 prog->xfunction->builtinsprofile++;
1269 ed = PRVM_EDICT_NUM(e);
1272 if (!PRVM_E_FLOAT(ed,f))
1274 if ((prvm_int_t)PRVM_E_FLOAT(ed,f) & s)
1276 VM_RETURN_EDICT(ed);
1281 VM_RETURN_EDICT(prog->edicts);
1285 ========================
1288 entity findchainflags(.float field, float match)
1289 ========================
1291 // LadyHavoc: chained search for flags in float fields
1292 void VM_findchainflags(prvm_prog_t *prog)
1297 prvm_edict_t *ent, *chain;
1300 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1303 chainfield = PRVM_G_INT(OFS_PARM2);
1305 chainfield = prog->fieldoffsets.chain;
1307 prog->error_cmd("VM_findchainflags: %s doesnt have the specified chain field !", prog->name);
1309 chain = (prvm_edict_t *)prog->edicts;
1311 f = PRVM_G_INT(OFS_PARM0);
1312 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
1314 ent = PRVM_NEXT_EDICT(prog->edicts);
1315 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1317 prog->xfunction->builtinsprofile++;
1320 if (!PRVM_E_FLOAT(ent,f))
1322 if (!((prvm_int_t)PRVM_E_FLOAT(ent,f) & s))
1325 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1329 VM_RETURN_EDICT(chain);
1336 string precache_sound (string sample)
1339 void VM_precache_sound(prvm_prog_t *prog)
1343 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1345 s = PRVM_G_STRING(OFS_PARM0);
1346 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1347 //VM_CheckEmptyString(prog, s);
1349 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1351 VM_Warning(prog, "VM_precache_sound: Failed to load %s for %s\n", s, prog->name);
1360 returns the same string as output
1362 does nothing, only used by qcc to build .pak archives
1365 void VM_precache_file(prvm_prog_t *prog)
1367 VM_SAFEPARMCOUNT(1,VM_precache_file);
1368 // precache_file is only used to copy files with qcc, it does nothing
1369 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1379 void VM_coredump(prvm_prog_t *prog)
1381 VM_SAFEPARMCOUNT(0,VM_coredump);
1383 Cbuf_AddText(cmd_local, "prvm_edicts ");
1384 Cbuf_AddText(cmd_local, prog->name);
1385 Cbuf_AddText(cmd_local, "\n");
1395 void VM_stackdump(prvm_prog_t *prog)
1397 VM_SAFEPARMCOUNT(0, VM_stackdump);
1399 PRVM_StackTrace(prog);
1410 void VM_crash(prvm_prog_t *prog)
1412 VM_SAFEPARMCOUNT(0, VM_crash);
1414 prog->error_cmd("Crash called by %s",prog->name);
1424 void VM_traceon(prvm_prog_t *prog)
1426 VM_SAFEPARMCOUNT(0,VM_traceon);
1438 void VM_traceoff(prvm_prog_t *prog)
1440 VM_SAFEPARMCOUNT(0,VM_traceoff);
1442 prog->trace = false;
1452 void VM_eprint(prvm_prog_t *prog)
1454 VM_SAFEPARMCOUNT(1,VM_eprint);
1456 PRVM_ED_PrintNum (prog, PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1466 void VM_rint(prvm_prog_t *prog)
1469 VM_SAFEPARMCOUNT(1,VM_rint);
1471 f = PRVM_G_FLOAT(OFS_PARM0);
1473 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1475 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1485 void VM_floor(prvm_prog_t *prog)
1487 VM_SAFEPARMCOUNT(1,VM_floor);
1489 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1499 void VM_ceil(prvm_prog_t *prog)
1501 VM_SAFEPARMCOUNT(1,VM_ceil);
1503 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1511 entity nextent(entity)
1514 void VM_nextent(prvm_prog_t *prog)
1519 VM_SAFEPARMCOUNT(1, VM_nextent);
1521 i = PRVM_G_EDICTNUM(OFS_PARM0);
1524 prog->xfunction->builtinsprofile++;
1526 if (i == prog->num_edicts)
1528 VM_RETURN_EDICT(prog->edicts);
1531 ent = PRVM_EDICT_NUM(i);
1534 VM_RETURN_EDICT(ent);
1540 //=============================================================================
1547 changelevel(string map)
1550 void VM_changelevel(prvm_prog_t *prog)
1553 VM_SAFEPARMCOUNT(1, VM_changelevel);
1557 VM_Warning(prog, "VM_changelevel: game is not server (%s)\n", prog->name);
1561 // make sure we don't issue two changelevels
1562 if (svs.changelevel_issued)
1564 svs.changelevel_issued = true;
1566 Cbuf_AddText(cmd_local, va(vabuf, sizeof(vabuf), "changelevel %s\n", PRVM_G_STRING(OFS_PARM0)));
1576 void VM_sin(prvm_prog_t *prog)
1578 VM_SAFEPARMCOUNT(1,VM_sin);
1579 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1588 void VM_cos(prvm_prog_t *prog)
1590 VM_SAFEPARMCOUNT(1,VM_cos);
1591 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1601 void VM_sqrt(prvm_prog_t *prog)
1603 VM_SAFEPARMCOUNT(1,VM_sqrt);
1604 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1614 void VM_asin(prvm_prog_t *prog)
1616 VM_SAFEPARMCOUNT(1,VM_asin);
1617 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1626 void VM_acos(prvm_prog_t *prog)
1628 VM_SAFEPARMCOUNT(1,VM_acos);
1629 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1638 void VM_atan(prvm_prog_t *prog)
1640 VM_SAFEPARMCOUNT(1,VM_atan);
1641 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1647 float atan2(float,float)
1650 void VM_atan2(prvm_prog_t *prog)
1652 VM_SAFEPARMCOUNT(2,VM_atan2);
1653 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1662 void VM_tan(prvm_prog_t *prog)
1664 VM_SAFEPARMCOUNT(1,VM_tan);
1665 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1672 Returns a vector of length < 1 and > 0
1677 void VM_randomvec(prvm_prog_t *prog)
1680 VM_SAFEPARMCOUNT(0, VM_randomvec);
1682 VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1685 //=============================================================================
1691 float registercvar (string name, string value[, float flags])
1694 void VM_registercvar(prvm_prog_t *prog)
1696 const char *name, *value;
1699 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1701 name = PRVM_G_STRING(OFS_PARM0);
1702 value = PRVM_G_STRING(OFS_PARM1);
1703 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1704 PRVM_G_FLOAT(OFS_RETURN) = 0;
1706 if(flags > CF_MAXFLAGSVAL)
1709 // first check to see if it has already been defined
1710 if (Cvar_FindVar (prog->console_cmd->cvars, name, prog->console_cmd->cvars_flagsmask))
1713 // check for overlap with a command
1714 if (Cmd_Exists(cmd_local, name))
1716 VM_Warning(prog, "VM_registercvar: %s is a command\n", name);
1720 Cvar_Get(prog->console_cmd->cvars, name, value, prog->console_cmd->cvars_flagsmask | flags, NULL);
1722 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1730 returns the minimum of two supplied floats
1732 float min(float a, float b, ...[float])
1735 void VM_min(prvm_prog_t *prog)
1737 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1738 // LadyHavoc: 3+ argument enhancement suggested by FrikaC
1739 if (prog->argc >= 3)
1742 float f = PRVM_G_FLOAT(OFS_PARM0);
1743 for (i = 1;i < prog->argc;i++)
1744 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1745 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1746 PRVM_G_FLOAT(OFS_RETURN) = f;
1749 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1756 returns the maximum of two supplied floats
1758 float max(float a, float b, ...[float])
1761 void VM_max(prvm_prog_t *prog)
1763 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1764 // LadyHavoc: 3+ argument enhancement suggested by FrikaC
1765 if (prog->argc >= 3)
1768 float f = PRVM_G_FLOAT(OFS_PARM0);
1769 for (i = 1;i < prog->argc;i++)
1770 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1771 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1772 PRVM_G_FLOAT(OFS_RETURN) = f;
1775 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1782 returns number bounded by supplied range
1784 float bound(float min, float value, float max)
1787 void VM_bound(prvm_prog_t *prog)
1789 VM_SAFEPARMCOUNT(3,VM_bound);
1790 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1797 returns a raised to power b
1799 float pow(float a, float b)
1802 void VM_pow(prvm_prog_t *prog)
1804 VM_SAFEPARMCOUNT(2,VM_pow);
1805 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1808 void VM_log(prvm_prog_t *prog)
1810 VM_SAFEPARMCOUNT(1,VM_log);
1811 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1814 void VM_Files_Init(prvm_prog_t *prog)
1817 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1818 prog->openfiles[i] = NULL;
1821 void VM_Files_CloseAll(prvm_prog_t *prog)
1824 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1826 if (prog->openfiles[i])
1827 FS_Close(prog->openfiles[i]);
1828 prog->openfiles[i] = NULL;
1832 static qfile_t *VM_GetFileHandle(prvm_prog_t *prog, int index)
1834 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1836 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, prog->name);
1839 if (prog->openfiles[index] == NULL)
1841 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, prog->name);
1844 return prog->openfiles[index];
1851 float fopen(string filename, float mode)
1854 // float(string filename, float mode) fopen = #110;
1855 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1856 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1857 void VM_fopen(prvm_prog_t *prog)
1860 const char *modestring, *filename;
1863 VM_SAFEPARMCOUNT(2,VM_fopen);
1865 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1866 if (prog->openfiles[filenum] == NULL)
1868 if (filenum >= PRVM_MAX_OPENFILES)
1870 PRVM_G_FLOAT(OFS_RETURN) = -2;
1871 VM_Warning(prog, "VM_fopen: %s ran out of file handles (%i)\n", prog->name, PRVM_MAX_OPENFILES);
1874 filename = PRVM_G_STRING(OFS_PARM0);
1875 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1878 case 0: // FILE_READ
1880 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
1881 if (prog->openfiles[filenum] == NULL)
1882 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
1884 case 1: // FILE_APPEND
1886 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1888 case 2: // FILE_WRITE
1890 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1893 PRVM_G_FLOAT(OFS_RETURN) = -3;
1894 VM_Warning(prog, "VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", prog->name, mode);
1898 if (prog->openfiles[filenum] == NULL)
1900 PRVM_G_FLOAT(OFS_RETURN) = -1;
1901 if (developer_extra.integer)
1902 VM_Warning(prog, "VM_fopen: %s: %s mode %s failed\n", prog->name, filename, modestring);
1906 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1907 if (developer_extra.integer)
1908 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", prog->name, filename, modestring, filenum);
1909 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin(prog);
1917 fclose(float fhandle)
1920 //void(float fhandle) fclose = #111; // closes a file
1921 void VM_fclose(prvm_prog_t *prog)
1925 VM_SAFEPARMCOUNT(1,VM_fclose);
1927 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1928 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1930 VM_Warning(prog, "VM_fclose: invalid file handle %i used in %s\n", filenum, prog->name);
1933 if (prog->openfiles[filenum] == NULL)
1935 VM_Warning(prog, "VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1938 FS_Close(prog->openfiles[filenum]);
1939 prog->openfiles[filenum] = NULL;
1940 if(prog->openfiles_origin[filenum])
1941 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1942 if (developer_extra.integer)
1943 Con_DPrintf("VM_fclose: %s: #%i closed\n", prog->name, filenum);
1950 string fgets(float fhandle)
1953 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1954 void VM_fgets(prvm_prog_t *prog)
1957 char string[VM_STRINGTEMP_LENGTH];
1960 VM_SAFEPARMCOUNT(1,VM_fgets);
1962 // set the return value regardless of any possible errors
1963 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1965 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1966 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1968 VM_Warning(prog, "VM_fgets: invalid file handle %i used in %s\n", filenum, prog->name);
1971 if (prog->openfiles[filenum] == NULL)
1973 VM_Warning(prog, "VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1979 c = FS_Getc(prog->openfiles[filenum]);
1980 if (c == '\r' || c == '\n' || c < 0)
1982 if (end < VM_STRINGTEMP_LENGTH - 1)
1986 // remove \n following \r
1989 c = FS_Getc(prog->openfiles[filenum]);
1991 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1993 if (developer_extra.integer)
1994 Con_DPrintf("fgets: %s: %s\n", prog->name, string);
1996 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2003 fputs(float fhandle, string s)
2006 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
2007 void VM_fputs(prvm_prog_t *prog)
2010 char string[VM_STRINGTEMP_LENGTH];
2013 VM_SAFEPARMCOUNT(2,VM_fputs);
2015 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
2016 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
2018 VM_Warning(prog, "VM_fputs: invalid file handle %i used in %s\n", filenum, prog->name);
2021 if (prog->openfiles[filenum] == NULL)
2023 VM_Warning(prog, "VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
2026 VM_VarString(prog, 1, string, sizeof(string));
2027 if ((stringlength = (int)strlen(string)))
2028 FS_Write(prog->openfiles[filenum], string, stringlength);
2029 if (developer_extra.integer)
2030 Con_DPrintf("fputs: %s: %s\n", prog->name, string);
2037 writetofile(float fhandle, entity ent)
2040 void VM_writetofile(prvm_prog_t *prog)
2045 VM_SAFEPARMCOUNT(2, VM_writetofile);
2047 file = VM_GetFileHandle(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
2050 VM_Warning(prog, "VM_writetofile: invalid or closed file handle\n");
2054 ent = PRVM_G_EDICT(OFS_PARM1);
2057 VM_Warning(prog, "VM_writetofile: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2061 PRVM_ED_Write (prog, file, ent);
2064 // KrimZon - DP_QC_ENTITYDATA
2069 float() numentityfields
2070 Return the number of entity fields - NOT offsets
2073 void VM_numentityfields(prvm_prog_t *prog)
2075 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2078 // KrimZon - DP_QC_ENTITYDATA
2083 string(float fieldnum) entityfieldname
2084 Return name of the specified field as a string, or empty if the field is invalid (warning)
2087 void VM_entityfieldname(prvm_prog_t *prog)
2090 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2092 if (i < 0 || i >= prog->numfielddefs)
2094 VM_Warning(prog, "VM_entityfieldname: %s: field index out of bounds\n", prog->name);
2095 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2099 d = &prog->fielddefs[i];
2100 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2103 // KrimZon - DP_QC_ENTITYDATA
2108 float(float fieldnum) entityfieldtype
2111 void VM_entityfieldtype(prvm_prog_t *prog)
2114 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2116 if (i < 0 || i >= prog->numfielddefs)
2118 VM_Warning(prog, "VM_entityfieldtype: %s: field index out of bounds\n", prog->name);
2119 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2123 d = &prog->fielddefs[i];
2124 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)d->type;
2127 // KrimZon - DP_QC_ENTITYDATA
2130 VM_getentityfieldstring
2132 string(float fieldnum, entity ent) getentityfieldstring
2135 void VM_getentityfieldstring(prvm_prog_t *prog)
2137 // put the data into a string
2142 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2143 char valuebuf[MAX_INPUTLINE];
2145 if (i < 0 || i >= prog->numfielddefs)
2147 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2148 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2152 d = &prog->fielddefs[i];
2155 ent = PRVM_G_EDICT(OFS_PARM1);
2158 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2159 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2162 val = (prvm_eval_t *)(ent->fields.fp + d->ofs);
2164 // if it's 0 or blank, return an empty string
2165 type = d->type & ~DEF_SAVEGLOBAL;
2166 for (j=0 ; j<prvm_type_size[type] ; j++)
2167 if (val->ivector[j])
2169 if (j == prvm_type_size[type])
2171 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2175 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_UglyValueString(prog, (etype_t)d->type, val, valuebuf, sizeof(valuebuf)));
2178 // KrimZon - DP_QC_ENTITYDATA
2181 VM_putentityfieldstring
2183 float(float fieldnum, entity ent, string s) putentityfieldstring
2186 void VM_putentityfieldstring(prvm_prog_t *prog)
2190 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2192 if (i < 0 || i >= prog->numfielddefs)
2194 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2195 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2199 d = &prog->fielddefs[i];
2202 ent = PRVM_G_EDICT(OFS_PARM1);
2205 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2206 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2210 // parse the string into the value
2211 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(prog, ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2218 float strlen(string s)
2221 //float(string s) strlen = #114; // returns how many characters are in a string
2222 void VM_strlen(prvm_prog_t *prog)
2224 VM_SAFEPARMCOUNT(1,VM_strlen);
2226 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2227 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2230 // DRESK - Decolorized String
2235 string strdecolorize(string s)
2238 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2239 void VM_strdecolorize(prvm_prog_t *prog)
2241 char szNewString[VM_STRINGTEMP_LENGTH];
2242 const char *szString;
2245 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2246 szString = PRVM_G_STRING(OFS_PARM0);
2247 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), true);
2248 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2251 // DRESK - String Length (not counting color codes)
2256 float strlennocol(string s)
2259 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2260 // For example, ^2Dresk returns a length of 5
2261 void VM_strlennocol(prvm_prog_t *prog)
2263 const char *szString;
2266 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2268 szString = PRVM_G_STRING(OFS_PARM0);
2270 //nCnt = (int)COM_StringLengthNoColors(szString, 0, NULL);
2271 nCnt = (int)u8_COM_StringLengthNoColors(szString, 0, NULL);
2273 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2276 // DRESK - String to Uppercase and Lowercase
2281 string strtolower(string s)
2284 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2285 void VM_strtolower(prvm_prog_t *prog)
2287 char szNewString[VM_STRINGTEMP_LENGTH];
2288 const char *szString;
2291 VM_SAFEPARMCOUNT(1,VM_strtolower);
2292 szString = PRVM_G_STRING(OFS_PARM0);
2294 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2296 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2303 string strtoupper(string s)
2306 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2307 void VM_strtoupper(prvm_prog_t *prog)
2309 char szNewString[VM_STRINGTEMP_LENGTH];
2310 const char *szString;
2313 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2314 szString = PRVM_G_STRING(OFS_PARM0);
2316 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2318 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2325 string strcat(string s, string...)
2328 //string(string s, string...) strcat = #115;
2329 // concatenates strings (for example "abc", "def" would return "abcdef")
2330 // and returns as a tempstring
2331 void VM_strcat(prvm_prog_t *prog)
2333 char s[VM_STRINGTEMP_LENGTH];
2334 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2336 VM_VarString(prog, 0, s, sizeof(s));
2337 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
2344 string substring(string s, float start, float length)
2347 // string(string s, float start, float length) substring = #116;
2348 // returns a section of a string as a tempstring
2349 void VM_substring(prvm_prog_t *prog)
2352 int u_slength = 0, u_start;
2355 char string[VM_STRINGTEMP_LENGTH];
2357 VM_SAFEPARMCOUNT(3,VM_substring);
2360 s = PRVM_G_STRING(OFS_PARM0);
2361 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2362 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2363 slength = strlen(s);
2365 if (start < 0) // FTE_STRINGS feature
2367 start = bound(0, start, slength);
2369 if (length < 0) // FTE_STRINGS feature
2370 length += slength - start + 1;
2371 maxlen = min((int)sizeof(string) - 1, slength - start);
2372 length = bound(0, length, maxlen);
2374 memcpy(string, s + start, length);
2376 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2379 s = PRVM_G_STRING(OFS_PARM0);
2380 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2381 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2383 if (start < 0) // FTE_STRINGS feature
2385 u_slength = (int)u8_strlen(s);
2387 start = bound(0, start, u_slength);
2390 if (length < 0) // FTE_STRINGS feature
2392 if (!u_slength) // it's not calculated when it's not needed above
2393 u_slength = (int)u8_strlen(s);
2394 length += u_slength - start + 1;
2397 // positive start, positive length
2398 u_start = u8_byteofs(s, start, NULL);
2401 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2404 u_length = u8_bytelen(s + u_start, length);
2405 if (u_length >= sizeof(string)-1)
2406 u_length = sizeof(string)-1;
2408 memcpy(string, s + u_start, u_length);
2409 string[u_length] = 0;
2410 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2417 string(string search, string replace, string subject) strreplace = #484;
2420 // replaces all occurrences of search with replace in the string subject, and returns the result
2421 void VM_strreplace(prvm_prog_t *prog)
2424 const char *search, *replace, *subject;
2425 char string[VM_STRINGTEMP_LENGTH];
2426 int search_len, replace_len, subject_len;
2428 VM_SAFEPARMCOUNT(3,VM_strreplace);
2430 search = PRVM_G_STRING(OFS_PARM0);
2431 replace = PRVM_G_STRING(OFS_PARM1);
2432 subject = PRVM_G_STRING(OFS_PARM2);
2434 search_len = (int)strlen(search);
2435 replace_len = (int)strlen(replace);
2436 subject_len = (int)strlen(subject);
2439 for (i = 0; i <= subject_len - search_len; i++)
2441 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2442 if (subject[i+j] != search[j])
2444 if (j == search_len)
2446 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2447 // found it at offset 'i'
2448 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2449 string[si++] = replace[j];
2452 i += search_len - 1;
2456 // the above would subtract 1 from i... so we
2457 // don't do that, but instead output the next
2459 if (si < (int)sizeof(string) - 1)
2460 string[si++] = subject[i];
2465 // in THIS case, we know search_len > 0, thus i < subject_len
2467 if (si < (int)sizeof(string) - 1)
2468 string[si++] = subject[i];
2471 // remaining chars (these cannot match)
2472 for (; i < subject_len; i++)
2473 if (si < (int)sizeof(string) - 1)
2474 string[si++] = subject[i];
2477 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2484 string(string search, string replace, string subject) strireplace = #485;
2487 // case-insensitive version of strreplace
2488 void VM_strireplace(prvm_prog_t *prog)
2491 const char *search, *replace, *subject;
2492 char string[VM_STRINGTEMP_LENGTH];
2493 int search_len, replace_len, subject_len;
2495 VM_SAFEPARMCOUNT(3, VM_strireplace);
2497 search = PRVM_G_STRING(OFS_PARM0);
2498 replace = PRVM_G_STRING(OFS_PARM1);
2499 subject = PRVM_G_STRING(OFS_PARM2);
2501 search_len = (int)strlen(search);
2502 replace_len = (int)strlen(replace);
2503 subject_len = (int)strlen(subject);
2506 for (i = 0; i <= subject_len - search_len; i++)
2508 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2509 if (tolower(subject[i+j]) != tolower(search[j]))
2511 if (j == search_len)
2513 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2514 // found it at offset 'i'
2515 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2516 string[si++] = replace[j];
2519 i += search_len - 1;
2523 // the above would subtract 1 from i... so we
2524 // don't do that, but instead output the next
2526 if (si < (int)sizeof(string) - 1)
2527 string[si++] = subject[i];
2532 // in THIS case, we know search_len > 0, thus i < subject_len
2534 if (si < (int)sizeof(string) - 1)
2535 string[si++] = subject[i];
2538 // remaining chars (these cannot match)
2539 for (; i < subject_len; i++)
2540 if (si < (int)sizeof(string) - 1)
2541 string[si++] = subject[i];
2544 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2551 vector stov(string s)
2554 //vector(string s) stov = #117; // returns vector value from a string
2555 void VM_stov(prvm_prog_t *prog)
2557 char string[VM_STRINGTEMP_LENGTH];
2559 VM_SAFEPARMCOUNT(1,VM_stov);
2561 VM_VarString(prog, 0, string, sizeof(string));
2562 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2569 string strzone(string s)
2572 //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)
2573 void VM_strzone(prvm_prog_t *prog)
2576 char string[VM_STRINGTEMP_LENGTH];
2579 VM_SAFEPARMCOUNT(1,VM_strzone);
2581 VM_VarString(prog, 0, string, sizeof(string));
2582 alloclen = strlen(string) + 1;
2583 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(prog, alloclen, &out);
2584 memcpy(out, string, alloclen);
2594 //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!!!)
2595 void VM_strunzone(prvm_prog_t *prog)
2597 VM_SAFEPARMCOUNT(1,VM_strunzone);
2598 PRVM_FreeString(prog, PRVM_G_INT(OFS_PARM0));
2605 float tokenize(string s)
2608 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2609 //this function originally written by KrimZon, made shorter by LadyHavoc
2610 //20040203: rewritten by LadyHavoc (no longer uses allocations)
2611 static int num_tokens = 0;
2612 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2613 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2614 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2615 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2616 void VM_tokenize (prvm_prog_t *prog)
2620 VM_SAFEPARMCOUNT(1,VM_tokenize);
2622 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2623 p = tokenize_string;
2628 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2631 // skip whitespace here to find token start pos
2632 while(*p && ISWHITESPACE(*p))
2635 tokens_startpos[num_tokens] = p - tokenize_string;
2636 if(!COM_ParseToken_VM_Tokenize(&p, false))
2638 tokens_endpos[num_tokens] = p - tokenize_string;
2639 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2643 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2646 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2647 void VM_tokenize_console (prvm_prog_t *prog)
2651 VM_SAFEPARMCOUNT(1, VM_tokenize_console);
2653 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2654 p = tokenize_string;
2659 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2662 // skip whitespace here to find token start pos
2663 while(*p && ISWHITESPACE(*p))
2666 tokens_startpos[num_tokens] = p - tokenize_string;
2667 if(!COM_ParseToken_Console(&p))
2669 tokens_endpos[num_tokens] = p - tokenize_string;
2670 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2674 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2679 VM_tokenizebyseparator
2681 float tokenizebyseparator(string s, string separator1, ...)
2684 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2685 //this function returns the token preceding each instance of a separator (of
2686 //which there can be multiple), and the text following the last separator
2687 //useful for parsing certain kinds of data like IP addresses
2689 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2690 //returns 4 and the tokens "10" "1" "2" "3".
2691 void VM_tokenizebyseparator (prvm_prog_t *prog)
2695 int separatorlen[7];
2696 const char *separators[7];
2699 char tokentext[MAX_INPUTLINE];
2701 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2703 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2704 p = tokenize_string;
2707 for (j = 1;j < prog->argc;j++)
2709 // skip any blank separator strings
2710 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2713 separators[numseparators] = s;
2714 separatorlen[numseparators] = (int)strlen(s);
2721 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2723 token = tokentext + j;
2724 tokens_startpos[num_tokens] = p - tokenize_string;
2728 for (k = 0;k < numseparators;k++)
2730 if (!strncmp(p, separators[k], separatorlen[k]))
2732 p += separatorlen[k];
2736 if (k < numseparators)
2738 if (j < (int)sizeof(tokentext)-1)
2739 tokentext[j++] = *p;
2743 tokens_endpos[num_tokens] = p0 - tokenize_string;
2744 if (j >= (int)sizeof(tokentext))
2747 tokens[num_tokens++] = PRVM_SetTempString(prog, token);
2752 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2755 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2756 //this function originally written by KrimZon, made shorter by LadyHavoc
2757 void VM_argv (prvm_prog_t *prog)
2761 VM_SAFEPARMCOUNT(1,VM_argv);
2763 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2766 token_num += num_tokens;
2768 if (token_num >= 0 && token_num < num_tokens)
2769 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2771 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2774 //float(float n) argv_start_index = #515; // returns the start index of a token
2775 void VM_argv_start_index (prvm_prog_t *prog)
2779 VM_SAFEPARMCOUNT(1,VM_argv);
2781 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2784 token_num += num_tokens;
2786 if (token_num >= 0 && token_num < num_tokens)
2787 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2789 PRVM_G_FLOAT(OFS_RETURN) = -1;
2792 //float(float n) argv_end_index = #516; // returns the end index of a token
2793 void VM_argv_end_index (prvm_prog_t *prog)
2797 VM_SAFEPARMCOUNT(1,VM_argv);
2799 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2802 token_num += num_tokens;
2804 if (token_num >= 0 && token_num < num_tokens)
2805 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2807 PRVM_G_FLOAT(OFS_RETURN) = -1;
2817 void VM_isserver(prvm_prog_t *prog)
2819 VM_SAFEPARMCOUNT(0, VM_isserver);
2821 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
2831 void VM_clientcount(prvm_prog_t *prog)
2833 VM_SAFEPARMCOUNT(0,VM_clientcount);
2835 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2845 void VM_clientstate(prvm_prog_t *prog)
2847 VM_SAFEPARMCOUNT(0,VM_clientstate);
2850 switch( cls.state ) {
2851 case ca_uninitialized:
2853 PRVM_G_FLOAT(OFS_RETURN) = 0;
2855 case ca_disconnected:
2856 PRVM_G_FLOAT(OFS_RETURN) = 1;
2859 PRVM_G_FLOAT(OFS_RETURN) = 2;
2862 // should never be reached!
2871 float getostype(prvm_prog_t *prog)
2873 */ // not used at the moment -> not included in the common list
2874 void VM_getostype(prvm_prog_t *prog)
2876 VM_SAFEPARMCOUNT(0,VM_getostype);
2881 OS_MAC - not supported
2885 PRVM_G_FLOAT(OFS_RETURN) = 0;
2886 #elif defined(MACOSX)
2887 PRVM_G_FLOAT(OFS_RETURN) = 2;
2889 PRVM_G_FLOAT(OFS_RETURN) = 1;
2897 float gettime(prvm_prog_t *prog)
2900 float CDAudio_GetPosition(void);
2901 void VM_gettime(prvm_prog_t *prog)
2905 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2909 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) host.realtime;
2913 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2916 case 0: // GETTIME_FRAMESTART
2917 PRVM_G_FLOAT(OFS_RETURN) = host.realtime;
2919 case 1: // GETTIME_REALTIME
2920 PRVM_G_FLOAT(OFS_RETURN) = Sys_DirtyTime();
2922 case 2: // GETTIME_HIRES
2923 PRVM_G_FLOAT(OFS_RETURN) = (Sys_DirtyTime() - host.dirtytime);
2925 case 3: // GETTIME_UPTIME
2926 PRVM_G_FLOAT(OFS_RETURN) = host.realtime;
2928 case 4: // GETTIME_CDTRACK
2929 PRVM_G_FLOAT(OFS_RETURN) = CDAudio_GetPosition();
2932 VM_Warning(prog, "VM_gettime: %s: unsupported timer specified, returning realtime\n", prog->name);
2933 PRVM_G_FLOAT(OFS_RETURN) = host.realtime;
2943 float getsoundtime(prvm_prog_t *prog)
2947 void VM_getsoundtime (prvm_prog_t *prog)
2949 int entnum, entchannel;
2950 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2952 if (prog == SVVM_prog)
2953 entnum = PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2954 else if (prog == CLVM_prog)
2955 entnum = MAX_EDICTS + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2958 VM_Warning(prog, "VM_getsoundtime: %s: not supported on this progs\n", prog->name);
2959 PRVM_G_FLOAT(OFS_RETURN) = -1;
2962 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2963 entchannel = CHAN_USER2ENGINE(entchannel);
2964 if (!IS_CHAN(entchannel))
2965 VM_Warning(prog, "VM_getsoundtime: %s: bad channel %i\n", prog->name, entchannel);
2966 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)S_GetEntChannelPosition(entnum, entchannel);
2973 string soundlength (string sample)
2976 void VM_soundlength (prvm_prog_t *prog)
2980 VM_SAFEPARMCOUNT(1, VM_soundlength);
2982 s = PRVM_G_STRING(OFS_PARM0);
2983 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2990 loadfromdata(string data)
2993 void VM_loadfromdata(prvm_prog_t *prog)
2995 VM_SAFEPARMCOUNT(1, VM_loadfromdata);
2997 PRVM_ED_LoadFromFile(prog, PRVM_G_STRING(OFS_PARM0));
3001 ========================
3004 parseentitydata(entity ent, string data)
3005 ========================
3007 void VM_parseentitydata(prvm_prog_t *prog)
3012 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
3014 // get edict and test it
3015 ent = PRVM_G_EDICT(OFS_PARM0);
3017 prog->error_cmd("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", prog->name, PRVM_NUM_FOR_EDICT(ent));
3019 data = PRVM_G_STRING(OFS_PARM1);
3021 // parse the opening brace
3022 if (!COM_ParseToken_Simple(&data, false, false, true) || com_token[0] != '{' )
3023 prog->error_cmd("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", prog->name, data );
3025 PRVM_ED_ParseEdict (prog, data, ent);
3032 loadfromfile(string file)
3035 void VM_loadfromfile(prvm_prog_t *prog)
3037 const char *filename;
3040 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3042 filename = PRVM_G_STRING(OFS_PARM0);
3043 if (FS_CheckNastyPath(filename, false))
3045 PRVM_G_FLOAT(OFS_RETURN) = -4;
3046 VM_Warning(prog, "VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", prog->name, filename);
3050 // not conform with VM_fopen
3051 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3053 PRVM_G_FLOAT(OFS_RETURN) = -1;
3055 PRVM_ED_LoadFromFile(prog, data);
3066 float mod(float val, float m)
3069 void VM_modulo(prvm_prog_t *prog)
3073 VM_SAFEPARMCOUNT(2, VM_modulo);
3075 val = PRVM_G_FLOAT(OFS_PARM0);
3076 m = PRVM_G_FLOAT(OFS_PARM1);
3078 // matches how gmqcc implements % when mod() builtin isn't defined, and FTEQW mod()
3080 PRVM_G_FLOAT(OFS_RETURN) = val - m * (prvm_int_t)(val / m);
3083 VM_Warning(prog, "Attempted modulo of %f by zero\n", val);
3084 PRVM_G_FLOAT(OFS_RETURN) = 0;
3088 static void VM_Search_Init(prvm_prog_t *prog)
3091 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3092 prog->opensearches[i] = NULL;
3095 static void VM_Search_Reset(prvm_prog_t *prog)
3098 // reset the fssearch list
3099 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3101 if(prog->opensearches[i])
3102 FS_FreeSearch(prog->opensearches[i]);
3103 prog->opensearches[i] = NULL;
3111 float search_begin(string pattern, float caseinsensitive, float quiet[, string packfile])
3114 void VM_search_begin(prvm_prog_t *prog)
3117 const char *packfile = NULL, *pattern;
3118 int caseinsens, quiet;
3120 VM_SAFEPARMCOUNTRANGE(3, 4, VM_search_begin);
3122 pattern = PRVM_G_STRING(OFS_PARM0);
3124 VM_CheckEmptyString(prog, pattern);
3126 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3127 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3129 // optional packfile parameter (DP_QC_FS_SEARCH_PACKFILE)
3131 packfile = PRVM_G_STRING(OFS_PARM3);
3133 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3134 if(!prog->opensearches[handle])
3137 if(handle >= PRVM_MAX_OPENSEARCHES)
3139 PRVM_G_FLOAT(OFS_RETURN) = -2;
3140 VM_Warning(prog, "VM_search_begin: %s ran out of search handles (%i)\n", prog->name, PRVM_MAX_OPENSEARCHES);
3144 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet, packfile)))
3145 PRVM_G_FLOAT(OFS_RETURN) = -1;
3148 prog->opensearches_origin[handle] = PRVM_AllocationOrigin(prog);
3149 PRVM_G_FLOAT(OFS_RETURN) = handle;
3157 void search_end(float handle)
3160 void VM_search_end(prvm_prog_t *prog)
3163 VM_SAFEPARMCOUNT(1, VM_search_end);
3165 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3167 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3169 VM_Warning(prog, "VM_search_end: invalid handle %i used in %s\n", handle, prog->name);
3172 if(prog->opensearches[handle] == NULL)
3174 VM_Warning(prog, "VM_search_end: no such handle %i in %s\n", handle, prog->name);
3178 FS_FreeSearch(prog->opensearches[handle]);
3179 prog->opensearches[handle] = NULL;
3180 if(prog->opensearches_origin[handle])
3181 PRVM_Free((char *)prog->opensearches_origin[handle]);
3188 float search_getsize(float handle)
3191 void VM_search_getsize(prvm_prog_t *prog)
3194 VM_SAFEPARMCOUNT(1, VM_search_getsize);
3196 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3198 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3200 VM_Warning(prog, "VM_search_getsize: invalid handle %i used in %s\n", handle, prog->name);
3203 if(prog->opensearches[handle] == NULL)
3205 VM_Warning(prog, "VM_search_getsize: no such handle %i in %s\n", handle, prog->name);
3209 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3214 VM_search_getfilename
3216 string search_getfilename(float handle, float num)
3219 void VM_search_getfilename(prvm_prog_t *prog)
3221 int handle, filenum;
3222 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3224 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3225 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3227 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3229 VM_Warning(prog, "VM_search_getfilename: invalid handle %i used in %s\n", handle, prog->name);
3232 if(prog->opensearches[handle] == NULL)
3234 VM_Warning(prog, "VM_search_getfilename: no such handle %i in %s\n", handle, prog->name);
3237 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3239 VM_Warning(prog, "VM_search_getfilename: invalid filenum %i in %s\n", filenum, prog->name);
3243 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, prog->opensearches[handle]->filenames[filenum]);
3250 string chr(float ascii)
3253 void VM_chr(prvm_prog_t *prog)
3257 VM_SAFEPARMCOUNT(1, VM_chr);
3259 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3262 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3267 VM_SAFEPARMCOUNT(1, VM_chr);
3269 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3271 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3278 string keynumtostring(float keynum)
3281 void VM_keynumtostring (prvm_prog_t *prog)
3283 char tinystr[TINYSTR_LEN];
3284 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3286 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, TINYSTR_LEN));
3291 VM_findkeysforcommand
3293 string findkeysforcommand(string command, float bindmap)
3295 the returned string is an altstring
3298 #define FKFC_NUMKEYS 5
3299 void M_FindKeysForCommand(const char *command, int *keys);
3300 void VM_findkeysforcommand(prvm_prog_t *prog)
3303 char ret[VM_STRINGTEMP_LENGTH];
3304 int keys[FKFC_NUMKEYS];
3309 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3311 cmd = PRVM_G_STRING(OFS_PARM0);
3313 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3315 bindmap = 0; // consistent to "bind"
3317 VM_CheckEmptyString(prog, cmd);
3319 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3322 for(i = 0; i < FKFC_NUMKEYS; i++)
3323 strlcat(ret, va(vabuf, sizeof(vabuf), " \'%i\'", keys[i]), sizeof(ret));
3325 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, ret);
3332 float stringtokeynum(string key)
3335 void VM_stringtokeynum (prvm_prog_t *prog)
3337 VM_SAFEPARMCOUNT( 1, VM_stringtokeynum );
3339 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3346 string getkeybind(float key, float bindmap)
3349 void VM_getkeybind (prvm_prog_t *prog)
3352 VM_SAFEPARMCOUNTRANGE(1, 2, VM_getkeybind);
3354 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3356 bindmap = 0; // consistent to "bind"
3358 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
3365 float setkeybind(float key, string cmd, float bindmap)
3368 void VM_setkeybind (prvm_prog_t *prog)
3371 VM_SAFEPARMCOUNTRANGE(2, 3, VM_setkeybind);
3373 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
3375 bindmap = 0; // consistent to "bind"
3377 PRVM_G_FLOAT(OFS_RETURN) = 0;
3378 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
3379 PRVM_G_FLOAT(OFS_RETURN) = 1;
3386 vector getbindmaps()
3389 void VM_getbindmaps (prvm_prog_t *prog)
3392 VM_SAFEPARMCOUNT(0, VM_getbindmaps);
3393 Key_GetBindMap(&fg, &bg);
3394 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
3395 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
3396 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3403 float setbindmaps(vector bindmap)
3406 void VM_setbindmaps (prvm_prog_t *prog)
3408 VM_SAFEPARMCOUNT(1, VM_setbindmaps);
3409 PRVM_G_FLOAT(OFS_RETURN) = 0;
3410 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
3411 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
3412 PRVM_G_FLOAT(OFS_RETURN) = 1;
3416 ========================
3419 float[bool] gecko_create( string name )
3420 ========================
3422 void VM_gecko_create(prvm_prog_t *prog) {
3424 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3428 ========================
3431 void gecko_destroy( string name )
3432 ========================
3434 void VM_gecko_destroy(prvm_prog_t *prog) {
3439 ========================
3442 void gecko_navigate( string name, string URI )
3443 ========================
3445 void VM_gecko_navigate(prvm_prog_t *prog) {
3450 ========================
3453 float[bool] gecko_keyevent( string name, float key, float eventtype )
3454 ========================
3456 void VM_gecko_keyevent(prvm_prog_t *prog) {
3458 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3462 ========================
3465 void gecko_mousemove( string name, float x, float y )
3466 ========================
3468 void VM_gecko_movemouse(prvm_prog_t *prog) {
3474 ========================
3477 void gecko_resize( string name, float w, float h )
3478 ========================
3480 void VM_gecko_resize(prvm_prog_t *prog) {
3486 ========================
3487 VM_gecko_get_texture_extent
3489 vector gecko_get_texture_extent( string name )
3490 ========================
3492 void VM_gecko_get_texture_extent(prvm_prog_t *prog) {
3494 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3495 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3504 Writes new values for v_forward, v_up, and v_right based on angles
3505 void makevectors(vector angle)
3508 void VM_makevectors (prvm_prog_t *prog)
3510 vec3_t angles, forward, right, up;
3511 VM_SAFEPARMCOUNT(1, VM_makevectors);
3512 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), angles);
3513 AngleVectors(angles, forward, right, up);
3514 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
3515 VectorCopy(right, PRVM_gameglobalvector(v_right));
3516 VectorCopy(up, PRVM_gameglobalvector(v_up));
3523 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3524 vectorvectors(vector)
3527 void VM_vectorvectors (prvm_prog_t *prog)
3529 vec3_t forward, right, up;
3530 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3531 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), forward);
3532 VectorVectors(forward, right, up);
3533 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
3534 VectorCopy(right, PRVM_gameglobalvector(v_right));
3535 VectorCopy(up, PRVM_gameglobalvector(v_up));
3538 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3539 void VM_bitshift (prvm_prog_t *prog)
3542 VM_SAFEPARMCOUNT(2, VM_bitshift);
3544 n1 = (prvm_int_t)fabs((prvm_vec_t)((prvm_int_t)PRVM_G_FLOAT(OFS_PARM0)));
3545 n2 = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
3547 PRVM_G_FLOAT(OFS_RETURN) = n1;
3550 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3552 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3555 ////////////////////////////////////////
3556 // AltString functions
3557 ////////////////////////////////////////
3560 ========================
3563 float altstr_count(string)
3564 ========================
3566 void VM_altstr_count(prvm_prog_t *prog)
3568 const char *altstr, *pos;
3571 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3573 altstr = PRVM_G_STRING( OFS_PARM0 );
3574 //VM_CheckEmptyString(prog, altstr );
3576 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3577 if( *pos == '\\' ) {
3581 } else if( *pos == '\'' ) {
3586 PRVM_G_FLOAT( OFS_RETURN ) = (prvm_vec_t) (count / 2);
3590 ========================
3593 string altstr_prepare(string)
3594 ========================
3596 void VM_altstr_prepare(prvm_prog_t *prog)
3598 const char *instr, *in;
3599 char outstr[VM_STRINGTEMP_LENGTH];
3602 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3604 instr = PRVM_G_STRING( OFS_PARM0 );
3606 for (in = instr, outpos = 0; *in && outpos < sizeof(outstr) - 1; ++in)
3608 if (*in == '\'' && outpos < sizeof(outstr) - 2)
3610 outstr[outpos++] = '\\';
3611 outstr[outpos++] = '\'';
3614 outstr[outpos++] = *in;
3618 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
3622 ========================
3625 string altstr_get(string, float)
3626 ========================
3628 void VM_altstr_get(prvm_prog_t *prog)
3630 const char *altstr, *pos;
3633 char outstr[VM_STRINGTEMP_LENGTH];
3635 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3637 altstr = PRVM_G_STRING( OFS_PARM0 );
3639 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3640 count = count * 2 + 1;
3642 for( pos = altstr ; *pos && count ; pos++ )
3643 if( *pos == '\\' ) {
3646 } else if( *pos == '\'' )
3650 PRVM_G_INT( OFS_RETURN ) = 0;
3654 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3655 if( *pos == '\\' ) {
3660 } else if( *pos == '\'' )
3666 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
3670 ========================
3673 string altstr_set(string altstr, float num, string set)
3674 ========================
3676 void VM_altstr_set(prvm_prog_t *prog)
3679 const char *altstr, *str;
3682 char outstr[VM_STRINGTEMP_LENGTH];
3684 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3686 altstr = PRVM_G_STRING( OFS_PARM0 );
3688 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3690 str = PRVM_G_STRING( OFS_PARM2 );
3693 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3698 } else if( *in == '\'' ) {
3703 for( ; *str; *out++ = *str++ );
3704 // now jump over the old content
3706 if( *in == '\'' || (*in == '\\' && !*++in) )
3709 strlcpy(out, in, outstr + sizeof(outstr) - out);
3710 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
3714 ========================
3717 string altstr_ins(string altstr, float num, string set)
3718 ========================
3720 void VM_altstr_ins(prvm_prog_t *prog)
3726 char outstr[VM_STRINGTEMP_LENGTH];
3728 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3730 in = PRVM_G_STRING( OFS_PARM0 );
3731 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3732 set = PRVM_G_STRING( OFS_PARM2 );
3735 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3740 } else if( *in == '\'' ) {
3745 for( ; *set ; *out++ = *set++ );
3748 strlcpy(out, in, outstr + sizeof(outstr) - out);
3749 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
3753 ////////////////////////////////////////
3754 // BufString functions
3755 ////////////////////////////////////////
3756 //[515]: string buffers support
3758 static size_t stringbuffers_sortlength;
3760 static void BufStr_Expand(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex)
3762 if (stringbuffer->max_strings <= strindex)
3764 char **oldstrings = stringbuffer->strings;
3765 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
3766 while (stringbuffer->max_strings <= strindex)
3767 stringbuffer->max_strings *= 2;
3768 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
3769 if (stringbuffer->num_strings > 0)
3770 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
3772 Mem_Free(oldstrings);
3776 static void BufStr_Shrink(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
3778 // reduce num_strings if there are empty string slots at the end
3779 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
3780 stringbuffer->num_strings--;
3782 // if empty, free the string pointer array
3783 if (stringbuffer->num_strings == 0)
3785 stringbuffer->max_strings = 0;
3786 if (stringbuffer->strings)
3787 Mem_Free(stringbuffer->strings);
3788 stringbuffer->strings = NULL;
3792 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3795 a = *((const char **) in1);
3796 b = *((const char **) in2);
3797 if(!a || !a[0]) return 1;
3798 if(!b || !b[0]) return -1;
3799 return strncmp(a, b, stringbuffers_sortlength);
3802 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3805 a = *((const char **) in1);
3806 b = *((const char **) in2);
3807 if(!a || !a[0]) return 1;
3808 if(!b || !b[0]) return -1;
3809 return strncmp(b, a, stringbuffers_sortlength);
3812 prvm_stringbuffer_t *BufStr_FindCreateReplace (prvm_prog_t *prog, int bufindex, int flags, const char *format)
3814 prvm_stringbuffer_t *stringbuffer;
3820 // find buffer with wanted index
3821 if (bufindex < (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray))
3823 if ( (stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, bufindex)) )
3825 if (stringbuffer->flags & STRINGBUFFER_TEMP)
3826 stringbuffer->flags = flags; // created but has not been used yet
3827 return stringbuffer;
3832 // allocate new buffer with wanted index
3835 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
3836 stringbuffer->flags = STRINGBUFFER_TEMP;
3837 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
3840 stringbuffer->flags = flags; // mark as used
3844 return stringbuffer;
3847 void BufStr_Set(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex, const char *str)
3851 if (!stringbuffer || strindex < 0)
3854 BufStr_Expand(prog, stringbuffer, strindex);
3855 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
3856 if (stringbuffer->strings[strindex])
3857 Mem_Free(stringbuffer->strings[strindex]);
3858 stringbuffer->strings[strindex] = NULL;
3862 // not the NULL string!
3863 alloclen = strlen(str) + 1;
3864 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
3865 memcpy(stringbuffer->strings[strindex], str, alloclen);
3868 BufStr_Shrink(prog, stringbuffer);
3871 void BufStr_Del(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
3878 for (i = 0;i < stringbuffer->num_strings;i++)
3879 if (stringbuffer->strings[i])
3880 Mem_Free(stringbuffer->strings[i]);
3881 if (stringbuffer->strings)
3882 Mem_Free(stringbuffer->strings);
3883 if(stringbuffer->origin)
3884 PRVM_Free((char *)stringbuffer->origin);
3885 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
3888 void BufStr_Flush(prvm_prog_t *prog)
3890 prvm_stringbuffer_t *stringbuffer;
3893 numbuffers = (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray);
3894 for (i = 0; i < numbuffers; i++)
3895 if ( (stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i)) )
3896 BufStr_Del(prog, stringbuffer);
3897 Mem_ExpandableArray_NewArray(&prog->stringbuffersarray, prog->progs_mempool, sizeof(prvm_stringbuffer_t), 64);
3901 ========================
3903 creates new buffer, and returns it's index, returns -1 if failed
3904 float buf_create(prvm_prog_t *prog) = #460;
3905 float newbuf(string format, float flags) = #460;
3906 ========================
3909 void VM_buf_create (prvm_prog_t *prog)
3911 prvm_stringbuffer_t *stringbuffer;
3914 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
3916 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
3917 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
3919 PRVM_G_FLOAT(OFS_RETURN) = -1;
3922 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
3923 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
3924 stringbuffer->origin = PRVM_AllocationOrigin(prog);
3925 // optional flags parm
3926 if (prog->argc >= 2)
3927 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & STRINGBUFFER_QCFLAGS;
3928 PRVM_G_FLOAT(OFS_RETURN) = i;
3934 ========================
3936 deletes buffer and all strings in it
3937 void buf_del(float bufhandle) = #461;
3938 ========================
3940 void VM_buf_del (prvm_prog_t *prog)
3942 prvm_stringbuffer_t *stringbuffer;
3943 VM_SAFEPARMCOUNT(1, VM_buf_del);
3944 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3946 BufStr_Del(prog, stringbuffer);
3949 VM_Warning(prog, "VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
3955 ========================
3957 how many strings are stored in buffer
3958 float buf_getsize(float bufhandle) = #462;
3959 ========================
3961 void VM_buf_getsize (prvm_prog_t *prog)
3963 prvm_stringbuffer_t *stringbuffer;
3964 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3966 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3969 PRVM_G_FLOAT(OFS_RETURN) = -1;
3970 VM_Warning(prog, "VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
3974 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
3978 ========================
3980 copy all content from one buffer to another, make sure it exists
3981 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3982 ========================
3984 void VM_buf_copy (prvm_prog_t *prog)
3986 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
3988 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3990 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3991 if(!srcstringbuffer)
3993 VM_Warning(prog, "VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
3996 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3997 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3999 VM_Warning(prog, "VM_buf_copy: source == destination (%i) in %s\n", i, prog->name);
4002 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4003 if(!dststringbuffer)
4005 VM_Warning(prog, "VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4009 for (i = 0;i < dststringbuffer->num_strings;i++)
4010 if (dststringbuffer->strings[i])
4011 Mem_Free(dststringbuffer->strings[i]);
4012 if (dststringbuffer->strings)
4013 Mem_Free(dststringbuffer->strings);
4014 *dststringbuffer = *srcstringbuffer;
4015 if (dststringbuffer->max_strings)
4016 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4018 for (i = 0;i < dststringbuffer->num_strings;i++)
4020 if (srcstringbuffer->strings[i])
4023 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4024 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4025 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4031 ========================
4033 sort buffer by beginnings of strings (cmplength defaults it's length)
4034 "backward == true" means that sorting goes upside-down
4035 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4036 ========================
4038 void VM_buf_sort (prvm_prog_t *prog)
4040 prvm_stringbuffer_t *stringbuffer;
4041 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4043 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4046 VM_Warning(prog, "VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4049 if(stringbuffer->num_strings <= 0)
4051 VM_Warning(prog, "VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4054 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4055 if(stringbuffers_sortlength <= 0)
4056 stringbuffers_sortlength = 0x7FFFFFFF;
4058 if(!PRVM_G_FLOAT(OFS_PARM2))
4059 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4061 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4063 BufStr_Shrink(prog, stringbuffer);
4067 ========================
4069 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4070 string buf_implode(float bufhandle, string glue) = #465;
4071 ========================
4073 void VM_buf_implode (prvm_prog_t *prog)
4075 prvm_stringbuffer_t *stringbuffer;
4076 char k[VM_STRINGTEMP_LENGTH];
4080 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4082 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4083 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4086 VM_Warning(prog, "VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4089 if(!stringbuffer->num_strings)
4091 sep = PRVM_G_STRING(OFS_PARM1);
4093 for(l = i = 0;i < stringbuffer->num_strings;i++)
4095 if(stringbuffer->strings[i])
4097 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4098 if (l >= sizeof(k) - 1)
4100 strlcat(k, sep, sizeof(k));
4101 strlcat(k, stringbuffer->strings[i], sizeof(k));
4104 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, k);
4108 ========================
4110 get a string from buffer, returns tempstring, dont str_unzone it!
4111 string bufstr_get(float bufhandle, float string_index) = #465;
4112 ========================
4114 void VM_bufstr_get (prvm_prog_t *prog)
4116 prvm_stringbuffer_t *stringbuffer;
4118 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4120 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4121 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4124 VM_Warning(prog, "VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4127 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4130 // VM_Warning(prog, "VM_bufstr_get: invalid string index %i used in %s\n", strindex, prog->name);
4133 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4134 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, stringbuffer->strings[strindex]);
4138 ========================
4140 copies a string into selected slot of buffer
4141 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4142 ========================
4144 void VM_bufstr_set (prvm_prog_t *prog)
4147 prvm_stringbuffer_t *stringbuffer;
4150 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4152 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4155 VM_Warning(prog, "VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4158 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4159 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4161 VM_Warning(prog, "VM_bufstr_set: invalid string index %i used in %s\n", strindex, prog->name);
4165 news = PRVM_G_STRING(OFS_PARM2);
4166 BufStr_Set(prog, stringbuffer, strindex, news);
4170 ========================
4172 adds string to buffer in first free slot and returns its index
4173 "order == true" means that string will be added after last "full" slot
4174 float bufstr_add(float bufhandle, string str, float order) = #467;
4175 ========================
4177 void VM_bufstr_add (prvm_prog_t *prog)
4179 int order, strindex;
4180 prvm_stringbuffer_t *stringbuffer;
4184 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4186 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4187 PRVM_G_FLOAT(OFS_RETURN) = -1;
4190 VM_Warning(prog, "VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4193 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
4195 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);
4198 string = PRVM_G_STRING(OFS_PARM1);
4199 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4201 strindex = stringbuffer->num_strings;
4203 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4204 if (stringbuffer->strings[strindex] == NULL)
4207 BufStr_Expand(prog, stringbuffer, strindex);
4209 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4210 alloclen = strlen(string) + 1;
4211 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4212 memcpy(stringbuffer->strings[strindex], string, alloclen);
4214 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4218 ========================
4220 delete string from buffer
4221 void bufstr_free(float bufhandle, float string_index) = #468;
4222 ========================
4224 void VM_bufstr_free (prvm_prog_t *prog)
4227 prvm_stringbuffer_t *stringbuffer;
4228 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4230 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4233 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4236 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4239 VM_Warning(prog, "VM_bufstr_free: invalid string index %i used in %s\n", i, prog->name);
4243 if (i < stringbuffer->num_strings)
4245 if(stringbuffer->strings[i])
4246 Mem_Free(stringbuffer->strings[i]);
4247 stringbuffer->strings[i] = NULL;
4250 BufStr_Shrink(prog, stringbuffer);
4254 ========================
4256 load a file into string buffer, return 0 or 1
4257 float buf_loadfile(string filename, float bufhandle) = #535;
4258 ========================
4260 void VM_buf_loadfile(prvm_prog_t *prog)
4263 prvm_stringbuffer_t *stringbuffer;
4264 char string[VM_STRINGTEMP_LENGTH];
4265 int strindex, c, end;
4266 const char *filename;
4270 VM_SAFEPARMCOUNT(2, VM_buf_loadfile);
4273 filename = PRVM_G_STRING(OFS_PARM0);
4274 file = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
4276 file = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
4279 if (developer_extra.integer)
4280 VM_Warning(prog, "VM_buf_loadfile: failed to open file %s in %s\n", filename, prog->name);
4281 PRVM_G_FLOAT(OFS_RETURN) = 0;
4285 // get string buffer
4286 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
4289 VM_Warning(prog, "VM_buf_loadfile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4290 PRVM_G_FLOAT(OFS_RETURN) = 0;
4294 // read file (append to the end of buffer)
4295 strindex = stringbuffer->num_strings;
4303 if (c == '\r' || c == '\n' || c < 0)
4305 if (end < VM_STRINGTEMP_LENGTH - 1)
4309 // remove \n following \r
4314 FS_UnGetc(file, (unsigned char)c);
4319 BufStr_Expand(prog, stringbuffer, strindex);
4320 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4321 alloclen = strlen(string) + 1;
4322 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4323 memcpy(stringbuffer->strings[strindex], string, alloclen);
4324 strindex = stringbuffer->num_strings;
4332 PRVM_G_FLOAT(OFS_RETURN) = 1;
4336 ========================
4338 writes stringbuffer to a file, returns 0 or 1
4339 float buf_writefile(float filehandle, float bufhandle, [, float startpos, float numstrings]) = #468;
4340 ========================
4343 void VM_buf_writefile(prvm_prog_t *prog)
4345 int filenum, strindex, strnum, strlength;
4346 prvm_stringbuffer_t *stringbuffer;
4348 VM_SAFEPARMCOUNTRANGE(2, 4, VM_buf_writefile);
4351 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
4352 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
4354 VM_Warning(prog, "VM_buf_writefile: invalid file handle %i used in %s\n", filenum, prog->name);
4357 if (prog->openfiles[filenum] == NULL)
4359 VM_Warning(prog, "VM_buf_writefile: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
4363 // get string buffer
4364 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
4367 VM_Warning(prog, "VM_buf_writefile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4368 PRVM_G_FLOAT(OFS_RETURN) = 0;
4372 // get start and end parms
4375 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
4376 strnum = (int)PRVM_G_FLOAT(OFS_PARM3);
4378 else if (prog->argc > 2)
4380 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
4381 strnum = stringbuffer->num_strings - strindex;
4386 strnum = stringbuffer->num_strings;
4388 if (strindex < 0 || strindex >= stringbuffer->num_strings)
4390 VM_Warning(prog, "VM_buf_writefile: wrong start string index %i used in %s\n", strindex, prog->name);
4391 PRVM_G_FLOAT(OFS_RETURN) = 0;
4396 VM_Warning(prog, "VM_buf_writefile: wrong strings count %i used in %s\n", strnum, prog->name);
4397 PRVM_G_FLOAT(OFS_RETURN) = 0;
4402 while(strindex < stringbuffer->num_strings && strnum)
4404 if (stringbuffer->strings[strindex])
4406 if ((strlength = (int)strlen(stringbuffer->strings[strindex])))
4407 FS_Write(prog->openfiles[filenum], stringbuffer->strings[strindex], strlength);
4408 FS_Write(prog->openfiles[filenum], "\n", 1);
4414 PRVM_G_FLOAT(OFS_RETURN) = 1;
4417 #define MATCH_AUTO 0
4418 #define MATCH_WHOLE 1
4419 #define MATCH_LEFT 2
4420 #define MATCH_RIGHT 3
4421 #define MATCH_MIDDLE 4
4422 #define MATCH_PATTERN 5
4424 static const char *detect_match_rule(char *pattern, int *matchrule)
4429 patternlength = (int)strlen(pattern);
4430 ppos = strchr(pattern, '*');
4431 qpos = strchr(pattern, '?');
4435 *matchrule = MATCH_PATTERN;
4438 // has * - left, mid, right or pattern
4441 // starts with * - may be right/mid or pattern
4442 if ((ppos - pattern) == 0)
4444 ppos = strchr(pattern+1, '*');
4448 *matchrule = MATCH_RIGHT;
4452 if ((ppos - pattern) == patternlength)
4454 *matchrule = MATCH_MIDDLE;
4459 *matchrule = MATCH_PATTERN;
4462 // end with * - left
4463 if ((ppos - pattern) == patternlength)
4465 *matchrule = MATCH_LEFT;
4470 *matchrule = MATCH_PATTERN;
4473 // have no wildcards - whole string
4474 *matchrule = MATCH_WHOLE;
4478 // todo: support UTF8
4479 static qbool match_rule(const char *string, int max_string, const char *pattern, int patternlength, int rule)
4484 return !strncmp(string, pattern, max_string) ? true : false;
4486 return !strncmp(string, pattern, patternlength) ? true : false;
4489 mid = strstr(string, pattern);
4490 return mid && !*(mid+patternlength);
4493 return strstr(string, pattern) ? true : false;
4495 return matchpattern_with_separator(string, pattern, false, "", false) ? true : false;
4499 ========================
4501 find an index of bufstring matching rule
4502 float bufstr_find(float bufhandle, string match, float matchrule, float startpos, float step) = #468;
4503 ========================
4506 void VM_bufstr_find(prvm_prog_t *prog)
4508 prvm_stringbuffer_t *stringbuffer;
4509 char string[VM_STRINGTEMP_LENGTH];
4510 int matchrule, matchlen, i, step;
4513 VM_SAFEPARMCOUNTRANGE(3, 5, VM_bufstr_find);
4515 PRVM_G_FLOAT(OFS_RETURN) = -1;
4517 // get string buffer
4518 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4521 VM_Warning(prog, "VM_bufstr_find: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4526 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
4527 if (matchrule < 0 || matchrule > 5)
4529 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
4533 match = PRVM_G_STRING(OFS_PARM1);
4536 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
4537 match = detect_match_rule(string, &matchrule);
4539 matchlen = (int)strlen(match);
4542 i = (prog->argc > 3) ? (int)PRVM_G_FLOAT(OFS_PARM3) : 0;
4543 step = (prog->argc > 4) ? (int)PRVM_G_FLOAT(OFS_PARM4) : 1;
4544 while(i < stringbuffer->num_strings)
4546 if (stringbuffer->strings[i] && match_rule(stringbuffer->strings[i], VM_STRINGTEMP_LENGTH, match, matchlen, matchrule))
4548 PRVM_G_FLOAT(OFS_RETURN) = i;
4556 ========================
4558 float matchpattern(string s, string pattern, float matchrule, float startpos) = #468;
4559 ========================
4561 void VM_matchpattern(prvm_prog_t *prog)
4563 const char *s, *match;
4564 char string[VM_STRINGTEMP_LENGTH];
4567 VM_SAFEPARMCOUNTRANGE(2, 4, VM_matchpattern);
4569 s = PRVM_G_STRING(OFS_PARM0);
4572 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
4573 if (matchrule < 0 || matchrule > 5)
4575 VM_Warning(prog, "VM_matchpattern: invalid match rule %i in %s\n", matchrule, prog->name);
4579 match = PRVM_G_STRING(OFS_PARM1);
4582 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
4583 match = detect_match_rule(string, &matchrule);
4587 l = (int)strlen(match);
4589 s += max(0, min((unsigned int)PRVM_G_FLOAT(OFS_PARM3), strlen(s)-1));
4592 PRVM_G_FLOAT(OFS_RETURN) = match_rule(s, VM_STRINGTEMP_LENGTH, match, l, matchrule);
4596 ========================
4598 ========================
4601 void VM_buf_cvarlist(prvm_prog_t *prog)
4604 const char *partial, *antipartial;
4605 size_t len, antilen;
4607 qbool ispattern, antiispattern;
4609 prvm_stringbuffer_t *stringbuffer;
4610 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4612 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4615 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4619 partial = PRVM_G_STRING(OFS_PARM1);
4623 len = strlen(partial);
4626 antipartial = PRVM_G_STRING(OFS_PARM2);
4632 antilen = strlen(antipartial);
4634 for (n = 0;n < stringbuffer->num_strings;n++)
4635 if (stringbuffer->strings[n])
4636 Mem_Free(stringbuffer->strings[n]);
4637 if (stringbuffer->strings)
4638 Mem_Free(stringbuffer->strings);
4639 stringbuffer->strings = NULL;
4641 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4642 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4645 for(cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
4647 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4650 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4656 stringbuffer->max_strings = stringbuffer->num_strings = n;
4657 if (stringbuffer->max_strings)
4658 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4661 for(cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
4663 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4666 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4669 alloclen = strlen(cvar->name) + 1;
4670 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4671 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4686 This was a major timewaster in progs, so it was converted to C
4689 void VM_changeyaw (prvm_prog_t *prog)
4692 float ideal, current, move, speed;
4694 // this is called (VERY HACKISHLY) by VM_SV_MoveToGoal, so it can not use any
4695 // parameters because they are the parameters to VM_SV_MoveToGoal, not this
4696 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4698 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
4699 if (ent == prog->edicts)
4701 VM_Warning(prog, "changeyaw: can not modify world entity\n");
4706 VM_Warning(prog, "changeyaw: can not modify free entity\n");
4709 current = PRVM_gameedictvector(ent, angles)[1];
4710 current = ANGLEMOD(current);
4711 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
4712 speed = PRVM_gameedictfloat(ent, yaw_speed);
4714 if (current == ideal)
4716 move = ideal - current;
4717 if (ideal > current)
4739 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
4747 void VM_changepitch (prvm_prog_t *prog)
4750 float ideal, current, move, speed;
4752 VM_SAFEPARMCOUNT(1, VM_changepitch);
4754 ent = PRVM_G_EDICT(OFS_PARM0);
4755 if (ent == prog->edicts)
4757 VM_Warning(prog, "changepitch: can not modify world entity\n");
4762 VM_Warning(prog, "changepitch: can not modify free entity\n");
4765 current = PRVM_gameedictvector(ent, angles)[0];
4766 current = ANGLEMOD(current);
4767 ideal = PRVM_gameedictfloat(ent, idealpitch);
4768 speed = PRVM_gameedictfloat(ent, pitch_speed);
4770 if (current == ideal)
4772 move = ideal - current;
4773 if (ideal > current)
4795 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
4799 void VM_uncolorstring (prvm_prog_t *prog)
4801 char szNewString[VM_STRINGTEMP_LENGTH];
4802 const char *szString;
4805 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4806 szString = PRVM_G_STRING(OFS_PARM0);
4807 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), true);
4808 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
4812 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4813 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4814 void VM_strstrofs (prvm_prog_t *prog)
4816 const char *instr, *match;
4818 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4819 instr = PRVM_G_STRING(OFS_PARM0);
4820 match = PRVM_G_STRING(OFS_PARM1);
4821 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
4822 firstofs = (int)u8_bytelen(instr, firstofs);
4824 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4826 PRVM_G_FLOAT(OFS_RETURN) = -1;
4830 match = strstr(instr+firstofs, match);
4832 PRVM_G_FLOAT(OFS_RETURN) = -1;
4834 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
4837 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4838 void VM_str2chr (prvm_prog_t *prog)
4843 VM_SAFEPARMCOUNT(2, VM_str2chr);
4844 s = PRVM_G_STRING(OFS_PARM0);
4845 index = (int)u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
4847 if((unsigned)index < strlen(s))
4849 if (utf8_enable.integer)
4850 ch = u8_getchar_noendptr(s + index);
4852 ch = (unsigned char)s[index];
4853 PRVM_G_FLOAT(OFS_RETURN) = ch;
4856 PRVM_G_FLOAT(OFS_RETURN) = 0;
4859 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4860 void VM_chr2str (prvm_prog_t *prog)
4865 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4866 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4867 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4869 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
4874 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4875 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
4876 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
4878 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
4881 static int chrconv_number(int i, int base, int conv)
4906 static int chrconv_punct(int i, int base, int conv)
4924 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4926 //convert case and colour seperatly...
4943 baset = 128*((charnum&1) == (convt-5));
4959 return i + basec + baset;
4961 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4962 //bulk convert a string. change case or colouring.
4963 void VM_strconv (prvm_prog_t *prog)
4965 int ccase, redalpha, rednum, len, i;
4966 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4967 unsigned char *result = resbuf;
4969 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4971 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4972 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4973 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4974 VM_VarString(prog, 3, (char *) resbuf, sizeof(resbuf));
4975 len = (int)strlen((char *) resbuf);
4977 for (i = 0; i < len; i++, result++) //should this be done backwards?
4979 if (*result >= '0' && *result <= '9') //normal numbers...
4980 *result = chrconv_number(*result, '0', rednum);
4981 else if (*result >= '0'+128 && *result <= '9'+128)
4982 *result = chrconv_number(*result, '0'+128, rednum);
4983 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4984 *result = chrconv_number(*result, '0'+128-30, rednum);
4985 else if (*result >= '0'-30 && *result <= '9'-30)
4986 *result = chrconv_number(*result, '0'-30, rednum);
4988 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4989 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4990 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4991 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4992 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4993 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4994 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4995 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4997 else if ((*result & 127) < 16 || !redalpha) //special chars..
4999 else if (*result < 128)
5000 *result = chrconv_punct(*result, 0, redalpha);
5002 *result = chrconv_punct(*result, 128, redalpha);
5006 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, (char *) resbuf);
5009 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5010 void VM_strpad (prvm_prog_t *prog)
5012 char src[VM_STRINGTEMP_LENGTH];
5013 char destbuf[VM_STRINGTEMP_LENGTH];
5015 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5016 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5017 VM_VarString(prog, 1, src, sizeof(src));
5019 // note: < 0 = left padding, > 0 = right padding,
5020 // this is reverse logic of printf!
5021 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5023 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, destbuf);
5026 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5027 //uses qw style \key\value strings
5028 void VM_infoadd (prvm_prog_t *prog)
5030 const char *info, *key;
5031 char value[VM_STRINGTEMP_LENGTH];
5032 char temp[VM_STRINGTEMP_LENGTH];
5034 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5035 info = PRVM_G_STRING(OFS_PARM0);
5036 key = PRVM_G_STRING(OFS_PARM1);
5037 VM_VarString(prog, 2, value, sizeof(value));
5039 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5041 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5043 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, temp);
5046 // #227 string(string info, string key) infoget (FTE_STRINGS)
5047 //uses qw style \key\value strings
5048 void VM_infoget (prvm_prog_t *prog)
5052 char value[VM_STRINGTEMP_LENGTH];
5054 VM_SAFEPARMCOUNT(2, VM_infoget);
5055 info = PRVM_G_STRING(OFS_PARM0);
5056 key = PRVM_G_STRING(OFS_PARM1);
5058 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5060 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, value);
5063 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5064 // also float(string s1, string s2) strcmp (FRIK_FILE)
5065 void VM_strncmp (prvm_prog_t *prog)
5067 const char *s1, *s2;
5068 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5069 s1 = PRVM_G_STRING(OFS_PARM0);
5070 s2 = PRVM_G_STRING(OFS_PARM1);
5073 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5077 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5081 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5082 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5083 void VM_strncasecmp (prvm_prog_t *prog)
5085 const char *s1, *s2;
5086 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5087 s1 = PRVM_G_STRING(OFS_PARM0);
5088 s2 = PRVM_G_STRING(OFS_PARM1);
5091 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5095 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5099 // #494 float(float caseinsensitive, string s, ...) crc16
5100 void VM_crc16(prvm_prog_t *prog)
5103 char s[VM_STRINGTEMP_LENGTH];
5104 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5105 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5106 VM_VarString(prog, 1, s, sizeof(s));
5107 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5110 // #639 float(string digest, string data, ...) digest_hex
5111 void VM_digest_hex(prvm_prog_t *prog)
5119 char s[VM_STRINGTEMP_LENGTH];
5122 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5123 digest = PRVM_G_STRING(OFS_PARM0);
5126 VM_VarString(prog, 1, s, sizeof(s));
5127 len = (int)strlen(s);
5131 if(!strcmp(digest, "MD4"))
5134 mdfour((unsigned char *) out, (unsigned char *) s, len);
5136 else if(!strcmp(digest, "SHA256") && Crypto_Available())
5139 sha256((unsigned char *) out, (unsigned char *) s, len);
5141 // no warning needed on mismatch - we return string_null to QC
5146 static const char *hexmap = "0123456789abcdef";
5147 for(i = 0; i < outlen; ++i)
5149 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
5150 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
5153 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outhex);
5156 PRVM_G_INT(OFS_RETURN) = 0;
5159 void VM_wasfreed (prvm_prog_t *prog)
5161 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5162 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->free;
5165 void VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace)
5167 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
5168 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
5169 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
5170 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
5171 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
5172 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
5173 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
5174 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
5175 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5176 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
5177 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
5178 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
5179 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(prog, trace->hittexture->name) : 0;
5182 void VM_ClearTraceGlobals(prvm_prog_t *prog)
5184 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5185 PRVM_gameglobalfloat(trace_allsolid) = 0;
5186 PRVM_gameglobalfloat(trace_startsolid) = 0;
5187 PRVM_gameglobalfloat(trace_fraction) = 0;
5188 PRVM_gameglobalfloat(trace_inwater) = 0;
5189 PRVM_gameglobalfloat(trace_inopen) = 0;
5190 VectorClear(PRVM_gameglobalvector(trace_endpos));
5191 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
5192 PRVM_gameglobalfloat(trace_plane_dist) = 0;
5193 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
5194 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
5195 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
5196 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
5197 PRVM_gameglobalstring(trace_dphittexturename) = 0;
5202 void VM_Cmd_Init(prvm_prog_t *prog)
5204 // only init the stuff for the current prog
5205 VM_Files_Init(prog);
5206 VM_Search_Init(prog);
5209 static void animatemodel_reset(prvm_prog_t *prog);
5211 void VM_Cmd_Reset(prvm_prog_t *prog)
5213 CL_PurgeOwner( MENUOWNER );
5214 VM_Search_Reset(prog);
5215 VM_Files_CloseAll(prog);
5216 animatemodel_reset(prog);
5219 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5220 // does URI escaping on a string (replace evil stuff by %AB escapes)
5221 void VM_uri_escape (prvm_prog_t *prog)
5223 char src[VM_STRINGTEMP_LENGTH];
5224 char dest[VM_STRINGTEMP_LENGTH];
5226 static const char *hex = "0123456789ABCDEF";
5228 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5229 VM_VarString(prog, 0, src, sizeof(src));
5231 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5233 if((*p >= 'A' && *p <= 'Z')
5234 || (*p >= 'a' && *p <= 'z')
5235 || (*p >= '0' && *p <= '9')
5236 || (*p == '-') || (*p == '_') || (*p == '.')
5237 || (*p == '!') || (*p == '~')
5238 || (*p == '\'') || (*p == '(') || (*p == ')'))
5243 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5244 *q++ = hex[ *(unsigned char *)p & 0xF];
5249 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
5252 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5253 // does URI unescaping on a string (get back the evil stuff)
5254 void VM_uri_unescape (prvm_prog_t *prog)
5256 char src[VM_STRINGTEMP_LENGTH];
5257 char dest[VM_STRINGTEMP_LENGTH];
5261 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5262 VM_VarString(prog, 0, src, sizeof(src));
5264 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5268 if(p[1] >= '0' && p[1] <= '9')
5270 else if(p[1] >= 'a' && p[1] <= 'f')
5271 hi = p[1] - 'a' + 10;
5272 else if(p[1] >= 'A' && p[1] <= 'F')
5273 hi = p[1] - 'A' + 10;
5276 if(p[2] >= '0' && p[2] <= '9')
5278 else if(p[2] >= 'a' && p[2] <= 'f')
5279 lo = p[2] - 'a' + 10;
5280 else if(p[2] >= 'A' && p[2] <= 'F')
5281 lo = p[2] - 'A' + 10;
5284 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5285 *q++ = (char) (hi * 0x10 + lo);
5296 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
5299 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5300 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5301 void VM_whichpack (prvm_prog_t *prog)
5303 const char *fn, *pack;
5305 VM_SAFEPARMCOUNT(1, VM_whichpack);
5306 fn = PRVM_G_STRING(OFS_PARM0);
5307 pack = FS_WhichPack(fn);
5309 PRVM_G_INT(OFS_RETURN) = pack ? PRVM_SetTempString(prog, pack) : 0;
5317 char buffer[MAX_INPUTLINE];
5319 unsigned char *postdata; // free when uri_to_prog_t is freed
5321 char *sigdata; // free when uri_to_prog_t is freed
5326 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5329 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5331 prog = handle->prog;
5334 // curl reply came too late... so just drop it
5335 if(handle->postdata)
5336 Z_Free(handle->postdata);
5338 Z_Free(handle->sigdata);
5343 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
5345 if(length_received >= sizeof(handle->buffer))
5346 length_received = sizeof(handle->buffer) - 1;
5347 handle->buffer[length_received] = 0;
5349 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5350 PRVM_G_FLOAT(OFS_PARM1) = status;
5351 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(prog, handle->buffer);
5352 prog->ExecuteProgram(prog, PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
5355 if(handle->postdata)
5356 Z_Free(handle->postdata);
5358 Z_Free(handle->sigdata);
5362 // 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
5363 // 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
5364 void VM_uri_get (prvm_prog_t *prog)
5369 uri_to_prog_t *handle;
5370 const char *posttype = NULL;
5371 const char *postseparator = NULL;
5372 int poststringbuffer = -1;
5374 const char *query_string = NULL;
5377 if(!PRVM_allfunction(URI_Get_Callback))
5378 prog->error_cmd("uri_get called by %s without URI_Get_Callback defined", prog->name);
5380 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
5382 url = PRVM_G_STRING(OFS_PARM0);
5383 id = PRVM_G_FLOAT(OFS_PARM1);
5385 posttype = PRVM_G_STRING(OFS_PARM2);
5387 postseparator = PRVM_G_STRING(OFS_PARM3);
5389 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
5391 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
5392 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!
5394 query_string = strchr(url, '?');
5397 lq = query_string ? strlen(query_string) : 0;
5399 handle->prog = prog;
5400 handle->starttime = prog->starttime;
5402 if(postseparator && posttype && *posttype)
5404 size_t l = strlen(postseparator);
5405 if(poststringbuffer >= 0)
5410 prvm_stringbuffer_t *stringbuffer;
5411 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
5414 VM_Warning(prog, "uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5418 for(i = 0;i < stringbuffer->num_strings;i++)
5422 if(stringbuffer->strings[i])
5423 ltotal += strlen(stringbuffer->strings[i]);
5425 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
5426 handle->postlen = ltotal;
5428 for(i = 0;i < stringbuffer->num_strings;i++)
5432 memcpy(handle->postdata + ltotal, postseparator, l);
5435 if(stringbuffer->strings[i])
5437 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
5438 ltotal += strlen(stringbuffer->strings[i]);
5441 if(ltotal != handle->postlen)
5442 prog->error_cmd("%s: string buffer content size mismatch, possible overrun", prog->name);
5446 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
5447 handle->postlen = l;
5448 memcpy(handle->postdata, postseparator, l);
5450 handle->postdata[handle->postlen] = 0;
5452 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
5455 // POST: we sign postdata \0 query string
5457 handle->sigdata = (char *)Z_Malloc(8192);
5458 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5459 l = strlen(handle->sigdata);
5460 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
5463 Z_Free(handle->sigdata);
5464 handle->sigdata = NULL;
5467 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5470 Z_Free(handle->sigdata);
5471 handle->sigdata = NULL;
5474 handle->siglen = l + ll;
5475 handle->sigdata[handle->siglen] = 0;
5478 strlcpy(handle->posttype, posttype, sizeof(handle->posttype));
5479 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, handle->posttype, handle->postdata, handle->postlen, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5483 if(postkeyid >= 0 && query_string)
5485 // GET: we sign JUST the query string
5487 handle->sigdata = (char *)Z_Malloc(8192);
5488 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5489 l = strlen(handle->sigdata);
5490 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
5493 Z_Free(handle->sigdata);
5494 handle->sigdata = NULL;
5497 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5500 Z_Free(handle->sigdata);
5501 handle->sigdata = NULL;
5504 handle->siglen = l + ll;
5505 handle->sigdata[handle->siglen] = 0;
5508 handle->postdata = NULL;
5509 handle->postlen = 0;
5510 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, NULL, NULL, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5514 PRVM_G_INT(OFS_RETURN) = 1;
5518 if(handle->postdata)
5519 Z_Free(handle->postdata);
5521 Z_Free(handle->sigdata);
5523 PRVM_G_INT(OFS_RETURN) = 0;
5527 void VM_netaddress_resolve (prvm_prog_t *prog)
5530 char normalized[128];
5532 lhnetaddress_t addr;
5534 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5536 ip = PRVM_G_STRING(OFS_PARM0);
5539 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5541 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5542 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, normalized);
5544 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
5547 //string(prvm_prog_t *prog) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5548 void VM_CL_getextresponse (prvm_prog_t *prog)
5550 VM_SAFEPARMCOUNT(0,VM_argv);
5552 if (cl_net_extresponse_count <= 0)
5553 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5557 --cl_net_extresponse_count;
5558 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5559 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, cl_net_extresponse[first]);
5563 void VM_SV_getextresponse (prvm_prog_t *prog)
5565 VM_SAFEPARMCOUNT(0,VM_argv);
5567 if (sv_net_extresponse_count <= 0)
5568 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5572 --sv_net_extresponse_count;
5573 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5574 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, sv_net_extresponse[first]);
5578 // DP_QC_NUDGEOUTOFSOLID
5579 // float(entity ent) nudgeoutofsolid = #567;
5580 void VM_nudgeoutofsolid(prvm_prog_t *prog)
5584 VM_SAFEPARMCOUNTRANGE(1, 1, VM_nudgeoutofsolid);
5586 ent = PRVM_G_EDICT(OFS_PARM0);
5587 if (ent == prog->edicts)
5589 VM_Warning(prog, "nudgeoutofsolid: can not modify world entity\n");
5590 PRVM_G_FLOAT(OFS_RETURN) = 0;
5595 VM_Warning(prog, "nudgeoutofsolid: can not modify free entity\n");
5596 PRVM_G_FLOAT(OFS_RETURN) = 0;
5600 PRVM_G_FLOAT(OFS_RETURN) = PHYS_NudgeOutOfSolid(prog, ent);
5602 if (PRVM_G_FLOAT(OFS_RETURN) > 0)
5604 if (prog == SVVM_prog)
5606 else if (prog == CLVM_prog)
5609 Sys_Error("PHYS_NudgeOutOfSolid: cannot be called from %s VM\n", prog->name);
5615 Common functions between menu.dat and clsprogs
5619 //#349 float() isdemo
5620 void VM_CL_isdemo (prvm_prog_t *prog)
5622 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
5623 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
5626 //#355 float() videoplaying
5627 void VM_CL_videoplaying (prvm_prog_t *prog)
5629 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
5630 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
5637 callfunction(...,string function_name)
5641 void VM_callfunction(prvm_prog_t *prog)
5646 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
5648 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
5650 VM_CheckEmptyString(prog, s);
5652 func = PRVM_ED_FindFunction(prog, s);
5655 prog->error_cmd("VM_callfunction: function %s not found !", s);
5656 else if (func->first_statement < 0)
5658 // negative statements are built in functions
5659 int builtinnumber = -func->first_statement;
5660 prog->xfunction->builtinsprofile++;
5661 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
5662 prog->builtins[builtinnumber](prog);
5664 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
5666 else if(func - prog->functions > 0)
5669 prog->ExecuteProgram(prog, func - prog->functions,"");
5678 float isfunction(string function_name)
5681 void VM_isfunction(prvm_prog_t *prog)
5686 VM_SAFEPARMCOUNT(1, VM_isfunction);
5688 s = PRVM_G_STRING(OFS_PARM0);
5690 VM_CheckEmptyString(prog, s);
5692 func = PRVM_ED_FindFunction(prog, s);
5695 PRVM_G_FLOAT(OFS_RETURN) = false;
5697 PRVM_G_FLOAT(OFS_RETURN) = true;
5704 string sprintf(string format, ...)
5708 void VM_sprintf(prvm_prog_t *prog)
5711 char outbuf[MAX_INPUTLINE];
5712 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
5715 int width, precision, thisarg, flags;
5719 static prvm_int_t dummyivec[3] = {0, 0, 0};
5720 static prvm_vec_t dummyvec[3] = {0, 0, 0};
5723 #define PRINTF_ALTERNATE 1
5724 #define PRINTF_ZEROPAD 2
5725 #define PRINTF_LEFT 4
5726 #define PRINTF_SPACEPOSITIVE 8
5727 #define PRINTF_SIGNPOSITIVE 16
5731 s = PRVM_G_STRING(OFS_PARM0);
5733 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
5734 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
5735 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
5736 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((prvm_int_t*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
5737 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
5752 // complete directive format:
5761 // is number following?
5762 if(*s >= '0' && *s <= '9')
5764 width = strtol(s, &err, 10);
5767 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
5780 flags |= PRINTF_ZEROPAD;
5782 width = -1; // it was just a flag
5794 case '#': flags |= PRINTF_ALTERNATE; break;
5795 case '0': flags |= PRINTF_ZEROPAD; break;
5796 case '-': flags |= PRINTF_LEFT; break;
5797 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
5798 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
5808 if(*s >= '0' && *s <= '9')
5810 width = strtol(s, &err, 10);
5811 if(!err || *err != '$')
5813 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
5820 width = GETARG_FLOAT(width);
5823 flags |= PRINTF_LEFT;
5827 else if(*s >= '0' && *s <= '9')
5829 width = strtol(s, &err, 10);
5832 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
5838 flags |= PRINTF_LEFT;
5842 // otherwise width stays -1
5851 if(*s >= '0' && *s <= '9')
5853 precision = strtol(s, &err, 10);
5854 if(!err || *err != '$')
5856 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
5862 precision = argpos++;
5863 precision = GETARG_FLOAT(precision);
5865 else if(*s >= '0' && *s <= '9')
5867 precision = strtol(s, &err, 10);
5870 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
5877 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
5886 case 'h': isfloat = 1; break;
5887 case 'l': isfloat = 0; break;
5888 case 'L': isfloat = 0; break;
5899 // now s points to the final directive char and is no longer changed
5914 if(*s != 's' && *s != 'c')
5915 if(flags & PRINTF_ALTERNATE) *f++ = '#';
5916 if(flags & PRINTF_ZEROPAD) *f++ = '0';
5917 if(flags & PRINTF_LEFT) *f++ = '-';
5918 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
5919 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
5926 if(*s == 'd' || *s == 'i' || *s == 'o' || *s == 'u' || *s == 'x' || *s == 'X')
5928 // make it use a good integer type
5929 for(p = INT_LOSSLESS_FORMAT_SIZE; *p; )
5935 if(width < 0) // not set
5941 if(precision < 0) // not set
5942 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
5944 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
5946 case 'o': case 'u': case 'x': case 'X':
5947 if(precision < 0) // not set
5948 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
5950 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
5952 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
5953 if(precision < 0) // not set
5954 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
5956 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
5960 if(precision < 0) // not set
5961 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
5962 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
5963 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
5964 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
5967 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
5968 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
5969 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
5970 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
5974 if(flags & PRINTF_ALTERNATE)
5976 if(precision < 0) // not set
5977 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
5979 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
5983 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
5985 const char *buf = u8_encodech(c, NULL, charbuf16);
5988 if(precision < 0) // not set
5989 precision = end - o - 1;
5990 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
5993 //spike FIXME -- 'S' for quoted tokenize-safe-or-print escaping of strings so stuff can safely survive console commands.
5995 if(flags & PRINTF_ALTERNATE)
5997 if(precision < 0) // not set
5998 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6000 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6004 if(precision < 0) // not set
6005 precision = end - o - 1;
6006 if(flags & PRINTF_SIGNPOSITIVE)
6007 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6009 o += u8_strpad_colorcodes(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6013 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6029 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outbuf);
6035 static model_t *getmodel(prvm_prog_t *prog, prvm_edict_t *ed)
6037 if (prog == SVVM_prog)
6038 return SV_GetModelFromEdict(ed);
6039 else if (prog == CLVM_prog)
6040 return CL_GetModelFromEdict(ed);
6045 struct animatemodel_cache
6048 frameblend_t frameblend[MAX_FRAMEBLENDS];
6049 skeleton_t *skeleton_p;
6050 skeleton_t skeleton;
6051 float *data_vertex3f;
6052 float *data_svector3f;
6053 float *data_tvector3f;
6054 float *data_normal3f;
6056 float *buf_vertex3f;
6057 float *buf_svector3f;
6058 float *buf_tvector3f;
6059 float *buf_normal3f;
6062 static void animatemodel_reset(prvm_prog_t *prog)
6064 if (!prog->animatemodel_cache)
6066 if(prog->animatemodel_cache->buf_vertex3f) Mem_Free(prog->animatemodel_cache->buf_vertex3f);
6067 if(prog->animatemodel_cache->buf_svector3f) Mem_Free(prog->animatemodel_cache->buf_svector3f);
6068 if(prog->animatemodel_cache->buf_tvector3f) Mem_Free(prog->animatemodel_cache->buf_tvector3f);
6069 if(prog->animatemodel_cache->buf_normal3f) Mem_Free(prog->animatemodel_cache->buf_normal3f);
6070 Mem_Free(prog->animatemodel_cache);
6073 static void animatemodel(prvm_prog_t *prog, model_t *model, prvm_edict_t *ed)
6075 skeleton_t *skeleton;
6076 int skeletonindex = -1;
6078 struct animatemodel_cache *animatemodel_cache;
6079 if (!prog->animatemodel_cache)
6081 prog->animatemodel_cache = (struct animatemodel_cache *)Mem_Alloc(prog->progs_mempool, sizeof(struct animatemodel_cache));
6082 memset(prog->animatemodel_cache, 0, sizeof(struct animatemodel_cache));
6084 animatemodel_cache = prog->animatemodel_cache;
6085 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6087 animatemodel_cache->data_vertex3f = model->surfmesh.data_vertex3f;
6088 animatemodel_cache->data_svector3f = model->surfmesh.data_svector3f;
6089 animatemodel_cache->data_tvector3f = model->surfmesh.data_tvector3f;
6090 animatemodel_cache->data_normal3f = model->surfmesh.data_normal3f;
6093 need |= (animatemodel_cache->model != model);
6094 VM_GenerateFrameGroupBlend(prog, ed->priv.server->framegroupblend, ed);
6095 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model, PRVM_serverglobalfloat(time));
6096 need |= (memcmp(&animatemodel_cache->frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6097 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6098 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6100 need |= (animatemodel_cache->skeleton_p != skeleton);
6102 need |= (memcmp(&animatemodel_cache->skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6105 if(model->surfmesh.num_vertices > animatemodel_cache->max_vertices)
6107 animatemodel_cache->max_vertices = model->surfmesh.num_vertices * 2;
6108 if(animatemodel_cache->buf_vertex3f) Mem_Free(animatemodel_cache->buf_vertex3f);
6109 if(animatemodel_cache->buf_svector3f) Mem_Free(animatemodel_cache->buf_svector3f);
6110 if(animatemodel_cache->buf_tvector3f) Mem_Free(animatemodel_cache->buf_tvector3f);
6111 if(animatemodel_cache->buf_normal3f) Mem_Free(animatemodel_cache->buf_normal3f);
6112 animatemodel_cache->buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6113 animatemodel_cache->buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6114 animatemodel_cache->buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6115 animatemodel_cache->buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6117 animatemodel_cache->data_vertex3f = animatemodel_cache->buf_vertex3f;
6118 animatemodel_cache->data_svector3f = animatemodel_cache->buf_svector3f;
6119 animatemodel_cache->data_tvector3f = animatemodel_cache->buf_tvector3f;
6120 animatemodel_cache->data_normal3f = animatemodel_cache->buf_normal3f;
6121 VM_UpdateEdictSkeleton(prog, ed, model, ed->priv.server->frameblend);
6122 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);
6123 animatemodel_cache->model = model;
6124 memcpy(&animatemodel_cache->frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6125 animatemodel_cache->skeleton_p = skeleton;
6127 memcpy(&animatemodel_cache->skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6130 static void getmatrix(prvm_prog_t *prog, prvm_edict_t *ed, matrix4x4_t *out)
6132 if (prog == SVVM_prog)
6133 SV_GetEntityMatrix(prog, ed, out, false);
6134 else if (prog == CLVM_prog)
6135 CL_GetEntityMatrix(prog, ed, out, false);
6137 *out = identitymatrix;
6140 static void applytransform_forward(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6143 getmatrix(prog, ed, &m);
6144 Matrix4x4_Transform(&m, in, out);
6147 static void applytransform_forward_direction(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6150 getmatrix(prog, ed, &m);
6151 Matrix4x4_Transform3x3(&m, in, out);
6154 static void applytransform_inverted(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6157 getmatrix(prog, ed, &m);
6158 Matrix4x4_Invert_Full(&n, &m);
6159 Matrix4x4_Transform3x3(&n, in, out);
6162 static void applytransform_forward_normal(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6166 getmatrix(prog, ed, &m);
6167 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6171 static void clippointtosurface(prvm_prog_t *prog, prvm_edict_t *ed, model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6174 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6176 animatemodel(prog, model, ed);
6177 bestdist = 1000000000;
6179 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6181 // clip original point to each triangle of the surface and find the
6182 // triangle that is closest
6183 v[0] = prog->animatemodel_cache->data_vertex3f + e[0] * 3;
6184 v[1] = prog->animatemodel_cache->data_vertex3f + e[1] * 3;
6185 v[2] = prog->animatemodel_cache->data_vertex3f + e[2] * 3;
6186 TriangleNormal(v[0], v[1], v[2], facenormal);
6187 VectorNormalize(facenormal);
6188 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6189 VectorMA(p, offsetdist, facenormal, temp);
6190 for (j = 0, k = 2;j < 3;k = j, j++)
6192 VectorSubtract(v[k], v[j], edgenormal);
6193 CrossProduct(edgenormal, facenormal, sidenormal);
6194 VectorNormalize(sidenormal);
6195 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6197 VectorMA(temp, offsetdist, sidenormal, temp);
6199 dist = VectorDistance2(temp, p);
6200 if (bestdist > dist)
6203 VectorCopy(temp, out);
6208 static msurface_t *getsurface(model_t *model, int surfacenum)
6210 if (surfacenum < 0 || surfacenum >= model->submodelsurfaces_end - model->submodelsurfaces_start)
6212 return model->data_surfaces + surfacenum + model->submodelsurfaces_start;
6216 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6217 void VM_getsurfacenumpoints(prvm_prog_t *prog)
6220 msurface_t *surface;
6221 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6222 // return 0 if no such surface
6223 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6225 PRVM_G_FLOAT(OFS_RETURN) = 0;
6229 // note: this (incorrectly) assumes it is a simple polygon
6230 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6232 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6233 void VM_getsurfacepoint(prvm_prog_t *prog)
6237 msurface_t *surface;
6240 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6241 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6242 ed = PRVM_G_EDICT(OFS_PARM0);
6243 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6245 // note: this (incorrectly) assumes it is a simple polygon
6246 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6247 if (pointnum < 0 || pointnum >= surface->num_vertices)
6249 animatemodel(prog, model, ed);
6250 applytransform_forward(prog, &(prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6251 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6253 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6254 // float SPA_POSITION = 0;
6255 // float SPA_S_AXIS = 1;
6256 // float SPA_T_AXIS = 2;
6257 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6258 // float SPA_TEXCOORDS0 = 4;
6259 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6260 // float SPA_LIGHTMAP0_COLOR = 6;
6261 void VM_getsurfacepointattribute(prvm_prog_t *prog)
6265 msurface_t *surface;
6270 VM_SAFEPARMCOUNT(4, VM_getsurfacepointattribute);
6271 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6272 ed = PRVM_G_EDICT(OFS_PARM0);
6273 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6275 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6276 if (pointnum < 0 || pointnum >= surface->num_vertices)
6278 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6280 animatemodel(prog, model, ed);
6282 switch( attributetype ) {
6283 // float SPA_POSITION = 0;
6285 applytransform_forward(prog, &(prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6286 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6288 // float SPA_S_AXIS = 1;
6290 applytransform_forward_direction(prog, &(prog->animatemodel_cache->data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6291 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6293 // float SPA_T_AXIS = 2;
6295 applytransform_forward_direction(prog, &(prog->animatemodel_cache->data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6296 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6298 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6300 applytransform_forward_direction(prog, &(prog->animatemodel_cache->data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6301 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6303 // float SPA_TEXCOORDS0 = 4;
6305 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6306 result[0] = texcoord[0];
6307 result[1] = texcoord[1];
6309 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6312 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6314 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6315 result[0] = texcoord[0];
6316 result[1] = texcoord[1];
6318 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6321 // float SPA_LIGHTMAP0_COLOR = 6;
6323 // ignore alpha for now..
6324 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
6327 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
6331 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
6332 void VM_getsurfacenormal(prvm_prog_t *prog)
6335 msurface_t *surface;
6338 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
6339 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6340 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6342 // note: this only returns the first triangle, so it doesn't work very
6343 // well for curved surfaces or arbitrary meshes
6344 animatemodel(prog, model, PRVM_G_EDICT(OFS_PARM0));
6345 TriangleNormal((prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex), (prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex) + 3, (prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex) + 6, normal);
6346 applytransform_forward_normal(prog, normal, PRVM_G_EDICT(OFS_PARM0), result);
6347 VectorNormalize(result);
6348 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6350 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
6351 void VM_getsurfacetexture(prvm_prog_t *prog)
6354 msurface_t *surface;
6355 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
6356 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6357 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6359 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, surface->texture->name);
6361 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
6362 void VM_getsurfacenearpoint(prvm_prog_t *prog)
6364 int surfacenum, best;
6366 vec_t dist, bestdist;
6369 msurface_t *surface;
6371 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
6372 PRVM_G_FLOAT(OFS_RETURN) = -1;
6373 ed = PRVM_G_EDICT(OFS_PARM0);
6374 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), point);
6376 if (!ed || ed->free)
6378 model = getmodel(prog, ed);
6379 if (!model || !model->num_surfaces)
6382 animatemodel(prog, model, ed);
6384 applytransform_inverted(prog, point, ed, p);
6386 bestdist = 1000000000;
6387 for (surfacenum = model->submodelsurfaces_start;surfacenum < model->submodelsurfaces_end;surfacenum++)
6389 surface = model->data_surfaces + surfacenum;
6390 // first see if the nearest point on the surface's box is closer than the previous match
6391 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
6392 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
6393 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
6394 dist = VectorLength2(clipped);
6395 if (dist < bestdist)
6397 // it is, check the nearest point on the actual geometry
6398 clippointtosurface(prog, ed, model, surface, p, clipped);
6399 VectorSubtract(clipped, p, clipped);
6400 dist += VectorLength2(clipped);
6401 if (dist < bestdist)
6403 // that's closer too, store it as the best match
6404 best = surfacenum - model->submodelsurfaces_start;
6409 PRVM_G_FLOAT(OFS_RETURN) = best;
6411 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
6412 void VM_getsurfaceclippedpoint(prvm_prog_t *prog)
6416 msurface_t *surface;
6418 VM_SAFEPARMCOUNT(3, VM_getsurfaceclippedpoint);
6419 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6420 ed = PRVM_G_EDICT(OFS_PARM0);
6421 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6423 animatemodel(prog, model, ed);
6424 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), inp);
6425 applytransform_inverted(prog, inp, ed, p);
6426 clippointtosurface(prog, ed, model, surface, p, out);
6427 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
6430 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
6431 void VM_getsurfacenumtriangles(prvm_prog_t *prog)
6434 msurface_t *surface;
6435 VM_SAFEPARMCOUNT(2, VM_getsurfacenumtriangles);
6436 // return 0 if no such surface
6437 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6439 PRVM_G_FLOAT(OFS_RETURN) = 0;
6443 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
6445 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
6446 void VM_getsurfacetriangle(prvm_prog_t *prog)
6448 const vec3_t d = {-1, -1, -1};
6451 msurface_t *surface;
6453 VM_SAFEPARMCOUNT(3, VM_getsurfacetriangle);
6454 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6455 ed = PRVM_G_EDICT(OFS_PARM0);
6456 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6458 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
6459 if (trinum < 0 || trinum >= surface->num_triangles)
6461 // FIXME: implement rotation/scaling
6462 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
6470 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(prog, ed, f); else World_Physics_ApplyCmd(ed, f)
6472 static edict_odefunc_t *VM_physics_newstackfunction(prvm_prog_t *prog, prvm_edict_t *ed, edict_odefunc_t *f)
6474 edict_odefunc_t *newfunc, *func;
6476 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
6477 memcpy(newfunc, f, sizeof(edict_odefunc_t));
6478 newfunc->next = NULL;
6479 if (!ed->priv.server->ode_func)
6480 ed->priv.server->ode_func = newfunc;
6483 for (func = ed->priv.server->ode_func; func->next; func = func->next);
6484 func->next = newfunc;
6490 // void(entity e, float physics_enabled) physics_enable = #;
6491 void VM_physics_enable(prvm_prog_t *prog)
6497 VM_SAFEPARMCOUNT(2, VM_physics_enable);
6499 ed = PRVM_G_EDICT(OFS_PARM0);
6502 if (developer.integer > 0)
6503 VM_Warning(prog, "VM_physics_enable: null entity!\n");
6506 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6507 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6509 VM_Warning(prog, "VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
6512 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
6513 VM_physics_ApplyCmd(ed, &f);
6517 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
6518 void VM_physics_addforce(prvm_prog_t *prog)
6524 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
6526 ed = PRVM_G_EDICT(OFS_PARM0);
6529 if (developer.integer > 0)
6530 VM_Warning(prog, "VM_physics_addforce: null entity!\n");
6533 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6534 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6536 VM_Warning(prog, "VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
6539 f.type = ODEFUNC_FORCE;
6540 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6541 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), f.v2);
6542 VM_physics_ApplyCmd(ed, &f);
6546 // void(entity e, vector torque) physics_addtorque = #;
6547 void VM_physics_addtorque(prvm_prog_t *prog)
6553 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
6555 ed = PRVM_G_EDICT(OFS_PARM0);
6558 if (developer.integer > 0)
6559 VM_Warning(prog, "VM_physics_addtorque: null entity!\n");
6562 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6563 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6565 VM_Warning(prog, "VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
6568 f.type = ODEFUNC_TORQUE;
6569 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6570 VM_physics_ApplyCmd(ed, &f);
6574 extern cvar_t prvm_coverage;
6575 void VM_coverage(prvm_prog_t *prog)
6577 VM_SAFEPARMCOUNT(0, VM_coverage);
6578 if (prog->explicit_profile[prog->xstatement]++ == 0 && (prvm_coverage.integer & 2))
6579 PRVM_ExplicitCoverageEvent(prog, prog->xfunction, prog->xstatement);