2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
19 extern cvar_t prvm_backtraceforwarnings;
21 extern dllhandle_t ode_dll;
24 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
25 void VM_Warning(prvm_prog_t *prog, const char *fmt, ...)
28 char msg[MAX_INPUTLINE];
29 static double recursive = -1;
32 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
37 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
38 if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
41 PRVM_PrintState(prog, 0);
47 //============================================================================
50 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
51 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
52 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
53 // TODO: will this war ever end? [2007-01-23 LordHavoc]
55 void VM_CheckEmptyString(prvm_prog_t *prog, const char *s)
57 if (ISWHITESPACE(s[0]))
58 prog->error_cmd("%s: Bad string", prog->name);
61 void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
63 // self.frame is the interpolation target (new frame)
64 // self.frame1time is the animation base time for the interpolation target
65 // self.frame2 is the interpolation start (previous frame)
66 // self.frame2time is the animation base time for the interpolation start
67 // self.lerpfrac is the interpolation strength for self.frame2
68 // self.lerpfrac3 is the interpolation strength for self.frame3
69 // self.lerpfrac4 is the interpolation strength for self.frame4
70 // pitch angle on a player model where the animator set up 5 sets of
71 // animations and the csqc simply lerps between sets)
72 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
73 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
74 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
75 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
76 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
77 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
78 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
79 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
80 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
81 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
82 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
83 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
84 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
87 // LordHavoc: quite tempting to break apart this function to reuse the
88 // duplicated code, but I suspect it is better for performance
90 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model, double curtime)
92 int sub2, numframes, f, i, k;
93 int isfirstframegroup = true;
95 double sublerp, lerp, d;
96 const animscene_t *scene;
97 const framegroupblend_t *g;
98 frameblend_t *blend = frameblend;
100 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
102 if (!model || !model->surfmesh.isanimated)
108 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
109 numframes = model->numframes;
110 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
113 if ((unsigned int)f >= (unsigned int)numframes)
115 if (developer_extra.integer)
116 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
124 if (isfirstframegroup)
127 isfirstframegroup = false;
132 if (model->animscenes)
134 scene = model->animscenes + f;
135 f = scene->firstframe;
136 if (scene->framecount > 1)
138 // this code path is only used on .zym models and torches
139 sublerp = scene->framerate * (curtime - g->start);
140 f = (int) floor(sublerp);
143 if (sublerp < (1.0 / 65536.0f))
145 if (sublerp > (65535.0f / 65536.0f))
151 f = (f % scene->framecount);
152 sub2 = (sub2 % scene->framecount);
154 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
155 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
157 // two framelerps produced from one animation
160 for (i = 0;i < MAX_FRAMEBLENDS;i++)
162 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
164 blend[i].subframe = sub2;
170 d = (1 - sublerp) * lerp;
175 for (i = 0;i < MAX_FRAMEBLENDS;i++)
177 if (blend[i].lerp <= 0 || blend[i].subframe == f)
179 blend[i].subframe = f;
188 void VM_UpdateEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
190 if (ed->priv.server->skeleton.model != edmodel)
192 VM_RemoveEdictSkeleton(prog, ed);
193 ed->priv.server->skeleton.model = edmodel;
195 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
197 if(ed->priv.server->skeleton.relativetransforms)
198 Mem_Free(ed->priv.server->skeleton.relativetransforms);
199 ed->priv.server->skeleton.relativetransforms = NULL;
204 int skeletonindex = -1;
205 skeleton_t *skeleton;
206 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
207 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
209 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
210 if (!ed->priv.server->skeleton.relativetransforms)
211 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
212 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
216 if(ed->priv.server->skeleton.relativetransforms)
217 Mem_Free(ed->priv.server->skeleton.relativetransforms);
218 ed->priv.server->skeleton.relativetransforms = NULL;
223 void VM_RemoveEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed)
225 if (ed->priv.server->skeleton.relativetransforms)
226 Mem_Free(ed->priv.server->skeleton.relativetransforms);
227 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
233 //============================================================================
236 void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength)
242 outend = out + outlength - 1;
243 for (i = first;i < prog->argc && out < outend;i++)
245 s = PRVM_G_STRING((OFS_PARM0+i*3));
246 while (out < outend && *s)
256 returns true if the extension is supported by the server
258 checkextension(extensionname)
262 // kind of helper function
263 static qboolean checkextension(prvm_prog_t *prog, const char *name)
266 const char *e, *start;
267 len = (int)strlen(name);
269 for (e = prog->extensionstring;*e;e++)
276 while (*e && *e != ' ')
278 if ((e - start) == len && !strncasecmp(start, name, len))
281 // special sheck for ODE
282 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
284 #ifndef LINK_TO_LIBODE
285 return ode_dll ? true : false;
287 #ifdef LINK_TO_LIBODE
296 // special sheck for d0_blind_id
297 if (!strcasecmp("DP_CRYPTO", name))
298 return Crypto_Available();
299 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
300 return Crypto_Available();
308 void VM_checkextension(prvm_prog_t *prog)
310 VM_SAFEPARMCOUNT(1,VM_checkextension);
312 PRVM_G_FLOAT(OFS_RETURN) = checkextension(prog, PRVM_G_STRING(OFS_PARM0));
319 This is a TERMINAL error, which will kill off the entire prog.
325 void VM_error(prvm_prog_t *prog)
328 char string[VM_STRINGTEMP_LENGTH];
330 VM_VarString(prog, 0, string, sizeof(string));
331 Con_Printf("======%s ERROR in %s:\n%s\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
332 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
333 PRVM_ED_Print(prog, ed, NULL);
335 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);
342 Dumps out self, then an error message. The program is aborted and self is
343 removed, but the level can continue.
348 void VM_objerror(prvm_prog_t *prog)
351 char string[VM_STRINGTEMP_LENGTH];
353 VM_VarString(prog, 0, string, sizeof(string));
354 Con_Printf("======OBJECT ERROR======\n"); // , prog->name, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
355 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
356 PRVM_ED_Print(prog, ed, NULL);
357 PRVM_ED_Free (prog, ed);
358 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);
370 void VM_print(prvm_prog_t *prog)
372 char string[VM_STRINGTEMP_LENGTH];
374 VM_VarString(prog, 0, string, sizeof(string));
382 broadcast print to everyone on server
387 void VM_bprint(prvm_prog_t *prog)
389 char string[VM_STRINGTEMP_LENGTH];
393 VM_Warning(prog, "VM_bprint: game is not server(%s) !\n", prog->name);
397 VM_VarString(prog, 0, string, sizeof(string));
398 SV_BroadcastPrint(string);
403 VM_sprint (menu & client but only if server.active == true)
405 single print to a specific client
407 sprint(float clientnum,...[string])
410 void VM_sprint(prvm_prog_t *prog)
414 char string[VM_STRINGTEMP_LENGTH];
416 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
418 //find client for this entity
419 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
420 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
422 VM_Warning(prog, "VM_sprint: %s: invalid client or server is not active !\n", prog->name);
426 client = svs.clients + clientnum;
427 if (!client->netconnection)
430 VM_VarString(prog, 1, string, sizeof(string));
431 MSG_WriteChar(&client->netconnection->message,svc_print);
432 MSG_WriteString(&client->netconnection->message, string);
439 single print to the screen
444 void VM_centerprint(prvm_prog_t *prog)
446 char string[VM_STRINGTEMP_LENGTH];
448 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
449 VM_VarString(prog, 0, string, sizeof(string));
450 SCR_CenterPrint(string);
457 vector normalize(vector)
460 void VM_normalize(prvm_prog_t *prog)
466 VM_SAFEPARMCOUNT(1,VM_normalize);
468 value1 = PRVM_G_VECTOR(OFS_PARM0);
470 f = VectorLength2(value1);
474 VectorScale(value1, f, newvalue);
477 VectorClear(newvalue);
479 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
489 void VM_vlen(prvm_prog_t *prog)
491 VM_SAFEPARMCOUNT(1,VM_vlen);
492 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
499 float vectoyaw(vector)
502 void VM_vectoyaw(prvm_prog_t *prog)
507 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
509 value1 = PRVM_G_VECTOR(OFS_PARM0);
511 if (value1[1] == 0 && value1[0] == 0)
515 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
520 PRVM_G_FLOAT(OFS_RETURN) = yaw;
528 vector vectoangles(vector[, vector])
531 void VM_vectoangles(prvm_prog_t *prog)
533 vec3_t result, forward, up;
534 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
536 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), forward);
539 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), up);
540 AnglesFromVectors(result, forward, up, true);
543 AnglesFromVectors(result, forward, NULL, true);
544 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
551 Returns a number from 0<= num < 1
556 void VM_random(prvm_prog_t *prog)
558 VM_SAFEPARMCOUNT(0,VM_random);
560 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
567 localsound(string sample)
570 void VM_localsound(prvm_prog_t *prog)
574 VM_SAFEPARMCOUNT(1,VM_localsound);
576 s = PRVM_G_STRING(OFS_PARM0);
578 if(!S_LocalSound (s))
580 PRVM_G_FLOAT(OFS_RETURN) = -4;
581 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
585 PRVM_G_FLOAT(OFS_RETURN) = 1;
595 void VM_break(prvm_prog_t *prog)
597 prog->error_cmd("%s: break statement", prog->name);
600 //============================================================================
606 Sends text over to the client's execution buffer
608 [localcmd (string, ...) or]
612 void VM_localcmd(prvm_prog_t *prog)
614 char string[VM_STRINGTEMP_LENGTH];
615 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
616 VM_VarString(prog, 0, string, sizeof(string));
617 Cbuf_AddText(string);
620 static qboolean PRVM_Cvar_ReadOk(const char *string)
623 cvar = Cvar_FindVar(string);
624 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
634 void VM_cvar(prvm_prog_t *prog)
636 char string[VM_STRINGTEMP_LENGTH];
637 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
638 VM_VarString(prog, 0, string, sizeof(string));
639 VM_CheckEmptyString(prog, string);
640 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
647 float cvar_type (string)
648 float CVAR_TYPEFLAG_EXISTS = 1;
649 float CVAR_TYPEFLAG_SAVED = 2;
650 float CVAR_TYPEFLAG_PRIVATE = 4;
651 float CVAR_TYPEFLAG_ENGINE = 8;
652 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
653 float CVAR_TYPEFLAG_READONLY = 32;
656 void VM_cvar_type(prvm_prog_t *prog)
658 char string[VM_STRINGTEMP_LENGTH];
662 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
663 VM_VarString(prog, 0, string, sizeof(string));
664 VM_CheckEmptyString(prog, string);
665 cvar = Cvar_FindVar(string);
670 PRVM_G_FLOAT(OFS_RETURN) = 0;
671 return; // CVAR_TYPE_NONE
674 ret = 1; // CVAR_EXISTS
675 if(cvar->flags & CVAR_SAVE)
676 ret |= 2; // CVAR_TYPE_SAVED
677 if(cvar->flags & CVAR_PRIVATE)
678 ret |= 4; // CVAR_TYPE_PRIVATE
679 if(!(cvar->flags & CVAR_ALLOCATED))
680 ret |= 8; // CVAR_TYPE_ENGINE
681 if(cvar->description != cvar_dummy_description)
682 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
683 if(cvar->flags & CVAR_READONLY)
684 ret |= 32; // CVAR_TYPE_READONLY
686 PRVM_G_FLOAT(OFS_RETURN) = ret;
693 const string VM_cvar_string (string, ...)
696 void VM_cvar_string(prvm_prog_t *prog)
698 char string[VM_STRINGTEMP_LENGTH];
699 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
700 VM_VarString(prog, 0, string, sizeof(string));
701 VM_CheckEmptyString(prog, string);
702 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
707 ========================
710 const string VM_cvar_defstring (string, ...)
711 ========================
713 void VM_cvar_defstring(prvm_prog_t *prog)
715 char string[VM_STRINGTEMP_LENGTH];
716 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
717 VM_VarString(prog, 0, string, sizeof(string));
718 VM_CheckEmptyString(prog, string);
719 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDefString(string));
723 ========================
726 const string VM_cvar_description (string, ...)
727 ========================
729 void VM_cvar_description(prvm_prog_t *prog)
731 char string[VM_STRINGTEMP_LENGTH];
732 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
733 VM_VarString(prog, 0, string, sizeof(string));
734 VM_CheckEmptyString(prog, string);
735 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDescription(string));
741 void cvar_set (string,string, ...)
744 void VM_cvar_set(prvm_prog_t *prog)
747 char string[VM_STRINGTEMP_LENGTH];
748 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
749 VM_VarString(prog, 1, string, sizeof(string));
750 name = PRVM_G_STRING(OFS_PARM0);
751 VM_CheckEmptyString(prog, name);
752 Cvar_Set(name, string);
762 void VM_dprint(prvm_prog_t *prog)
764 char string[VM_STRINGTEMP_LENGTH];
765 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
766 VM_VarString(prog, 0, string, sizeof(string));
768 Con_DPrintf("%s", string);
770 Con_DPrintf("%s: %s", prog->name, string);
782 void VM_ftos(prvm_prog_t *prog)
787 VM_SAFEPARMCOUNT(1, VM_ftos);
789 v = PRVM_G_FLOAT(OFS_PARM0);
791 if ((prvm_vec_t)((prvm_int_t)v) == v)
792 dpsnprintf(s, sizeof(s), "%.0f", v);
794 dpsnprintf(s, sizeof(s), "%f", v);
795 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
806 void VM_fabs(prvm_prog_t *prog)
810 VM_SAFEPARMCOUNT(1,VM_fabs);
812 v = PRVM_G_FLOAT(OFS_PARM0);
813 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
824 void VM_vtos(prvm_prog_t *prog)
828 VM_SAFEPARMCOUNT(1,VM_vtos);
830 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]);
831 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
842 void VM_etos(prvm_prog_t *prog)
846 VM_SAFEPARMCOUNT(1, VM_etos);
848 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
849 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
856 float stof(...[string])
859 void VM_stof(prvm_prog_t *prog)
861 char string[VM_STRINGTEMP_LENGTH];
862 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
863 VM_VarString(prog, 0, string, sizeof(string));
864 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
868 ========================
872 ========================
874 void VM_itof(prvm_prog_t *prog)
876 VM_SAFEPARMCOUNT(1, VM_itof);
877 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
881 ========================
884 entity ftoe(float num)
885 ========================
887 void VM_ftoe(prvm_prog_t *prog)
890 VM_SAFEPARMCOUNT(1, VM_ftoe);
892 ent = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM0);
893 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
894 ent = 0; // return world instead of a free or invalid entity
896 PRVM_G_INT(OFS_RETURN) = ent;
900 ========================
903 float etof(entity ent)
904 ========================
906 void VM_etof(prvm_prog_t *prog)
908 VM_SAFEPARMCOUNT(1, VM_etof);
909 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
916 string strftime(float uselocaltime, string[, string ...])
919 void VM_strftime(prvm_prog_t *prog)
928 char fmt[VM_STRINGTEMP_LENGTH];
929 char result[VM_STRINGTEMP_LENGTH];
930 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
931 VM_VarString(prog, 1, fmt, sizeof(fmt));
934 if (PRVM_G_FLOAT(OFS_PARM0))
935 tmresult = localtime_s(&tm, &t);
937 tmresult = gmtime_s(&tm, &t);
940 if (PRVM_G_FLOAT(OFS_PARM0))
947 PRVM_G_INT(OFS_RETURN) = 0;
951 strftime(result, sizeof(result), fmt, &tm);
953 strftime(result, sizeof(result), fmt, tm);
955 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, result);
966 void VM_spawn(prvm_prog_t *prog)
969 VM_SAFEPARMCOUNT(0, VM_spawn);
970 prog->xfunction->builtinsprofile += 20;
971 ed = PRVM_ED_Alloc(prog);
983 void VM_remove(prvm_prog_t *prog)
986 prog->xfunction->builtinsprofile += 20;
988 VM_SAFEPARMCOUNT(1, VM_remove);
990 ed = PRVM_G_EDICT(OFS_PARM0);
991 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
993 if (developer.integer > 0)
994 VM_Warning(prog, "VM_remove: tried to remove the null entity or a reserved entity!\n" );
996 else if( ed->priv.required->free )
998 if (developer.integer > 0)
999 VM_Warning(prog, "VM_remove: tried to remove an already freed entity!\n" );
1002 PRVM_ED_Free (prog, ed);
1009 entity find(entity start, .string field, string match)
1013 void VM_find(prvm_prog_t *prog)
1020 VM_SAFEPARMCOUNT(3,VM_find);
1022 e = PRVM_G_EDICTNUM(OFS_PARM0);
1023 f = PRVM_G_INT(OFS_PARM1);
1024 s = PRVM_G_STRING(OFS_PARM2);
1026 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1027 // expects it to find all the monsters, so we must be careful to support
1030 for (e++ ; e < prog->num_edicts ; e++)
1032 prog->xfunction->builtinsprofile++;
1033 ed = PRVM_EDICT_NUM(e);
1034 if (ed->priv.required->free)
1036 t = PRVM_E_STRING(ed,f);
1041 VM_RETURN_EDICT(ed);
1046 VM_RETURN_EDICT(prog->edicts);
1053 entity findfloat(entity start, .float field, float match)
1054 entity findentity(entity start, .entity field, entity match)
1057 // LordHavoc: added this for searching float, int, and entity reference fields
1058 void VM_findfloat(prvm_prog_t *prog)
1065 VM_SAFEPARMCOUNT(3,VM_findfloat);
1067 e = PRVM_G_EDICTNUM(OFS_PARM0);
1068 f = PRVM_G_INT(OFS_PARM1);
1069 s = PRVM_G_FLOAT(OFS_PARM2);
1071 for (e++ ; e < prog->num_edicts ; e++)
1073 prog->xfunction->builtinsprofile++;
1074 ed = PRVM_EDICT_NUM(e);
1075 if (ed->priv.required->free)
1077 if (PRVM_E_FLOAT(ed,f) == s)
1079 VM_RETURN_EDICT(ed);
1084 VM_RETURN_EDICT(prog->edicts);
1091 entity findchain(.string field, string match)
1094 // chained search for strings in entity fields
1095 // entity(.string field, string match) findchain = #402;
1096 void VM_findchain(prvm_prog_t *prog)
1101 prvm_edict_t *ent, *chain;
1104 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1107 chainfield = PRVM_G_INT(OFS_PARM2);
1109 chainfield = prog->fieldoffsets.chain;
1111 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1113 chain = prog->edicts;
1115 f = PRVM_G_INT(OFS_PARM0);
1116 s = PRVM_G_STRING(OFS_PARM1);
1118 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1119 // expects it to find all the monsters, so we must be careful to support
1122 ent = PRVM_NEXT_EDICT(prog->edicts);
1123 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1125 prog->xfunction->builtinsprofile++;
1126 if (ent->priv.required->free)
1128 t = PRVM_E_STRING(ent,f);
1134 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1138 VM_RETURN_EDICT(chain);
1145 entity findchainfloat(.string field, float match)
1146 entity findchainentity(.string field, entity match)
1149 // LordHavoc: chained search for float, int, and entity reference fields
1150 // entity(.string field, float match) findchainfloat = #403;
1151 void VM_findchainfloat(prvm_prog_t *prog)
1156 prvm_edict_t *ent, *chain;
1159 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1162 chainfield = PRVM_G_INT(OFS_PARM2);
1164 chainfield = prog->fieldoffsets.chain;
1166 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1168 chain = (prvm_edict_t *)prog->edicts;
1170 f = PRVM_G_INT(OFS_PARM0);
1171 s = PRVM_G_FLOAT(OFS_PARM1);
1173 ent = PRVM_NEXT_EDICT(prog->edicts);
1174 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1176 prog->xfunction->builtinsprofile++;
1177 if (ent->priv.required->free)
1179 if (PRVM_E_FLOAT(ent,f) != s)
1182 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1186 VM_RETURN_EDICT(chain);
1190 ========================
1193 entity findflags(entity start, .float field, float match)
1194 ========================
1196 // LordHavoc: search for flags in float fields
1197 void VM_findflags(prvm_prog_t *prog)
1204 VM_SAFEPARMCOUNT(3, VM_findflags);
1207 e = PRVM_G_EDICTNUM(OFS_PARM0);
1208 f = PRVM_G_INT(OFS_PARM1);
1209 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM2);
1211 for (e++ ; e < prog->num_edicts ; e++)
1213 prog->xfunction->builtinsprofile++;
1214 ed = PRVM_EDICT_NUM(e);
1215 if (ed->priv.required->free)
1217 if (!PRVM_E_FLOAT(ed,f))
1219 if ((prvm_int_t)PRVM_E_FLOAT(ed,f) & s)
1221 VM_RETURN_EDICT(ed);
1226 VM_RETURN_EDICT(prog->edicts);
1230 ========================
1233 entity findchainflags(.float field, float match)
1234 ========================
1236 // LordHavoc: chained search for flags in float fields
1237 void VM_findchainflags(prvm_prog_t *prog)
1242 prvm_edict_t *ent, *chain;
1245 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1248 chainfield = PRVM_G_INT(OFS_PARM2);
1250 chainfield = prog->fieldoffsets.chain;
1252 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1254 chain = (prvm_edict_t *)prog->edicts;
1256 f = PRVM_G_INT(OFS_PARM0);
1257 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
1259 ent = PRVM_NEXT_EDICT(prog->edicts);
1260 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1262 prog->xfunction->builtinsprofile++;
1263 if (ent->priv.required->free)
1265 if (!PRVM_E_FLOAT(ent,f))
1267 if (!((prvm_int_t)PRVM_E_FLOAT(ent,f) & s))
1270 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1274 VM_RETURN_EDICT(chain);
1281 string precache_sound (string sample)
1284 void VM_precache_sound(prvm_prog_t *prog)
1288 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1290 s = PRVM_G_STRING(OFS_PARM0);
1291 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1292 //VM_CheckEmptyString(prog, s);
1294 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1296 VM_Warning(prog, "VM_precache_sound: Failed to load %s for %s\n", s, prog->name);
1305 returns the same string as output
1307 does nothing, only used by qcc to build .pak archives
1310 void VM_precache_file(prvm_prog_t *prog)
1312 VM_SAFEPARMCOUNT(1,VM_precache_file);
1313 // precache_file is only used to copy files with qcc, it does nothing
1314 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1324 void VM_coredump(prvm_prog_t *prog)
1326 VM_SAFEPARMCOUNT(0,VM_coredump);
1328 Cbuf_AddText("prvm_edicts ");
1329 Cbuf_AddText(prog->name);
1340 void VM_stackdump(prvm_prog_t *prog)
1342 VM_SAFEPARMCOUNT(0, VM_stackdump);
1344 PRVM_StackTrace(prog);
1355 void VM_crash(prvm_prog_t *prog)
1357 VM_SAFEPARMCOUNT(0, VM_crash);
1359 prog->error_cmd("Crash called by %s",prog->name);
1369 void VM_traceon(prvm_prog_t *prog)
1371 VM_SAFEPARMCOUNT(0,VM_traceon);
1383 void VM_traceoff(prvm_prog_t *prog)
1385 VM_SAFEPARMCOUNT(0,VM_traceoff);
1387 prog->trace = false;
1397 void VM_eprint(prvm_prog_t *prog)
1399 VM_SAFEPARMCOUNT(1,VM_eprint);
1401 PRVM_ED_PrintNum (prog, PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1411 void VM_rint(prvm_prog_t *prog)
1414 VM_SAFEPARMCOUNT(1,VM_rint);
1416 f = PRVM_G_FLOAT(OFS_PARM0);
1418 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1420 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1430 void VM_floor(prvm_prog_t *prog)
1432 VM_SAFEPARMCOUNT(1,VM_floor);
1434 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1444 void VM_ceil(prvm_prog_t *prog)
1446 VM_SAFEPARMCOUNT(1,VM_ceil);
1448 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1456 entity nextent(entity)
1459 void VM_nextent(prvm_prog_t *prog)
1464 VM_SAFEPARMCOUNT(1, VM_nextent);
1466 i = PRVM_G_EDICTNUM(OFS_PARM0);
1469 prog->xfunction->builtinsprofile++;
1471 if (i == prog->num_edicts)
1473 VM_RETURN_EDICT(prog->edicts);
1476 ent = PRVM_EDICT_NUM(i);
1477 if (!ent->priv.required->free)
1479 VM_RETURN_EDICT(ent);
1485 //=============================================================================
1492 changelevel(string map)
1495 void VM_changelevel(prvm_prog_t *prog)
1498 VM_SAFEPARMCOUNT(1, VM_changelevel);
1502 VM_Warning(prog, "VM_changelevel: game is not server (%s)\n", prog->name);
1506 // make sure we don't issue two changelevels
1507 if (svs.changelevel_issued)
1509 svs.changelevel_issued = true;
1511 Cbuf_AddText(va(vabuf, sizeof(vabuf), "changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1521 void VM_sin(prvm_prog_t *prog)
1523 VM_SAFEPARMCOUNT(1,VM_sin);
1524 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1533 void VM_cos(prvm_prog_t *prog)
1535 VM_SAFEPARMCOUNT(1,VM_cos);
1536 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1546 void VM_sqrt(prvm_prog_t *prog)
1548 VM_SAFEPARMCOUNT(1,VM_sqrt);
1549 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1559 void VM_asin(prvm_prog_t *prog)
1561 VM_SAFEPARMCOUNT(1,VM_asin);
1562 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1571 void VM_acos(prvm_prog_t *prog)
1573 VM_SAFEPARMCOUNT(1,VM_acos);
1574 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1583 void VM_atan(prvm_prog_t *prog)
1585 VM_SAFEPARMCOUNT(1,VM_atan);
1586 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1592 float atan2(float,float)
1595 void VM_atan2(prvm_prog_t *prog)
1597 VM_SAFEPARMCOUNT(2,VM_atan2);
1598 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1607 void VM_tan(prvm_prog_t *prog)
1609 VM_SAFEPARMCOUNT(1,VM_tan);
1610 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1617 Returns a vector of length < 1 and > 0
1622 void VM_randomvec(prvm_prog_t *prog)
1625 VM_SAFEPARMCOUNT(0, VM_randomvec);
1627 VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1630 //=============================================================================
1636 float registercvar (string name, string value[, float flags])
1639 void VM_registercvar(prvm_prog_t *prog)
1641 const char *name, *value;
1644 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1646 name = PRVM_G_STRING(OFS_PARM0);
1647 value = PRVM_G_STRING(OFS_PARM1);
1648 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1649 PRVM_G_FLOAT(OFS_RETURN) = 0;
1651 if(flags > CVAR_MAXFLAGSVAL)
1654 // first check to see if it has already been defined
1655 if (Cvar_FindVar (name))
1658 // check for overlap with a command
1659 if (Cmd_Exists (name))
1661 VM_Warning(prog, "VM_registercvar: %s is a command\n", name);
1665 Cvar_Get(name, value, flags, NULL);
1667 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1675 returns the minimum of two supplied floats
1677 float min(float a, float b, ...[float])
1680 void VM_min(prvm_prog_t *prog)
1682 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1683 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1684 if (prog->argc >= 3)
1687 float f = PRVM_G_FLOAT(OFS_PARM0);
1688 for (i = 1;i < prog->argc;i++)
1689 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1690 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1691 PRVM_G_FLOAT(OFS_RETURN) = f;
1694 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1701 returns the maximum of two supplied floats
1703 float max(float a, float b, ...[float])
1706 void VM_max(prvm_prog_t *prog)
1708 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1709 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1710 if (prog->argc >= 3)
1713 float f = PRVM_G_FLOAT(OFS_PARM0);
1714 for (i = 1;i < prog->argc;i++)
1715 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1716 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1717 PRVM_G_FLOAT(OFS_RETURN) = f;
1720 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1727 returns number bounded by supplied range
1729 float bound(float min, float value, float max)
1732 void VM_bound(prvm_prog_t *prog)
1734 VM_SAFEPARMCOUNT(3,VM_bound);
1735 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1742 returns a raised to power b
1744 float pow(float a, float b)
1747 void VM_pow(prvm_prog_t *prog)
1749 VM_SAFEPARMCOUNT(2,VM_pow);
1750 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1753 void VM_log(prvm_prog_t *prog)
1755 VM_SAFEPARMCOUNT(1,VM_log);
1756 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1759 void VM_Files_Init(prvm_prog_t *prog)
1762 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1763 prog->openfiles[i] = NULL;
1766 void VM_Files_CloseAll(prvm_prog_t *prog)
1769 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1771 if (prog->openfiles[i])
1772 FS_Close(prog->openfiles[i]);
1773 prog->openfiles[i] = NULL;
1777 static qfile_t *VM_GetFileHandle(prvm_prog_t *prog, int index)
1779 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1781 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, prog->name);
1784 if (prog->openfiles[index] == NULL)
1786 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, prog->name);
1789 return prog->openfiles[index];
1796 float fopen(string filename, float mode)
1799 // float(string filename, float mode) fopen = #110;
1800 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1801 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1802 void VM_fopen(prvm_prog_t *prog)
1805 const char *modestring, *filename;
1808 VM_SAFEPARMCOUNT(2,VM_fopen);
1810 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1811 if (prog->openfiles[filenum] == NULL)
1813 if (filenum >= PRVM_MAX_OPENFILES)
1815 PRVM_G_FLOAT(OFS_RETURN) = -2;
1816 VM_Warning(prog, "VM_fopen: %s ran out of file handles (%i)\n", prog->name, PRVM_MAX_OPENFILES);
1819 filename = PRVM_G_STRING(OFS_PARM0);
1820 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1823 case 0: // FILE_READ
1825 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
1826 if (prog->openfiles[filenum] == NULL)
1827 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
1829 case 1: // FILE_APPEND
1831 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1833 case 2: // FILE_WRITE
1835 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1838 PRVM_G_FLOAT(OFS_RETURN) = -3;
1839 VM_Warning(prog, "VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", prog->name, mode);
1843 if (prog->openfiles[filenum] == NULL)
1845 PRVM_G_FLOAT(OFS_RETURN) = -1;
1846 if (developer_extra.integer)
1847 VM_Warning(prog, "VM_fopen: %s: %s mode %s failed\n", prog->name, filename, modestring);
1851 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1852 if (developer_extra.integer)
1853 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", prog->name, filename, modestring, filenum);
1854 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin(prog);
1862 fclose(float fhandle)
1865 //void(float fhandle) fclose = #111; // closes a file
1866 void VM_fclose(prvm_prog_t *prog)
1870 VM_SAFEPARMCOUNT(1,VM_fclose);
1872 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1873 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1875 VM_Warning(prog, "VM_fclose: invalid file handle %i used in %s\n", filenum, prog->name);
1878 if (prog->openfiles[filenum] == NULL)
1880 VM_Warning(prog, "VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1883 FS_Close(prog->openfiles[filenum]);
1884 prog->openfiles[filenum] = NULL;
1885 if(prog->openfiles_origin[filenum])
1886 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1887 if (developer_extra.integer)
1888 Con_DPrintf("VM_fclose: %s: #%i closed\n", prog->name, filenum);
1895 string fgets(float fhandle)
1898 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1899 void VM_fgets(prvm_prog_t *prog)
1902 char string[VM_STRINGTEMP_LENGTH];
1905 VM_SAFEPARMCOUNT(1,VM_fgets);
1907 // set the return value regardless of any possible errors
1908 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1910 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1911 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1913 VM_Warning(prog, "VM_fgets: invalid file handle %i used in %s\n", filenum, prog->name);
1916 if (prog->openfiles[filenum] == NULL)
1918 VM_Warning(prog, "VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1924 c = FS_Getc(prog->openfiles[filenum]);
1925 if (c == '\r' || c == '\n' || c < 0)
1927 if (end < VM_STRINGTEMP_LENGTH - 1)
1931 // remove \n following \r
1934 c = FS_Getc(prog->openfiles[filenum]);
1936 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1938 if (developer_extra.integer)
1939 Con_DPrintf("fgets: %s: %s\n", prog->name, string);
1941 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
1948 fputs(float fhandle, string s)
1951 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1952 void VM_fputs(prvm_prog_t *prog)
1955 char string[VM_STRINGTEMP_LENGTH];
1958 VM_SAFEPARMCOUNT(2,VM_fputs);
1960 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1961 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1963 VM_Warning(prog, "VM_fputs: invalid file handle %i used in %s\n", filenum, prog->name);
1966 if (prog->openfiles[filenum] == NULL)
1968 VM_Warning(prog, "VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1971 VM_VarString(prog, 1, string, sizeof(string));
1972 if ((stringlength = (int)strlen(string)))
1973 FS_Write(prog->openfiles[filenum], string, stringlength);
1974 if (developer_extra.integer)
1975 Con_DPrintf("fputs: %s: %s\n", prog->name, string);
1982 writetofile(float fhandle, entity ent)
1985 void VM_writetofile(prvm_prog_t *prog)
1990 VM_SAFEPARMCOUNT(2, VM_writetofile);
1992 file = VM_GetFileHandle(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
1995 VM_Warning(prog, "VM_writetofile: invalid or closed file handle\n");
1999 ent = PRVM_G_EDICT(OFS_PARM1);
2000 if(ent->priv.required->free)
2002 VM_Warning(prog, "VM_writetofile: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2006 PRVM_ED_Write (prog, file, ent);
2009 // KrimZon - DP_QC_ENTITYDATA
2014 float() numentityfields
2015 Return the number of entity fields - NOT offsets
2018 void VM_numentityfields(prvm_prog_t *prog)
2020 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2023 // KrimZon - DP_QC_ENTITYDATA
2028 string(float fieldnum) entityfieldname
2029 Return name of the specified field as a string, or empty if the field is invalid (warning)
2032 void VM_entityfieldname(prvm_prog_t *prog)
2035 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2037 if (i < 0 || i >= prog->numfielddefs)
2039 VM_Warning(prog, "VM_entityfieldname: %s: field index out of bounds\n", prog->name);
2040 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2044 d = &prog->fielddefs[i];
2045 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2048 // KrimZon - DP_QC_ENTITYDATA
2053 float(float fieldnum) entityfieldtype
2056 void VM_entityfieldtype(prvm_prog_t *prog)
2059 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2061 if (i < 0 || i >= prog->numfielddefs)
2063 VM_Warning(prog, "VM_entityfieldtype: %s: field index out of bounds\n", prog->name);
2064 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2068 d = &prog->fielddefs[i];
2069 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)d->type;
2072 // KrimZon - DP_QC_ENTITYDATA
2075 VM_getentityfieldstring
2077 string(float fieldnum, entity ent) getentityfieldstring
2080 void VM_getentityfieldstring(prvm_prog_t *prog)
2082 // put the data into a string
2087 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2088 char valuebuf[MAX_INPUTLINE];
2090 if (i < 0 || i >= prog->numfielddefs)
2092 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2093 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2097 d = &prog->fielddefs[i];
2100 ent = PRVM_G_EDICT(OFS_PARM1);
2101 if(ent->priv.required->free)
2103 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2104 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2107 val = (prvm_eval_t *)(ent->fields.fp + d->ofs);
2109 // if it's 0 or blank, return an empty string
2110 type = d->type & ~DEF_SAVEGLOBAL;
2111 for (j=0 ; j<prvm_type_size[type] ; j++)
2112 if (val->ivector[j])
2114 if (j == prvm_type_size[type])
2116 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2120 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_UglyValueString(prog, (etype_t)d->type, val, valuebuf, sizeof(valuebuf)));
2123 // KrimZon - DP_QC_ENTITYDATA
2126 VM_putentityfieldstring
2128 float(float fieldnum, entity ent, string s) putentityfieldstring
2131 void VM_putentityfieldstring(prvm_prog_t *prog)
2135 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2137 if (i < 0 || i >= prog->numfielddefs)
2139 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2140 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2144 d = &prog->fielddefs[i];
2147 ent = PRVM_G_EDICT(OFS_PARM1);
2148 if(ent->priv.required->free)
2150 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2151 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2155 // parse the string into the value
2156 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(prog, ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2163 float strlen(string s)
2166 //float(string s) strlen = #114; // returns how many characters are in a string
2167 void VM_strlen(prvm_prog_t *prog)
2169 VM_SAFEPARMCOUNT(1,VM_strlen);
2171 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2172 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2175 // DRESK - Decolorized String
2180 string strdecolorize(string s)
2183 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2184 void VM_strdecolorize(prvm_prog_t *prog)
2186 char szNewString[VM_STRINGTEMP_LENGTH];
2187 const char *szString;
2190 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2191 szString = PRVM_G_STRING(OFS_PARM0);
2192 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2193 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2196 // DRESK - String Length (not counting color codes)
2201 float strlennocol(string s)
2204 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2205 // For example, ^2Dresk returns a length of 5
2206 void VM_strlennocol(prvm_prog_t *prog)
2208 const char *szString;
2211 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2213 szString = PRVM_G_STRING(OFS_PARM0);
2215 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2216 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2218 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2221 // DRESK - String to Uppercase and Lowercase
2226 string strtolower(string s)
2229 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2230 void VM_strtolower(prvm_prog_t *prog)
2232 char szNewString[VM_STRINGTEMP_LENGTH];
2233 const char *szString;
2236 VM_SAFEPARMCOUNT(1,VM_strtolower);
2237 szString = PRVM_G_STRING(OFS_PARM0);
2239 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2241 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2248 string strtoupper(string s)
2251 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2252 void VM_strtoupper(prvm_prog_t *prog)
2254 char szNewString[VM_STRINGTEMP_LENGTH];
2255 const char *szString;
2258 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2259 szString = PRVM_G_STRING(OFS_PARM0);
2261 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2263 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2270 string strcat(string,string,...[string])
2273 //string(string s1, string s2) strcat = #115;
2274 // concatenates two strings (for example "abc", "def" would return "abcdef")
2275 // and returns as a tempstring
2276 void VM_strcat(prvm_prog_t *prog)
2278 char s[VM_STRINGTEMP_LENGTH];
2279 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2281 VM_VarString(prog, 0, s, sizeof(s));
2282 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
2289 string substring(string s, float start, float length)
2292 // string(string s, float start, float length) substring = #116;
2293 // returns a section of a string as a tempstring
2294 void VM_substring(prvm_prog_t *prog)
2297 int u_slength = 0, u_start;
2300 char string[VM_STRINGTEMP_LENGTH];
2302 VM_SAFEPARMCOUNT(3,VM_substring);
2305 s = PRVM_G_STRING(OFS_PARM0);
2306 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2307 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2308 slength = strlen(s);
2310 if (start < 0) // FTE_STRINGS feature
2312 start = bound(0, start, slength);
2314 if (length < 0) // FTE_STRINGS feature
2315 length += slength - start + 1;
2316 maxlen = min((int)sizeof(string) - 1, slength - start);
2317 length = bound(0, length, maxlen);
2319 memcpy(string, s + start, length);
2321 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2324 s = PRVM_G_STRING(OFS_PARM0);
2325 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2326 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2328 if (start < 0) // FTE_STRINGS feature
2330 u_slength = u8_strlen(s);
2332 start = bound(0, start, u_slength);
2335 if (length < 0) // FTE_STRINGS feature
2337 if (!u_slength) // it's not calculated when it's not needed above
2338 u_slength = u8_strlen(s);
2339 length += u_slength - start + 1;
2342 // positive start, positive length
2343 u_start = u8_byteofs(s, start, NULL);
2346 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2349 u_length = u8_bytelen(s + u_start, length);
2350 if (u_length >= sizeof(string)-1)
2351 u_length = sizeof(string)-1;
2353 memcpy(string, s + u_start, u_length);
2354 string[u_length] = 0;
2355 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2362 string(string search, string replace, string subject) strreplace = #484;
2365 // replaces all occurrences of search with replace in the string subject, and returns the result
2366 void VM_strreplace(prvm_prog_t *prog)
2369 const char *search, *replace, *subject;
2370 char string[VM_STRINGTEMP_LENGTH];
2371 int search_len, replace_len, subject_len;
2373 VM_SAFEPARMCOUNT(3,VM_strreplace);
2375 search = PRVM_G_STRING(OFS_PARM0);
2376 replace = PRVM_G_STRING(OFS_PARM1);
2377 subject = PRVM_G_STRING(OFS_PARM2);
2379 search_len = (int)strlen(search);
2380 replace_len = (int)strlen(replace);
2381 subject_len = (int)strlen(subject);
2384 for (i = 0; i <= subject_len - search_len; i++)
2386 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2387 if (subject[i+j] != search[j])
2389 if (j == search_len)
2391 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2392 // found it at offset 'i'
2393 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2394 string[si++] = replace[j];
2397 i += search_len - 1;
2401 // the above would subtract 1 from i... so we
2402 // don't do that, but instead output the next
2404 if (si < (int)sizeof(string) - 1)
2405 string[si++] = subject[i];
2410 // in THIS case, we know search_len > 0, thus i < subject_len
2412 if (si < (int)sizeof(string) - 1)
2413 string[si++] = subject[i];
2416 // remaining chars (these cannot match)
2417 for (; i < subject_len; i++)
2418 if (si < (int)sizeof(string) - 1)
2419 string[si++] = subject[i];
2422 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2429 string(string search, string replace, string subject) strireplace = #485;
2432 // case-insensitive version of strreplace
2433 void VM_strireplace(prvm_prog_t *prog)
2436 const char *search, *replace, *subject;
2437 char string[VM_STRINGTEMP_LENGTH];
2438 int search_len, replace_len, subject_len;
2440 VM_SAFEPARMCOUNT(3,VM_strreplace);
2442 search = PRVM_G_STRING(OFS_PARM0);
2443 replace = PRVM_G_STRING(OFS_PARM1);
2444 subject = PRVM_G_STRING(OFS_PARM2);
2446 search_len = (int)strlen(search);
2447 replace_len = (int)strlen(replace);
2448 subject_len = (int)strlen(subject);
2451 for (i = 0; i <= subject_len - search_len; i++)
2453 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2454 if (tolower(subject[i+j]) != tolower(search[j]))
2456 if (j == search_len)
2458 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2459 // found it at offset 'i'
2460 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2461 string[si++] = replace[j];
2464 i += search_len - 1;
2468 // the above would subtract 1 from i... so we
2469 // don't do that, but instead output the next
2471 if (si < (int)sizeof(string) - 1)
2472 string[si++] = subject[i];
2477 // in THIS case, we know search_len > 0, thus i < subject_len
2479 if (si < (int)sizeof(string) - 1)
2480 string[si++] = subject[i];
2483 // remaining chars (these cannot match)
2484 for (; i < subject_len; i++)
2485 if (si < (int)sizeof(string) - 1)
2486 string[si++] = subject[i];
2489 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2496 vector stov(string s)
2499 //vector(string s) stov = #117; // returns vector value from a string
2500 void VM_stov(prvm_prog_t *prog)
2502 char string[VM_STRINGTEMP_LENGTH];
2504 VM_SAFEPARMCOUNT(1,VM_stov);
2506 VM_VarString(prog, 0, string, sizeof(string));
2507 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2514 string strzone(string s)
2517 //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)
2518 void VM_strzone(prvm_prog_t *prog)
2521 char string[VM_STRINGTEMP_LENGTH];
2524 VM_SAFEPARMCOUNT(1,VM_strzone);
2526 VM_VarString(prog, 0, string, sizeof(string));
2527 alloclen = strlen(string) + 1;
2528 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(prog, alloclen, &out);
2529 memcpy(out, string, alloclen);
2539 //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!!!)
2540 void VM_strunzone(prvm_prog_t *prog)
2542 VM_SAFEPARMCOUNT(1,VM_strunzone);
2543 PRVM_FreeString(prog, PRVM_G_INT(OFS_PARM0));
2548 VM_command (used by client and menu)
2550 clientcommand(float client, string s) (for client and menu)
2553 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2554 //this function originally written by KrimZon, made shorter by LordHavoc
2555 void VM_clcommand (prvm_prog_t *prog)
2557 client_t *temp_client;
2560 VM_SAFEPARMCOUNT(2,VM_clcommand);
2562 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2563 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2565 VM_Warning(prog, "VM_clientcommand: %s: invalid client/server is not active !\n", prog->name);
2569 temp_client = host_client;
2570 host_client = svs.clients + i;
2571 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client, true);
2572 host_client = temp_client;
2580 float tokenize(string s)
2583 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2584 //this function originally written by KrimZon, made shorter by LordHavoc
2585 //20040203: rewritten by LordHavoc (no longer uses allocations)
2586 static int num_tokens = 0;
2587 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2588 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2589 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2590 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2591 void VM_tokenize (prvm_prog_t *prog)
2595 VM_SAFEPARMCOUNT(1,VM_tokenize);
2597 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2598 p = tokenize_string;
2603 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2606 // skip whitespace here to find token start pos
2607 while(*p && ISWHITESPACE(*p))
2610 tokens_startpos[num_tokens] = p - tokenize_string;
2611 if(!COM_ParseToken_VM_Tokenize(&p, false))
2613 tokens_endpos[num_tokens] = p - tokenize_string;
2614 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2618 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2621 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2622 void VM_tokenize_console (prvm_prog_t *prog)
2626 VM_SAFEPARMCOUNT(1,VM_tokenize);
2628 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2629 p = tokenize_string;
2634 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2637 // skip whitespace here to find token start pos
2638 while(*p && ISWHITESPACE(*p))
2641 tokens_startpos[num_tokens] = p - tokenize_string;
2642 if(!COM_ParseToken_Console(&p))
2644 tokens_endpos[num_tokens] = p - tokenize_string;
2645 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2649 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2654 VM_tokenizebyseparator
2656 float tokenizebyseparator(string s, string separator1, ...)
2659 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2660 //this function returns the token preceding each instance of a separator (of
2661 //which there can be multiple), and the text following the last separator
2662 //useful for parsing certain kinds of data like IP addresses
2664 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2665 //returns 4 and the tokens "10" "1" "2" "3".
2666 void VM_tokenizebyseparator (prvm_prog_t *prog)
2670 int separatorlen[7];
2671 const char *separators[7];
2674 char tokentext[MAX_INPUTLINE];
2676 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2678 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2679 p = tokenize_string;
2682 for (j = 1;j < prog->argc;j++)
2684 // skip any blank separator strings
2685 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2688 separators[numseparators] = s;
2689 separatorlen[numseparators] = strlen(s);
2696 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2698 token = tokentext + j;
2699 tokens_startpos[num_tokens] = p - tokenize_string;
2703 for (k = 0;k < numseparators;k++)
2705 if (!strncmp(p, separators[k], separatorlen[k]))
2707 p += separatorlen[k];
2711 if (k < numseparators)
2713 if (j < (int)sizeof(tokentext)-1)
2714 tokentext[j++] = *p;
2718 tokens_endpos[num_tokens] = p0 - tokenize_string;
2719 if (j >= (int)sizeof(tokentext))
2722 tokens[num_tokens++] = PRVM_SetTempString(prog, token);
2727 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2730 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2731 //this function originally written by KrimZon, made shorter by LordHavoc
2732 void VM_argv (prvm_prog_t *prog)
2736 VM_SAFEPARMCOUNT(1,VM_argv);
2738 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2741 token_num += num_tokens;
2743 if (token_num >= 0 && token_num < num_tokens)
2744 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2746 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2749 //float(float n) argv_start_index = #515; // returns the start index of a token
2750 void VM_argv_start_index (prvm_prog_t *prog)
2754 VM_SAFEPARMCOUNT(1,VM_argv);
2756 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2759 token_num += num_tokens;
2761 if (token_num >= 0 && token_num < num_tokens)
2762 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2764 PRVM_G_FLOAT(OFS_RETURN) = -1;
2767 //float(float n) argv_end_index = #516; // returns the end index of a token
2768 void VM_argv_end_index (prvm_prog_t *prog)
2772 VM_SAFEPARMCOUNT(1,VM_argv);
2774 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2777 token_num += num_tokens;
2779 if (token_num >= 0 && token_num < num_tokens)
2780 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2782 PRVM_G_FLOAT(OFS_RETURN) = -1;
2792 void VM_isserver(prvm_prog_t *prog)
2794 VM_SAFEPARMCOUNT(0,VM_serverstate);
2796 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2806 void VM_clientcount(prvm_prog_t *prog)
2808 VM_SAFEPARMCOUNT(0,VM_clientcount);
2810 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2820 void VM_clientstate(prvm_prog_t *prog)
2822 VM_SAFEPARMCOUNT(0,VM_clientstate);
2825 switch( cls.state ) {
2826 case ca_uninitialized:
2828 PRVM_G_FLOAT(OFS_RETURN) = 0;
2830 case ca_disconnected:
2831 PRVM_G_FLOAT(OFS_RETURN) = 1;
2834 PRVM_G_FLOAT(OFS_RETURN) = 2;
2837 // should never be reached!
2846 float getostype(prvm_prog_t *prog)
2848 */ // not used at the moment -> not included in the common list
2849 void VM_getostype(prvm_prog_t *prog)
2851 VM_SAFEPARMCOUNT(0,VM_getostype);
2856 OS_MAC - not supported
2860 PRVM_G_FLOAT(OFS_RETURN) = 0;
2861 #elif defined(MACOSX)
2862 PRVM_G_FLOAT(OFS_RETURN) = 2;
2864 PRVM_G_FLOAT(OFS_RETURN) = 1;
2872 float gettime(prvm_prog_t *prog)
2876 float CDAudio_GetPosition(void);
2878 void VM_gettime(prvm_prog_t *prog)
2882 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2886 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) realtime;
2890 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2893 case 0: // GETTIME_FRAMESTART
2894 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2896 case 1: // GETTIME_REALTIME
2897 PRVM_G_FLOAT(OFS_RETURN) = Sys_DirtyTime();
2899 case 2: // GETTIME_HIRES
2900 PRVM_G_FLOAT(OFS_RETURN) = (Sys_DirtyTime() - host_dirtytime);
2902 case 3: // GETTIME_UPTIME
2903 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2906 case 4: // GETTIME_CDTRACK
2907 PRVM_G_FLOAT(OFS_RETURN) = CDAudio_GetPosition();
2911 VM_Warning(prog, "VM_gettime: %s: unsupported timer specified, returning realtime\n", prog->name);
2912 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2922 float getsoundtime(prvm_prog_t *prog)
2926 void VM_getsoundtime (prvm_prog_t *prog)
2928 int entnum, entchannel;
2929 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2931 if (prog == SVVM_prog)
2932 entnum = PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2933 else if (prog == CLVM_prog)
2934 entnum = MAX_EDICTS + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2937 VM_Warning(prog, "VM_getsoundtime: %s: not supported on this progs\n", prog->name);
2938 PRVM_G_FLOAT(OFS_RETURN) = -1;
2941 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2942 entchannel = CHAN_USER2ENGINE(entchannel);
2943 if (!IS_CHAN(entchannel))
2944 VM_Warning(prog, "VM_getsoundtime: %s: bad channel %i\n", prog->name, entchannel);
2945 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)S_GetEntChannelPosition(entnum, entchannel);
2952 string soundlength (string sample)
2955 void VM_soundlength (prvm_prog_t *prog)
2959 VM_SAFEPARMCOUNT(1, VM_soundlength);
2961 s = PRVM_G_STRING(OFS_PARM0);
2962 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2969 loadfromdata(string data)
2972 void VM_loadfromdata(prvm_prog_t *prog)
2974 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2976 PRVM_ED_LoadFromFile(prog, PRVM_G_STRING(OFS_PARM0));
2980 ========================
2983 parseentitydata(entity ent, string data)
2984 ========================
2986 void VM_parseentitydata(prvm_prog_t *prog)
2991 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2993 // get edict and test it
2994 ent = PRVM_G_EDICT(OFS_PARM0);
2995 if (ent->priv.required->free)
2996 prog->error_cmd("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", prog->name, PRVM_NUM_FOR_EDICT(ent));
2998 data = PRVM_G_STRING(OFS_PARM1);
3000 // parse the opening brace
3001 if (!COM_ParseToken_Simple(&data, false, false, true) || com_token[0] != '{' )
3002 prog->error_cmd("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", prog->name, data );
3004 PRVM_ED_ParseEdict (prog, data, ent);
3011 loadfromfile(string file)
3014 void VM_loadfromfile(prvm_prog_t *prog)
3016 const char *filename;
3019 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3021 filename = PRVM_G_STRING(OFS_PARM0);
3022 if (FS_CheckNastyPath(filename, false))
3024 PRVM_G_FLOAT(OFS_RETURN) = -4;
3025 VM_Warning(prog, "VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", prog->name, filename);
3029 // not conform with VM_fopen
3030 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3032 PRVM_G_FLOAT(OFS_RETURN) = -1;
3034 PRVM_ED_LoadFromFile(prog, data);
3045 float mod(float val, float m)
3048 void VM_modulo(prvm_prog_t *prog)
3051 VM_SAFEPARMCOUNT(2,VM_module);
3053 val = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM0);
3054 m = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM1);
3056 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) (val % m);
3059 static void VM_Search_Init(prvm_prog_t *prog)
3062 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3063 prog->opensearches[i] = NULL;
3066 static void VM_Search_Reset(prvm_prog_t *prog)
3069 // reset the fssearch list
3070 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3072 if(prog->opensearches[i])
3073 FS_FreeSearch(prog->opensearches[i]);
3074 prog->opensearches[i] = NULL;
3082 float search_begin(string pattern, float caseinsensitive, float quiet)
3085 void VM_search_begin(prvm_prog_t *prog)
3088 const char *pattern;
3089 int caseinsens, quiet;
3091 VM_SAFEPARMCOUNT(3, VM_search_begin);
3093 pattern = PRVM_G_STRING(OFS_PARM0);
3095 VM_CheckEmptyString(prog, pattern);
3097 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3098 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3100 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3101 if(!prog->opensearches[handle])
3104 if(handle >= PRVM_MAX_OPENSEARCHES)
3106 PRVM_G_FLOAT(OFS_RETURN) = -2;
3107 VM_Warning(prog, "VM_search_begin: %s ran out of search handles (%i)\n", prog->name, PRVM_MAX_OPENSEARCHES);
3111 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3112 PRVM_G_FLOAT(OFS_RETURN) = -1;
3115 prog->opensearches_origin[handle] = PRVM_AllocationOrigin(prog);
3116 PRVM_G_FLOAT(OFS_RETURN) = handle;
3124 void search_end(float handle)
3127 void VM_search_end(prvm_prog_t *prog)
3130 VM_SAFEPARMCOUNT(1, VM_search_end);
3132 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3134 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3136 VM_Warning(prog, "VM_search_end: invalid handle %i used in %s\n", handle, prog->name);
3139 if(prog->opensearches[handle] == NULL)
3141 VM_Warning(prog, "VM_search_end: no such handle %i in %s\n", handle, prog->name);
3145 FS_FreeSearch(prog->opensearches[handle]);
3146 prog->opensearches[handle] = NULL;
3147 if(prog->opensearches_origin[handle])
3148 PRVM_Free((char *)prog->opensearches_origin[handle]);
3155 float search_getsize(float handle)
3158 void VM_search_getsize(prvm_prog_t *prog)
3161 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3163 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3165 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3167 VM_Warning(prog, "VM_search_getsize: invalid handle %i used in %s\n", handle, prog->name);
3170 if(prog->opensearches[handle] == NULL)
3172 VM_Warning(prog, "VM_search_getsize: no such handle %i in %s\n", handle, prog->name);
3176 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3181 VM_search_getfilename
3183 string search_getfilename(float handle, float num)
3186 void VM_search_getfilename(prvm_prog_t *prog)
3188 int handle, filenum;
3189 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3191 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3192 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3194 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3196 VM_Warning(prog, "VM_search_getfilename: invalid handle %i used in %s\n", handle, prog->name);
3199 if(prog->opensearches[handle] == NULL)
3201 VM_Warning(prog, "VM_search_getfilename: no such handle %i in %s\n", handle, prog->name);
3204 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3206 VM_Warning(prog, "VM_search_getfilename: invalid filenum %i in %s\n", filenum, prog->name);
3210 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, prog->opensearches[handle]->filenames[filenum]);
3217 string chr(float ascii)
3220 void VM_chr(prvm_prog_t *prog)
3224 VM_SAFEPARMCOUNT(1, VM_chr);
3226 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3229 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3234 VM_SAFEPARMCOUNT(1, VM_chr);
3236 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3238 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3241 //=============================================================================
3242 // Draw builtins (client & menu)
3248 float iscachedpic(string pic)
3251 void VM_iscachedpic(prvm_prog_t *prog)
3253 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3255 // drawq hasnt such a function, thus always return true
3256 PRVM_G_FLOAT(OFS_RETURN) = false;
3263 string precache_pic(string pic)
3266 #define PRECACHE_PIC_FROMWAD 1 /* FTEQW, not supported here */
3267 #define PRECACHE_PIC_NOTPERSISTENT 2
3268 //#define PRECACHE_PIC_NOCLAMP 4
3269 #define PRECACHE_PIC_MIPMAP 8
3270 void VM_precache_pic(prvm_prog_t *prog)
3275 VM_SAFEPARMCOUNTRANGE(1, 2, VM_precache_pic);
3277 s = PRVM_G_STRING(OFS_PARM0);
3278 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3279 VM_CheckEmptyString(prog, s);
3283 int f = PRVM_G_FLOAT(OFS_PARM1);
3284 if(f & PRECACHE_PIC_NOTPERSISTENT)
3285 flags |= CACHEPICFLAG_NOTPERSISTENT;
3286 //if(f & PRECACHE_PIC_NOCLAMP)
3287 // flags |= CACHEPICFLAG_NOCLAMP;
3288 if(f & PRECACHE_PIC_MIPMAP)
3289 flags |= CACHEPICFLAG_MIPMAP;
3292 // AK Draw_CachePic is supposed to always return a valid pointer
3293 if( Draw_CachePic_Flags(s, flags)->tex == r_texture_notexture )
3294 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3304 void VM_freepic(prvm_prog_t *prog)
3308 VM_SAFEPARMCOUNT(1,VM_freepic);
3310 s = PRVM_G_STRING(OFS_PARM0);
3311 VM_CheckEmptyString(prog, s);
3316 static void getdrawfontscale(prvm_prog_t *prog, float *sx, float *sy)
3320 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3321 if(VectorLength2(v) > 0)
3328 static dp_font_t *getdrawfont(prvm_prog_t *prog)
3330 int f = (int) PRVM_drawglobalfloat(drawfont);
3331 if(f < 0 || f >= dp_fonts.maxsize)
3332 return FONT_DEFAULT;
3333 return &dp_fonts.f[f];
3340 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3343 void VM_drawcharacter(prvm_prog_t *prog)
3345 prvm_vec_t *pos,*scale,*rgb;
3349 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3351 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3354 PRVM_G_FLOAT(OFS_RETURN) = -1;
3355 VM_Warning(prog, "VM_drawcharacter: %s passed null character !\n",prog->name);
3359 pos = PRVM_G_VECTOR(OFS_PARM0);
3360 scale = PRVM_G_VECTOR(OFS_PARM2);
3361 rgb = PRVM_G_VECTOR(OFS_PARM3);
3362 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3364 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3366 PRVM_G_FLOAT(OFS_RETURN) = -2;
3367 VM_Warning(prog, "VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3371 if(pos[2] || scale[2])
3372 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")));
3374 if(!scale[0] || !scale[1])
3376 PRVM_G_FLOAT(OFS_RETURN) = -3;
3377 VM_Warning(prog, "VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3381 getdrawfontscale(prog, &sx, &sy);
3382 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));
3383 PRVM_G_FLOAT(OFS_RETURN) = 1;
3390 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3393 void VM_drawstring(prvm_prog_t *prog)
3395 prvm_vec_t *pos,*scale,*rgb;
3399 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3401 string = PRVM_G_STRING(OFS_PARM1);
3402 pos = PRVM_G_VECTOR(OFS_PARM0);
3403 scale = PRVM_G_VECTOR(OFS_PARM2);
3404 rgb = PRVM_G_VECTOR(OFS_PARM3);
3405 if (prog->argc >= 6)
3406 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3408 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3410 PRVM_G_FLOAT(OFS_RETURN) = -2;
3411 VM_Warning(prog, "VM_drawstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3415 if(!scale[0] || !scale[1])
3417 PRVM_G_FLOAT(OFS_RETURN) = -3;
3418 VM_Warning(prog, "VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3422 if(pos[2] || scale[2])
3423 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")));
3425 getdrawfontscale(prog, &sx, &sy);
3426 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));
3427 //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);
3428 PRVM_G_FLOAT(OFS_RETURN) = 1;
3433 VM_drawcolorcodedstring
3435 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3437 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3440 void VM_drawcolorcodedstring(prvm_prog_t *prog)
3442 prvm_vec_t *pos, *scale;
3446 float sx, sy, alpha;
3448 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3450 if (prog->argc == 6) // full 6 parms, like normal drawstring
3452 pos = PRVM_G_VECTOR(OFS_PARM0);
3453 string = PRVM_G_STRING(OFS_PARM1);
3454 scale = PRVM_G_VECTOR(OFS_PARM2);
3455 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3456 alpha = PRVM_G_FLOAT(OFS_PARM4);
3457 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3461 pos = PRVM_G_VECTOR(OFS_PARM0);
3462 string = PRVM_G_STRING(OFS_PARM1);
3463 scale = PRVM_G_VECTOR(OFS_PARM2);
3467 alpha = PRVM_G_FLOAT(OFS_PARM3);
3468 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3471 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3473 PRVM_G_FLOAT(OFS_RETURN) = -2;
3474 VM_Warning(prog, "VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3478 if(!scale[0] || !scale[1])
3480 PRVM_G_FLOAT(OFS_RETURN) = -3;
3481 VM_Warning(prog, "VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3485 if(pos[2] || scale[2])
3486 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")));
3488 getdrawfontscale(prog, &sx, &sy);
3489 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));
3490 if (prog->argc == 6) // also return vector of last color
3491 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3493 PRVM_G_FLOAT(OFS_RETURN) = 1;
3499 float stringwidth(string text, float allowColorCodes, float size)
3502 void VM_stringwidth(prvm_prog_t *prog)
3506 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3510 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3512 getdrawfontscale(prog, &sx, &sy);
3515 Vector2Copy(PRVM_G_VECTOR(OFS_PARM2), szv);
3520 // we want the width for 8x8 font size, divided by 8
3521 Vector2Set(szv, 8, 8);
3523 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3524 if(sx >= 0.9 && sx <= 1.1)
3532 string = PRVM_G_STRING(OFS_PARM0);
3533 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3535 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(prog), 1000000000) * mult;
3539 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3547 string = PRVM_G_STRING(OFS_PARM0);
3548 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3550 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3558 float findfont(string s)
3562 static float getdrawfontnum(const char *fontname)
3566 for(i = 0; i < dp_fonts.maxsize; ++i)
3567 if(!strcmp(dp_fonts.f[i].title, fontname))
3572 void VM_findfont(prvm_prog_t *prog)
3574 VM_SAFEPARMCOUNT(1,VM_findfont);
3575 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3582 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3586 void VM_loadfont(prvm_prog_t *prog)
3588 const char *fontname, *filelist, *sizes, *c, *cm;
3589 char mainfont[MAX_QPATH];
3591 float sz, scale, voffset;
3594 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3596 fontname = PRVM_G_STRING(OFS_PARM0);
3598 fontname = "default";
3600 filelist = PRVM_G_STRING(OFS_PARM1);
3602 filelist = "gfx/conchars";
3604 sizes = PRVM_G_STRING(OFS_PARM2);
3610 if (prog->argc >= 4)
3612 i = PRVM_G_FLOAT(OFS_PARM3);
3613 if (i >= 0 && i < dp_fonts.maxsize)
3616 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3620 f = FindFont(fontname, true);
3623 PRVM_G_FLOAT(OFS_RETURN) = -1;
3624 return; // something go wrong
3627 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3628 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3630 // first font is handled "normally"
3631 c = strchr(filelist, ':');
3632 cm = strchr(filelist, ',');
3633 if(c && (!cm || c < cm))
3634 f->req_face = atoi(c+1);
3640 if(!c || (c - filelist) > MAX_QPATH)
3641 strlcpy(mainfont, filelist, sizeof(mainfont));
3644 memcpy(mainfont, filelist, c - filelist);
3645 mainfont[c - filelist] = 0;
3649 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3651 c = strchr(filelist, ',');
3657 c = strchr(filelist, ':');
3658 cm = strchr(filelist, ',');
3659 if(c && (!cm || c < cm))
3660 f->fallback_faces[i] = atoi(c+1);
3663 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3666 if(!c || (c-filelist) > MAX_QPATH)
3668 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3672 memcpy(f->fallbacks[i], filelist, c - filelist);
3673 f->fallbacks[i][c - filelist] = 0;
3678 for(i = 0; i < MAX_FONT_SIZES; ++i)
3679 f->req_sizes[i] = -1;
3680 for (numsizes = 0,c = sizes;;)
3682 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3684 sz = atof(com_token);
3686 if (sz < 0.001f || sz > 1000.0f)
3688 VM_Warning(prog, "VM_loadfont: crap size %s", com_token);
3692 if (numsizes == MAX_FONT_SIZES)
3694 VM_Warning(prog, "VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3697 f->req_sizes[numsizes] = sz;
3701 // additional scale/hoffset parms
3704 if (prog->argc >= 5)
3706 scale = PRVM_G_FLOAT(OFS_PARM4);
3710 if (prog->argc >= 6)
3711 voffset = PRVM_G_FLOAT(OFS_PARM5);
3714 LoadFont(true, mainfont, f, scale, voffset);
3716 // return index of loaded font
3717 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3724 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3727 void VM_drawpic(prvm_prog_t *prog)
3729 const char *picname;
3730 prvm_vec_t *size, *pos, *rgb;
3733 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3735 picname = PRVM_G_STRING(OFS_PARM1);
3736 VM_CheckEmptyString(prog, picname);
3738 // is pic cached ? no function yet for that
3741 PRVM_G_FLOAT(OFS_RETURN) = -4;
3742 VM_Warning(prog, "VM_drawpic: %s: %s not cached !\n", prog->name, picname);
3746 pos = PRVM_G_VECTOR(OFS_PARM0);
3747 size = PRVM_G_VECTOR(OFS_PARM2);
3748 rgb = PRVM_G_VECTOR(OFS_PARM3);
3749 if (prog->argc >= 6)
3750 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3752 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3754 PRVM_G_FLOAT(OFS_RETURN) = -2;
3755 VM_Warning(prog, "VM_drawpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3759 if(pos[2] || size[2])
3760 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")));
3762 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);
3763 PRVM_G_FLOAT(OFS_RETURN) = 1;
3769 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3772 void VM_drawrotpic(prvm_prog_t *prog)
3774 const char *picname;
3775 prvm_vec_t *size, *pos, *org, *rgb;
3778 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3780 picname = PRVM_G_STRING(OFS_PARM1);
3781 VM_CheckEmptyString(prog, picname);
3783 // is pic cached ? no function yet for that
3786 PRVM_G_FLOAT(OFS_RETURN) = -4;
3787 VM_Warning(prog, "VM_drawrotpic: %s: %s not cached !\n", prog->name, picname);
3791 pos = PRVM_G_VECTOR(OFS_PARM0);
3792 size = PRVM_G_VECTOR(OFS_PARM2);
3793 org = PRVM_G_VECTOR(OFS_PARM3);
3794 rgb = PRVM_G_VECTOR(OFS_PARM5);
3795 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3797 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3799 PRVM_G_FLOAT(OFS_RETURN) = -2;
3800 VM_Warning(prog, "VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3804 if(pos[2] || size[2] || org[2])
3805 VM_Warning(prog, "VM_drawrotpic: z value from pos/size/org discarded\n");
3807 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);
3808 PRVM_G_FLOAT(OFS_RETURN) = 1;
3814 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3818 void VM_drawsubpic(prvm_prog_t *prog)
3820 const char *picname;
3821 prvm_vec_t *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3824 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3826 picname = PRVM_G_STRING(OFS_PARM2);
3827 VM_CheckEmptyString(prog, picname);
3829 // is pic cached ? no function yet for that
3832 PRVM_G_FLOAT(OFS_RETURN) = -4;
3833 VM_Warning(prog, "VM_drawsubpic: %s: %s not cached !\n", prog->name, picname);
3837 pos = PRVM_G_VECTOR(OFS_PARM0);
3838 size = PRVM_G_VECTOR(OFS_PARM1);
3839 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3840 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3841 rgb = PRVM_G_VECTOR(OFS_PARM5);
3842 alpha = PRVM_G_FLOAT(OFS_PARM6);
3843 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3845 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3847 PRVM_G_FLOAT(OFS_RETURN) = -2;
3848 VM_Warning(prog, "VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3852 if(pos[2] || size[2])
3853 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")));
3855 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3857 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3858 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3859 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3860 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3862 PRVM_G_FLOAT(OFS_RETURN) = 1;
3869 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3872 void VM_drawfill(prvm_prog_t *prog)
3874 prvm_vec_t *size, *pos, *rgb;
3877 VM_SAFEPARMCOUNT(5,VM_drawfill);
3880 pos = PRVM_G_VECTOR(OFS_PARM0);
3881 size = PRVM_G_VECTOR(OFS_PARM1);
3882 rgb = PRVM_G_VECTOR(OFS_PARM2);
3883 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3885 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3887 PRVM_G_FLOAT(OFS_RETURN) = -2;
3888 VM_Warning(prog, "VM_drawfill: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3892 if(pos[2] || size[2])
3893 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")));
3895 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3896 PRVM_G_FLOAT(OFS_RETURN) = 1;
3903 drawsetcliparea(float x, float y, float width, float height)
3906 void VM_drawsetcliparea(prvm_prog_t *prog)
3909 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3911 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3912 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3913 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3914 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3916 DrawQ_SetClipArea(x, y, w, h);
3921 VM_drawresetcliparea
3926 void VM_drawresetcliparea(prvm_prog_t *prog)
3928 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3930 DrawQ_ResetClipArea();
3937 vector getimagesize(string pic)
3940 void VM_getimagesize(prvm_prog_t *prog)
3945 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3947 p = PRVM_G_STRING(OFS_PARM0);
3948 VM_CheckEmptyString(prog, p);
3950 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3951 if( pic->tex == r_texture_notexture )
3953 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3954 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3958 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3959 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3961 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3968 string keynumtostring(float keynum)
3971 void VM_keynumtostring (prvm_prog_t *prog)
3974 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3976 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, sizeof(tinystr)));
3981 VM_findkeysforcommand
3983 string findkeysforcommand(string command, float bindmap)
3985 the returned string is an altstring
3988 #define FKFC_NUMKEYS 5
3989 void M_FindKeysForCommand(const char *command, int *keys);
3990 void VM_findkeysforcommand(prvm_prog_t *prog)
3993 char ret[VM_STRINGTEMP_LENGTH];
3994 int keys[FKFC_NUMKEYS];
3999 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
4001 cmd = PRVM_G_STRING(OFS_PARM0);
4003 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4005 bindmap = 0; // consistent to "bind"
4007 VM_CheckEmptyString(prog, cmd);
4009 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
4012 for(i = 0; i < FKFC_NUMKEYS; i++)
4013 strlcat(ret, va(vabuf, sizeof(vabuf), " \'%i\'", keys[i]), sizeof(ret));
4015 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, ret);
4022 float stringtokeynum(string key)
4025 void VM_stringtokeynum (prvm_prog_t *prog)
4027 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
4029 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
4036 string getkeybind(float key, float bindmap)
4039 void VM_getkeybind (prvm_prog_t *prog)
4042 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4044 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4046 bindmap = 0; // consistent to "bind"
4048 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4055 float setkeybind(float key, string cmd, float bindmap)
4058 void VM_setkeybind (prvm_prog_t *prog)
4061 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4063 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4065 bindmap = 0; // consistent to "bind"
4067 PRVM_G_FLOAT(OFS_RETURN) = 0;
4068 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4069 PRVM_G_FLOAT(OFS_RETURN) = 1;
4076 vector getbindmaps()
4079 void VM_getbindmaps (prvm_prog_t *prog)
4082 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4083 Key_GetBindMap(&fg, &bg);
4084 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4085 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4086 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4093 float setbindmaps(vector bindmap)
4096 void VM_setbindmaps (prvm_prog_t *prog)
4098 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4099 PRVM_G_FLOAT(OFS_RETURN) = 0;
4100 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4101 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4102 PRVM_G_FLOAT(OFS_RETURN) = 1;
4105 // CL_Video interface functions
4108 ========================
4111 float cin_open(string file, string name)
4112 ========================
4114 void VM_cin_open(prvm_prog_t *prog)
4119 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4121 file = PRVM_G_STRING( OFS_PARM0 );
4122 name = PRVM_G_STRING( OFS_PARM1 );
4124 VM_CheckEmptyString(prog, file );
4125 VM_CheckEmptyString(prog, name );
4127 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4128 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4130 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4134 ========================
4137 void cin_close(string name)
4138 ========================
4140 void VM_cin_close(prvm_prog_t *prog)
4144 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4146 name = PRVM_G_STRING( OFS_PARM0 );
4147 VM_CheckEmptyString(prog, name );
4149 CL_CloseVideo( CL_GetVideoByName( name ) );
4153 ========================
4155 void cin_setstate(string name, float type)
4156 ========================
4158 void VM_cin_setstate(prvm_prog_t *prog)
4161 clvideostate_t state;
4164 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4166 name = PRVM_G_STRING( OFS_PARM0 );
4167 VM_CheckEmptyString(prog, name );
4169 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4171 video = CL_GetVideoByName( name );
4172 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4173 CL_SetVideoState( video, state );
4177 ========================
4180 float cin_getstate(string name)
4181 ========================
4183 void VM_cin_getstate(prvm_prog_t *prog)
4188 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4190 name = PRVM_G_STRING( OFS_PARM0 );
4191 VM_CheckEmptyString(prog, name );
4193 video = CL_GetVideoByName( name );
4195 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4197 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4201 ========================
4204 void cin_restart(string name)
4205 ========================
4207 void VM_cin_restart(prvm_prog_t *prog)
4212 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4214 name = PRVM_G_STRING( OFS_PARM0 );
4215 VM_CheckEmptyString(prog, name );
4217 video = CL_GetVideoByName( name );
4219 CL_RestartVideo( video );
4223 ========================
4226 float[bool] gecko_create( string name )
4227 ========================
4229 void VM_gecko_create(prvm_prog_t *prog) {
4231 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4235 ========================
4238 void gecko_destroy( string name )
4239 ========================
4241 void VM_gecko_destroy(prvm_prog_t *prog) {
4246 ========================
4249 void gecko_navigate( string name, string URI )
4250 ========================
4252 void VM_gecko_navigate(prvm_prog_t *prog) {
4257 ========================
4260 float[bool] gecko_keyevent( string name, float key, float eventtype )
4261 ========================
4263 void VM_gecko_keyevent(prvm_prog_t *prog) {
4265 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4269 ========================
4272 void gecko_mousemove( string name, float x, float y )
4273 ========================
4275 void VM_gecko_movemouse(prvm_prog_t *prog) {
4281 ========================
4284 void gecko_resize( string name, float w, float h )
4285 ========================
4287 void VM_gecko_resize(prvm_prog_t *prog) {
4293 ========================
4294 VM_gecko_get_texture_extent
4296 vector gecko_get_texture_extent( string name )
4297 ========================
4299 void VM_gecko_get_texture_extent(prvm_prog_t *prog) {
4301 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4302 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4311 Writes new values for v_forward, v_up, and v_right based on angles
4312 void makevectors(vector angle)
4315 void VM_makevectors (prvm_prog_t *prog)
4317 vec3_t angles, forward, right, up;
4318 VM_SAFEPARMCOUNT(1, VM_makevectors);
4319 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), angles);
4320 AngleVectors(angles, forward, right, up);
4321 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
4322 VectorCopy(right, PRVM_gameglobalvector(v_right));
4323 VectorCopy(up, PRVM_gameglobalvector(v_up));
4330 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4331 vectorvectors(vector)
4334 void VM_vectorvectors (prvm_prog_t *prog)
4336 vec3_t forward, right, up;
4337 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4338 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), forward);
4339 VectorVectors(forward, right, up);
4340 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
4341 VectorCopy(right, PRVM_gameglobalvector(v_right));
4342 VectorCopy(up, PRVM_gameglobalvector(v_up));
4346 ========================
4349 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4350 ========================
4352 void VM_drawline (prvm_prog_t *prog)
4354 prvm_vec_t *c1, *c2, *rgb;
4356 unsigned char flags;
4358 VM_SAFEPARMCOUNT(6, VM_drawline);
4359 width = PRVM_G_FLOAT(OFS_PARM0);
4360 c1 = PRVM_G_VECTOR(OFS_PARM1);
4361 c2 = PRVM_G_VECTOR(OFS_PARM2);
4362 rgb = PRVM_G_VECTOR(OFS_PARM3);
4363 alpha = PRVM_G_FLOAT(OFS_PARM4);
4364 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4365 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4368 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4369 void VM_bitshift (prvm_prog_t *prog)
4372 VM_SAFEPARMCOUNT(2, VM_bitshift);
4374 n1 = (prvm_int_t)fabs((prvm_vec_t)((prvm_int_t)PRVM_G_FLOAT(OFS_PARM0)));
4375 n2 = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
4377 PRVM_G_FLOAT(OFS_RETURN) = n1;
4380 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4382 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4385 ////////////////////////////////////////
4386 // AltString functions
4387 ////////////////////////////////////////
4390 ========================
4393 float altstr_count(string)
4394 ========================
4396 void VM_altstr_count(prvm_prog_t *prog)
4398 const char *altstr, *pos;
4401 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4403 altstr = PRVM_G_STRING( OFS_PARM0 );
4404 //VM_CheckEmptyString(prog, altstr );
4406 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4407 if( *pos == '\\' ) {
4411 } else if( *pos == '\'' ) {
4416 PRVM_G_FLOAT( OFS_RETURN ) = (prvm_vec_t) (count / 2);
4420 ========================
4423 string altstr_prepare(string)
4424 ========================
4426 void VM_altstr_prepare(prvm_prog_t *prog)
4429 const char *instr, *in;
4431 char outstr[VM_STRINGTEMP_LENGTH];
4433 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4435 instr = PRVM_G_STRING( OFS_PARM0 );
4437 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4446 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4450 ========================
4453 string altstr_get(string, float)
4454 ========================
4456 void VM_altstr_get(prvm_prog_t *prog)
4458 const char *altstr, *pos;
4461 char outstr[VM_STRINGTEMP_LENGTH];
4463 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4465 altstr = PRVM_G_STRING( OFS_PARM0 );
4467 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4468 count = count * 2 + 1;
4470 for( pos = altstr ; *pos && count ; pos++ )
4471 if( *pos == '\\' ) {
4474 } else if( *pos == '\'' )
4478 PRVM_G_INT( OFS_RETURN ) = 0;
4482 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4483 if( *pos == '\\' ) {
4488 } else if( *pos == '\'' )
4494 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4498 ========================
4501 string altstr_set(string altstr, float num, string set)
4502 ========================
4504 void VM_altstr_set(prvm_prog_t *prog)
4507 const char *altstr, *str;
4510 char outstr[VM_STRINGTEMP_LENGTH];
4512 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4514 altstr = PRVM_G_STRING( OFS_PARM0 );
4516 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4518 str = PRVM_G_STRING( OFS_PARM2 );
4521 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4526 } else if( *in == '\'' ) {
4531 for( ; *str; *out++ = *str++ );
4532 // now jump over the old content
4534 if( *in == '\'' || (*in == '\\' && !*++in) )
4537 strlcpy(out, in, outstr + sizeof(outstr) - out);
4538 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4542 ========================
4545 string altstr_ins(string altstr, float num, string set)
4546 ========================
4548 void VM_altstr_ins(prvm_prog_t *prog)
4554 char outstr[VM_STRINGTEMP_LENGTH];
4556 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4558 in = PRVM_G_STRING( OFS_PARM0 );
4559 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4560 set = PRVM_G_STRING( OFS_PARM2 );
4563 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4568 } else if( *in == '\'' ) {
4573 for( ; *set ; *out++ = *set++ );
4576 strlcpy(out, in, outstr + sizeof(outstr) - out);
4577 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4581 ////////////////////////////////////////
4582 // BufString functions
4583 ////////////////////////////////////////
4584 //[515]: string buffers support
4586 static size_t stringbuffers_sortlength;
4588 static void BufStr_Expand(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex)
4590 if (stringbuffer->max_strings <= strindex)
4592 char **oldstrings = stringbuffer->strings;
4593 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4594 while (stringbuffer->max_strings <= strindex)
4595 stringbuffer->max_strings *= 2;
4596 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4597 if (stringbuffer->num_strings > 0)
4598 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4600 Mem_Free(oldstrings);
4604 static void BufStr_Shrink(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
4606 // reduce num_strings if there are empty string slots at the end
4607 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4608 stringbuffer->num_strings--;
4610 // if empty, free the string pointer array
4611 if (stringbuffer->num_strings == 0)
4613 stringbuffer->max_strings = 0;
4614 if (stringbuffer->strings)
4615 Mem_Free(stringbuffer->strings);
4616 stringbuffer->strings = NULL;
4620 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4623 a = *((const char **) in1);
4624 b = *((const char **) in2);
4625 if(!a || !a[0]) return 1;
4626 if(!b || !b[0]) return -1;
4627 return strncmp(a, b, stringbuffers_sortlength);
4630 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4633 a = *((const char **) in1);
4634 b = *((const char **) in2);
4635 if(!a || !a[0]) return 1;
4636 if(!b || !b[0]) return -1;
4637 return strncmp(b, a, stringbuffers_sortlength);
4640 prvm_stringbuffer_t *BufStr_FindCreateReplace (prvm_prog_t *prog, int bufindex, int flags, char *format)
4642 prvm_stringbuffer_t *stringbuffer;
4648 // find buffer with wanted index
4649 if (bufindex < (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray))
4651 if ( (stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, bufindex)) )
4653 if (stringbuffer->flags & STRINGBUFFER_TEMP)
4654 stringbuffer->flags = flags; // created but has not been used yet
4655 return stringbuffer;
4660 // allocate new buffer with wanted index
4663 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4664 stringbuffer->flags = STRINGBUFFER_TEMP;
4665 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4668 stringbuffer->flags = flags; // mark as used
4672 return stringbuffer;
4675 void BufStr_Set(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex, const char *str)
4679 if (!stringbuffer || strindex < 0)
4682 BufStr_Expand(prog, stringbuffer, strindex);
4683 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4684 if (stringbuffer->strings[strindex])
4685 Mem_Free(stringbuffer->strings[strindex]);
4686 stringbuffer->strings[strindex] = NULL;
4690 // not the NULL string!
4691 alloclen = strlen(str) + 1;
4692 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4693 memcpy(stringbuffer->strings[strindex], str, alloclen);
4696 BufStr_Shrink(prog, stringbuffer);
4699 void BufStr_Del(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
4706 for (i = 0;i < stringbuffer->num_strings;i++)
4707 if (stringbuffer->strings[i])
4708 Mem_Free(stringbuffer->strings[i]);
4709 if (stringbuffer->strings)
4710 Mem_Free(stringbuffer->strings);
4711 if(stringbuffer->origin)
4712 PRVM_Free((char *)stringbuffer->origin);
4713 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4716 void BufStr_Flush(prvm_prog_t *prog)
4718 prvm_stringbuffer_t *stringbuffer;
4721 numbuffers = Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray);
4722 for (i = 0; i < numbuffers; i++)
4723 if ( (stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i)) )
4724 BufStr_Del(prog, stringbuffer);
4725 Mem_ExpandableArray_NewArray(&prog->stringbuffersarray, prog->progs_mempool, sizeof(prvm_stringbuffer_t), 64);
4729 ========================
4731 creates new buffer, and returns it's index, returns -1 if failed
4732 float buf_create(prvm_prog_t *prog) = #460;
4733 float newbuf(string format, float flags) = #460;
4734 ========================
4737 void VM_buf_create (prvm_prog_t *prog)
4739 prvm_stringbuffer_t *stringbuffer;
4742 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4744 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4745 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4747 PRVM_G_FLOAT(OFS_RETURN) = -1;
4750 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4751 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4752 stringbuffer->origin = PRVM_AllocationOrigin(prog);
4753 // optional flags parm
4754 if (prog->argc >= 2)
4755 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & STRINGBUFFER_QCFLAGS;
4756 PRVM_G_FLOAT(OFS_RETURN) = i;
4762 ========================
4764 deletes buffer and all strings in it
4765 void buf_del(float bufhandle) = #461;
4766 ========================
4768 void VM_buf_del (prvm_prog_t *prog)
4770 prvm_stringbuffer_t *stringbuffer;
4771 VM_SAFEPARMCOUNT(1, VM_buf_del);
4772 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4774 BufStr_Del(prog, stringbuffer);
4777 VM_Warning(prog, "VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4783 ========================
4785 how many strings are stored in buffer
4786 float buf_getsize(float bufhandle) = #462;
4787 ========================
4789 void VM_buf_getsize (prvm_prog_t *prog)
4791 prvm_stringbuffer_t *stringbuffer;
4792 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4794 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4797 PRVM_G_FLOAT(OFS_RETURN) = -1;
4798 VM_Warning(prog, "VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4802 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4806 ========================
4808 copy all content from one buffer to another, make sure it exists
4809 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4810 ========================
4812 void VM_buf_copy (prvm_prog_t *prog)
4814 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4816 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4818 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4819 if(!srcstringbuffer)
4821 VM_Warning(prog, "VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4824 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4825 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4827 VM_Warning(prog, "VM_buf_copy: source == destination (%i) in %s\n", i, prog->name);
4830 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4831 if(!dststringbuffer)
4833 VM_Warning(prog, "VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4837 for (i = 0;i < dststringbuffer->num_strings;i++)
4838 if (dststringbuffer->strings[i])
4839 Mem_Free(dststringbuffer->strings[i]);
4840 if (dststringbuffer->strings)
4841 Mem_Free(dststringbuffer->strings);
4842 *dststringbuffer = *srcstringbuffer;
4843 if (dststringbuffer->max_strings)
4844 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4846 for (i = 0;i < dststringbuffer->num_strings;i++)
4848 if (srcstringbuffer->strings[i])
4851 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4852 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4853 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4859 ========================
4861 sort buffer by beginnings of strings (cmplength defaults it's length)
4862 "backward == TRUE" means that sorting goes upside-down
4863 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4864 ========================
4866 void VM_buf_sort (prvm_prog_t *prog)
4868 prvm_stringbuffer_t *stringbuffer;
4869 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4871 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4874 VM_Warning(prog, "VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4877 if(stringbuffer->num_strings <= 0)
4879 VM_Warning(prog, "VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4882 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4883 if(stringbuffers_sortlength <= 0)
4884 stringbuffers_sortlength = 0x7FFFFFFF;
4886 if(!PRVM_G_FLOAT(OFS_PARM2))
4887 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4889 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4891 BufStr_Shrink(prog, stringbuffer);
4895 ========================
4897 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4898 string buf_implode(float bufhandle, string glue) = #465;
4899 ========================
4901 void VM_buf_implode (prvm_prog_t *prog)
4903 prvm_stringbuffer_t *stringbuffer;
4904 char k[VM_STRINGTEMP_LENGTH];
4908 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4910 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4911 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4914 VM_Warning(prog, "VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4917 if(!stringbuffer->num_strings)
4919 sep = PRVM_G_STRING(OFS_PARM1);
4921 for(l = i = 0;i < stringbuffer->num_strings;i++)
4923 if(stringbuffer->strings[i])
4925 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4926 if (l >= sizeof(k) - 1)
4928 strlcat(k, sep, sizeof(k));
4929 strlcat(k, stringbuffer->strings[i], sizeof(k));
4932 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, k);
4936 ========================
4938 get a string from buffer, returns tempstring, dont str_unzone it!
4939 string bufstr_get(float bufhandle, float string_index) = #465;
4940 ========================
4942 void VM_bufstr_get (prvm_prog_t *prog)
4944 prvm_stringbuffer_t *stringbuffer;
4946 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4948 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4949 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4952 VM_Warning(prog, "VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4955 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4958 // VM_Warning(prog, "VM_bufstr_get: invalid string index %i used in %s\n", strindex, prog->name);
4961 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4962 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, stringbuffer->strings[strindex]);
4966 ========================
4968 copies a string into selected slot of buffer
4969 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4970 ========================
4972 void VM_bufstr_set (prvm_prog_t *prog)
4975 prvm_stringbuffer_t *stringbuffer;
4978 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4980 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4983 VM_Warning(prog, "VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4986 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4987 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4989 VM_Warning(prog, "VM_bufstr_set: invalid string index %i used in %s\n", strindex, prog->name);
4993 news = PRVM_G_STRING(OFS_PARM2);
4994 BufStr_Set(prog, stringbuffer, strindex, news);
4998 ========================
5000 adds string to buffer in first free slot and returns its index
5001 "order == TRUE" means that string will be added after last "full" slot
5002 float bufstr_add(float bufhandle, string str, float order) = #467;
5003 ========================
5005 void VM_bufstr_add (prvm_prog_t *prog)
5007 int order, strindex;
5008 prvm_stringbuffer_t *stringbuffer;
5012 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
5014 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5015 PRVM_G_FLOAT(OFS_RETURN) = -1;
5018 VM_Warning(prog, "VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5021 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
5023 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);
5026 string = PRVM_G_STRING(OFS_PARM1);
5027 order = (int)PRVM_G_FLOAT(OFS_PARM2);
5029 strindex = stringbuffer->num_strings;
5031 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
5032 if (stringbuffer->strings[strindex] == NULL)
5035 BufStr_Expand(prog, stringbuffer, strindex);
5037 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5038 alloclen = strlen(string) + 1;
5039 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5040 memcpy(stringbuffer->strings[strindex], string, alloclen);
5042 PRVM_G_FLOAT(OFS_RETURN) = strindex;
5046 ========================
5048 delete string from buffer
5049 void bufstr_free(float bufhandle, float string_index) = #468;
5050 ========================
5052 void VM_bufstr_free (prvm_prog_t *prog)
5055 prvm_stringbuffer_t *stringbuffer;
5056 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
5058 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5061 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5064 i = (int)PRVM_G_FLOAT(OFS_PARM1);
5067 VM_Warning(prog, "VM_bufstr_free: invalid string index %i used in %s\n", i, prog->name);
5071 if (i < stringbuffer->num_strings)
5073 if(stringbuffer->strings[i])
5074 Mem_Free(stringbuffer->strings[i]);
5075 stringbuffer->strings[i] = NULL;
5078 BufStr_Shrink(prog, stringbuffer);
5082 ========================
5084 load a file into string buffer, return 0 or 1
5085 float buf_loadfile(string filename, float bufhandle) = #535;
5086 ========================
5088 void VM_buf_loadfile(prvm_prog_t *prog)
5091 prvm_stringbuffer_t *stringbuffer;
5092 char string[VM_STRINGTEMP_LENGTH];
5093 int filenum, strindex, c, end;
5094 const char *filename;
5097 VM_SAFEPARMCOUNT(2, VM_buf_loadfile);
5100 filename = PRVM_G_STRING(OFS_PARM0);
5101 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
5102 if (prog->openfiles[filenum] == NULL)
5104 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
5105 if (prog->openfiles[filenum] == NULL)
5106 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
5107 if (prog->openfiles[filenum] == NULL)
5109 if (developer_extra.integer)
5110 VM_Warning(prog, "VM_buf_loadfile: failed to open file %s in %s\n", filename, prog->name);
5111 PRVM_G_FLOAT(OFS_RETURN) = 0;
5115 // get string buffer
5116 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5119 VM_Warning(prog, "VM_buf_loadfile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5120 PRVM_G_FLOAT(OFS_RETURN) = 0;
5124 // read file (append to the end of buffer)
5125 strindex = stringbuffer->num_strings;
5132 c = FS_Getc(prog->openfiles[filenum]);
5133 if (c == '\r' || c == '\n' || c < 0)
5135 if (end < VM_STRINGTEMP_LENGTH - 1)
5139 // remove \n following \r
5142 c = FS_Getc(prog->openfiles[filenum]);
5144 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
5149 BufStr_Expand(prog, stringbuffer, strindex);
5150 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5151 alloclen = strlen(string) + 1;
5152 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5153 memcpy(stringbuffer->strings[strindex], string, alloclen);
5154 strindex = stringbuffer->num_strings;
5161 FS_Close(prog->openfiles[filenum]);
5162 prog->openfiles[filenum] = NULL;
5163 if (prog->openfiles_origin[filenum])
5164 PRVM_Free((char *)prog->openfiles_origin[filenum]);
5165 PRVM_G_FLOAT(OFS_RETURN) = 1;
5169 ========================
5171 writes stringbuffer to a file, returns 0 or 1
5172 float buf_writefile(float filehandle, float bufhandle, [, float startpos, float numstrings]) = #468;
5173 ========================
5176 void VM_buf_writefile(prvm_prog_t *prog)
5178 int filenum, strindex, strnum, strlength;
5179 prvm_stringbuffer_t *stringbuffer;
5181 VM_SAFEPARMCOUNTRANGE(2, 4, VM_buf_writefile);
5184 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
5185 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
5187 VM_Warning(prog, "VM_buf_writefile: invalid file handle %i used in %s\n", filenum, prog->name);
5190 if (prog->openfiles[filenum] == NULL)
5192 VM_Warning(prog, "VM_buf_writefile: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
5196 // get string buffer
5197 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5200 VM_Warning(prog, "VM_buf_writefile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5201 PRVM_G_FLOAT(OFS_RETURN) = 0;
5205 // get start and end parms
5208 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5209 strnum = (int)PRVM_G_FLOAT(OFS_PARM3);
5211 else if (prog->argc > 2)
5213 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5214 strnum = stringbuffer->num_strings - strindex;
5219 strnum = stringbuffer->num_strings;
5221 if (strindex < 0 || strindex >= stringbuffer->num_strings)
5223 VM_Warning(prog, "VM_buf_writefile: wrong start string index %i used in %s\n", strindex, prog->name);
5224 PRVM_G_FLOAT(OFS_RETURN) = 0;
5229 VM_Warning(prog, "VM_buf_writefile: wrong strings count %i used in %s\n", strnum, prog->name);
5230 PRVM_G_FLOAT(OFS_RETURN) = 0;
5235 while(strindex < stringbuffer->num_strings && strnum)
5237 if (stringbuffer->strings[strindex])
5239 if ((strlength = strlen(stringbuffer->strings[strindex])))
5240 FS_Write(prog->openfiles[filenum], stringbuffer->strings[strindex], strlength);
5241 FS_Write(prog->openfiles[filenum], "\n", 1);
5247 PRVM_G_FLOAT(OFS_RETURN) = 1;
5250 #define MATCH_AUTO 0
5251 #define MATCH_WHOLE 1
5252 #define MATCH_LEFT 2
5253 #define MATCH_RIGHT 3
5254 #define MATCH_MIDDLE 4
5255 #define MATCH_PATTERN 5
5257 static const char *detect_match_rule(char *pattern, int *matchrule)
5262 patternlength = strlen(pattern);
5263 ppos = strchr(pattern, '*');
5264 qpos = strchr(pattern, '?');
5268 *matchrule = MATCH_PATTERN;
5271 // has * - left, mid, right or pattern
5274 // starts with * - may be right/mid or pattern
5275 if ((ppos - pattern) == 0)
5277 ppos = strchr(pattern+1, '*');
5281 *matchrule = MATCH_RIGHT;
5285 if ((ppos - pattern) == patternlength)
5287 *matchrule = MATCH_MIDDLE;
5292 *matchrule = MATCH_PATTERN;
5295 // end with * - left
5296 if ((ppos - pattern) == patternlength)
5298 *matchrule = MATCH_LEFT;
5303 *matchrule = MATCH_PATTERN;
5306 // have no wildcards - whole string
5307 *matchrule = MATCH_WHOLE;
5311 // todo: support UTF8
5312 static qboolean match_rule(const char *string, int max_string, const char *pattern, int patternlength, int rule)
5317 return !strncmp(string, pattern, max_string) ? true : false;
5319 return !strncmp(string, pattern, patternlength) ? true : false;
5322 mid = strstr(string, pattern);
5323 return mid && !*(mid+patternlength);
5326 return strstr(string, pattern) ? true : false;
5328 return matchpattern_with_separator(string, pattern, false, "", false) ? true : false;
5332 ========================
5334 find an index of bufstring matching rule
5335 float bufstr_find(float bufhandle, string match, float matchrule, float startpos, float step) = #468;
5336 ========================
5339 void VM_bufstr_find(prvm_prog_t *prog)
5341 prvm_stringbuffer_t *stringbuffer;
5342 char string[VM_STRINGTEMP_LENGTH];
5343 int matchrule, matchlen, i, step;
5346 VM_SAFEPARMCOUNTRANGE(3, 5, VM_bufstr_find);
5348 PRVM_G_FLOAT(OFS_RETURN) = -1;
5350 // get string buffer
5351 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5354 VM_Warning(prog, "VM_bufstr_find: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5359 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5360 if (matchrule < 0 && matchrule > 5)
5362 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
5366 match = PRVM_G_STRING(OFS_PARM1);
5369 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5370 match = detect_match_rule(string, &matchrule);
5372 matchlen = strlen(match);
5375 i = (prog->argc > 3) ? (int)PRVM_G_FLOAT(OFS_PARM3) : 0;
5376 step = (prog->argc > 4) ? (int)PRVM_G_FLOAT(OFS_PARM4) : 1;
5377 while(i < stringbuffer->num_strings)
5379 if (stringbuffer->strings[i] && match_rule(stringbuffer->strings[i], VM_STRINGTEMP_LENGTH, match, matchlen, matchrule))
5381 PRVM_G_FLOAT(OFS_RETURN) = i;
5389 ========================
5391 float matchpattern(string s, string pattern, float matchrule, float startpos) = #468;
5392 ========================
5394 void VM_matchpattern(prvm_prog_t *prog)
5396 const char *s, *match;
5397 char string[VM_STRINGTEMP_LENGTH];
5400 VM_SAFEPARMCOUNTRANGE(2, 4, VM_matchpattern);
5402 s = PRVM_G_STRING(OFS_PARM0);
5405 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5406 if (matchrule < 0 && matchrule > 5)
5408 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
5412 match = PRVM_G_STRING(OFS_PARM1);
5415 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5416 match = detect_match_rule(string, &matchrule);
5422 s += max(0, min((unsigned int)PRVM_G_FLOAT(OFS_PARM3), strlen(s)-1));
5425 PRVM_G_FLOAT(OFS_RETURN) = match_rule(s, VM_STRINGTEMP_LENGTH, match, l, matchrule);
5429 ========================
5431 ========================
5434 void VM_buf_cvarlist(prvm_prog_t *prog)
5437 const char *partial, *antipartial;
5438 size_t len, antilen;
5440 qboolean ispattern, antiispattern;
5442 prvm_stringbuffer_t *stringbuffer;
5443 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5445 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5448 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5452 partial = PRVM_G_STRING(OFS_PARM1);
5456 len = strlen(partial);
5459 antipartial = PRVM_G_STRING(OFS_PARM2);
5465 antilen = strlen(antipartial);
5467 for (n = 0;n < stringbuffer->num_strings;n++)
5468 if (stringbuffer->strings[n])
5469 Mem_Free(stringbuffer->strings[n]);
5470 if (stringbuffer->strings)
5471 Mem_Free(stringbuffer->strings);
5472 stringbuffer->strings = NULL;
5474 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5475 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5478 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5480 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5483 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5489 stringbuffer->max_strings = stringbuffer->num_strings = n;
5490 if (stringbuffer->max_strings)
5491 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5494 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5496 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5499 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5502 alloclen = strlen(cvar->name) + 1;
5503 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5504 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5519 This was a major timewaster in progs, so it was converted to C
5522 void VM_changeyaw (prvm_prog_t *prog)
5525 float ideal, current, move, speed;
5527 // this is called (VERY HACKISHLY) by VM_SV_MoveToGoal, so it can not use any
5528 // parameters because they are the parameters to VM_SV_MoveToGoal, not this
5529 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5531 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5532 if (ent == prog->edicts)
5534 VM_Warning(prog, "changeyaw: can not modify world entity\n");
5537 if (ent->priv.server->free)
5539 VM_Warning(prog, "changeyaw: can not modify free entity\n");
5542 current = PRVM_gameedictvector(ent, angles)[1];
5543 current = ANGLEMOD(current);
5544 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5545 speed = PRVM_gameedictfloat(ent, yaw_speed);
5547 if (current == ideal)
5549 move = ideal - current;
5550 if (ideal > current)
5572 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5580 void VM_changepitch (prvm_prog_t *prog)
5583 float ideal, current, move, speed;
5585 VM_SAFEPARMCOUNT(1, VM_changepitch);
5587 ent = PRVM_G_EDICT(OFS_PARM0);
5588 if (ent == prog->edicts)
5590 VM_Warning(prog, "changepitch: can not modify world entity\n");
5593 if (ent->priv.server->free)
5595 VM_Warning(prog, "changepitch: can not modify free entity\n");
5598 current = PRVM_gameedictvector(ent, angles)[0];
5599 current = ANGLEMOD(current);
5600 ideal = PRVM_gameedictfloat(ent, idealpitch);
5601 speed = PRVM_gameedictfloat(ent, pitch_speed);
5603 if (current == ideal)
5605 move = ideal - current;
5606 if (ideal > current)
5628 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5632 void VM_uncolorstring (prvm_prog_t *prog)
5634 char szNewString[VM_STRINGTEMP_LENGTH];
5635 const char *szString;
5638 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5639 szString = PRVM_G_STRING(OFS_PARM0);
5640 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5641 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
5645 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5646 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5647 void VM_strstrofs (prvm_prog_t *prog)
5649 const char *instr, *match;
5651 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5652 instr = PRVM_G_STRING(OFS_PARM0);
5653 match = PRVM_G_STRING(OFS_PARM1);
5654 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5655 firstofs = u8_bytelen(instr, firstofs);
5657 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5659 PRVM_G_FLOAT(OFS_RETURN) = -1;
5663 match = strstr(instr+firstofs, match);
5665 PRVM_G_FLOAT(OFS_RETURN) = -1;
5667 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5670 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5671 void VM_str2chr (prvm_prog_t *prog)
5676 VM_SAFEPARMCOUNT(2, VM_str2chr);
5677 s = PRVM_G_STRING(OFS_PARM0);
5678 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5680 if((unsigned)index < strlen(s))
5682 if (utf8_enable.integer)
5683 ch = u8_getchar_noendptr(s + index);
5685 ch = (unsigned char)s[index];
5686 PRVM_G_FLOAT(OFS_RETURN) = ch;
5689 PRVM_G_FLOAT(OFS_RETURN) = 0;
5692 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5693 void VM_chr2str (prvm_prog_t *prog)
5698 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5699 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5700 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5702 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5707 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5708 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5709 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5711 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5714 static int chrconv_number(int i, int base, int conv)
5739 static int chrconv_punct(int i, int base, int conv)
5757 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5759 //convert case and colour seperatly...
5776 baset = 128*((charnum&1) == (convt-5));
5792 return i + basec + baset;
5794 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5795 //bulk convert a string. change case or colouring.
5796 void VM_strconv (prvm_prog_t *prog)
5798 int ccase, redalpha, rednum, len, i;
5799 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5800 unsigned char *result = resbuf;
5802 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5804 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5805 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5806 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5807 VM_VarString(prog, 3, (char *) resbuf, sizeof(resbuf));
5808 len = strlen((char *) resbuf);
5810 for (i = 0; i < len; i++, result++) //should this be done backwards?
5812 if (*result >= '0' && *result <= '9') //normal numbers...
5813 *result = chrconv_number(*result, '0', rednum);
5814 else if (*result >= '0'+128 && *result <= '9'+128)
5815 *result = chrconv_number(*result, '0'+128, rednum);
5816 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5817 *result = chrconv_number(*result, '0'+128-30, rednum);
5818 else if (*result >= '0'-30 && *result <= '9'-30)
5819 *result = chrconv_number(*result, '0'-30, rednum);
5821 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5822 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5823 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5824 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5825 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5826 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5827 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5828 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5830 else if ((*result & 127) < 16 || !redalpha) //special chars..
5832 else if (*result < 128)
5833 *result = chrconv_punct(*result, 0, redalpha);
5835 *result = chrconv_punct(*result, 128, redalpha);
5839 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, (char *) resbuf);
5842 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5843 void VM_strpad (prvm_prog_t *prog)
5845 char src[VM_STRINGTEMP_LENGTH];
5846 char destbuf[VM_STRINGTEMP_LENGTH];
5848 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5849 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5850 VM_VarString(prog, 1, src, sizeof(src));
5852 // note: < 0 = left padding, > 0 = right padding,
5853 // this is reverse logic of printf!
5854 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5856 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, destbuf);
5859 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5860 //uses qw style \key\value strings
5861 void VM_infoadd (prvm_prog_t *prog)
5863 const char *info, *key;
5864 char value[VM_STRINGTEMP_LENGTH];
5865 char temp[VM_STRINGTEMP_LENGTH];
5867 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5868 info = PRVM_G_STRING(OFS_PARM0);
5869 key = PRVM_G_STRING(OFS_PARM1);
5870 VM_VarString(prog, 2, value, sizeof(value));
5872 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5874 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5876 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, temp);
5879 // #227 string(string info, string key) infoget (FTE_STRINGS)
5880 //uses qw style \key\value strings
5881 void VM_infoget (prvm_prog_t *prog)
5885 char value[VM_STRINGTEMP_LENGTH];
5887 VM_SAFEPARMCOUNT(2, VM_infoget);
5888 info = PRVM_G_STRING(OFS_PARM0);
5889 key = PRVM_G_STRING(OFS_PARM1);
5891 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5893 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, value);
5896 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5897 // also float(string s1, string s2) strcmp (FRIK_FILE)
5898 void VM_strncmp (prvm_prog_t *prog)
5900 const char *s1, *s2;
5901 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5902 s1 = PRVM_G_STRING(OFS_PARM0);
5903 s2 = PRVM_G_STRING(OFS_PARM1);
5906 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5910 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5914 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5915 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5916 void VM_strncasecmp (prvm_prog_t *prog)
5918 const char *s1, *s2;
5919 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5920 s1 = PRVM_G_STRING(OFS_PARM0);
5921 s2 = PRVM_G_STRING(OFS_PARM1);
5924 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5928 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5932 // #494 float(float caseinsensitive, string s, ...) crc16
5933 void VM_crc16(prvm_prog_t *prog)
5936 char s[VM_STRINGTEMP_LENGTH];
5937 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5938 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5939 VM_VarString(prog, 1, s, sizeof(s));
5940 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5943 // #639 float(string digest, string data, ...) digest_hex
5944 void VM_digest_hex(prvm_prog_t *prog)
5952 char s[VM_STRINGTEMP_LENGTH];
5955 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5956 digest = PRVM_G_STRING(OFS_PARM0);
5959 VM_VarString(prog, 1, s, sizeof(s));
5964 if(!strcmp(digest, "MD4"))
5967 mdfour((unsigned char *) out, (unsigned char *) s, len);
5969 else if(!strcmp(digest, "SHA256") && Crypto_Available())
5972 sha256((unsigned char *) out, (unsigned char *) s, len);
5974 // no warning needed on mismatch - we return string_null to QC
5979 static const char *hexmap = "0123456789abcdef";
5980 for(i = 0; i < outlen; ++i)
5982 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
5983 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
5986 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outhex);
5989 PRVM_G_INT(OFS_RETURN) = 0;
5992 void VM_wasfreed (prvm_prog_t *prog)
5994 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5995 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5998 void VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace)
6000 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
6001 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
6002 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
6003 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
6004 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
6005 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
6006 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
6007 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
6008 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
6009 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
6010 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
6011 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
6012 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(prog, trace->hittexture->name) : 0;
6015 void VM_ClearTraceGlobals(prvm_prog_t *prog)
6017 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
6018 PRVM_gameglobalfloat(trace_allsolid) = 0;
6019 PRVM_gameglobalfloat(trace_startsolid) = 0;
6020 PRVM_gameglobalfloat(trace_fraction) = 0;
6021 PRVM_gameglobalfloat(trace_inwater) = 0;
6022 PRVM_gameglobalfloat(trace_inopen) = 0;
6023 VectorClear(PRVM_gameglobalvector(trace_endpos));
6024 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
6025 PRVM_gameglobalfloat(trace_plane_dist) = 0;
6026 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
6027 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
6028 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
6029 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
6030 PRVM_gameglobalstring(trace_dphittexturename) = 0;
6035 void VM_Cmd_Init(prvm_prog_t *prog)
6037 // only init the stuff for the current prog
6038 VM_Files_Init(prog);
6039 VM_Search_Init(prog);
6042 void VM_Cmd_Reset(prvm_prog_t *prog)
6044 CL_PurgeOwner( MENUOWNER );
6045 VM_Search_Reset(prog);
6046 VM_Files_CloseAll(prog);
6049 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
6050 // does URI escaping on a string (replace evil stuff by %AB escapes)
6051 void VM_uri_escape (prvm_prog_t *prog)
6053 char src[VM_STRINGTEMP_LENGTH];
6054 char dest[VM_STRINGTEMP_LENGTH];
6056 static const char *hex = "0123456789ABCDEF";
6058 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
6059 VM_VarString(prog, 0, src, sizeof(src));
6061 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
6063 if((*p >= 'A' && *p <= 'Z')
6064 || (*p >= 'a' && *p <= 'z')
6065 || (*p >= '0' && *p <= '9')
6066 || (*p == '-') || (*p == '_') || (*p == '.')
6067 || (*p == '!') || (*p == '~')
6068 || (*p == '\'') || (*p == '(') || (*p == ')'))
6073 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
6074 *q++ = hex[ *(unsigned char *)p & 0xF];
6079 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
6082 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
6083 // does URI unescaping on a string (get back the evil stuff)
6084 void VM_uri_unescape (prvm_prog_t *prog)
6086 char src[VM_STRINGTEMP_LENGTH];
6087 char dest[VM_STRINGTEMP_LENGTH];
6091 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
6092 VM_VarString(prog, 0, src, sizeof(src));
6094 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
6098 if(p[1] >= '0' && p[1] <= '9')
6100 else if(p[1] >= 'a' && p[1] <= 'f')
6101 hi = p[1] - 'a' + 10;
6102 else if(p[1] >= 'A' && p[1] <= 'F')
6103 hi = p[1] - 'A' + 10;
6106 if(p[2] >= '0' && p[2] <= '9')
6108 else if(p[2] >= 'a' && p[2] <= 'f')
6109 lo = p[2] - 'a' + 10;
6110 else if(p[2] >= 'A' && p[2] <= 'F')
6111 lo = p[2] - 'A' + 10;
6114 if(hi != 0 || lo != 0) // don't unescape NUL bytes
6115 *q++ = (char) (hi * 0x10 + lo);
6126 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
6129 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
6130 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
6131 void VM_whichpack (prvm_prog_t *prog)
6133 const char *fn, *pack;
6135 VM_SAFEPARMCOUNT(1, VM_whichpack);
6136 fn = PRVM_G_STRING(OFS_PARM0);
6137 pack = FS_WhichPack(fn);
6139 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, pack ? pack : "");
6147 char buffer[MAX_INPUTLINE];
6149 unsigned char *postdata; // free when uri_to_prog_t is freed
6151 char *sigdata; // free when uri_to_prog_t is freed
6156 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
6159 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
6161 prog = handle->prog;
6164 // curl reply came too late... so just drop it
6165 if(handle->postdata)
6166 Z_Free(handle->postdata);
6168 Z_Free(handle->sigdata);
6173 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
6175 if(length_received >= sizeof(handle->buffer))
6176 length_received = sizeof(handle->buffer) - 1;
6177 handle->buffer[length_received] = 0;
6179 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
6180 PRVM_G_FLOAT(OFS_PARM1) = status;
6181 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(prog, handle->buffer);
6182 prog->ExecuteProgram(prog, PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
6185 if(handle->postdata)
6186 Z_Free(handle->postdata);
6188 Z_Free(handle->sigdata);
6192 // 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
6193 // 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
6194 void VM_uri_get (prvm_prog_t *prog)
6199 uri_to_prog_t *handle;
6200 const char *posttype = NULL;
6201 const char *postseparator = NULL;
6202 int poststringbuffer = -1;
6204 const char *query_string = NULL;
6207 if(!PRVM_allfunction(URI_Get_Callback))
6208 prog->error_cmd("uri_get called by %s without URI_Get_Callback defined", prog->name);
6210 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
6212 url = PRVM_G_STRING(OFS_PARM0);
6213 id = PRVM_G_FLOAT(OFS_PARM1);
6215 posttype = PRVM_G_STRING(OFS_PARM2);
6217 postseparator = PRVM_G_STRING(OFS_PARM3);
6219 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
6221 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
6222 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!
6224 query_string = strchr(url, '?');
6227 lq = query_string ? strlen(query_string) : 0;
6229 handle->prog = prog;
6230 handle->starttime = prog->starttime;
6232 if(postseparator && posttype && *posttype)
6234 size_t l = strlen(postseparator);
6235 if(poststringbuffer >= 0)
6240 prvm_stringbuffer_t *stringbuffer;
6241 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
6244 VM_Warning(prog, "uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
6248 for(i = 0;i < stringbuffer->num_strings;i++)
6252 if(stringbuffer->strings[i])
6253 ltotal += strlen(stringbuffer->strings[i]);
6255 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
6256 handle->postlen = ltotal;
6258 for(i = 0;i < stringbuffer->num_strings;i++)
6262 memcpy(handle->postdata + ltotal, postseparator, l);
6265 if(stringbuffer->strings[i])
6267 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
6268 ltotal += strlen(stringbuffer->strings[i]);
6271 if(ltotal != handle->postlen)
6272 prog->error_cmd("%s: string buffer content size mismatch, possible overrun", prog->name);
6276 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
6277 handle->postlen = l;
6278 memcpy(handle->postdata, postseparator, l);
6280 handle->postdata[handle->postlen] = 0;
6282 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
6285 // POST: we sign postdata \0 query string
6287 handle->sigdata = (char *)Z_Malloc(8192);
6288 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6289 l = strlen(handle->sigdata);
6290 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
6293 Z_Free(handle->sigdata);
6294 handle->sigdata = NULL;
6297 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6300 Z_Free(handle->sigdata);
6301 handle->sigdata = NULL;
6304 handle->siglen = l + ll;
6305 handle->sigdata[handle->siglen] = 0;
6308 strlcpy(handle->posttype, posttype, sizeof(handle->posttype));
6309 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, handle->posttype, handle->postdata, handle->postlen, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6313 if(postkeyid >= 0 && query_string)
6315 // GET: we sign JUST the query string
6317 handle->sigdata = (char *)Z_Malloc(8192);
6318 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6319 l = strlen(handle->sigdata);
6320 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
6323 Z_Free(handle->sigdata);
6324 handle->sigdata = NULL;
6327 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6330 Z_Free(handle->sigdata);
6331 handle->sigdata = NULL;
6334 handle->siglen = l + ll;
6335 handle->sigdata[handle->siglen] = 0;
6338 handle->postdata = NULL;
6339 handle->postlen = 0;
6340 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, NULL, NULL, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6344 PRVM_G_INT(OFS_RETURN) = 1;
6348 if(handle->postdata)
6349 Z_Free(handle->postdata);
6351 Z_Free(handle->sigdata);
6353 PRVM_G_INT(OFS_RETURN) = 0;
6357 void VM_netaddress_resolve (prvm_prog_t *prog)
6360 char normalized[128];
6362 lhnetaddress_t addr;
6364 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
6366 ip = PRVM_G_STRING(OFS_PARM0);
6369 port = (int) PRVM_G_FLOAT(OFS_PARM1);
6371 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
6372 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, normalized);
6374 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
6377 //string(prvm_prog_t *prog) getextresponse = #624; // returns the next extResponse packet that was sent to this client
6378 void VM_CL_getextresponse (prvm_prog_t *prog)
6380 VM_SAFEPARMCOUNT(0,VM_argv);
6382 if (cl_net_extresponse_count <= 0)
6383 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6387 --cl_net_extresponse_count;
6388 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6389 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, cl_net_extresponse[first]);
6393 void VM_SV_getextresponse (prvm_prog_t *prog)
6395 VM_SAFEPARMCOUNT(0,VM_argv);
6397 if (sv_net_extresponse_count <= 0)
6398 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6402 --sv_net_extresponse_count;
6403 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6404 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, sv_net_extresponse[first]);
6410 Common functions between menu.dat and clsprogs
6414 //#349 float() isdemo
6415 void VM_CL_isdemo (prvm_prog_t *prog)
6417 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
6418 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
6421 //#355 float() videoplaying
6422 void VM_CL_videoplaying (prvm_prog_t *prog)
6424 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
6425 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6432 callfunction(...,string function_name)
6436 void VM_callfunction(prvm_prog_t *prog)
6441 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6443 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6445 VM_CheckEmptyString(prog, s);
6447 func = PRVM_ED_FindFunction(prog, s);
6450 prog->error_cmd("VM_callfunciton: function %s not found !", s);
6451 else if (func->first_statement < 0)
6453 // negative statements are built in functions
6454 int builtinnumber = -func->first_statement;
6455 prog->xfunction->builtinsprofile++;
6456 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6457 prog->builtins[builtinnumber](prog);
6459 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
6461 else if(func - prog->functions > 0)
6464 prog->ExecuteProgram(prog, func - prog->functions,"");
6473 float isfunction(string function_name)
6476 void VM_isfunction(prvm_prog_t *prog)
6481 VM_SAFEPARMCOUNT(1, VM_isfunction);
6483 s = PRVM_G_STRING(OFS_PARM0);
6485 VM_CheckEmptyString(prog, s);
6487 func = PRVM_ED_FindFunction(prog, s);
6490 PRVM_G_FLOAT(OFS_RETURN) = false;
6492 PRVM_G_FLOAT(OFS_RETURN) = true;
6499 string sprintf(string format, ...)
6503 void VM_sprintf(prvm_prog_t *prog)
6506 char outbuf[MAX_INPUTLINE];
6507 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6510 int width, precision, thisarg, flags;
6514 static prvm_int_t dummyivec[3] = {0, 0, 0};
6515 static prvm_vec_t dummyvec[3] = {0, 0, 0};
6518 #define PRINTF_ALTERNATE 1
6519 #define PRINTF_ZEROPAD 2
6520 #define PRINTF_LEFT 4
6521 #define PRINTF_SPACEPOSITIVE 8
6522 #define PRINTF_SIGNPOSITIVE 16
6526 s = PRVM_G_STRING(OFS_PARM0);
6528 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6529 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6530 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6531 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((prvm_int_t*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6532 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6547 // complete directive format:
6556 // is number following?
6557 if(*s >= '0' && *s <= '9')
6559 width = strtol(s, &err, 10);
6562 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6575 flags |= PRINTF_ZEROPAD;
6577 width = -1; // it was just a flag
6589 case '#': flags |= PRINTF_ALTERNATE; break;
6590 case '0': flags |= PRINTF_ZEROPAD; break;
6591 case '-': flags |= PRINTF_LEFT; break;
6592 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6593 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6603 if(*s >= '0' && *s <= '9')
6605 width = strtol(s, &err, 10);
6606 if(!err || *err != '$')
6608 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6615 width = GETARG_FLOAT(width);
6618 flags |= PRINTF_LEFT;
6622 else if(*s >= '0' && *s <= '9')
6624 width = strtol(s, &err, 10);
6627 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6633 flags |= PRINTF_LEFT;
6637 // otherwise width stays -1
6646 if(*s >= '0' && *s <= '9')
6648 precision = strtol(s, &err, 10);
6649 if(!err || *err != '$')
6651 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6657 precision = argpos++;
6658 precision = GETARG_FLOAT(precision);
6660 else if(*s >= '0' && *s <= '9')
6662 precision = strtol(s, &err, 10);
6665 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6672 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6681 case 'h': isfloat = 1; break;
6682 case 'l': isfloat = 0; break;
6683 case 'L': isfloat = 0; break;
6694 // now s points to the final directive char and is no longer changed
6709 if(*s != 's' && *s != 'c')
6710 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6711 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6712 if(flags & PRINTF_LEFT) *f++ = '-';
6713 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6714 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6721 if(*s == 'd' || *s == 'i' || *s == 'o' || *s == 'u' || *s == 'x' || *s == 'X')
6723 // make it use a good integer type
6724 for(p = INT_LOSSLESS_FORMAT_SIZE; *p; )
6730 if(width < 0) // not set
6736 if(precision < 0) // not set
6737 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
6739 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
6741 case 'o': case 'u': case 'x': case 'X':
6742 if(precision < 0) // not set
6743 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
6745 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
6747 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6748 if(precision < 0) // not set
6749 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6751 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6755 if(precision < 0) // not set
6756 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6757 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6758 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6759 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6762 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6763 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6764 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6765 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6769 if(flags & PRINTF_ALTERNATE)
6771 if(precision < 0) // not set
6772 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6774 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6778 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6780 const char *buf = u8_encodech(c, NULL, charbuf16);
6783 if(precision < 0) // not set
6784 precision = end - o - 1;
6785 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6789 if(flags & PRINTF_ALTERNATE)
6791 if(precision < 0) // not set
6792 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6794 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6798 if(precision < 0) // not set
6799 precision = end - o - 1;
6800 if(flags & PRINTF_SIGNPOSITIVE)
6801 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6803 o += u8_strpad_colorcodes(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6807 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6822 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outbuf);
6828 static dp_model_t *getmodel(prvm_prog_t *prog, prvm_edict_t *ed)
6830 if (prog == SVVM_prog)
6831 return SV_GetModelFromEdict(ed);
6832 else if (prog == CLVM_prog)
6833 return CL_GetModelFromEdict(ed);
6840 unsigned int progid;
6842 frameblend_t frameblend[MAX_FRAMEBLENDS];
6843 skeleton_t *skeleton_p;
6844 skeleton_t skeleton;
6845 float *data_vertex3f;
6846 float *data_svector3f;
6847 float *data_tvector3f;
6848 float *data_normal3f;
6850 float *buf_vertex3f;
6851 float *buf_svector3f;
6852 float *buf_tvector3f;
6853 float *buf_normal3f;
6855 animatemodel_cache_t;
6856 static animatemodel_cache_t animatemodel_cache;
6858 static void animatemodel(prvm_prog_t *prog, dp_model_t *model, prvm_edict_t *ed)
6860 skeleton_t *skeleton;
6861 int skeletonindex = -1;
6862 qboolean need = false;
6863 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6865 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6866 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6867 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6868 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6871 if(animatemodel_cache.progid != prog->id)
6872 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6873 need |= (animatemodel_cache.model != model);
6874 VM_GenerateFrameGroupBlend(prog, ed->priv.server->framegroupblend, ed);
6875 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model, PRVM_serverglobalfloat(time));
6876 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6877 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6878 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6880 need |= (animatemodel_cache.skeleton_p != skeleton);
6882 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6885 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6887 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6888 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6889 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6890 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6891 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6892 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6893 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6894 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6895 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6897 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6898 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6899 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6900 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6901 VM_UpdateEdictSkeleton(prog, ed, model, ed->priv.server->frameblend);
6902 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);
6903 animatemodel_cache.progid = prog->id;
6904 animatemodel_cache.model = model;
6905 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6906 animatemodel_cache.skeleton_p = skeleton;
6908 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6911 static void getmatrix(prvm_prog_t *prog, prvm_edict_t *ed, matrix4x4_t *out)
6913 if (prog == SVVM_prog)
6914 SV_GetEntityMatrix(prog, ed, out, false);
6915 else if (prog == CLVM_prog)
6916 CL_GetEntityMatrix(prog, ed, out, false);
6918 *out = identitymatrix;
6921 static void applytransform_forward(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6924 getmatrix(prog, ed, &m);
6925 Matrix4x4_Transform(&m, in, out);
6928 static void applytransform_forward_direction(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6931 getmatrix(prog, ed, &m);
6932 Matrix4x4_Transform3x3(&m, in, out);
6935 static void applytransform_inverted(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6938 getmatrix(prog, ed, &m);
6939 Matrix4x4_Invert_Full(&n, &m);
6940 Matrix4x4_Transform3x3(&n, in, out);
6943 static void applytransform_forward_normal(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6947 getmatrix(prog, ed, &m);
6948 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6952 static void clippointtosurface(prvm_prog_t *prog, prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6955 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6957 animatemodel(prog, model, ed);
6958 bestdist = 1000000000;
6960 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6962 // clip original point to each triangle of the surface and find the
6963 // triangle that is closest
6964 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6965 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6966 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6967 TriangleNormal(v[0], v[1], v[2], facenormal);
6968 VectorNormalize(facenormal);
6969 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6970 VectorMA(p, offsetdist, facenormal, temp);
6971 for (j = 0, k = 2;j < 3;k = j, j++)
6973 VectorSubtract(v[k], v[j], edgenormal);
6974 CrossProduct(edgenormal, facenormal, sidenormal);
6975 VectorNormalize(sidenormal);
6976 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6978 VectorMA(temp, offsetdist, sidenormal, temp);
6980 dist = VectorDistance2(temp, p);
6981 if (bestdist > dist)
6984 VectorCopy(temp, out);
6989 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6991 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6993 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6997 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6998 void VM_getsurfacenumpoints(prvm_prog_t *prog)
7001 msurface_t *surface;
7002 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
7003 // return 0 if no such surface
7004 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7006 PRVM_G_FLOAT(OFS_RETURN) = 0;
7010 // note: this (incorrectly) assumes it is a simple polygon
7011 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
7013 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
7014 void VM_getsurfacepoint(prvm_prog_t *prog)
7018 msurface_t *surface;
7021 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
7022 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7023 ed = PRVM_G_EDICT(OFS_PARM0);
7024 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7026 // note: this (incorrectly) assumes it is a simple polygon
7027 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
7028 if (pointnum < 0 || pointnum >= surface->num_vertices)
7030 animatemodel(prog, model, ed);
7031 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7032 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7034 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
7035 // float SPA_POSITION = 0;
7036 // float SPA_S_AXIS = 1;
7037 // float SPA_T_AXIS = 2;
7038 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
7039 // float SPA_TEXCOORDS0 = 4;
7040 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
7041 // float SPA_LIGHTMAP0_COLOR = 6;
7042 void VM_getsurfacepointattribute(prvm_prog_t *prog)
7046 msurface_t *surface;
7051 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
7052 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7053 ed = PRVM_G_EDICT(OFS_PARM0);
7054 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7056 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
7057 if (pointnum < 0 || pointnum >= surface->num_vertices)
7059 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
7061 animatemodel(prog, model, ed);
7063 switch( attributetype ) {
7064 // float SPA_POSITION = 0;
7066 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7067 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7069 // float SPA_S_AXIS = 1;
7071 applytransform_forward_direction(prog, &(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7072 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7074 // float SPA_T_AXIS = 2;
7076 applytransform_forward_direction(prog, &(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7077 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7079 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
7081 applytransform_forward_direction(prog, &(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7082 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7084 // float SPA_TEXCOORDS0 = 4;
7086 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
7087 result[0] = texcoord[0];
7088 result[1] = texcoord[1];
7090 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7093 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
7095 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
7096 result[0] = texcoord[0];
7097 result[1] = texcoord[1];
7099 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7102 // float SPA_LIGHTMAP0_COLOR = 6;
7104 // ignore alpha for now..
7105 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
7108 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
7112 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
7113 void VM_getsurfacenormal(prvm_prog_t *prog)
7116 msurface_t *surface;
7119 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
7120 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7121 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7123 // note: this only returns the first triangle, so it doesn't work very
7124 // well for curved surfaces or arbitrary meshes
7125 animatemodel(prog, model, PRVM_G_EDICT(OFS_PARM0));
7126 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);
7127 applytransform_forward_normal(prog, normal, PRVM_G_EDICT(OFS_PARM0), result);
7128 VectorNormalize(result);
7129 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7131 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
7132 void VM_getsurfacetexture(prvm_prog_t *prog)
7135 msurface_t *surface;
7136 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
7137 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
7138 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7140 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, surface->texture->name);
7142 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
7143 void VM_getsurfacenearpoint(prvm_prog_t *prog)
7145 int surfacenum, best;
7147 vec_t dist, bestdist;
7150 msurface_t *surface;
7152 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
7153 PRVM_G_FLOAT(OFS_RETURN) = -1;
7154 ed = PRVM_G_EDICT(OFS_PARM0);
7155 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), point);
7157 if (!ed || ed->priv.server->free)
7159 model = getmodel(prog, ed);
7160 if (!model || !model->num_surfaces)
7163 animatemodel(prog, model, ed);
7165 applytransform_inverted(prog, point, ed, p);
7167 bestdist = 1000000000;
7168 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
7170 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
7171 // first see if the nearest point on the surface's box is closer than the previous match
7172 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
7173 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
7174 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
7175 dist = VectorLength2(clipped);
7176 if (dist < bestdist)
7178 // it is, check the nearest point on the actual geometry
7179 clippointtosurface(prog, ed, model, surface, p, clipped);
7180 VectorSubtract(clipped, p, clipped);
7181 dist += VectorLength2(clipped);
7182 if (dist < bestdist)
7184 // that's closer too, store it as the best match
7190 PRVM_G_FLOAT(OFS_RETURN) = best;
7192 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
7193 void VM_getsurfaceclippedpoint(prvm_prog_t *prog)
7197 msurface_t *surface;
7199 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
7200 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7201 ed = PRVM_G_EDICT(OFS_PARM0);
7202 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7204 animatemodel(prog, model, ed);
7205 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), inp);
7206 applytransform_inverted(prog, inp, ed, p);
7207 clippointtosurface(prog, ed, model, surface, p, out);
7208 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
7211 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
7212 void VM_getsurfacenumtriangles(prvm_prog_t *prog)
7215 msurface_t *surface;
7216 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
7217 // return 0 if no such surface
7218 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7220 PRVM_G_FLOAT(OFS_RETURN) = 0;
7224 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
7226 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
7227 void VM_getsurfacetriangle(prvm_prog_t *prog)
7229 const vec3_t d = {-1, -1, -1};
7232 msurface_t *surface;
7234 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
7235 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7236 ed = PRVM_G_EDICT(OFS_PARM0);
7237 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7239 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
7240 if (trinum < 0 || trinum >= surface->num_triangles)
7242 // FIXME: implement rotation/scaling
7243 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
7250 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(prog, ed, f); else World_Physics_ApplyCmd(ed, f)
7252 static edict_odefunc_t *VM_physics_newstackfunction(prvm_prog_t *prog, prvm_edict_t *ed, edict_odefunc_t *f)
7254 edict_odefunc_t *newfunc, *func;
7256 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
7257 memcpy(newfunc, f, sizeof(edict_odefunc_t));
7258 newfunc->next = NULL;
7259 if (!ed->priv.server->ode_func)
7260 ed->priv.server->ode_func = newfunc;
7263 for (func = ed->priv.server->ode_func; func->next; func = func->next);
7264 func->next = newfunc;
7269 // void(entity e, float physics_enabled) physics_enable = #;
7270 void VM_physics_enable(prvm_prog_t *prog)
7275 VM_SAFEPARMCOUNT(2, VM_physics_enable);
7276 ed = PRVM_G_EDICT(OFS_PARM0);
7279 if (developer.integer > 0)
7280 VM_Warning(prog, "VM_physics_enable: null entity!\n");
7283 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7284 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7286 VM_Warning(prog, "VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
7289 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
7290 VM_physics_ApplyCmd(ed, &f);
7293 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
7294 void VM_physics_addforce(prvm_prog_t *prog)
7299 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
7300 ed = PRVM_G_EDICT(OFS_PARM0);
7303 if (developer.integer > 0)
7304 VM_Warning(prog, "VM_physics_addforce: null entity!\n");
7307 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7308 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7310 VM_Warning(prog, "VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
7313 f.type = ODEFUNC_FORCE;
7314 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7315 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), f.v2);
7316 VM_physics_ApplyCmd(ed, &f);
7319 // void(entity e, vector torque) physics_addtorque = #;
7320 void VM_physics_addtorque(prvm_prog_t *prog)
7325 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
7326 ed = PRVM_G_EDICT(OFS_PARM0);
7329 if (developer.integer > 0)
7330 VM_Warning(prog, "VM_physics_addtorque: null entity!\n");
7333 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7334 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7336 VM_Warning(prog, "VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
7339 f.type = ODEFUNC_TORQUE;
7340 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7341 VM_physics_ApplyCmd(ed, &f);