2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
19 extern cvar_t prvm_backtraceforwarnings;
20 extern dllhandle_t ode_dll;
22 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
23 void VM_Warning(prvm_prog_t *prog, const char *fmt, ...)
26 char msg[MAX_INPUTLINE];
27 static double recursive = -1;
30 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
35 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
36 if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
39 PRVM_PrintState(prog);
45 //============================================================================
48 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
49 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
50 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
51 // TODO: will this war ever end? [2007-01-23 LordHavoc]
53 void VM_CheckEmptyString(prvm_prog_t *prog, const char *s)
55 if (ISWHITESPACE(s[0]))
56 prog->error_cmd("%s: Bad string", prog->name);
59 void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
61 // self.frame is the interpolation target (new frame)
62 // self.frame1time is the animation base time for the interpolation target
63 // self.frame2 is the interpolation start (previous frame)
64 // self.frame2time is the animation base time for the interpolation start
65 // self.lerpfrac is the interpolation strength for self.frame2
66 // self.lerpfrac3 is the interpolation strength for self.frame3
67 // self.lerpfrac4 is the interpolation strength for self.frame4
68 // pitch angle on a player model where the animator set up 5 sets of
69 // animations and the csqc simply lerps between sets)
70 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
71 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
72 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
73 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
74 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
75 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
76 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
77 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
78 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
79 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
80 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
81 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
82 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
85 // LordHavoc: quite tempting to break apart this function to reuse the
86 // duplicated code, but I suspect it is better for performance
88 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model, double curtime)
90 int sub2, numframes, f, i, k;
91 int isfirstframegroup = true;
93 double sublerp, lerp, d;
94 const animscene_t *scene;
95 const framegroupblend_t *g;
96 frameblend_t *blend = frameblend;
98 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
100 if (!model || !model->surfmesh.isanimated)
106 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
107 numframes = model->numframes;
108 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
111 if ((unsigned int)f >= (unsigned int)numframes)
113 if (developer_extra.integer)
114 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
122 if (isfirstframegroup)
125 isfirstframegroup = false;
130 if (model->animscenes)
132 scene = model->animscenes + f;
133 f = scene->firstframe;
134 if (scene->framecount > 1)
136 // this code path is only used on .zym models and torches
137 sublerp = scene->framerate * (curtime - g->start);
138 f = (int) floor(sublerp);
141 if (sublerp < (1.0 / 65536.0f))
143 if (sublerp > (65535.0f / 65536.0f))
149 f = (f % scene->framecount);
150 sub2 = (sub2 % scene->framecount);
152 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
153 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
155 // two framelerps produced from one animation
158 for (i = 0;i < MAX_FRAMEBLENDS;i++)
160 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
162 blend[i].subframe = sub2;
168 d = (1 - sublerp) * lerp;
173 for (i = 0;i < MAX_FRAMEBLENDS;i++)
175 if (blend[i].lerp <= 0 || blend[i].subframe == f)
177 blend[i].subframe = f;
186 void VM_UpdateEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
188 if (ed->priv.server->skeleton.model != edmodel)
190 VM_RemoveEdictSkeleton(prog, ed);
191 ed->priv.server->skeleton.model = edmodel;
193 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
195 if(ed->priv.server->skeleton.relativetransforms)
196 Mem_Free(ed->priv.server->skeleton.relativetransforms);
197 ed->priv.server->skeleton.relativetransforms = NULL;
202 int skeletonindex = -1;
203 skeleton_t *skeleton;
204 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
205 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
207 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
208 if (!ed->priv.server->skeleton.relativetransforms)
209 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
210 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
214 if(ed->priv.server->skeleton.relativetransforms)
215 Mem_Free(ed->priv.server->skeleton.relativetransforms);
216 ed->priv.server->skeleton.relativetransforms = NULL;
221 void VM_RemoveEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed)
223 if (ed->priv.server->skeleton.relativetransforms)
224 Mem_Free(ed->priv.server->skeleton.relativetransforms);
225 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
231 //============================================================================
234 void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength)
240 outend = out + outlength - 1;
241 for (i = first;i < prog->argc && out < outend;i++)
243 s = PRVM_G_STRING((OFS_PARM0+i*3));
244 while (out < outend && *s)
254 returns true if the extension is supported by the server
256 checkextension(extensionname)
260 // kind of helper function
261 static qboolean checkextension(prvm_prog_t *prog, const char *name)
264 const char *e, *start;
265 len = (int)strlen(name);
267 for (e = prog->extensionstring;*e;e++)
274 while (*e && *e != ' ')
276 if ((e - start) == len && !strncasecmp(start, name, len))
278 // special sheck for ODE
279 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
282 return ode_dll ? true : false;
292 // special sheck for d0_blind_id
293 if (!strcasecmp("DP_CRYPTO", name))
294 return Crypto_Available();
295 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
296 return Crypto_Available();
304 void VM_checkextension(prvm_prog_t *prog)
306 VM_SAFEPARMCOUNT(1,VM_checkextension);
308 PRVM_G_FLOAT(OFS_RETURN) = checkextension(prog, PRVM_G_STRING(OFS_PARM0));
315 This is a TERMINAL error, which will kill off the entire prog.
321 void VM_error(prvm_prog_t *prog)
324 char string[VM_STRINGTEMP_LENGTH];
326 VM_VarString(prog, 0, string, sizeof(string));
327 Con_Printf("======%s ERROR in %s:\n%s\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
328 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
329 PRVM_ED_Print(prog, ed, NULL);
331 prog->error_cmd("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
338 Dumps out self, then an error message. The program is aborted and self is
339 removed, but the level can continue.
344 void VM_objerror(prvm_prog_t *prog)
347 char string[VM_STRINGTEMP_LENGTH];
349 VM_VarString(prog, 0, string, sizeof(string));
350 Con_Printf("======OBJECT ERROR======\n"); // , prog->name, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
351 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
352 PRVM_ED_Print(prog, ed, NULL);
353 PRVM_ED_Free (prog, ed);
354 Con_Printf("%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
366 void VM_print(prvm_prog_t *prog)
368 char string[VM_STRINGTEMP_LENGTH];
370 VM_VarString(prog, 0, string, sizeof(string));
378 broadcast print to everyone on server
383 void VM_bprint(prvm_prog_t *prog)
385 char string[VM_STRINGTEMP_LENGTH];
389 VM_Warning(prog, "VM_bprint: game is not server(%s) !\n", prog->name);
393 VM_VarString(prog, 0, string, sizeof(string));
394 SV_BroadcastPrint(string);
399 VM_sprint (menu & client but only if server.active == true)
401 single print to a specific client
403 sprint(float clientnum,...[string])
406 void VM_sprint(prvm_prog_t *prog)
410 char string[VM_STRINGTEMP_LENGTH];
412 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
414 //find client for this entity
415 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
416 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
418 VM_Warning(prog, "VM_sprint: %s: invalid client or server is not active !\n", prog->name);
422 client = svs.clients + clientnum;
423 if (!client->netconnection)
426 VM_VarString(prog, 1, string, sizeof(string));
427 MSG_WriteChar(&client->netconnection->message,svc_print);
428 MSG_WriteString(&client->netconnection->message, string);
435 single print to the screen
440 void VM_centerprint(prvm_prog_t *prog)
442 char string[VM_STRINGTEMP_LENGTH];
444 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
445 VM_VarString(prog, 0, string, sizeof(string));
446 SCR_CenterPrint(string);
453 vector normalize(vector)
456 void VM_normalize(prvm_prog_t *prog)
462 VM_SAFEPARMCOUNT(1,VM_normalize);
464 value1 = PRVM_G_VECTOR(OFS_PARM0);
466 f = VectorLength2(value1);
470 VectorScale(value1, f, newvalue);
473 VectorClear(newvalue);
475 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
485 void VM_vlen(prvm_prog_t *prog)
487 VM_SAFEPARMCOUNT(1,VM_vlen);
488 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
495 float vectoyaw(vector)
498 void VM_vectoyaw(prvm_prog_t *prog)
503 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
505 value1 = PRVM_G_VECTOR(OFS_PARM0);
507 if (value1[1] == 0 && value1[0] == 0)
511 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
516 PRVM_G_FLOAT(OFS_RETURN) = yaw;
524 vector vectoangles(vector[, vector])
527 void VM_vectoangles(prvm_prog_t *prog)
529 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
531 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
538 Returns a number from 0<= num < 1
543 void VM_random(prvm_prog_t *prog)
545 VM_SAFEPARMCOUNT(0,VM_random);
547 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
554 localsound(string sample)
557 void VM_localsound(prvm_prog_t *prog)
561 VM_SAFEPARMCOUNT(1,VM_localsound);
563 s = PRVM_G_STRING(OFS_PARM0);
565 if(!S_LocalSound (s))
567 PRVM_G_FLOAT(OFS_RETURN) = -4;
568 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
572 PRVM_G_FLOAT(OFS_RETURN) = 1;
582 void VM_break(prvm_prog_t *prog)
584 prog->error_cmd("%s: break statement", prog->name);
587 //============================================================================
593 Sends text over to the client's execution buffer
595 [localcmd (string, ...) or]
599 void VM_localcmd(prvm_prog_t *prog)
601 char string[VM_STRINGTEMP_LENGTH];
602 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
603 VM_VarString(prog, 0, string, sizeof(string));
604 Cbuf_AddText(string);
607 static qboolean PRVM_Cvar_ReadOk(const char *string)
610 cvar = Cvar_FindVar(string);
611 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
621 void VM_cvar(prvm_prog_t *prog)
623 char string[VM_STRINGTEMP_LENGTH];
624 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
625 VM_VarString(prog, 0, string, sizeof(string));
626 VM_CheckEmptyString(prog, string);
627 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
634 float cvar_type (string)
635 float CVAR_TYPEFLAG_EXISTS = 1;
636 float CVAR_TYPEFLAG_SAVED = 2;
637 float CVAR_TYPEFLAG_PRIVATE = 4;
638 float CVAR_TYPEFLAG_ENGINE = 8;
639 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
640 float CVAR_TYPEFLAG_READONLY = 32;
643 void VM_cvar_type(prvm_prog_t *prog)
645 char string[VM_STRINGTEMP_LENGTH];
649 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
650 VM_VarString(prog, 0, string, sizeof(string));
651 VM_CheckEmptyString(prog, string);
652 cvar = Cvar_FindVar(string);
657 PRVM_G_FLOAT(OFS_RETURN) = 0;
658 return; // CVAR_TYPE_NONE
661 ret = 1; // CVAR_EXISTS
662 if(cvar->flags & CVAR_SAVE)
663 ret |= 2; // CVAR_TYPE_SAVED
664 if(cvar->flags & CVAR_PRIVATE)
665 ret |= 4; // CVAR_TYPE_PRIVATE
666 if(!(cvar->flags & CVAR_ALLOCATED))
667 ret |= 8; // CVAR_TYPE_ENGINE
668 if(cvar->description != cvar_dummy_description)
669 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
670 if(cvar->flags & CVAR_READONLY)
671 ret |= 32; // CVAR_TYPE_READONLY
673 PRVM_G_FLOAT(OFS_RETURN) = ret;
680 const string VM_cvar_string (string, ...)
683 void VM_cvar_string(prvm_prog_t *prog)
685 char string[VM_STRINGTEMP_LENGTH];
686 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
687 VM_VarString(prog, 0, string, sizeof(string));
688 VM_CheckEmptyString(prog, string);
689 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
694 ========================
697 const string VM_cvar_defstring (string, ...)
698 ========================
700 void VM_cvar_defstring(prvm_prog_t *prog)
702 char string[VM_STRINGTEMP_LENGTH];
703 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
704 VM_VarString(prog, 0, string, sizeof(string));
705 VM_CheckEmptyString(prog, string);
706 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDefString(string));
710 ========================
713 const string VM_cvar_description (string, ...)
714 ========================
716 void VM_cvar_description(prvm_prog_t *prog)
718 char string[VM_STRINGTEMP_LENGTH];
719 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
720 VM_VarString(prog, 0, string, sizeof(string));
721 VM_CheckEmptyString(prog, string);
722 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDescription(string));
728 void cvar_set (string,string, ...)
731 void VM_cvar_set(prvm_prog_t *prog)
734 char string[VM_STRINGTEMP_LENGTH];
735 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
736 VM_VarString(prog, 1, string, sizeof(string));
737 name = PRVM_G_STRING(OFS_PARM0);
738 VM_CheckEmptyString(prog, name);
739 Cvar_Set(name, string);
749 void VM_dprint(prvm_prog_t *prog)
751 char string[VM_STRINGTEMP_LENGTH];
752 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
753 VM_VarString(prog, 0, string, sizeof(string));
755 Con_DPrintf("%s", string);
757 Con_DPrintf("%s: %s", prog->name, string);
769 void VM_ftos(prvm_prog_t *prog)
774 VM_SAFEPARMCOUNT(1, VM_ftos);
776 v = PRVM_G_FLOAT(OFS_PARM0);
778 if ((float)((int)v) == v)
779 dpsnprintf(s, sizeof(s), "%i", (int)v);
781 dpsnprintf(s, sizeof(s), "%f", v);
782 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
793 void VM_fabs(prvm_prog_t *prog)
797 VM_SAFEPARMCOUNT(1,VM_fabs);
799 v = PRVM_G_FLOAT(OFS_PARM0);
800 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
811 void VM_vtos(prvm_prog_t *prog)
815 VM_SAFEPARMCOUNT(1,VM_vtos);
817 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]);
818 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
829 void VM_etos(prvm_prog_t *prog)
833 VM_SAFEPARMCOUNT(1, VM_etos);
835 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
836 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
843 float stof(...[string])
846 void VM_stof(prvm_prog_t *prog)
848 char string[VM_STRINGTEMP_LENGTH];
849 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
850 VM_VarString(prog, 0, string, sizeof(string));
851 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
855 ========================
859 ========================
861 void VM_itof(prvm_prog_t *prog)
863 VM_SAFEPARMCOUNT(1, VM_itof);
864 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
868 ========================
871 entity ftoe(float num)
872 ========================
874 void VM_ftoe(prvm_prog_t *prog)
877 VM_SAFEPARMCOUNT(1, VM_ftoe);
879 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
880 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
881 ent = 0; // return world instead of a free or invalid entity
883 PRVM_G_INT(OFS_RETURN) = ent;
887 ========================
890 float etof(entity ent)
891 ========================
893 void VM_etof(prvm_prog_t *prog)
895 VM_SAFEPARMCOUNT(1, VM_etof);
896 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
903 string strftime(float uselocaltime, string[, string ...])
906 void VM_strftime(prvm_prog_t *prog)
915 char fmt[VM_STRINGTEMP_LENGTH];
916 char result[VM_STRINGTEMP_LENGTH];
917 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
918 VM_VarString(prog, 1, fmt, sizeof(fmt));
921 if (PRVM_G_FLOAT(OFS_PARM0))
922 tmresult = localtime_s(&tm, &t);
924 tmresult = gmtime_s(&tm, &t);
927 if (PRVM_G_FLOAT(OFS_PARM0))
934 PRVM_G_INT(OFS_RETURN) = 0;
938 strftime(result, sizeof(result), fmt, &tm);
940 strftime(result, sizeof(result), fmt, tm);
942 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, result);
953 void VM_spawn(prvm_prog_t *prog)
956 VM_SAFEPARMCOUNT(0, VM_spawn);
957 prog->xfunction->builtinsprofile += 20;
958 ed = PRVM_ED_Alloc(prog);
970 void VM_remove(prvm_prog_t *prog)
973 prog->xfunction->builtinsprofile += 20;
975 VM_SAFEPARMCOUNT(1, VM_remove);
977 ed = PRVM_G_EDICT(OFS_PARM0);
978 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
980 if (developer.integer > 0)
981 VM_Warning(prog, "VM_remove: tried to remove the null entity or a reserved entity!\n" );
983 else if( ed->priv.required->free )
985 if (developer.integer > 0)
986 VM_Warning(prog, "VM_remove: tried to remove an already freed entity!\n" );
989 PRVM_ED_Free (prog, ed);
996 entity find(entity start, .string field, string match)
1000 void VM_find(prvm_prog_t *prog)
1007 VM_SAFEPARMCOUNT(3,VM_find);
1009 e = PRVM_G_EDICTNUM(OFS_PARM0);
1010 f = PRVM_G_INT(OFS_PARM1);
1011 s = PRVM_G_STRING(OFS_PARM2);
1013 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1014 // expects it to find all the monsters, so we must be careful to support
1017 for (e++ ; e < prog->num_edicts ; e++)
1019 prog->xfunction->builtinsprofile++;
1020 ed = PRVM_EDICT_NUM(e);
1021 if (ed->priv.required->free)
1023 t = PRVM_E_STRING(ed,f);
1028 VM_RETURN_EDICT(ed);
1033 VM_RETURN_EDICT(prog->edicts);
1040 entity findfloat(entity start, .float field, float match)
1041 entity findentity(entity start, .entity field, entity match)
1044 // LordHavoc: added this for searching float, int, and entity reference fields
1045 void VM_findfloat(prvm_prog_t *prog)
1052 VM_SAFEPARMCOUNT(3,VM_findfloat);
1054 e = PRVM_G_EDICTNUM(OFS_PARM0);
1055 f = PRVM_G_INT(OFS_PARM1);
1056 s = PRVM_G_FLOAT(OFS_PARM2);
1058 for (e++ ; e < prog->num_edicts ; e++)
1060 prog->xfunction->builtinsprofile++;
1061 ed = PRVM_EDICT_NUM(e);
1062 if (ed->priv.required->free)
1064 if (PRVM_E_FLOAT(ed,f) == s)
1066 VM_RETURN_EDICT(ed);
1071 VM_RETURN_EDICT(prog->edicts);
1078 entity findchain(.string field, string match)
1081 // chained search for strings in entity fields
1082 // entity(.string field, string match) findchain = #402;
1083 void VM_findchain(prvm_prog_t *prog)
1088 prvm_edict_t *ent, *chain;
1091 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1094 chainfield = PRVM_G_INT(OFS_PARM2);
1096 chainfield = prog->fieldoffsets.chain;
1098 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1100 chain = prog->edicts;
1102 f = PRVM_G_INT(OFS_PARM0);
1103 s = PRVM_G_STRING(OFS_PARM1);
1105 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1106 // expects it to find all the monsters, so we must be careful to support
1109 ent = PRVM_NEXT_EDICT(prog->edicts);
1110 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1112 prog->xfunction->builtinsprofile++;
1113 if (ent->priv.required->free)
1115 t = PRVM_E_STRING(ent,f);
1121 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1125 VM_RETURN_EDICT(chain);
1132 entity findchainfloat(.string field, float match)
1133 entity findchainentity(.string field, entity match)
1136 // LordHavoc: chained search for float, int, and entity reference fields
1137 // entity(.string field, float match) findchainfloat = #403;
1138 void VM_findchainfloat(prvm_prog_t *prog)
1143 prvm_edict_t *ent, *chain;
1146 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1149 chainfield = PRVM_G_INT(OFS_PARM2);
1151 chainfield = prog->fieldoffsets.chain;
1153 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1155 chain = (prvm_edict_t *)prog->edicts;
1157 f = PRVM_G_INT(OFS_PARM0);
1158 s = PRVM_G_FLOAT(OFS_PARM1);
1160 ent = PRVM_NEXT_EDICT(prog->edicts);
1161 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1163 prog->xfunction->builtinsprofile++;
1164 if (ent->priv.required->free)
1166 if (PRVM_E_FLOAT(ent,f) != s)
1169 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1173 VM_RETURN_EDICT(chain);
1177 ========================
1180 entity findflags(entity start, .float field, float match)
1181 ========================
1183 // LordHavoc: search for flags in float fields
1184 void VM_findflags(prvm_prog_t *prog)
1191 VM_SAFEPARMCOUNT(3, VM_findflags);
1194 e = PRVM_G_EDICTNUM(OFS_PARM0);
1195 f = PRVM_G_INT(OFS_PARM1);
1196 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1198 for (e++ ; e < prog->num_edicts ; e++)
1200 prog->xfunction->builtinsprofile++;
1201 ed = PRVM_EDICT_NUM(e);
1202 if (ed->priv.required->free)
1204 if (!PRVM_E_FLOAT(ed,f))
1206 if ((int)PRVM_E_FLOAT(ed,f) & s)
1208 VM_RETURN_EDICT(ed);
1213 VM_RETURN_EDICT(prog->edicts);
1217 ========================
1220 entity findchainflags(.float field, float match)
1221 ========================
1223 // LordHavoc: chained search for flags in float fields
1224 void VM_findchainflags(prvm_prog_t *prog)
1229 prvm_edict_t *ent, *chain;
1232 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1235 chainfield = PRVM_G_INT(OFS_PARM2);
1237 chainfield = prog->fieldoffsets.chain;
1239 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1241 chain = (prvm_edict_t *)prog->edicts;
1243 f = PRVM_G_INT(OFS_PARM0);
1244 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1246 ent = PRVM_NEXT_EDICT(prog->edicts);
1247 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1249 prog->xfunction->builtinsprofile++;
1250 if (ent->priv.required->free)
1252 if (!PRVM_E_FLOAT(ent,f))
1254 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1257 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1261 VM_RETURN_EDICT(chain);
1268 string precache_sound (string sample)
1271 void VM_precache_sound(prvm_prog_t *prog)
1275 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1277 s = PRVM_G_STRING(OFS_PARM0);
1278 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1279 //VM_CheckEmptyString(prog, s);
1281 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1283 VM_Warning(prog, "VM_precache_sound: Failed to load %s for %s\n", s, prog->name);
1292 returns the same string as output
1294 does nothing, only used by qcc to build .pak archives
1297 void VM_precache_file(prvm_prog_t *prog)
1299 VM_SAFEPARMCOUNT(1,VM_precache_file);
1300 // precache_file is only used to copy files with qcc, it does nothing
1301 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1311 void VM_coredump(prvm_prog_t *prog)
1313 VM_SAFEPARMCOUNT(0,VM_coredump);
1315 Cbuf_AddText("prvm_edicts ");
1316 Cbuf_AddText(prog->name);
1327 void VM_stackdump(prvm_prog_t *prog)
1329 VM_SAFEPARMCOUNT(0, VM_stackdump);
1331 PRVM_StackTrace(prog);
1342 void VM_crash(prvm_prog_t *prog)
1344 VM_SAFEPARMCOUNT(0, VM_crash);
1346 prog->error_cmd("Crash called by %s",prog->name);
1356 void VM_traceon(prvm_prog_t *prog)
1358 VM_SAFEPARMCOUNT(0,VM_traceon);
1370 void VM_traceoff(prvm_prog_t *prog)
1372 VM_SAFEPARMCOUNT(0,VM_traceoff);
1374 prog->trace = false;
1384 void VM_eprint(prvm_prog_t *prog)
1386 VM_SAFEPARMCOUNT(1,VM_eprint);
1388 PRVM_ED_PrintNum (prog, PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1398 void VM_rint(prvm_prog_t *prog)
1401 VM_SAFEPARMCOUNT(1,VM_rint);
1403 f = PRVM_G_FLOAT(OFS_PARM0);
1405 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1407 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1417 void VM_floor(prvm_prog_t *prog)
1419 VM_SAFEPARMCOUNT(1,VM_floor);
1421 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1431 void VM_ceil(prvm_prog_t *prog)
1433 VM_SAFEPARMCOUNT(1,VM_ceil);
1435 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1443 entity nextent(entity)
1446 void VM_nextent(prvm_prog_t *prog)
1451 VM_SAFEPARMCOUNT(1, VM_nextent);
1453 i = PRVM_G_EDICTNUM(OFS_PARM0);
1456 prog->xfunction->builtinsprofile++;
1458 if (i == prog->num_edicts)
1460 VM_RETURN_EDICT(prog->edicts);
1463 ent = PRVM_EDICT_NUM(i);
1464 if (!ent->priv.required->free)
1466 VM_RETURN_EDICT(ent);
1472 //=============================================================================
1479 changelevel(string map)
1482 void VM_changelevel(prvm_prog_t *prog)
1485 VM_SAFEPARMCOUNT(1, VM_changelevel);
1489 VM_Warning(prog, "VM_changelevel: game is not server (%s)\n", prog->name);
1493 // make sure we don't issue two changelevels
1494 if (svs.changelevel_issued)
1496 svs.changelevel_issued = true;
1498 Cbuf_AddText(va(vabuf, sizeof(vabuf), "changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1508 void VM_sin(prvm_prog_t *prog)
1510 VM_SAFEPARMCOUNT(1,VM_sin);
1511 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1520 void VM_cos(prvm_prog_t *prog)
1522 VM_SAFEPARMCOUNT(1,VM_cos);
1523 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1533 void VM_sqrt(prvm_prog_t *prog)
1535 VM_SAFEPARMCOUNT(1,VM_sqrt);
1536 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1546 void VM_asin(prvm_prog_t *prog)
1548 VM_SAFEPARMCOUNT(1,VM_asin);
1549 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1558 void VM_acos(prvm_prog_t *prog)
1560 VM_SAFEPARMCOUNT(1,VM_acos);
1561 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1570 void VM_atan(prvm_prog_t *prog)
1572 VM_SAFEPARMCOUNT(1,VM_atan);
1573 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1579 float atan2(float,float)
1582 void VM_atan2(prvm_prog_t *prog)
1584 VM_SAFEPARMCOUNT(2,VM_atan2);
1585 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1594 void VM_tan(prvm_prog_t *prog)
1596 VM_SAFEPARMCOUNT(1,VM_tan);
1597 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1604 Returns a vector of length < 1 and > 0
1609 void VM_randomvec(prvm_prog_t *prog)
1612 VM_SAFEPARMCOUNT(0, VM_randomvec);
1614 VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1617 //=============================================================================
1623 float registercvar (string name, string value[, float flags])
1626 void VM_registercvar(prvm_prog_t *prog)
1628 const char *name, *value;
1631 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1633 name = PRVM_G_STRING(OFS_PARM0);
1634 value = PRVM_G_STRING(OFS_PARM1);
1635 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1636 PRVM_G_FLOAT(OFS_RETURN) = 0;
1638 if(flags > CVAR_MAXFLAGSVAL)
1641 // first check to see if it has already been defined
1642 if (Cvar_FindVar (name))
1645 // check for overlap with a command
1646 if (Cmd_Exists (name))
1648 VM_Warning(prog, "VM_registercvar: %s is a command\n", name);
1652 Cvar_Get(name, value, flags, NULL);
1654 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1662 returns the minimum of two supplied floats
1664 float min(float a, float b, ...[float])
1667 void VM_min(prvm_prog_t *prog)
1669 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1670 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1671 if (prog->argc >= 3)
1674 float f = PRVM_G_FLOAT(OFS_PARM0);
1675 for (i = 1;i < prog->argc;i++)
1676 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1677 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1678 PRVM_G_FLOAT(OFS_RETURN) = f;
1681 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1688 returns the maximum of two supplied floats
1690 float max(float a, float b, ...[float])
1693 void VM_max(prvm_prog_t *prog)
1695 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1696 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1697 if (prog->argc >= 3)
1700 float f = PRVM_G_FLOAT(OFS_PARM0);
1701 for (i = 1;i < prog->argc;i++)
1702 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1703 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1704 PRVM_G_FLOAT(OFS_RETURN) = f;
1707 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1714 returns number bounded by supplied range
1716 float bound(float min, float value, float max)
1719 void VM_bound(prvm_prog_t *prog)
1721 VM_SAFEPARMCOUNT(3,VM_bound);
1722 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1729 returns a raised to power b
1731 float pow(float a, float b)
1734 void VM_pow(prvm_prog_t *prog)
1736 VM_SAFEPARMCOUNT(2,VM_pow);
1737 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1740 void VM_log(prvm_prog_t *prog)
1742 VM_SAFEPARMCOUNT(1,VM_log);
1743 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1746 void VM_Files_Init(prvm_prog_t *prog)
1749 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1750 prog->openfiles[i] = NULL;
1753 void VM_Files_CloseAll(prvm_prog_t *prog)
1756 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1758 if (prog->openfiles[i])
1759 FS_Close(prog->openfiles[i]);
1760 prog->openfiles[i] = NULL;
1764 static qfile_t *VM_GetFileHandle(prvm_prog_t *prog, int index)
1766 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1768 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, prog->name);
1771 if (prog->openfiles[index] == NULL)
1773 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, prog->name);
1776 return prog->openfiles[index];
1783 float fopen(string filename, float mode)
1786 // float(string filename, float mode) fopen = #110;
1787 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1788 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1789 void VM_fopen(prvm_prog_t *prog)
1792 const char *modestring, *filename;
1795 VM_SAFEPARMCOUNT(2,VM_fopen);
1797 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1798 if (prog->openfiles[filenum] == NULL)
1800 if (filenum >= PRVM_MAX_OPENFILES)
1802 PRVM_G_FLOAT(OFS_RETURN) = -2;
1803 VM_Warning(prog, "VM_fopen: %s ran out of file handles (%i)\n", prog->name, PRVM_MAX_OPENFILES);
1806 filename = PRVM_G_STRING(OFS_PARM0);
1807 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1810 case 0: // FILE_READ
1812 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
1813 if (prog->openfiles[filenum] == NULL)
1814 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
1816 case 1: // FILE_APPEND
1818 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1820 case 2: // FILE_WRITE
1822 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1825 PRVM_G_FLOAT(OFS_RETURN) = -3;
1826 VM_Warning(prog, "VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", prog->name, mode);
1830 if (prog->openfiles[filenum] == NULL)
1832 PRVM_G_FLOAT(OFS_RETURN) = -1;
1833 if (developer_extra.integer)
1834 VM_Warning(prog, "VM_fopen: %s: %s mode %s failed\n", prog->name, filename, modestring);
1838 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1839 if (developer_extra.integer)
1840 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", prog->name, filename, modestring, filenum);
1841 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin(prog);
1849 fclose(float fhandle)
1852 //void(float fhandle) fclose = #111; // closes a file
1853 void VM_fclose(prvm_prog_t *prog)
1857 VM_SAFEPARMCOUNT(1,VM_fclose);
1859 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1860 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1862 VM_Warning(prog, "VM_fclose: invalid file handle %i used in %s\n", filenum, prog->name);
1865 if (prog->openfiles[filenum] == NULL)
1867 VM_Warning(prog, "VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1870 FS_Close(prog->openfiles[filenum]);
1871 prog->openfiles[filenum] = NULL;
1872 if(prog->openfiles_origin[filenum])
1873 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1874 if (developer_extra.integer)
1875 Con_DPrintf("VM_fclose: %s: #%i closed\n", prog->name, filenum);
1882 string fgets(float fhandle)
1885 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1886 void VM_fgets(prvm_prog_t *prog)
1889 char string[VM_STRINGTEMP_LENGTH];
1892 VM_SAFEPARMCOUNT(1,VM_fgets);
1894 // set the return value regardless of any possible errors
1895 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1897 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1898 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1900 VM_Warning(prog, "VM_fgets: invalid file handle %i used in %s\n", filenum, prog->name);
1903 if (prog->openfiles[filenum] == NULL)
1905 VM_Warning(prog, "VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1911 c = FS_Getc(prog->openfiles[filenum]);
1912 if (c == '\r' || c == '\n' || c < 0)
1914 if (end < VM_STRINGTEMP_LENGTH - 1)
1918 // remove \n following \r
1921 c = FS_Getc(prog->openfiles[filenum]);
1923 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1925 if (developer_extra.integer)
1926 Con_DPrintf("fgets: %s: %s\n", prog->name, string);
1928 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
1935 fputs(float fhandle, string s)
1938 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1939 void VM_fputs(prvm_prog_t *prog)
1942 char string[VM_STRINGTEMP_LENGTH];
1945 VM_SAFEPARMCOUNT(2,VM_fputs);
1947 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1948 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1950 VM_Warning(prog, "VM_fputs: invalid file handle %i used in %s\n", filenum, prog->name);
1953 if (prog->openfiles[filenum] == NULL)
1955 VM_Warning(prog, "VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1958 VM_VarString(prog, 1, string, sizeof(string));
1959 if ((stringlength = (int)strlen(string)))
1960 FS_Write(prog->openfiles[filenum], string, stringlength);
1961 if (developer_extra.integer)
1962 Con_DPrintf("fputs: %s: %s\n", prog->name, string);
1969 writetofile(float fhandle, entity ent)
1972 void VM_writetofile(prvm_prog_t *prog)
1977 VM_SAFEPARMCOUNT(2, VM_writetofile);
1979 file = VM_GetFileHandle(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
1982 VM_Warning(prog, "VM_writetofile: invalid or closed file handle\n");
1986 ent = PRVM_G_EDICT(OFS_PARM1);
1987 if(ent->priv.required->free)
1989 VM_Warning(prog, "VM_writetofile: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
1993 PRVM_ED_Write (prog, file, ent);
1996 // KrimZon - DP_QC_ENTITYDATA
2001 float() numentityfields
2002 Return the number of entity fields - NOT offsets
2005 void VM_numentityfields(prvm_prog_t *prog)
2007 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2010 // KrimZon - DP_QC_ENTITYDATA
2015 string(float fieldnum) entityfieldname
2016 Return name of the specified field as a string, or empty if the field is invalid (warning)
2019 void VM_entityfieldname(prvm_prog_t *prog)
2022 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2024 if (i < 0 || i >= prog->numfielddefs)
2026 VM_Warning(prog, "VM_entityfieldname: %s: field index out of bounds\n", prog->name);
2027 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2031 d = &prog->fielddefs[i];
2032 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2035 // KrimZon - DP_QC_ENTITYDATA
2040 float(float fieldnum) entityfieldtype
2043 void VM_entityfieldtype(prvm_prog_t *prog)
2046 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2048 if (i < 0 || i >= prog->numfielddefs)
2050 VM_Warning(prog, "VM_entityfieldtype: %s: field index out of bounds\n", prog->name);
2051 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2055 d = &prog->fielddefs[i];
2056 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2059 // KrimZon - DP_QC_ENTITYDATA
2062 VM_getentityfieldstring
2064 string(float fieldnum, entity ent) getentityfieldstring
2067 void VM_getentityfieldstring(prvm_prog_t *prog)
2069 // put the data into a string
2074 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2075 char valuebuf[MAX_INPUTLINE];
2077 if (i < 0 || i >= prog->numfielddefs)
2079 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2080 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2084 d = &prog->fielddefs[i];
2087 ent = PRVM_G_EDICT(OFS_PARM1);
2088 if(ent->priv.required->free)
2090 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2091 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2094 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2096 // if it's 0 or blank, return an empty string
2097 type = d->type & ~DEF_SAVEGLOBAL;
2098 for (j=0 ; j<prvm_type_size[type] ; j++)
2101 if (j == prvm_type_size[type])
2103 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2107 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_UglyValueString(prog, (etype_t)d->type, (prvm_eval_t *)v, valuebuf, sizeof(valuebuf)));
2110 // KrimZon - DP_QC_ENTITYDATA
2113 VM_putentityfieldstring
2115 float(float fieldnum, entity ent, string s) putentityfieldstring
2118 void VM_putentityfieldstring(prvm_prog_t *prog)
2122 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2124 if (i < 0 || i >= prog->numfielddefs)
2126 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2127 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2131 d = &prog->fielddefs[i];
2134 ent = PRVM_G_EDICT(OFS_PARM1);
2135 if(ent->priv.required->free)
2137 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2138 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2142 // parse the string into the value
2143 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(prog, ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2150 float strlen(string s)
2153 //float(string s) strlen = #114; // returns how many characters are in a string
2154 void VM_strlen(prvm_prog_t *prog)
2156 VM_SAFEPARMCOUNT(1,VM_strlen);
2158 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2159 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2162 // DRESK - Decolorized String
2167 string strdecolorize(string s)
2170 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2171 void VM_strdecolorize(prvm_prog_t *prog)
2173 char szNewString[VM_STRINGTEMP_LENGTH];
2174 const char *szString;
2177 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2178 szString = PRVM_G_STRING(OFS_PARM0);
2179 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2180 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2183 // DRESK - String Length (not counting color codes)
2188 float strlennocol(string s)
2191 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2192 // For example, ^2Dresk returns a length of 5
2193 void VM_strlennocol(prvm_prog_t *prog)
2195 const char *szString;
2198 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2200 szString = PRVM_G_STRING(OFS_PARM0);
2202 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2203 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2205 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2208 // DRESK - String to Uppercase and Lowercase
2213 string strtolower(string s)
2216 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2217 void VM_strtolower(prvm_prog_t *prog)
2219 char szNewString[VM_STRINGTEMP_LENGTH];
2220 const char *szString;
2223 VM_SAFEPARMCOUNT(1,VM_strtolower);
2224 szString = PRVM_G_STRING(OFS_PARM0);
2226 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2228 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2235 string strtoupper(string s)
2238 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2239 void VM_strtoupper(prvm_prog_t *prog)
2241 char szNewString[VM_STRINGTEMP_LENGTH];
2242 const char *szString;
2245 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2246 szString = PRVM_G_STRING(OFS_PARM0);
2248 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2250 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2257 string strcat(string,string,...[string])
2260 //string(string s1, string s2) strcat = #115;
2261 // concatenates two strings (for example "abc", "def" would return "abcdef")
2262 // and returns as a tempstring
2263 void VM_strcat(prvm_prog_t *prog)
2265 char s[VM_STRINGTEMP_LENGTH];
2266 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2268 VM_VarString(prog, 0, s, sizeof(s));
2269 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
2276 string substring(string s, float start, float length)
2279 // string(string s, float start, float length) substring = #116;
2280 // returns a section of a string as a tempstring
2281 void VM_substring(prvm_prog_t *prog)
2284 int u_slength = 0, u_start;
2287 char string[VM_STRINGTEMP_LENGTH];
2289 VM_SAFEPARMCOUNT(3,VM_substring);
2292 s = PRVM_G_STRING(OFS_PARM0);
2293 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2294 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2295 slength = strlen(s);
2297 if (start < 0) // FTE_STRINGS feature
2299 start = bound(0, start, slength);
2301 if (length < 0) // FTE_STRINGS feature
2302 length += slength - start + 1;
2303 maxlen = min((int)sizeof(string) - 1, slength - start);
2304 length = bound(0, length, maxlen);
2306 memcpy(string, s + start, length);
2308 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2311 s = PRVM_G_STRING(OFS_PARM0);
2312 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2313 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2315 if (start < 0) // FTE_STRINGS feature
2317 u_slength = u8_strlen(s);
2319 start = bound(0, start, u_slength);
2322 if (length < 0) // FTE_STRINGS feature
2324 if (!u_slength) // it's not calculated when it's not needed above
2325 u_slength = u8_strlen(s);
2326 length += u_slength - start + 1;
2329 // positive start, positive length
2330 u_start = u8_byteofs(s, start, NULL);
2333 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2336 u_length = u8_bytelen(s + u_start, length);
2337 if (u_length >= sizeof(string)-1)
2338 u_length = sizeof(string)-1;
2340 memcpy(string, s + u_start, u_length);
2341 string[u_length] = 0;
2342 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2349 string(string search, string replace, string subject) strreplace = #484;
2352 // replaces all occurrences of search with replace in the string subject, and returns the result
2353 void VM_strreplace(prvm_prog_t *prog)
2356 const char *search, *replace, *subject;
2357 char string[VM_STRINGTEMP_LENGTH];
2358 int search_len, replace_len, subject_len;
2360 VM_SAFEPARMCOUNT(3,VM_strreplace);
2362 search = PRVM_G_STRING(OFS_PARM0);
2363 replace = PRVM_G_STRING(OFS_PARM1);
2364 subject = PRVM_G_STRING(OFS_PARM2);
2366 search_len = (int)strlen(search);
2367 replace_len = (int)strlen(replace);
2368 subject_len = (int)strlen(subject);
2371 for (i = 0; i <= subject_len - search_len; i++)
2373 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2374 if (subject[i+j] != search[j])
2376 if (j == search_len)
2378 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2379 // found it at offset 'i'
2380 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2381 string[si++] = replace[j];
2384 i += search_len - 1;
2388 // the above would subtract 1 from i... so we
2389 // don't do that, but instead output the next
2391 if (si < (int)sizeof(string) - 1)
2392 string[si++] = subject[i];
2397 // in THIS case, we know search_len > 0, thus i < subject_len
2399 if (si < (int)sizeof(string) - 1)
2400 string[si++] = subject[i];
2403 // remaining chars (these cannot match)
2404 for (; i < subject_len; i++)
2405 if (si < (int)sizeof(string) - 1)
2406 string[si++] = subject[i];
2409 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2416 string(string search, string replace, string subject) strireplace = #485;
2419 // case-insensitive version of strreplace
2420 void VM_strireplace(prvm_prog_t *prog)
2423 const char *search, *replace, *subject;
2424 char string[VM_STRINGTEMP_LENGTH];
2425 int search_len, replace_len, subject_len;
2427 VM_SAFEPARMCOUNT(3,VM_strreplace);
2429 search = PRVM_G_STRING(OFS_PARM0);
2430 replace = PRVM_G_STRING(OFS_PARM1);
2431 subject = PRVM_G_STRING(OFS_PARM2);
2433 search_len = (int)strlen(search);
2434 replace_len = (int)strlen(replace);
2435 subject_len = (int)strlen(subject);
2438 for (i = 0; i <= subject_len - search_len; i++)
2440 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2441 if (tolower(subject[i+j]) != tolower(search[j]))
2443 if (j == search_len)
2445 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2446 // found it at offset 'i'
2447 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2448 string[si++] = replace[j];
2451 i += search_len - 1;
2455 // the above would subtract 1 from i... so we
2456 // don't do that, but instead output the next
2458 if (si < (int)sizeof(string) - 1)
2459 string[si++] = subject[i];
2464 // in THIS case, we know search_len > 0, thus i < subject_len
2466 if (si < (int)sizeof(string) - 1)
2467 string[si++] = subject[i];
2470 // remaining chars (these cannot match)
2471 for (; i < subject_len; i++)
2472 if (si < (int)sizeof(string) - 1)
2473 string[si++] = subject[i];
2476 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2483 vector stov(string s)
2486 //vector(string s) stov = #117; // returns vector value from a string
2487 void VM_stov(prvm_prog_t *prog)
2489 char string[VM_STRINGTEMP_LENGTH];
2491 VM_SAFEPARMCOUNT(1,VM_stov);
2493 VM_VarString(prog, 0, string, sizeof(string));
2494 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2501 string strzone(string s)
2504 //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)
2505 void VM_strzone(prvm_prog_t *prog)
2508 char string[VM_STRINGTEMP_LENGTH];
2511 VM_SAFEPARMCOUNT(1,VM_strzone);
2513 VM_VarString(prog, 0, string, sizeof(string));
2514 alloclen = strlen(string) + 1;
2515 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(prog, alloclen, &out);
2516 memcpy(out, string, alloclen);
2526 //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!!!)
2527 void VM_strunzone(prvm_prog_t *prog)
2529 VM_SAFEPARMCOUNT(1,VM_strunzone);
2530 PRVM_FreeString(prog, PRVM_G_INT(OFS_PARM0));
2535 VM_command (used by client and menu)
2537 clientcommand(float client, string s) (for client and menu)
2540 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2541 //this function originally written by KrimZon, made shorter by LordHavoc
2542 void VM_clcommand (prvm_prog_t *prog)
2544 client_t *temp_client;
2547 VM_SAFEPARMCOUNT(2,VM_clcommand);
2549 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2550 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2552 VM_Warning(prog, "VM_clientcommand: %s: invalid client/server is not active !\n", prog->name);
2556 temp_client = host_client;
2557 host_client = svs.clients + i;
2558 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client, true);
2559 host_client = temp_client;
2567 float tokenize(string s)
2570 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2571 //this function originally written by KrimZon, made shorter by LordHavoc
2572 //20040203: rewritten by LordHavoc (no longer uses allocations)
2573 static int num_tokens = 0;
2574 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2575 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2576 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2577 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2578 void VM_tokenize (prvm_prog_t *prog)
2582 VM_SAFEPARMCOUNT(1,VM_tokenize);
2584 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2585 p = tokenize_string;
2590 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2593 // skip whitespace here to find token start pos
2594 while(*p && ISWHITESPACE(*p))
2597 tokens_startpos[num_tokens] = p - tokenize_string;
2598 if(!COM_ParseToken_VM_Tokenize(&p, false))
2600 tokens_endpos[num_tokens] = p - tokenize_string;
2601 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2605 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2608 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2609 void VM_tokenize_console (prvm_prog_t *prog)
2613 VM_SAFEPARMCOUNT(1,VM_tokenize);
2615 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2616 p = tokenize_string;
2621 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2624 // skip whitespace here to find token start pos
2625 while(*p && ISWHITESPACE(*p))
2628 tokens_startpos[num_tokens] = p - tokenize_string;
2629 if(!COM_ParseToken_Console(&p))
2631 tokens_endpos[num_tokens] = p - tokenize_string;
2632 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2636 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2641 VM_tokenizebyseparator
2643 float tokenizebyseparator(string s, string separator1, ...)
2646 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2647 //this function returns the token preceding each instance of a separator (of
2648 //which there can be multiple), and the text following the last separator
2649 //useful for parsing certain kinds of data like IP addresses
2651 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2652 //returns 4 and the tokens "10" "1" "2" "3".
2653 void VM_tokenizebyseparator (prvm_prog_t *prog)
2657 int separatorlen[7];
2658 const char *separators[7];
2661 char tokentext[MAX_INPUTLINE];
2663 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2665 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2666 p = tokenize_string;
2669 for (j = 1;j < prog->argc;j++)
2671 // skip any blank separator strings
2672 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2675 separators[numseparators] = s;
2676 separatorlen[numseparators] = strlen(s);
2683 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2685 token = tokentext + j;
2686 tokens_startpos[num_tokens] = p - tokenize_string;
2690 for (k = 0;k < numseparators;k++)
2692 if (!strncmp(p, separators[k], separatorlen[k]))
2694 p += separatorlen[k];
2698 if (k < numseparators)
2700 if (j < (int)sizeof(tokentext)-1)
2701 tokentext[j++] = *p;
2705 tokens_endpos[num_tokens] = p0 - tokenize_string;
2706 if (j >= (int)sizeof(tokentext))
2709 tokens[num_tokens++] = PRVM_SetTempString(prog, token);
2714 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2717 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2718 //this function originally written by KrimZon, made shorter by LordHavoc
2719 void VM_argv (prvm_prog_t *prog)
2723 VM_SAFEPARMCOUNT(1,VM_argv);
2725 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2728 token_num += num_tokens;
2730 if (token_num >= 0 && token_num < num_tokens)
2731 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2733 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2736 //float(float n) argv_start_index = #515; // returns the start index of a token
2737 void VM_argv_start_index (prvm_prog_t *prog)
2741 VM_SAFEPARMCOUNT(1,VM_argv);
2743 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2746 token_num += num_tokens;
2748 if (token_num >= 0 && token_num < num_tokens)
2749 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2751 PRVM_G_FLOAT(OFS_RETURN) = -1;
2754 //float(float n) argv_end_index = #516; // returns the end index of a token
2755 void VM_argv_end_index (prvm_prog_t *prog)
2759 VM_SAFEPARMCOUNT(1,VM_argv);
2761 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2764 token_num += num_tokens;
2766 if (token_num >= 0 && token_num < num_tokens)
2767 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2769 PRVM_G_FLOAT(OFS_RETURN) = -1;
2779 void VM_isserver(prvm_prog_t *prog)
2781 VM_SAFEPARMCOUNT(0,VM_serverstate);
2783 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2793 void VM_clientcount(prvm_prog_t *prog)
2795 VM_SAFEPARMCOUNT(0,VM_clientcount);
2797 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2807 void VM_clientstate(prvm_prog_t *prog)
2809 VM_SAFEPARMCOUNT(0,VM_clientstate);
2812 switch( cls.state ) {
2813 case ca_uninitialized:
2815 PRVM_G_FLOAT(OFS_RETURN) = 0;
2817 case ca_disconnected:
2818 PRVM_G_FLOAT(OFS_RETURN) = 1;
2821 PRVM_G_FLOAT(OFS_RETURN) = 2;
2824 // should never be reached!
2833 float getostype(prvm_prog_t *prog)
2835 */ // not used at the moment -> not included in the common list
2836 void VM_getostype(prvm_prog_t *prog)
2838 VM_SAFEPARMCOUNT(0,VM_getostype);
2843 OS_MAC - not supported
2847 PRVM_G_FLOAT(OFS_RETURN) = 0;
2848 #elif defined(MACOSX)
2849 PRVM_G_FLOAT(OFS_RETURN) = 2;
2851 PRVM_G_FLOAT(OFS_RETURN) = 1;
2859 float gettime(prvm_prog_t *prog)
2862 float CDAudio_GetPosition(void);
2863 void VM_gettime(prvm_prog_t *prog)
2867 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2871 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2875 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2878 case 0: // GETTIME_FRAMESTART
2879 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2881 case 1: // GETTIME_REALTIME
2882 PRVM_G_FLOAT(OFS_RETURN) = Sys_DirtyTime();
2884 case 2: // GETTIME_HIRES
2885 PRVM_G_FLOAT(OFS_RETURN) = (Sys_DirtyTime() - host_dirtytime);
2887 case 3: // GETTIME_UPTIME
2888 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2890 case 4: // GETTIME_CDTRACK
2891 PRVM_G_FLOAT(OFS_RETURN) = CDAudio_GetPosition();
2894 VM_Warning(prog, "VM_gettime: %s: unsupported timer specified, returning realtime\n", prog->name);
2895 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2905 float getsoundtime(prvm_prog_t *prog)
2909 void VM_getsoundtime (prvm_prog_t *prog)
2911 int entnum, entchannel;
2912 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2914 if (prog == SVVM_prog)
2915 entnum = PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2916 else if (prog == CLVM_prog)
2917 entnum = MAX_EDICTS + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2920 VM_Warning(prog, "VM_getsoundtime: %s: not supported on this progs\n", prog->name);
2921 PRVM_G_FLOAT(OFS_RETURN) = -1;
2924 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2925 entchannel = CHAN_USER2ENGINE(entchannel);
2926 if (!IS_CHAN(entchannel))
2927 VM_Warning(prog, "VM_getsoundtime: %s: bad channel %i\n", prog->name, entchannel);
2928 PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2935 string soundlength (string sample)
2938 void VM_soundlength (prvm_prog_t *prog)
2942 VM_SAFEPARMCOUNT(1, VM_soundlength);
2944 s = PRVM_G_STRING(OFS_PARM0);
2945 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2952 loadfromdata(string data)
2955 void VM_loadfromdata(prvm_prog_t *prog)
2957 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2959 PRVM_ED_LoadFromFile(prog, PRVM_G_STRING(OFS_PARM0));
2963 ========================
2966 parseentitydata(entity ent, string data)
2967 ========================
2969 void VM_parseentitydata(prvm_prog_t *prog)
2974 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2976 // get edict and test it
2977 ent = PRVM_G_EDICT(OFS_PARM0);
2978 if (ent->priv.required->free)
2979 prog->error_cmd("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", prog->name, PRVM_NUM_FOR_EDICT(ent));
2981 data = PRVM_G_STRING(OFS_PARM1);
2983 // parse the opening brace
2984 if (!COM_ParseToken_Simple(&data, false, false, true) || com_token[0] != '{' )
2985 prog->error_cmd("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", prog->name, data );
2987 PRVM_ED_ParseEdict (prog, data, ent);
2994 loadfromfile(string file)
2997 void VM_loadfromfile(prvm_prog_t *prog)
2999 const char *filename;
3002 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3004 filename = PRVM_G_STRING(OFS_PARM0);
3005 if (FS_CheckNastyPath(filename, false))
3007 PRVM_G_FLOAT(OFS_RETURN) = -4;
3008 VM_Warning(prog, "VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", prog->name, filename);
3012 // not conform with VM_fopen
3013 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3015 PRVM_G_FLOAT(OFS_RETURN) = -1;
3017 PRVM_ED_LoadFromFile(prog, data);
3028 float mod(float val, float m)
3031 void VM_modulo(prvm_prog_t *prog)
3034 VM_SAFEPARMCOUNT(2,VM_module);
3036 val = (int) PRVM_G_FLOAT(OFS_PARM0);
3037 m = (int) PRVM_G_FLOAT(OFS_PARM1);
3039 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3042 static void VM_Search_Init(prvm_prog_t *prog)
3045 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3046 prog->opensearches[i] = NULL;
3049 static void VM_Search_Reset(prvm_prog_t *prog)
3052 // reset the fssearch list
3053 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3055 if(prog->opensearches[i])
3056 FS_FreeSearch(prog->opensearches[i]);
3057 prog->opensearches[i] = NULL;
3065 float search_begin(string pattern, float caseinsensitive, float quiet)
3068 void VM_search_begin(prvm_prog_t *prog)
3071 const char *pattern;
3072 int caseinsens, quiet;
3074 VM_SAFEPARMCOUNT(3, VM_search_begin);
3076 pattern = PRVM_G_STRING(OFS_PARM0);
3078 VM_CheckEmptyString(prog, pattern);
3080 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3081 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3083 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3084 if(!prog->opensearches[handle])
3087 if(handle >= PRVM_MAX_OPENSEARCHES)
3089 PRVM_G_FLOAT(OFS_RETURN) = -2;
3090 VM_Warning(prog, "VM_search_begin: %s ran out of search handles (%i)\n", prog->name, PRVM_MAX_OPENSEARCHES);
3094 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3095 PRVM_G_FLOAT(OFS_RETURN) = -1;
3098 prog->opensearches_origin[handle] = PRVM_AllocationOrigin(prog);
3099 PRVM_G_FLOAT(OFS_RETURN) = handle;
3107 void search_end(float handle)
3110 void VM_search_end(prvm_prog_t *prog)
3113 VM_SAFEPARMCOUNT(1, VM_search_end);
3115 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3117 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3119 VM_Warning(prog, "VM_search_end: invalid handle %i used in %s\n", handle, prog->name);
3122 if(prog->opensearches[handle] == NULL)
3124 VM_Warning(prog, "VM_search_end: no such handle %i in %s\n", handle, prog->name);
3128 FS_FreeSearch(prog->opensearches[handle]);
3129 prog->opensearches[handle] = NULL;
3130 if(prog->opensearches_origin[handle])
3131 PRVM_Free((char *)prog->opensearches_origin[handle]);
3138 float search_getsize(float handle)
3141 void VM_search_getsize(prvm_prog_t *prog)
3144 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3146 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3148 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3150 VM_Warning(prog, "VM_search_getsize: invalid handle %i used in %s\n", handle, prog->name);
3153 if(prog->opensearches[handle] == NULL)
3155 VM_Warning(prog, "VM_search_getsize: no such handle %i in %s\n", handle, prog->name);
3159 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3164 VM_search_getfilename
3166 string search_getfilename(float handle, float num)
3169 void VM_search_getfilename(prvm_prog_t *prog)
3171 int handle, filenum;
3172 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3174 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3175 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3177 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3179 VM_Warning(prog, "VM_search_getfilename: invalid handle %i used in %s\n", handle, prog->name);
3182 if(prog->opensearches[handle] == NULL)
3184 VM_Warning(prog, "VM_search_getfilename: no such handle %i in %s\n", handle, prog->name);
3187 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3189 VM_Warning(prog, "VM_search_getfilename: invalid filenum %i in %s\n", filenum, prog->name);
3193 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, prog->opensearches[handle]->filenames[filenum]);
3200 string chr(float ascii)
3203 void VM_chr(prvm_prog_t *prog)
3207 VM_SAFEPARMCOUNT(1, VM_chr);
3209 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3212 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3217 VM_SAFEPARMCOUNT(1, VM_chr);
3219 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3221 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3224 //=============================================================================
3225 // Draw builtins (client & menu)
3231 float iscachedpic(string pic)
3234 void VM_iscachedpic(prvm_prog_t *prog)
3236 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3238 // drawq hasnt such a function, thus always return true
3239 PRVM_G_FLOAT(OFS_RETURN) = false;
3246 string precache_pic(string pic)
3249 #define PRECACHE_PIC_FROMWAD 1 /* FTEQW, not supported here */
3250 #define PRECACHE_PIC_NOTPERSISTENT 2
3251 //#define PRECACHE_PIC_NOCLAMP 4
3252 #define PRECACHE_PIC_MIPMAP 8
3253 void VM_precache_pic(prvm_prog_t *prog)
3258 VM_SAFEPARMCOUNTRANGE(1, 2, VM_precache_pic);
3260 s = PRVM_G_STRING(OFS_PARM0);
3261 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3262 VM_CheckEmptyString(prog, s);
3266 int f = PRVM_G_FLOAT(OFS_PARM1);
3267 if(f & PRECACHE_PIC_NOTPERSISTENT)
3268 flags |= CACHEPICFLAG_NOTPERSISTENT;
3269 //if(f & PRECACHE_PIC_NOCLAMP)
3270 // flags |= CACHEPICFLAG_NOCLAMP;
3271 if(f & PRECACHE_PIC_MIPMAP)
3272 flags |= CACHEPICFLAG_MIPMAP;
3275 // AK Draw_CachePic is supposed to always return a valid pointer
3276 if( Draw_CachePic_Flags(s, flags)->tex == r_texture_notexture )
3277 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3287 void VM_freepic(prvm_prog_t *prog)
3291 VM_SAFEPARMCOUNT(1,VM_freepic);
3293 s = PRVM_G_STRING(OFS_PARM0);
3294 VM_CheckEmptyString(prog, s);
3299 static void getdrawfontscale(prvm_prog_t *prog, float *sx, float *sy)
3303 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3304 if(VectorLength2(v) > 0)
3311 static dp_font_t *getdrawfont(prvm_prog_t *prog)
3313 int f = (int) PRVM_drawglobalfloat(drawfont);
3314 if(f < 0 || f >= dp_fonts.maxsize)
3315 return FONT_DEFAULT;
3316 return &dp_fonts.f[f];
3323 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3326 void VM_drawcharacter(prvm_prog_t *prog)
3328 float *pos,*scale,*rgb;
3332 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3334 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3337 PRVM_G_FLOAT(OFS_RETURN) = -1;
3338 VM_Warning(prog, "VM_drawcharacter: %s passed null character !\n",prog->name);
3342 pos = PRVM_G_VECTOR(OFS_PARM0);
3343 scale = PRVM_G_VECTOR(OFS_PARM2);
3344 rgb = PRVM_G_VECTOR(OFS_PARM3);
3345 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3347 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3349 PRVM_G_FLOAT(OFS_RETURN) = -2;
3350 VM_Warning(prog, "VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3354 if(pos[2] || scale[2])
3355 VM_Warning(prog, "VM_drawcharacter: z value%c from %s discarded\n",(pos[2] && scale[2]) ? 's' : 0,((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3357 if(!scale[0] || !scale[1])
3359 PRVM_G_FLOAT(OFS_RETURN) = -3;
3360 VM_Warning(prog, "VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3364 getdrawfontscale(prog, &sx, &sy);
3365 DrawQ_String_Scale(pos[0], pos[1], &character, 1, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont(prog));
3366 PRVM_G_FLOAT(OFS_RETURN) = 1;
3373 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3376 void VM_drawstring(prvm_prog_t *prog)
3378 float *pos,*scale,*rgb;
3382 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3384 string = PRVM_G_STRING(OFS_PARM1);
3385 pos = PRVM_G_VECTOR(OFS_PARM0);
3386 scale = PRVM_G_VECTOR(OFS_PARM2);
3387 rgb = PRVM_G_VECTOR(OFS_PARM3);
3388 if (prog->argc >= 6)
3389 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3391 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3393 PRVM_G_FLOAT(OFS_RETURN) = -2;
3394 VM_Warning(prog, "VM_drawstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3398 if(!scale[0] || !scale[1])
3400 PRVM_G_FLOAT(OFS_RETURN) = -3;
3401 VM_Warning(prog, "VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3405 if(pos[2] || scale[2])
3406 VM_Warning(prog, "VM_drawstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3408 getdrawfontscale(prog, &sx, &sy);
3409 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont(prog));
3410 //Font_DrawString(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
3411 PRVM_G_FLOAT(OFS_RETURN) = 1;
3416 VM_drawcolorcodedstring
3418 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3420 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3423 void VM_drawcolorcodedstring(prvm_prog_t *prog)
3429 float sx, sy, alpha;
3431 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3433 if (prog->argc == 6) // full 6 parms, like normal drawstring
3435 pos = PRVM_G_VECTOR(OFS_PARM0);
3436 string = PRVM_G_STRING(OFS_PARM1);
3437 scale = PRVM_G_VECTOR(OFS_PARM2);
3438 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3439 alpha = PRVM_G_FLOAT(OFS_PARM4);
3440 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3444 pos = PRVM_G_VECTOR(OFS_PARM0);
3445 string = PRVM_G_STRING(OFS_PARM1);
3446 scale = PRVM_G_VECTOR(OFS_PARM2);
3450 alpha = PRVM_G_FLOAT(OFS_PARM3);
3451 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3454 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3456 PRVM_G_FLOAT(OFS_RETURN) = -2;
3457 VM_Warning(prog, "VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3461 if(!scale[0] || !scale[1])
3463 PRVM_G_FLOAT(OFS_RETURN) = -3;
3464 VM_Warning(prog, "VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3468 if(pos[2] || scale[2])
3469 VM_Warning(prog, "VM_drawcolorcodedstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3471 getdrawfontscale(prog, &sx, &sy);
3472 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], alpha, flag, NULL, false, getdrawfont(prog));
3473 if (prog->argc == 6) // also return vector of last color
3474 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3476 PRVM_G_FLOAT(OFS_RETURN) = 1;
3482 float stringwidth(string text, float allowColorCodes, float size)
3485 void VM_stringwidth(prvm_prog_t *prog)
3489 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3493 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3495 getdrawfontscale(prog, &sx, &sy);
3498 szv = PRVM_G_VECTOR(OFS_PARM2);
3503 // we want the width for 8x8 font size, divided by 8
3504 static float defsize[] = {8, 8};
3507 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3508 if(sx >= 0.9 && sx <= 1.1)
3516 string = PRVM_G_STRING(OFS_PARM0);
3517 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3519 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(prog), 1000000000) * mult;
3523 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3531 string = PRVM_G_STRING(OFS_PARM0);
3532 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3534 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3542 float findfont(string s)
3546 static float getdrawfontnum(const char *fontname)
3550 for(i = 0; i < dp_fonts.maxsize; ++i)
3551 if(!strcmp(dp_fonts.f[i].title, fontname))
3556 void VM_findfont(prvm_prog_t *prog)
3558 VM_SAFEPARMCOUNT(1,VM_findfont);
3559 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3566 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3570 void VM_loadfont(prvm_prog_t *prog)
3572 const char *fontname, *filelist, *sizes, *c, *cm;
3573 char mainfont[MAX_QPATH];
3575 float sz, scale, voffset;
3578 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3580 fontname = PRVM_G_STRING(OFS_PARM0);
3582 fontname = "default";
3584 filelist = PRVM_G_STRING(OFS_PARM1);
3586 filelist = "gfx/conchars";
3588 sizes = PRVM_G_STRING(OFS_PARM2);
3594 if (prog->argc >= 4)
3596 i = PRVM_G_FLOAT(OFS_PARM3);
3597 if (i >= 0 && i < dp_fonts.maxsize)
3600 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3604 f = FindFont(fontname, true);
3607 PRVM_G_FLOAT(OFS_RETURN) = -1;
3608 return; // something go wrong
3611 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3612 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3614 // first font is handled "normally"
3615 c = strchr(filelist, ':');
3616 cm = strchr(filelist, ',');
3617 if(c && (!cm || c < cm))
3618 f->req_face = atoi(c+1);
3624 if(!c || (c - filelist) > MAX_QPATH)
3625 strlcpy(mainfont, filelist, sizeof(mainfont));
3628 memcpy(mainfont, filelist, c - filelist);
3629 mainfont[c - filelist] = 0;
3633 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3635 c = strchr(filelist, ',');
3641 c = strchr(filelist, ':');
3642 cm = strchr(filelist, ',');
3643 if(c && (!cm || c < cm))
3644 f->fallback_faces[i] = atoi(c+1);
3647 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3650 if(!c || (c-filelist) > MAX_QPATH)
3652 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3656 memcpy(f->fallbacks[i], filelist, c - filelist);
3657 f->fallbacks[i][c - filelist] = 0;
3662 for(i = 0; i < MAX_FONT_SIZES; ++i)
3663 f->req_sizes[i] = -1;
3664 for (numsizes = 0,c = sizes;;)
3666 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3668 sz = atof(com_token);
3670 if (sz < 0.001f || sz > 1000.0f)
3672 VM_Warning(prog, "VM_loadfont: crap size %s", com_token);
3676 if (numsizes == MAX_FONT_SIZES)
3678 VM_Warning(prog, "VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3681 f->req_sizes[numsizes] = sz;
3685 // additional scale/hoffset parms
3688 if (prog->argc >= 5)
3690 scale = PRVM_G_FLOAT(OFS_PARM4);
3694 if (prog->argc >= 6)
3695 voffset = PRVM_G_FLOAT(OFS_PARM5);
3698 LoadFont(true, mainfont, f, scale, voffset);
3700 // return index of loaded font
3701 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3708 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3711 void VM_drawpic(prvm_prog_t *prog)
3713 const char *picname;
3714 float *size, *pos, *rgb;
3717 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3719 picname = PRVM_G_STRING(OFS_PARM1);
3720 VM_CheckEmptyString(prog, picname);
3722 // is pic cached ? no function yet for that
3725 PRVM_G_FLOAT(OFS_RETURN) = -4;
3726 VM_Warning(prog, "VM_drawpic: %s: %s not cached !\n", prog->name, picname);
3730 pos = PRVM_G_VECTOR(OFS_PARM0);
3731 size = PRVM_G_VECTOR(OFS_PARM2);
3732 rgb = PRVM_G_VECTOR(OFS_PARM3);
3733 if (prog->argc >= 6)
3734 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3736 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3738 PRVM_G_FLOAT(OFS_RETURN) = -2;
3739 VM_Warning(prog, "VM_drawpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3743 if(pos[2] || size[2])
3744 VM_Warning(prog, "VM_drawpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3746 DrawQ_Pic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
3747 PRVM_G_FLOAT(OFS_RETURN) = 1;
3753 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3756 void VM_drawrotpic(prvm_prog_t *prog)
3758 const char *picname;
3759 float *size, *pos, *org, *rgb;
3762 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3764 picname = PRVM_G_STRING(OFS_PARM1);
3765 VM_CheckEmptyString(prog, picname);
3767 // is pic cached ? no function yet for that
3770 PRVM_G_FLOAT(OFS_RETURN) = -4;
3771 VM_Warning(prog, "VM_drawrotpic: %s: %s not cached !\n", prog->name, picname);
3775 pos = PRVM_G_VECTOR(OFS_PARM0);
3776 size = PRVM_G_VECTOR(OFS_PARM2);
3777 org = PRVM_G_VECTOR(OFS_PARM3);
3778 rgb = PRVM_G_VECTOR(OFS_PARM5);
3779 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3781 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3783 PRVM_G_FLOAT(OFS_RETURN) = -2;
3784 VM_Warning(prog, "VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3788 if(pos[2] || size[2] || org[2])
3789 VM_Warning(prog, "VM_drawrotpic: z value from pos/size/org discarded\n");
3791 DrawQ_RotPic(pos[0], pos[1], Draw_CachePic_Flags(picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], org[0], org[1], PRVM_G_FLOAT(OFS_PARM4), rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM6), flag);
3792 PRVM_G_FLOAT(OFS_RETURN) = 1;
3798 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3802 void VM_drawsubpic(prvm_prog_t *prog)
3804 const char *picname;
3805 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3808 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3810 picname = PRVM_G_STRING(OFS_PARM2);
3811 VM_CheckEmptyString(prog, picname);
3813 // is pic cached ? no function yet for that
3816 PRVM_G_FLOAT(OFS_RETURN) = -4;
3817 VM_Warning(prog, "VM_drawsubpic: %s: %s not cached !\n", prog->name, picname);
3821 pos = PRVM_G_VECTOR(OFS_PARM0);
3822 size = PRVM_G_VECTOR(OFS_PARM1);
3823 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3824 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3825 rgb = PRVM_G_VECTOR(OFS_PARM5);
3826 alpha = PRVM_G_FLOAT(OFS_PARM6);
3827 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3829 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3831 PRVM_G_FLOAT(OFS_RETURN) = -2;
3832 VM_Warning(prog, "VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3836 if(pos[2] || size[2])
3837 VM_Warning(prog, "VM_drawsubpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3839 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3841 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3842 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3843 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3844 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3846 PRVM_G_FLOAT(OFS_RETURN) = 1;
3853 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3856 void VM_drawfill(prvm_prog_t *prog)
3858 float *size, *pos, *rgb;
3861 VM_SAFEPARMCOUNT(5,VM_drawfill);
3864 pos = PRVM_G_VECTOR(OFS_PARM0);
3865 size = PRVM_G_VECTOR(OFS_PARM1);
3866 rgb = PRVM_G_VECTOR(OFS_PARM2);
3867 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3869 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3871 PRVM_G_FLOAT(OFS_RETURN) = -2;
3872 VM_Warning(prog, "VM_drawfill: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3876 if(pos[2] || size[2])
3877 VM_Warning(prog, "VM_drawfill: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3879 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3880 PRVM_G_FLOAT(OFS_RETURN) = 1;
3887 drawsetcliparea(float x, float y, float width, float height)
3890 void VM_drawsetcliparea(prvm_prog_t *prog)
3893 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3895 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3896 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3897 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3898 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3900 DrawQ_SetClipArea(x, y, w, h);
3905 VM_drawresetcliparea
3910 void VM_drawresetcliparea(prvm_prog_t *prog)
3912 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3914 DrawQ_ResetClipArea();
3921 vector getimagesize(string pic)
3924 void VM_getimagesize(prvm_prog_t *prog)
3929 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3931 p = PRVM_G_STRING(OFS_PARM0);
3932 VM_CheckEmptyString(prog, p);
3934 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3935 if( pic->tex == r_texture_notexture )
3937 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3938 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3942 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3943 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3945 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3952 string keynumtostring(float keynum)
3955 void VM_keynumtostring (prvm_prog_t *prog)
3958 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3960 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, sizeof(tinystr)));
3965 VM_findkeysforcommand
3967 string findkeysforcommand(string command, float bindmap)
3969 the returned string is an altstring
3972 #define FKFC_NUMKEYS 5
3973 void M_FindKeysForCommand(const char *command, int *keys);
3974 void VM_findkeysforcommand(prvm_prog_t *prog)
3977 char ret[VM_STRINGTEMP_LENGTH];
3978 int keys[FKFC_NUMKEYS];
3983 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3985 cmd = PRVM_G_STRING(OFS_PARM0);
3987 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3989 bindmap = 0; // consistent to "bind"
3991 VM_CheckEmptyString(prog, cmd);
3993 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3996 for(i = 0; i < FKFC_NUMKEYS; i++)
3997 strlcat(ret, va(vabuf, sizeof(vabuf), " \'%i\'", keys[i]), sizeof(ret));
3999 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, ret);
4006 float stringtokeynum(string key)
4009 void VM_stringtokeynum (prvm_prog_t *prog)
4011 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
4013 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
4020 string getkeybind(float key, float bindmap)
4023 void VM_getkeybind (prvm_prog_t *prog)
4026 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4028 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4030 bindmap = 0; // consistent to "bind"
4032 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4039 float setkeybind(float key, string cmd, float bindmap)
4042 void VM_setkeybind (prvm_prog_t *prog)
4045 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4047 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4049 bindmap = 0; // consistent to "bind"
4051 PRVM_G_FLOAT(OFS_RETURN) = 0;
4052 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4053 PRVM_G_FLOAT(OFS_RETURN) = 1;
4060 vector getbindmaps()
4063 void VM_getbindmaps (prvm_prog_t *prog)
4066 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4067 Key_GetBindMap(&fg, &bg);
4068 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4069 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4070 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4077 float setbindmaps(vector bindmap)
4080 void VM_setbindmaps (prvm_prog_t *prog)
4082 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4083 PRVM_G_FLOAT(OFS_RETURN) = 0;
4084 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4085 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4086 PRVM_G_FLOAT(OFS_RETURN) = 1;
4089 // CL_Video interface functions
4092 ========================
4095 float cin_open(string file, string name)
4096 ========================
4098 void VM_cin_open(prvm_prog_t *prog)
4103 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4105 file = PRVM_G_STRING( OFS_PARM0 );
4106 name = PRVM_G_STRING( OFS_PARM1 );
4108 VM_CheckEmptyString(prog, file );
4109 VM_CheckEmptyString(prog, name );
4111 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4112 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4114 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4118 ========================
4121 void cin_close(string name)
4122 ========================
4124 void VM_cin_close(prvm_prog_t *prog)
4128 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4130 name = PRVM_G_STRING( OFS_PARM0 );
4131 VM_CheckEmptyString(prog, name );
4133 CL_CloseVideo( CL_GetVideoByName( name ) );
4137 ========================
4139 void cin_setstate(string name, float type)
4140 ========================
4142 void VM_cin_setstate(prvm_prog_t *prog)
4145 clvideostate_t state;
4148 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4150 name = PRVM_G_STRING( OFS_PARM0 );
4151 VM_CheckEmptyString(prog, name );
4153 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4155 video = CL_GetVideoByName( name );
4156 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4157 CL_SetVideoState( video, state );
4161 ========================
4164 float cin_getstate(string name)
4165 ========================
4167 void VM_cin_getstate(prvm_prog_t *prog)
4172 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4174 name = PRVM_G_STRING( OFS_PARM0 );
4175 VM_CheckEmptyString(prog, name );
4177 video = CL_GetVideoByName( name );
4179 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4181 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4185 ========================
4188 void cin_restart(string name)
4189 ========================
4191 void VM_cin_restart(prvm_prog_t *prog)
4196 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4198 name = PRVM_G_STRING( OFS_PARM0 );
4199 VM_CheckEmptyString(prog, name );
4201 video = CL_GetVideoByName( name );
4203 CL_RestartVideo( video );
4207 ========================
4210 float[bool] gecko_create( string name )
4211 ========================
4213 void VM_gecko_create(prvm_prog_t *prog) {
4215 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4219 ========================
4222 void gecko_destroy( string name )
4223 ========================
4225 void VM_gecko_destroy(prvm_prog_t *prog) {
4230 ========================
4233 void gecko_navigate( string name, string URI )
4234 ========================
4236 void VM_gecko_navigate(prvm_prog_t *prog) {
4241 ========================
4244 float[bool] gecko_keyevent( string name, float key, float eventtype )
4245 ========================
4247 void VM_gecko_keyevent(prvm_prog_t *prog) {
4249 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4253 ========================
4256 void gecko_mousemove( string name, float x, float y )
4257 ========================
4259 void VM_gecko_movemouse(prvm_prog_t *prog) {
4265 ========================
4268 void gecko_resize( string name, float w, float h )
4269 ========================
4271 void VM_gecko_resize(prvm_prog_t *prog) {
4277 ========================
4278 VM_gecko_get_texture_extent
4280 vector gecko_get_texture_extent( string name )
4281 ========================
4283 void VM_gecko_get_texture_extent(prvm_prog_t *prog) {
4285 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4286 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4295 Writes new values for v_forward, v_up, and v_right based on angles
4296 void makevectors(vector angle)
4299 void VM_makevectors (prvm_prog_t *prog)
4301 VM_SAFEPARMCOUNT(1, VM_makevectors);
4302 AngleVectors(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4309 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4310 vectorvectors(vector)
4313 void VM_vectorvectors (prvm_prog_t *prog)
4315 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4316 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward));
4317 VectorVectors(PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4321 ========================
4324 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4325 ========================
4327 void VM_drawline (prvm_prog_t *prog)
4329 float *c1, *c2, *rgb;
4331 unsigned char flags;
4333 VM_SAFEPARMCOUNT(6, VM_drawline);
4334 width = PRVM_G_FLOAT(OFS_PARM0);
4335 c1 = PRVM_G_VECTOR(OFS_PARM1);
4336 c2 = PRVM_G_VECTOR(OFS_PARM2);
4337 rgb = PRVM_G_VECTOR(OFS_PARM3);
4338 alpha = PRVM_G_FLOAT(OFS_PARM4);
4339 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4340 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4343 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4344 void VM_bitshift (prvm_prog_t *prog)
4347 VM_SAFEPARMCOUNT(2, VM_bitshift);
4349 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4350 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4352 PRVM_G_FLOAT(OFS_RETURN) = n1;
4355 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4357 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4360 ////////////////////////////////////////
4361 // AltString functions
4362 ////////////////////////////////////////
4365 ========================
4368 float altstr_count(string)
4369 ========================
4371 void VM_altstr_count(prvm_prog_t *prog)
4373 const char *altstr, *pos;
4376 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4378 altstr = PRVM_G_STRING( OFS_PARM0 );
4379 //VM_CheckEmptyString(prog, altstr );
4381 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4382 if( *pos == '\\' ) {
4386 } else if( *pos == '\'' ) {
4391 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4395 ========================
4398 string altstr_prepare(string)
4399 ========================
4401 void VM_altstr_prepare(prvm_prog_t *prog)
4404 const char *instr, *in;
4406 char outstr[VM_STRINGTEMP_LENGTH];
4408 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4410 instr = PRVM_G_STRING( OFS_PARM0 );
4412 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4421 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4425 ========================
4428 string altstr_get(string, float)
4429 ========================
4431 void VM_altstr_get(prvm_prog_t *prog)
4433 const char *altstr, *pos;
4436 char outstr[VM_STRINGTEMP_LENGTH];
4438 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4440 altstr = PRVM_G_STRING( OFS_PARM0 );
4442 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4443 count = count * 2 + 1;
4445 for( pos = altstr ; *pos && count ; pos++ )
4446 if( *pos == '\\' ) {
4449 } else if( *pos == '\'' )
4453 PRVM_G_INT( OFS_RETURN ) = 0;
4457 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4458 if( *pos == '\\' ) {
4463 } else if( *pos == '\'' )
4469 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4473 ========================
4476 string altstr_set(string altstr, float num, string set)
4477 ========================
4479 void VM_altstr_set(prvm_prog_t *prog)
4482 const char *altstr, *str;
4485 char outstr[VM_STRINGTEMP_LENGTH];
4487 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4489 altstr = PRVM_G_STRING( OFS_PARM0 );
4491 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4493 str = PRVM_G_STRING( OFS_PARM2 );
4496 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4501 } else if( *in == '\'' ) {
4506 for( ; *str; *out++ = *str++ );
4507 // now jump over the old content
4509 if( *in == '\'' || (*in == '\\' && !*++in) )
4512 strlcpy(out, in, outstr + sizeof(outstr) - out);
4513 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4517 ========================
4520 string altstr_ins(string altstr, float num, string set)
4521 ========================
4523 void VM_altstr_ins(prvm_prog_t *prog)
4529 char outstr[VM_STRINGTEMP_LENGTH];
4531 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4533 in = PRVM_G_STRING( OFS_PARM0 );
4534 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4535 set = PRVM_G_STRING( OFS_PARM2 );
4538 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4543 } else if( *in == '\'' ) {
4548 for( ; *set ; *out++ = *set++ );
4551 strlcpy(out, in, outstr + sizeof(outstr) - out);
4552 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4556 ////////////////////////////////////////
4557 // BufString functions
4558 ////////////////////////////////////////
4559 //[515]: string buffers support
4561 static size_t stringbuffers_sortlength;
4563 static void BufStr_Expand(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex)
4565 if (stringbuffer->max_strings <= strindex)
4567 char **oldstrings = stringbuffer->strings;
4568 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4569 while (stringbuffer->max_strings <= strindex)
4570 stringbuffer->max_strings *= 2;
4571 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4572 if (stringbuffer->num_strings > 0)
4573 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4575 Mem_Free(oldstrings);
4579 static void BufStr_Shrink(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
4581 // reduce num_strings if there are empty string slots at the end
4582 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4583 stringbuffer->num_strings--;
4585 // if empty, free the string pointer array
4586 if (stringbuffer->num_strings == 0)
4588 stringbuffer->max_strings = 0;
4589 if (stringbuffer->strings)
4590 Mem_Free(stringbuffer->strings);
4591 stringbuffer->strings = NULL;
4595 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4598 a = *((const char **) in1);
4599 b = *((const char **) in2);
4600 if(!a || !a[0]) return 1;
4601 if(!b || !b[0]) return -1;
4602 return strncmp(a, b, stringbuffers_sortlength);
4605 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4608 a = *((const char **) in1);
4609 b = *((const char **) in2);
4610 if(!a || !a[0]) return 1;
4611 if(!b || !b[0]) return -1;
4612 return strncmp(b, a, stringbuffers_sortlength);
4616 ========================
4618 creates new buffer, and returns it's index, returns -1 if failed
4619 float buf_create(prvm_prog_t *prog) = #460;
4620 float newbuf(string format, float flags) = #460;
4621 ========================
4624 void VM_buf_create (prvm_prog_t *prog)
4626 prvm_stringbuffer_t *stringbuffer;
4629 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4631 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4632 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4634 PRVM_G_FLOAT(OFS_RETURN) = -1;
4637 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4638 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4639 stringbuffer->origin = PRVM_AllocationOrigin(prog);
4640 // optional flags parm
4641 if (prog->argc >= 2)
4642 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4643 PRVM_G_FLOAT(OFS_RETURN) = i;
4649 ========================
4651 deletes buffer and all strings in it
4652 void buf_del(float bufhandle) = #461;
4653 ========================
4655 void VM_buf_del (prvm_prog_t *prog)
4657 prvm_stringbuffer_t *stringbuffer;
4658 VM_SAFEPARMCOUNT(1, VM_buf_del);
4659 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4663 for (i = 0;i < stringbuffer->num_strings;i++)
4664 if (stringbuffer->strings[i])
4665 Mem_Free(stringbuffer->strings[i]);
4666 if (stringbuffer->strings)
4667 Mem_Free(stringbuffer->strings);
4668 if(stringbuffer->origin)
4669 PRVM_Free((char *)stringbuffer->origin);
4670 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4674 VM_Warning(prog, "VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4680 ========================
4682 how many strings are stored in buffer
4683 float buf_getsize(float bufhandle) = #462;
4684 ========================
4686 void VM_buf_getsize (prvm_prog_t *prog)
4688 prvm_stringbuffer_t *stringbuffer;
4689 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4691 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4694 PRVM_G_FLOAT(OFS_RETURN) = -1;
4695 VM_Warning(prog, "VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4699 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4703 ========================
4705 copy all content from one buffer to another, make sure it exists
4706 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4707 ========================
4709 void VM_buf_copy (prvm_prog_t *prog)
4711 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4713 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4715 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4716 if(!srcstringbuffer)
4718 VM_Warning(prog, "VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4721 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4722 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4724 VM_Warning(prog, "VM_buf_copy: source == destination (%i) in %s\n", i, prog->name);
4727 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4728 if(!dststringbuffer)
4730 VM_Warning(prog, "VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4734 for (i = 0;i < dststringbuffer->num_strings;i++)
4735 if (dststringbuffer->strings[i])
4736 Mem_Free(dststringbuffer->strings[i]);
4737 if (dststringbuffer->strings)
4738 Mem_Free(dststringbuffer->strings);
4739 *dststringbuffer = *srcstringbuffer;
4740 if (dststringbuffer->max_strings)
4741 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4743 for (i = 0;i < dststringbuffer->num_strings;i++)
4745 if (srcstringbuffer->strings[i])
4748 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4749 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4750 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4756 ========================
4758 sort buffer by beginnings of strings (cmplength defaults it's length)
4759 "backward == TRUE" means that sorting goes upside-down
4760 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4761 ========================
4763 void VM_buf_sort (prvm_prog_t *prog)
4765 prvm_stringbuffer_t *stringbuffer;
4766 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4768 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4771 VM_Warning(prog, "VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4774 if(stringbuffer->num_strings <= 0)
4776 VM_Warning(prog, "VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4779 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4780 if(stringbuffers_sortlength <= 0)
4781 stringbuffers_sortlength = 0x7FFFFFFF;
4783 if(!PRVM_G_FLOAT(OFS_PARM2))
4784 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4786 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4788 BufStr_Shrink(prog, stringbuffer);
4792 ========================
4794 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4795 string buf_implode(float bufhandle, string glue) = #465;
4796 ========================
4798 void VM_buf_implode (prvm_prog_t *prog)
4800 prvm_stringbuffer_t *stringbuffer;
4801 char k[VM_STRINGTEMP_LENGTH];
4805 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4807 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4808 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4811 VM_Warning(prog, "VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4814 if(!stringbuffer->num_strings)
4816 sep = PRVM_G_STRING(OFS_PARM1);
4818 for(l = i = 0;i < stringbuffer->num_strings;i++)
4820 if(stringbuffer->strings[i])
4822 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4823 if (l >= sizeof(k) - 1)
4825 strlcat(k, sep, sizeof(k));
4826 strlcat(k, stringbuffer->strings[i], sizeof(k));
4829 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, k);
4833 ========================
4835 get a string from buffer, returns tempstring, dont str_unzone it!
4836 string bufstr_get(float bufhandle, float string_index) = #465;
4837 ========================
4839 void VM_bufstr_get (prvm_prog_t *prog)
4841 prvm_stringbuffer_t *stringbuffer;
4843 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4845 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4846 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4849 VM_Warning(prog, "VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4852 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4855 // VM_Warning(prog, "VM_bufstr_get: invalid string index %i used in %s\n", strindex, prog->name);
4858 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4859 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, stringbuffer->strings[strindex]);
4863 ========================
4865 copies a string into selected slot of buffer
4866 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4867 ========================
4869 void VM_bufstr_set (prvm_prog_t *prog)
4873 prvm_stringbuffer_t *stringbuffer;
4876 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4878 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4881 VM_Warning(prog, "VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4884 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4885 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4887 VM_Warning(prog, "VM_bufstr_set: invalid string index %i used in %s\n", strindex, prog->name);
4891 BufStr_Expand(prog, stringbuffer, strindex);
4892 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4894 if(stringbuffer->strings[strindex])
4895 Mem_Free(stringbuffer->strings[strindex]);
4896 stringbuffer->strings[strindex] = NULL;
4898 if(PRVM_G_INT(OFS_PARM2))
4900 // not the NULL string!
4901 news = PRVM_G_STRING(OFS_PARM2);
4902 alloclen = strlen(news) + 1;
4903 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4904 memcpy(stringbuffer->strings[strindex], news, alloclen);
4907 BufStr_Shrink(prog, stringbuffer);
4911 ========================
4913 adds string to buffer in first free slot and returns its index
4914 "order == TRUE" means that string will be added after last "full" slot
4915 float bufstr_add(float bufhandle, string str, float order) = #467;
4916 ========================
4918 void VM_bufstr_add (prvm_prog_t *prog)
4920 int order, strindex;
4921 prvm_stringbuffer_t *stringbuffer;
4925 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4927 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4928 PRVM_G_FLOAT(OFS_RETURN) = -1;
4931 VM_Warning(prog, "VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4934 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
4936 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);
4939 string = PRVM_G_STRING(OFS_PARM1);
4940 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4942 strindex = stringbuffer->num_strings;
4944 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4945 if (stringbuffer->strings[strindex] == NULL)
4948 BufStr_Expand(prog, stringbuffer, strindex);
4950 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4951 alloclen = strlen(string) + 1;
4952 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4953 memcpy(stringbuffer->strings[strindex], string, alloclen);
4955 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4959 ========================
4961 delete string from buffer
4962 void bufstr_free(float bufhandle, float string_index) = #468;
4963 ========================
4965 void VM_bufstr_free (prvm_prog_t *prog)
4968 prvm_stringbuffer_t *stringbuffer;
4969 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4971 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4974 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4977 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4980 VM_Warning(prog, "VM_bufstr_free: invalid string index %i used in %s\n", i, prog->name);
4984 if (i < stringbuffer->num_strings)
4986 if(stringbuffer->strings[i])
4987 Mem_Free(stringbuffer->strings[i]);
4988 stringbuffer->strings[i] = NULL;
4991 BufStr_Shrink(prog, stringbuffer);
4995 ========================
4997 load a file into string buffer, return 0 or 1
4998 float buf_loadfile(string filename, float bufhandle) = #535;
4999 ========================
5001 void VM_buf_loadfile(prvm_prog_t *prog)
5004 prvm_stringbuffer_t *stringbuffer;
5005 char string[VM_STRINGTEMP_LENGTH];
5006 int filenum, strindex, c, end;
5007 const char *filename;
5010 VM_SAFEPARMCOUNT(2, VM_buf_loadfile);
5013 filename = PRVM_G_STRING(OFS_PARM0);
5014 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
5015 if (prog->openfiles[filenum] == NULL)
5017 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
5018 if (prog->openfiles[filenum] == NULL)
5019 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
5020 if (prog->openfiles[filenum] == NULL)
5022 if (developer_extra.integer)
5023 VM_Warning(prog, "VM_buf_loadfile: failed to open file %s in %s\n", filename, prog->name, filename);
5024 PRVM_G_FLOAT(OFS_RETURN) = 0;
5028 // get string buffer
5029 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5032 VM_Warning(prog, "VM_buf_loadfile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5033 PRVM_G_FLOAT(OFS_RETURN) = 0;
5037 // read file (append to the end of buffer)
5038 strindex = stringbuffer->num_strings;
5045 c = FS_Getc(prog->openfiles[filenum]);
5046 if (c == '\r' || c == '\n' || c < 0)
5048 if (end < VM_STRINGTEMP_LENGTH - 1)
5052 // remove \n following \r
5055 c = FS_Getc(prog->openfiles[filenum]);
5057 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
5062 BufStr_Expand(prog, stringbuffer, strindex);
5063 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5064 alloclen = strlen(string) + 1;
5065 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5066 memcpy(stringbuffer->strings[strindex], string, alloclen);
5067 strindex = stringbuffer->num_strings;
5074 FS_Close(prog->openfiles[filenum]);
5075 prog->openfiles[filenum] = NULL;
5076 if (prog->openfiles_origin[filenum])
5077 PRVM_Free((char *)prog->openfiles_origin[filenum]);
5078 PRVM_G_FLOAT(OFS_RETURN) = 1;
5082 ========================
5084 writes stringbuffer to a file, returns 0 or 1
5085 float buf_writefile(float filehandle, float bufhandle, [, float startpos, float numstrings]) = #468;
5086 ========================
5089 void VM_buf_writefile(prvm_prog_t *prog)
5091 int filenum, strindex, strnum, strlength;
5092 prvm_stringbuffer_t *stringbuffer;
5094 VM_SAFEPARMCOUNTRANGE(2, 4, VM_buf_writefile);
5097 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
5098 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
5100 VM_Warning(prog, "VM_buf_writefile: invalid file handle %i used in %s\n", filenum, prog->name);
5103 if (prog->openfiles[filenum] == NULL)
5105 VM_Warning(prog, "VM_buf_writefile: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
5109 // get string buffer
5110 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5113 VM_Warning(prog, "VM_buf_writefile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5114 PRVM_G_FLOAT(OFS_RETURN) = 0;
5118 // get start and end parms
5121 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5122 strnum = (int)PRVM_G_FLOAT(OFS_PARM3);
5124 else if (prog->argc > 2)
5126 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5127 strnum = stringbuffer->num_strings - strindex;
5132 strnum = stringbuffer->num_strings;
5134 if (strindex < 0 || strindex >= stringbuffer->num_strings)
5136 VM_Warning(prog, "VM_buf_writefile: wrong start string index %i used in %s\n", strindex, prog->name);
5137 PRVM_G_FLOAT(OFS_RETURN) = 0;
5142 VM_Warning(prog, "VM_buf_writefile: wrong strings count %i used in %s\n", strnum, prog->name);
5143 PRVM_G_FLOAT(OFS_RETURN) = 0;
5148 while(strindex < stringbuffer->num_strings && strnum)
5150 if (stringbuffer->strings[strindex])
5152 if ((strlength = strlen(stringbuffer->strings[strindex])))
5153 FS_Write(prog->openfiles[filenum], stringbuffer->strings[strindex], strlength);
5154 FS_Write(prog->openfiles[filenum], "\n", 1);
5160 PRVM_G_FLOAT(OFS_RETURN) = 1;
5163 #define MATCH_AUTO 0
5164 #define MATCH_WHOLE 1
5165 #define MATCH_LEFT 2
5166 #define MATCH_RIGHT 3
5167 #define MATCH_MIDDLE 4
5168 #define MATCH_PATTERN 5
5170 const char *detect_match_rule(char *pattern, int *matchrule)
5175 patternlength = strlen(pattern);
5176 ppos = strchr(pattern, '*');
5177 qpos = strchr(pattern, '?');
5181 *matchrule = MATCH_PATTERN;
5184 // has * - left, mid, right or pattern
5187 // starts with * - may be right/mid or pattern
5188 if ((ppos - pattern) == 0)
5190 ppos = strchr(pattern+1, '*');
5194 *matchrule = MATCH_RIGHT;
5198 if ((ppos - pattern) == patternlength)
5200 *matchrule = MATCH_MIDDLE;
5205 *matchrule = MATCH_PATTERN;
5208 // end with * - left
5209 if ((ppos - pattern) == patternlength)
5211 *matchrule = MATCH_LEFT;
5216 *matchrule = MATCH_PATTERN;
5219 // have no wildcards - whole string
5220 *matchrule = MATCH_WHOLE;
5224 // todo: support UTF8
5225 qboolean match_rule(const char *string, int max_string, const char *pattern, int patternlength, int rule)
5230 return !strncmp(string, pattern, max_string) ? true : false;
5232 return !strncmp(string, pattern, patternlength) ? true : false;
5235 mid = strstr(string, pattern);
5236 return mid && !*(mid+patternlength);
5239 return strstr(string, pattern) ? true : false;
5241 return matchpattern_with_separator(string, pattern, false, "", false) ? true : false;
5245 ========================
5247 find an index of bufstring matching rule
5248 float bufstr_find(float bufhandle, string match, float matchrule, float startpos, float step) = #468;
5249 ========================
5252 void VM_bufstr_find(prvm_prog_t *prog)
5254 prvm_stringbuffer_t *stringbuffer;
5255 char string[VM_STRINGTEMP_LENGTH];
5256 int matchrule, matchlen, i, step;
5259 VM_SAFEPARMCOUNTRANGE(3, 5, VM_bufstr_find);
5261 PRVM_G_FLOAT(OFS_RETURN) = -1;
5263 // get string buffer
5264 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5267 VM_Warning(prog, "VM_bufstr_find: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5272 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5273 if (matchrule < 0 && matchrule > 5)
5275 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
5279 match = PRVM_G_STRING(OFS_PARM1);
5282 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5283 match = detect_match_rule(string, &matchrule);
5285 matchlen = strlen(match);
5288 i = (prog->argc > 3) ? (int)PRVM_G_FLOAT(OFS_PARM3) : 0;
5289 step = (prog->argc > 4) ? (int)PRVM_G_FLOAT(OFS_PARM4) : 1;
5290 while(i < stringbuffer->num_strings)
5292 if (stringbuffer->strings[i] && match_rule(stringbuffer->strings[i], VM_STRINGTEMP_LENGTH, match, matchlen, matchrule))
5294 PRVM_G_FLOAT(OFS_RETURN) = i;
5302 ========================
5304 float matchpattern(string s, string pattern, float matchrule, float startpos) = #468;
5305 ========================
5307 void VM_matchpattern(prvm_prog_t *prog)
5309 const char *s, *match;
5310 char string[VM_STRINGTEMP_LENGTH];
5313 VM_SAFEPARMCOUNTRANGE(2, 4, VM_matchpattern);
5315 s = PRVM_G_STRING(OFS_PARM0);
5318 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5319 if (matchrule < 0 && matchrule > 5)
5321 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
5325 match = PRVM_G_STRING(OFS_PARM1);
5328 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5329 match = detect_match_rule(string, &matchrule);
5335 s += max(0, min((unsigned int)PRVM_G_FLOAT(OFS_PARM3), strlen(s)-1));
5338 PRVM_G_FLOAT(OFS_RETURN) = match_rule(s, VM_STRINGTEMP_LENGTH, match, l, matchrule);
5342 ========================
5344 ========================
5347 void VM_buf_cvarlist(prvm_prog_t *prog)
5350 const char *partial, *antipartial;
5351 size_t len, antilen;
5353 qboolean ispattern, antiispattern;
5355 prvm_stringbuffer_t *stringbuffer;
5356 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5358 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5361 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5365 partial = PRVM_G_STRING(OFS_PARM1);
5369 len = strlen(partial);
5372 antipartial = PRVM_G_STRING(OFS_PARM2);
5378 antilen = strlen(antipartial);
5380 for (n = 0;n < stringbuffer->num_strings;n++)
5381 if (stringbuffer->strings[n])
5382 Mem_Free(stringbuffer->strings[n]);
5383 if (stringbuffer->strings)
5384 Mem_Free(stringbuffer->strings);
5385 stringbuffer->strings = NULL;
5387 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5388 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5391 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5393 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5396 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5402 stringbuffer->max_strings = stringbuffer->num_strings = n;
5403 if (stringbuffer->max_strings)
5404 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5407 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5409 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5412 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5415 alloclen = strlen(cvar->name) + 1;
5416 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5417 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5432 This was a major timewaster in progs, so it was converted to C
5435 void VM_changeyaw (prvm_prog_t *prog)
5438 float ideal, current, move, speed;
5440 // this is called (VERY HACKISHLY) by VM_SV_MoveToGoal, so it can not use any
5441 // parameters because they are the parameters to VM_SV_MoveToGoal, not this
5442 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5444 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5445 if (ent == prog->edicts)
5447 VM_Warning(prog, "changeyaw: can not modify world entity\n");
5450 if (ent->priv.server->free)
5452 VM_Warning(prog, "changeyaw: can not modify free entity\n");
5455 current = PRVM_gameedictvector(ent, angles)[1];
5456 current = ANGLEMOD(current);
5457 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5458 speed = PRVM_gameedictfloat(ent, yaw_speed);
5460 if (current == ideal)
5462 move = ideal - current;
5463 if (ideal > current)
5485 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5493 void VM_changepitch (prvm_prog_t *prog)
5496 float ideal, current, move, speed;
5498 VM_SAFEPARMCOUNT(1, VM_changepitch);
5500 ent = PRVM_G_EDICT(OFS_PARM0);
5501 if (ent == prog->edicts)
5503 VM_Warning(prog, "changepitch: can not modify world entity\n");
5506 if (ent->priv.server->free)
5508 VM_Warning(prog, "changepitch: can not modify free entity\n");
5511 current = PRVM_gameedictvector(ent, angles)[0];
5512 current = ANGLEMOD(current);
5513 ideal = PRVM_gameedictfloat(ent, idealpitch);
5514 speed = PRVM_gameedictfloat(ent, pitch_speed);
5516 if (current == ideal)
5518 move = ideal - current;
5519 if (ideal > current)
5541 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5545 void VM_uncolorstring (prvm_prog_t *prog)
5547 char szNewString[VM_STRINGTEMP_LENGTH];
5548 const char *szString;
5551 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5552 szString = PRVM_G_STRING(OFS_PARM0);
5553 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5554 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
5558 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5559 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5560 void VM_strstrofs (prvm_prog_t *prog)
5562 const char *instr, *match;
5564 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5565 instr = PRVM_G_STRING(OFS_PARM0);
5566 match = PRVM_G_STRING(OFS_PARM1);
5567 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5568 firstofs = u8_bytelen(instr, firstofs);
5570 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5572 PRVM_G_FLOAT(OFS_RETURN) = -1;
5576 match = strstr(instr+firstofs, match);
5578 PRVM_G_FLOAT(OFS_RETURN) = -1;
5580 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5583 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5584 void VM_str2chr (prvm_prog_t *prog)
5589 VM_SAFEPARMCOUNT(2, VM_str2chr);
5590 s = PRVM_G_STRING(OFS_PARM0);
5591 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5593 if((unsigned)index < strlen(s))
5595 if (utf8_enable.integer)
5596 ch = u8_getchar_noendptr(s + index);
5598 ch = (unsigned char)s[index];
5599 PRVM_G_FLOAT(OFS_RETURN) = ch;
5602 PRVM_G_FLOAT(OFS_RETURN) = 0;
5605 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5606 void VM_chr2str (prvm_prog_t *prog)
5611 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5612 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5613 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5615 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5620 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5621 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5622 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5624 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5627 static int chrconv_number(int i, int base, int conv)
5652 static int chrconv_punct(int i, int base, int conv)
5670 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5672 //convert case and colour seperatly...
5689 baset = 128*((charnum&1) == (convt-5));
5705 return i + basec + baset;
5707 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5708 //bulk convert a string. change case or colouring.
5709 void VM_strconv (prvm_prog_t *prog)
5711 int ccase, redalpha, rednum, len, i;
5712 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5713 unsigned char *result = resbuf;
5715 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5717 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5718 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5719 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5720 VM_VarString(prog, 3, (char *) resbuf, sizeof(resbuf));
5721 len = strlen((char *) resbuf);
5723 for (i = 0; i < len; i++, result++) //should this be done backwards?
5725 if (*result >= '0' && *result <= '9') //normal numbers...
5726 *result = chrconv_number(*result, '0', rednum);
5727 else if (*result >= '0'+128 && *result <= '9'+128)
5728 *result = chrconv_number(*result, '0'+128, rednum);
5729 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5730 *result = chrconv_number(*result, '0'+128-30, rednum);
5731 else if (*result >= '0'-30 && *result <= '9'-30)
5732 *result = chrconv_number(*result, '0'-30, rednum);
5734 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5735 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5736 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5737 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5738 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5739 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5740 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5741 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5743 else if ((*result & 127) < 16 || !redalpha) //special chars..
5745 else if (*result < 128)
5746 *result = chrconv_punct(*result, 0, redalpha);
5748 *result = chrconv_punct(*result, 128, redalpha);
5752 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, (char *) resbuf);
5755 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5756 void VM_strpad (prvm_prog_t *prog)
5758 char src[VM_STRINGTEMP_LENGTH];
5759 char destbuf[VM_STRINGTEMP_LENGTH];
5761 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5762 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5763 VM_VarString(prog, 1, src, sizeof(src));
5765 // note: < 0 = left padding, > 0 = right padding,
5766 // this is reverse logic of printf!
5767 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5769 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, destbuf);
5772 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5773 //uses qw style \key\value strings
5774 void VM_infoadd (prvm_prog_t *prog)
5776 const char *info, *key;
5777 char value[VM_STRINGTEMP_LENGTH];
5778 char temp[VM_STRINGTEMP_LENGTH];
5780 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5781 info = PRVM_G_STRING(OFS_PARM0);
5782 key = PRVM_G_STRING(OFS_PARM1);
5783 VM_VarString(prog, 2, value, sizeof(value));
5785 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5787 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5789 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, temp);
5792 // #227 string(string info, string key) infoget (FTE_STRINGS)
5793 //uses qw style \key\value strings
5794 void VM_infoget (prvm_prog_t *prog)
5798 char value[VM_STRINGTEMP_LENGTH];
5800 VM_SAFEPARMCOUNT(2, VM_infoget);
5801 info = PRVM_G_STRING(OFS_PARM0);
5802 key = PRVM_G_STRING(OFS_PARM1);
5804 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5806 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, value);
5809 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5810 // also float(string s1, string s2) strcmp (FRIK_FILE)
5811 void VM_strncmp (prvm_prog_t *prog)
5813 const char *s1, *s2;
5814 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5815 s1 = PRVM_G_STRING(OFS_PARM0);
5816 s2 = PRVM_G_STRING(OFS_PARM1);
5819 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5823 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5827 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5828 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5829 void VM_strncasecmp (prvm_prog_t *prog)
5831 const char *s1, *s2;
5832 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5833 s1 = PRVM_G_STRING(OFS_PARM0);
5834 s2 = PRVM_G_STRING(OFS_PARM1);
5837 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5841 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5845 // #494 float(float caseinsensitive, string s, ...) crc16
5846 void VM_crc16(prvm_prog_t *prog)
5849 char s[VM_STRINGTEMP_LENGTH];
5850 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5851 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5852 VM_VarString(prog, 1, s, sizeof(s));
5853 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5856 // #639 float(string digest, string data, ...) digest_hex
5857 void VM_digest_hex(prvm_prog_t *prog)
5865 char s[VM_STRINGTEMP_LENGTH];
5868 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5869 digest = PRVM_G_STRING(OFS_PARM0);
5872 VM_VarString(prog, 1, s, sizeof(s));
5877 if(!strcmp(digest, "MD4"))
5880 mdfour((unsigned char *) out, (unsigned char *) s, len);
5882 else if(!strcmp(digest, "SHA256") && Crypto_Available())
5885 sha256((unsigned char *) out, (unsigned char *) s, len);
5887 // no warning needed on mismatch - we return string_null to QC
5892 static const char *hexmap = "0123456789abcdef";
5893 for(i = 0; i < outlen; ++i)
5895 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
5896 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
5899 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outhex);
5902 PRVM_G_INT(OFS_RETURN) = 0;
5905 void VM_wasfreed (prvm_prog_t *prog)
5907 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5908 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5911 void VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace)
5913 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
5914 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
5915 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
5916 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
5917 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
5918 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
5919 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
5920 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
5921 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5922 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
5923 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
5924 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
5925 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(prog, trace->hittexture->name) : 0;
5928 void VM_ClearTraceGlobals(prvm_prog_t *prog)
5930 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5931 PRVM_gameglobalfloat(trace_allsolid) = 0;
5932 PRVM_gameglobalfloat(trace_startsolid) = 0;
5933 PRVM_gameglobalfloat(trace_fraction) = 0;
5934 PRVM_gameglobalfloat(trace_inwater) = 0;
5935 PRVM_gameglobalfloat(trace_inopen) = 0;
5936 VectorClear(PRVM_gameglobalvector(trace_endpos));
5937 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
5938 PRVM_gameglobalfloat(trace_plane_dist) = 0;
5939 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
5940 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
5941 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
5942 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
5943 PRVM_gameglobalstring(trace_dphittexturename) = 0;
5948 void VM_Cmd_Init(prvm_prog_t *prog)
5950 // only init the stuff for the current prog
5951 VM_Files_Init(prog);
5952 VM_Search_Init(prog);
5955 void VM_Cmd_Reset(prvm_prog_t *prog)
5957 CL_PurgeOwner( MENUOWNER );
5958 VM_Search_Reset(prog);
5959 VM_Files_CloseAll(prog);
5962 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5963 // does URI escaping on a string (replace evil stuff by %AB escapes)
5964 void VM_uri_escape (prvm_prog_t *prog)
5966 char src[VM_STRINGTEMP_LENGTH];
5967 char dest[VM_STRINGTEMP_LENGTH];
5969 static const char *hex = "0123456789ABCDEF";
5971 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5972 VM_VarString(prog, 0, src, sizeof(src));
5974 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5976 if((*p >= 'A' && *p <= 'Z')
5977 || (*p >= 'a' && *p <= 'z')
5978 || (*p >= '0' && *p <= '9')
5979 || (*p == '-') || (*p == '_') || (*p == '.')
5980 || (*p == '!') || (*p == '~')
5981 || (*p == '\'') || (*p == '(') || (*p == ')'))
5986 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5987 *q++ = hex[ *(unsigned char *)p & 0xF];
5992 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
5995 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5996 // does URI unescaping on a string (get back the evil stuff)
5997 void VM_uri_unescape (prvm_prog_t *prog)
5999 char src[VM_STRINGTEMP_LENGTH];
6000 char dest[VM_STRINGTEMP_LENGTH];
6004 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
6005 VM_VarString(prog, 0, src, sizeof(src));
6007 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
6011 if(p[1] >= '0' && p[1] <= '9')
6013 else if(p[1] >= 'a' && p[1] <= 'f')
6014 hi = p[1] - 'a' + 10;
6015 else if(p[1] >= 'A' && p[1] <= 'F')
6016 hi = p[1] - 'A' + 10;
6019 if(p[2] >= '0' && p[2] <= '9')
6021 else if(p[2] >= 'a' && p[2] <= 'f')
6022 lo = p[2] - 'a' + 10;
6023 else if(p[2] >= 'A' && p[2] <= 'F')
6024 lo = p[2] - 'A' + 10;
6027 if(hi != 0 || lo != 0) // don't unescape NUL bytes
6028 *q++ = (char) (hi * 0x10 + lo);
6039 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
6042 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
6043 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
6044 void VM_whichpack (prvm_prog_t *prog)
6046 const char *fn, *pack;
6048 VM_SAFEPARMCOUNT(1, VM_whichpack);
6049 fn = PRVM_G_STRING(OFS_PARM0);
6050 pack = FS_WhichPack(fn);
6052 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, pack ? pack : "");
6060 char buffer[MAX_INPUTLINE];
6061 unsigned char *postdata; // free when uri_to_prog_t is freed
6063 char *sigdata; // free when uri_to_prog_t is freed
6068 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
6071 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
6073 prog = handle->prog;
6076 // curl reply came too late... so just drop it
6077 if(handle->postdata)
6078 Z_Free(handle->postdata);
6080 Z_Free(handle->sigdata);
6085 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
6087 if(length_received >= sizeof(handle->buffer))
6088 length_received = sizeof(handle->buffer) - 1;
6089 handle->buffer[length_received] = 0;
6091 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
6092 PRVM_G_FLOAT(OFS_PARM1) = status;
6093 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(prog, handle->buffer);
6094 prog->ExecuteProgram(prog, PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
6097 if(handle->postdata)
6098 Z_Free(handle->postdata);
6100 Z_Free(handle->sigdata);
6104 // 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
6105 // 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
6106 void VM_uri_get (prvm_prog_t *prog)
6111 uri_to_prog_t *handle;
6112 const char *posttype = NULL;
6113 const char *postseparator = NULL;
6114 int poststringbuffer = -1;
6116 const char *query_string = NULL;
6119 if(!PRVM_allfunction(URI_Get_Callback))
6120 prog->error_cmd("uri_get called by %s without URI_Get_Callback defined", prog->name);
6122 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
6124 url = PRVM_G_STRING(OFS_PARM0);
6125 id = PRVM_G_FLOAT(OFS_PARM1);
6127 posttype = PRVM_G_STRING(OFS_PARM2);
6129 postseparator = PRVM_G_STRING(OFS_PARM3);
6131 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
6133 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
6134 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!
6136 query_string = strchr(url, '?');
6139 lq = query_string ? strlen(query_string) : 0;
6141 handle->prog = prog;
6142 handle->starttime = prog->starttime;
6144 if(postseparator && posttype && *posttype)
6146 size_t l = strlen(postseparator);
6147 if(poststringbuffer >= 0)
6152 prvm_stringbuffer_t *stringbuffer;
6153 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
6156 VM_Warning(prog, "uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
6160 for(i = 0;i < stringbuffer->num_strings;i++)
6164 if(stringbuffer->strings[i])
6165 ltotal += strlen(stringbuffer->strings[i]);
6167 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
6168 handle->postlen = ltotal;
6170 for(i = 0;i < stringbuffer->num_strings;i++)
6174 memcpy(handle->postdata + ltotal, postseparator, l);
6177 if(stringbuffer->strings[i])
6179 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
6180 ltotal += strlen(stringbuffer->strings[i]);
6183 if(ltotal != handle->postlen)
6184 prog->error_cmd("%s: string buffer content size mismatch, possible overrun", prog->name);
6188 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
6189 handle->postlen = l;
6190 memcpy(handle->postdata, postseparator, l);
6192 handle->postdata[handle->postlen] = 0;
6194 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
6197 // POST: we sign postdata \0 query string
6199 handle->sigdata = (char *)Z_Malloc(8192);
6200 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6201 l = strlen(handle->sigdata);
6202 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
6205 Z_Free(handle->sigdata);
6206 handle->sigdata = NULL;
6209 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6212 Z_Free(handle->sigdata);
6213 handle->sigdata = NULL;
6216 handle->siglen = l + ll;
6217 handle->sigdata[handle->siglen] = 0;
6220 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, posttype, handle->postdata, handle->postlen, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6224 if(postkeyid >= 0 && query_string)
6226 // GET: we sign JUST the query string
6228 handle->sigdata = (char *)Z_Malloc(8192);
6229 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6230 l = strlen(handle->sigdata);
6231 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
6234 Z_Free(handle->sigdata);
6235 handle->sigdata = NULL;
6238 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6241 Z_Free(handle->sigdata);
6242 handle->sigdata = NULL;
6245 handle->siglen = l + ll;
6246 handle->sigdata[handle->siglen] = 0;
6249 handle->postdata = NULL;
6250 handle->postlen = 0;
6251 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, NULL, NULL, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6255 PRVM_G_INT(OFS_RETURN) = 1;
6259 if(handle->postdata)
6260 Z_Free(handle->postdata);
6262 Z_Free(handle->sigdata);
6264 PRVM_G_INT(OFS_RETURN) = 0;
6268 void VM_netaddress_resolve (prvm_prog_t *prog)
6271 char normalized[128];
6273 lhnetaddress_t addr;
6275 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
6277 ip = PRVM_G_STRING(OFS_PARM0);
6280 port = (int) PRVM_G_FLOAT(OFS_PARM1);
6282 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
6283 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, normalized);
6285 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
6288 //string(prvm_prog_t *prog) getextresponse = #624; // returns the next extResponse packet that was sent to this client
6289 void VM_CL_getextresponse (prvm_prog_t *prog)
6291 VM_SAFEPARMCOUNT(0,VM_argv);
6293 if (cl_net_extresponse_count <= 0)
6294 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6298 --cl_net_extresponse_count;
6299 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6300 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, cl_net_extresponse[first]);
6304 void VM_SV_getextresponse (prvm_prog_t *prog)
6306 VM_SAFEPARMCOUNT(0,VM_argv);
6308 if (sv_net_extresponse_count <= 0)
6309 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6313 --sv_net_extresponse_count;
6314 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6315 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, sv_net_extresponse[first]);
6321 Common functions between menu.dat and clsprogs
6325 //#349 float() isdemo
6326 void VM_CL_isdemo (prvm_prog_t *prog)
6328 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
6329 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
6332 //#355 float() videoplaying
6333 void VM_CL_videoplaying (prvm_prog_t *prog)
6335 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
6336 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6343 callfunction(...,string function_name)
6347 void VM_callfunction(prvm_prog_t *prog)
6352 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6354 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6356 VM_CheckEmptyString(prog, s);
6358 func = PRVM_ED_FindFunction(prog, s);
6361 prog->error_cmd("VM_callfunciton: function %s not found !", s);
6362 else if (func->first_statement < 0)
6364 // negative statements are built in functions
6365 int builtinnumber = -func->first_statement;
6366 prog->xfunction->builtinsprofile++;
6367 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6368 prog->builtins[builtinnumber](prog);
6370 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
6372 else if(func - prog->functions > 0)
6375 prog->ExecuteProgram(prog, func - prog->functions,"");
6384 float isfunction(string function_name)
6387 void VM_isfunction(prvm_prog_t *prog)
6392 VM_SAFEPARMCOUNT(1, VM_isfunction);
6394 s = PRVM_G_STRING(OFS_PARM0);
6396 VM_CheckEmptyString(prog, s);
6398 func = PRVM_ED_FindFunction(prog, s);
6401 PRVM_G_FLOAT(OFS_RETURN) = false;
6403 PRVM_G_FLOAT(OFS_RETURN) = true;
6410 string sprintf(string format, ...)
6414 void VM_sprintf(prvm_prog_t *prog)
6417 char outbuf[MAX_INPUTLINE];
6418 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6420 int width, precision, thisarg, flags;
6424 static int dummyivec[3] = {0, 0, 0};
6425 static float dummyvec[3] = {0, 0, 0};
6428 #define PRINTF_ALTERNATE 1
6429 #define PRINTF_ZEROPAD 2
6430 #define PRINTF_LEFT 4
6431 #define PRINTF_SPACEPOSITIVE 8
6432 #define PRINTF_SIGNPOSITIVE 16
6436 s = PRVM_G_STRING(OFS_PARM0);
6438 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6439 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6440 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6441 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((int*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6442 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6457 // complete directive format:
6466 // is number following?
6467 if(*s >= '0' && *s <= '9')
6469 width = strtol(s, &err, 10);
6472 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6485 flags |= PRINTF_ZEROPAD;
6487 width = -1; // it was just a flag
6499 case '#': flags |= PRINTF_ALTERNATE; break;
6500 case '0': flags |= PRINTF_ZEROPAD; break;
6501 case '-': flags |= PRINTF_LEFT; break;
6502 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6503 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6513 if(*s >= '0' && *s <= '9')
6515 width = strtol(s, &err, 10);
6516 if(!err || *err != '$')
6518 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6525 width = GETARG_FLOAT(width);
6528 flags |= PRINTF_LEFT;
6532 else if(*s >= '0' && *s <= '9')
6534 width = strtol(s, &err, 10);
6537 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6543 flags |= PRINTF_LEFT;
6547 // otherwise width stays -1
6556 if(*s >= '0' && *s <= '9')
6558 precision = strtol(s, &err, 10);
6559 if(!err || *err != '$')
6561 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6567 precision = argpos++;
6568 precision = GETARG_FLOAT(precision);
6570 else if(*s >= '0' && *s <= '9')
6572 precision = strtol(s, &err, 10);
6575 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6582 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6591 case 'h': isfloat = 1; break;
6592 case 'l': isfloat = 0; break;
6593 case 'L': isfloat = 0; break;
6604 // now s points to the final directive char and is no longer changed
6619 if(*s != 's' && *s != 'c')
6620 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6621 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6622 if(flags & PRINTF_LEFT) *f++ = '-';
6623 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6624 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6634 if(width < 0) // not set
6640 if(precision < 0) // not set
6641 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6643 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6645 case 'o': case 'u': case 'x': case 'X':
6646 if(precision < 0) // not set
6647 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6649 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6651 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6652 if(precision < 0) // not set
6653 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6655 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6659 if(precision < 0) // not set
6660 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6661 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6662 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6663 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6666 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6667 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6668 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6669 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6673 if(flags & PRINTF_ALTERNATE)
6675 if(precision < 0) // not set
6676 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6678 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6682 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6684 const char *buf = u8_encodech(c, NULL, charbuf16);
6687 if(precision < 0) // not set
6688 precision = end - o - 1;
6689 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6693 if(flags & PRINTF_ALTERNATE)
6695 if(precision < 0) // not set
6696 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6698 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6702 if(precision < 0) // not set
6703 precision = end - o - 1;
6704 if(flags & PRINTF_SIGNPOSITIVE)
6705 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6707 o += u8_strpad_colorcodes(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6711 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6726 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outbuf);
6732 static dp_model_t *getmodel(prvm_prog_t *prog, prvm_edict_t *ed)
6734 if (prog == SVVM_prog)
6735 return SV_GetModelFromEdict(ed);
6736 else if (prog == CLVM_prog)
6737 return CL_GetModelFromEdict(ed);
6744 unsigned int progid;
6746 frameblend_t frameblend[MAX_FRAMEBLENDS];
6747 skeleton_t *skeleton_p;
6748 skeleton_t skeleton;
6749 float *data_vertex3f;
6750 float *data_svector3f;
6751 float *data_tvector3f;
6752 float *data_normal3f;
6754 float *buf_vertex3f;
6755 float *buf_svector3f;
6756 float *buf_tvector3f;
6757 float *buf_normal3f;
6759 animatemodel_cache_t;
6760 static animatemodel_cache_t animatemodel_cache;
6762 static void animatemodel(prvm_prog_t *prog, dp_model_t *model, prvm_edict_t *ed)
6764 skeleton_t *skeleton;
6765 int skeletonindex = -1;
6766 qboolean need = false;
6767 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6769 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6770 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6771 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6772 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6775 if(animatemodel_cache.progid != prog->id)
6776 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6777 need |= (animatemodel_cache.model != model);
6778 VM_GenerateFrameGroupBlend(prog, ed->priv.server->framegroupblend, ed);
6779 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model, PRVM_serverglobalfloat(time));
6780 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6781 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6782 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6784 need |= (animatemodel_cache.skeleton_p != skeleton);
6786 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6789 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6791 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6792 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6793 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6794 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6795 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6796 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6797 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6798 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6799 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6801 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6802 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6803 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6804 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6805 VM_UpdateEdictSkeleton(prog, ed, model, ed->priv.server->frameblend);
6806 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);
6807 animatemodel_cache.progid = prog->id;
6808 animatemodel_cache.model = model;
6809 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6810 animatemodel_cache.skeleton_p = skeleton;
6812 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6815 static void getmatrix(prvm_prog_t *prog, prvm_edict_t *ed, matrix4x4_t *out)
6817 if (prog == SVVM_prog)
6818 SV_GetEntityMatrix(prog, ed, out, false);
6819 else if (prog == CLVM_prog)
6820 CL_GetEntityMatrix(prog, ed, out, false);
6822 *out = identitymatrix;
6825 static void applytransform_forward(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6828 getmatrix(prog, ed, &m);
6829 Matrix4x4_Transform(&m, in, out);
6832 static void applytransform_forward_direction(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6835 getmatrix(prog, ed, &m);
6836 Matrix4x4_Transform3x3(&m, in, out);
6839 static void applytransform_inverted(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6842 getmatrix(prog, ed, &m);
6843 Matrix4x4_Invert_Full(&n, &m);
6844 Matrix4x4_Transform3x3(&n, in, out);
6847 static void applytransform_forward_normal(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6851 getmatrix(prog, ed, &m);
6852 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6856 static void clippointtosurface(prvm_prog_t *prog, prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6859 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6861 animatemodel(prog, model, ed);
6862 bestdist = 1000000000;
6864 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6866 // clip original point to each triangle of the surface and find the
6867 // triangle that is closest
6868 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6869 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6870 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6871 TriangleNormal(v[0], v[1], v[2], facenormal);
6872 VectorNormalize(facenormal);
6873 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6874 VectorMA(p, offsetdist, facenormal, temp);
6875 for (j = 0, k = 2;j < 3;k = j, j++)
6877 VectorSubtract(v[k], v[j], edgenormal);
6878 CrossProduct(edgenormal, facenormal, sidenormal);
6879 VectorNormalize(sidenormal);
6880 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6882 VectorMA(temp, offsetdist, sidenormal, temp);
6884 dist = VectorDistance2(temp, p);
6885 if (bestdist > dist)
6888 VectorCopy(temp, out);
6893 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6895 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6897 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6901 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6902 void VM_getsurfacenumpoints(prvm_prog_t *prog)
6905 msurface_t *surface;
6906 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6907 // return 0 if no such surface
6908 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6910 PRVM_G_FLOAT(OFS_RETURN) = 0;
6914 // note: this (incorrectly) assumes it is a simple polygon
6915 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6917 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6918 void VM_getsurfacepoint(prvm_prog_t *prog)
6922 msurface_t *surface;
6924 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6925 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6926 ed = PRVM_G_EDICT(OFS_PARM0);
6927 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6929 // note: this (incorrectly) assumes it is a simple polygon
6930 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6931 if (pointnum < 0 || pointnum >= surface->num_vertices)
6933 animatemodel(prog, model, ed);
6934 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6936 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6937 // float SPA_POSITION = 0;
6938 // float SPA_S_AXIS = 1;
6939 // float SPA_T_AXIS = 2;
6940 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6941 // float SPA_TEXCOORDS0 = 4;
6942 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6943 // float SPA_LIGHTMAP0_COLOR = 6;
6944 void VM_getsurfacepointattribute(prvm_prog_t *prog)
6948 msurface_t *surface;
6952 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6953 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6954 ed = PRVM_G_EDICT(OFS_PARM0);
6955 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6957 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6958 if (pointnum < 0 || pointnum >= surface->num_vertices)
6960 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6962 animatemodel(prog, model, ed);
6964 switch( attributetype ) {
6965 // float SPA_POSITION = 0;
6967 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6969 // float SPA_S_AXIS = 1;
6971 applytransform_forward_direction(prog, &(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6973 // float SPA_T_AXIS = 2;
6975 applytransform_forward_direction(prog, &(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6977 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6979 applytransform_forward_direction(prog, &(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6981 // float SPA_TEXCOORDS0 = 4;
6983 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6984 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6985 ret[0] = texcoord[0];
6986 ret[1] = texcoord[1];
6990 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6992 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6993 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6994 ret[0] = texcoord[0];
6995 ret[1] = texcoord[1];
6999 // float SPA_LIGHTMAP0_COLOR = 6;
7001 // ignore alpha for now..
7002 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
7005 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
7009 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
7010 void VM_getsurfacenormal(prvm_prog_t *prog)
7013 msurface_t *surface;
7015 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
7016 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7017 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7019 // note: this only returns the first triangle, so it doesn't work very
7020 // well for curved surfaces or arbitrary meshes
7021 animatemodel(prog, model, PRVM_G_EDICT(OFS_PARM0));
7022 TriangleNormal((animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex), (animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex) + 3, (animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex) + 6, normal);
7023 applytransform_forward_normal(prog, normal, PRVM_G_EDICT(OFS_PARM0), PRVM_G_VECTOR(OFS_RETURN));
7024 VectorNormalize(PRVM_G_VECTOR(OFS_RETURN));
7026 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
7027 void VM_getsurfacetexture(prvm_prog_t *prog)
7030 msurface_t *surface;
7031 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
7032 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
7033 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7035 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, surface->texture->name);
7037 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
7038 void VM_getsurfacenearpoint(prvm_prog_t *prog)
7040 int surfacenum, best;
7042 vec_t dist, bestdist;
7045 msurface_t *surface;
7047 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
7048 PRVM_G_FLOAT(OFS_RETURN) = -1;
7049 ed = PRVM_G_EDICT(OFS_PARM0);
7050 point = PRVM_G_VECTOR(OFS_PARM1);
7052 if (!ed || ed->priv.server->free)
7054 model = getmodel(prog, ed);
7055 if (!model || !model->num_surfaces)
7058 animatemodel(prog, model, ed);
7060 applytransform_inverted(prog, point, ed, p);
7062 bestdist = 1000000000;
7063 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
7065 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
7066 // first see if the nearest point on the surface's box is closer than the previous match
7067 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
7068 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
7069 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
7070 dist = VectorLength2(clipped);
7071 if (dist < bestdist)
7073 // it is, check the nearest point on the actual geometry
7074 clippointtosurface(prog, ed, model, surface, p, clipped);
7075 VectorSubtract(clipped, p, clipped);
7076 dist += VectorLength2(clipped);
7077 if (dist < bestdist)
7079 // that's closer too, store it as the best match
7085 PRVM_G_FLOAT(OFS_RETURN) = best;
7087 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
7088 void VM_getsurfaceclippedpoint(prvm_prog_t *prog)
7092 msurface_t *surface;
7094 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
7095 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7096 ed = PRVM_G_EDICT(OFS_PARM0);
7097 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7099 animatemodel(prog, model, ed);
7100 applytransform_inverted(prog, PRVM_G_VECTOR(OFS_PARM2), ed, p);
7101 clippointtosurface(prog, ed, model, surface, p, out);
7102 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
7105 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
7106 void VM_getsurfacenumtriangles(prvm_prog_t *prog)
7109 msurface_t *surface;
7110 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
7111 // return 0 if no such surface
7112 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7114 PRVM_G_FLOAT(OFS_RETURN) = 0;
7118 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
7120 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
7121 void VM_getsurfacetriangle(prvm_prog_t *prog)
7123 const vec3_t d = {-1, -1, -1};
7126 msurface_t *surface;
7128 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
7129 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7130 ed = PRVM_G_EDICT(OFS_PARM0);
7131 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7133 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
7134 if (trinum < 0 || trinum >= surface->num_triangles)
7136 // FIXME: implement rotation/scaling
7137 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
7144 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(prog, ed, f); else World_Physics_ApplyCmd(ed, f)
7146 static edict_odefunc_t *VM_physics_newstackfunction(prvm_prog_t *prog, prvm_edict_t *ed, edict_odefunc_t *f)
7148 edict_odefunc_t *newfunc, *func;
7150 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
7151 memcpy(newfunc, f, sizeof(edict_odefunc_t));
7152 newfunc->next = NULL;
7153 if (!ed->priv.server->ode_func)
7154 ed->priv.server->ode_func = newfunc;
7157 for (func = ed->priv.server->ode_func; func->next; func = func->next);
7158 func->next = newfunc;
7163 // void(entity e, float physics_enabled) physics_enable = #;
7164 void VM_physics_enable(prvm_prog_t *prog)
7169 VM_SAFEPARMCOUNT(2, VM_physics_enable);
7170 ed = PRVM_G_EDICT(OFS_PARM0);
7173 if (developer.integer > 0)
7174 VM_Warning(prog, "VM_physics_enable: null entity!\n");
7177 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7178 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7180 VM_Warning(prog, "VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
7183 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
7184 VM_physics_ApplyCmd(ed, &f);
7187 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
7188 void VM_physics_addforce(prvm_prog_t *prog)
7193 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
7194 ed = PRVM_G_EDICT(OFS_PARM0);
7197 if (developer.integer > 0)
7198 VM_Warning(prog, "VM_physics_addforce: null entity!\n");
7201 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7202 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7204 VM_Warning(prog, "VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
7207 f.type = ODEFUNC_FORCE;
7208 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7209 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), f.v2);
7210 VM_physics_ApplyCmd(ed, &f);
7213 // void(entity e, vector torque) physics_addtorque = #;
7214 void VM_physics_addtorque(prvm_prog_t *prog)
7219 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
7220 ed = PRVM_G_EDICT(OFS_PARM0);
7223 if (developer.integer > 0)
7224 VM_Warning(prog, "VM_physics_addtorque: null entity!\n");
7227 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7228 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7230 VM_Warning(prog, "VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
7233 f.type = ODEFUNC_TORQUE;
7234 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7235 VM_physics_ApplyCmd(ed, &f);