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
15 extern cvar_t prvm_backtraceforwarnings;
17 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
18 void VM_Warning(const char *fmt, ...)
21 char msg[MAX_INPUTLINE];
22 static double recursive = -1;
25 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
30 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
31 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
40 //============================================================================
43 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
44 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
45 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
46 // TODO: will this war ever end? [2007-01-23 LordHavoc]
48 void VM_CheckEmptyString (const char *s)
50 if (ISWHITESPACE(s[0]))
51 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
54 void VM_GenerateFrameGroupBlend(framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
57 // self.frame is the interpolation target (new frame)
58 // self.frame1time is the animation base time for the interpolation target
59 // self.frame2 is the interpolation start (previous frame)
60 // self.frame2time is the animation base time for the interpolation start
61 // self.lerpfrac is the interpolation strength for self.frame2
62 // self.lerpfrac3 is the interpolation strength for self.frame3
63 // self.lerpfrac4 is the interpolation strength for self.frame4
64 // pitch angle on a player model where the animator set up 5 sets of
65 // animations and the csqc simply lerps between sets)
66 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame))) framegroupblend[0].frame = (int) val->_float;
67 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) framegroupblend[1].frame = (int) val->_float;
68 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame3))) framegroupblend[2].frame = (int) val->_float;
69 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame4))) framegroupblend[3].frame = (int) val->_float;
70 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) framegroupblend[0].start = val->_float;
71 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) framegroupblend[1].start = val->_float;
72 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame3time))) framegroupblend[2].start = val->_float;
73 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame4time))) framegroupblend[3].start = val->_float;
74 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) framegroupblend[1].lerp = val->_float;
75 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac3))) framegroupblend[2].lerp = val->_float;
76 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac4))) framegroupblend[3].lerp = val->_float;
77 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
78 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
81 // LordHavoc: quite tempting to break apart this function to reuse the
82 // duplicated code, but I suspect it is better for performance
84 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model)
86 int sub2, numframes, f, i, k;
87 int isfirstframegroup = true;
89 double sublerp, lerp, d;
90 const animscene_t *scene;
91 const framegroupblend_t *g;
92 frameblend_t *blend = frameblend;
94 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
96 if (!model || !model->surfmesh.isanimated)
102 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
103 numframes = model->numframes;
104 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
107 if ((unsigned int)f >= (unsigned int)numframes)
109 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
117 if (isfirstframegroup)
120 isfirstframegroup = false;
125 if (model->animscenes)
127 scene = model->animscenes + f;
128 f = scene->firstframe;
129 if (scene->framecount > 1)
131 // this code path is only used on .zym models and torches
132 sublerp = scene->framerate * (cl.time - g->start);
133 f = (int) floor(sublerp);
136 if (sublerp < (1.0 / 65536.0f))
138 if (sublerp > (65535.0f / 65536.0f))
144 f = (f % scene->framecount);
145 sub2 = (sub2 % scene->framecount);
147 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
148 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
150 // two framelerps produced from one animation
153 for (i = 0;i < MAX_FRAMEBLENDS;i++)
155 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
157 blend[i].subframe = sub2;
163 d = (1 - sublerp) * lerp;
168 for (i = 0;i < MAX_FRAMEBLENDS;i++)
170 if (blend[i].lerp <= 0 || blend[i].subframe == f)
172 blend[i].subframe = f;
181 void VM_UpdateEdictSkeleton(prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
183 if (ed->priv.server->skeleton.model != edmodel)
185 VM_RemoveEdictSkeleton(ed);
186 ed->priv.server->skeleton.model = edmodel;
188 if (!ed->priv.server->skeleton.relativetransforms && ed->priv.server->skeleton.model && ed->priv.server->skeleton.model->num_bones)
189 ed->priv.server->skeleton.relativetransforms = Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
190 if (ed->priv.server->skeleton.relativetransforms)
192 int skeletonindex = 0;
193 skeleton_t *skeleton;
195 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.skeletonindex))) skeletonindex = (int)val->_float;
196 if (skeletonindex > 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
198 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
199 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
203 // generated skeleton from frame animation
206 int numbones = ed->priv.server->skeleton.model->num_bones;
207 const short *framebones6s;
209 float scale = ed->priv.server->skeleton.model->num_posescale;
210 matrix4x4_t *relativetransforms = ed->priv.server->skeleton.relativetransforms;
212 memset(relativetransforms, 0, numbones * sizeof(matrix4x4_t));
213 for (blendindex = 0;blendindex < MAX_FRAMEBLENDS && frameblend[blendindex].lerp > 0;blendindex++)
215 lerp = frameblend[blendindex].lerp;
216 framebones6s = ed->priv.server->skeleton.model->data_poses6s + 6 * frameblend[blendindex].subframe * numbones;
217 for (bonenum = 0;bonenum < numbones;bonenum++)
219 Matrix4x4_FromBonePose6s(&matrix, scale, framebones6s + 6 * bonenum);
220 Matrix4x4_Accumulate(&ed->priv.server->skeleton.relativetransforms[bonenum], &matrix, lerp);
227 void VM_RemoveEdictSkeleton(prvm_edict_t *ed)
229 if (ed->priv.server->skeleton.relativetransforms)
230 Mem_Free(ed->priv.server->skeleton.relativetransforms);
231 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
237 //============================================================================
240 void VM_VarString(int first, char *out, int outlength)
246 outend = out + outlength - 1;
247 for (i = first;i < prog->argc && out < outend;i++)
249 s = PRVM_G_STRING((OFS_PARM0+i*3));
250 while (out < outend && *s)
260 returns true if the extension is supported by the server
262 checkextension(extensionname)
266 // kind of helper function
267 static qboolean checkextension(const char *name)
271 len = (int)strlen(name);
273 for (e = prog->extensionstring;*e;e++)
280 while (*e && *e != ' ')
282 if ((e - start) == len && !strncasecmp(start, name, len))
288 void VM_checkextension (void)
290 VM_SAFEPARMCOUNT(1,VM_checkextension);
292 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
299 This is a TERMINAL error, which will kill off the entire prog.
308 char string[VM_STRINGTEMP_LENGTH];
310 VM_VarString(0, string, sizeof(string));
311 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
312 if (prog->globaloffsets.self >= 0)
314 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
315 PRVM_ED_Print(ed, NULL);
318 PRVM_ERROR ("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
325 Dumps out self, then an error message. The program is aborted and self is
326 removed, but the level can continue.
331 void VM_objerror (void)
334 char string[VM_STRINGTEMP_LENGTH];
336 VM_VarString(0, string, sizeof(string));
337 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
338 if (prog->globaloffsets.self >= 0)
340 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
341 PRVM_ED_Print(ed, NULL);
346 // objerror has to display the object fields -> else call
347 PRVM_ERROR ("VM_objecterror: self not defined !");
348 Con_Printf("%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
362 char string[VM_STRINGTEMP_LENGTH];
364 VM_VarString(0, string, sizeof(string));
372 broadcast print to everyone on server
377 void VM_bprint (void)
379 char string[VM_STRINGTEMP_LENGTH];
383 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
387 VM_VarString(0, string, sizeof(string));
388 SV_BroadcastPrint(string);
393 VM_sprint (menu & client but only if server.active == true)
395 single print to a specific client
397 sprint(float clientnum,...[string])
400 void VM_sprint (void)
404 char string[VM_STRINGTEMP_LENGTH];
406 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
408 //find client for this entity
409 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
410 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
412 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
416 client = svs.clients + clientnum;
417 if (!client->netconnection)
420 VM_VarString(1, string, sizeof(string));
421 MSG_WriteChar(&client->netconnection->message,svc_print);
422 MSG_WriteString(&client->netconnection->message, string);
429 single print to the screen
434 void VM_centerprint (void)
436 char string[VM_STRINGTEMP_LENGTH];
438 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
439 VM_VarString(0, string, sizeof(string));
440 SCR_CenterPrint(string);
447 vector normalize(vector)
450 void VM_normalize (void)
456 VM_SAFEPARMCOUNT(1,VM_normalize);
458 value1 = PRVM_G_VECTOR(OFS_PARM0);
460 f = VectorLength2(value1);
464 VectorScale(value1, f, newvalue);
467 VectorClear(newvalue);
469 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
481 VM_SAFEPARMCOUNT(1,VM_vlen);
482 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
489 float vectoyaw(vector)
492 void VM_vectoyaw (void)
497 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
499 value1 = PRVM_G_VECTOR(OFS_PARM0);
501 if (value1[1] == 0 && value1[0] == 0)
505 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
510 PRVM_G_FLOAT(OFS_RETURN) = yaw;
518 vector vectoangles(vector[, vector])
521 void VM_vectoangles (void)
523 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
525 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
532 Returns a number from 0<= num < 1
537 void VM_random (void)
539 VM_SAFEPARMCOUNT(0,VM_random);
541 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
548 localsound(string sample)
551 void VM_localsound(void)
555 VM_SAFEPARMCOUNT(1,VM_localsound);
557 s = PRVM_G_STRING(OFS_PARM0);
559 if(!S_LocalSound (s))
561 PRVM_G_FLOAT(OFS_RETURN) = -4;
562 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
566 PRVM_G_FLOAT(OFS_RETURN) = 1;
578 PRVM_ERROR ("%s: break statement", PRVM_NAME);
581 //============================================================================
587 Sends text over to the client's execution buffer
589 [localcmd (string, ...) or]
593 void VM_localcmd (void)
595 char string[VM_STRINGTEMP_LENGTH];
596 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
597 VM_VarString(0, string, sizeof(string));
598 Cbuf_AddText(string);
601 static qboolean PRVM_Cvar_ReadOk(const char *string)
604 cvar = Cvar_FindVar(string);
605 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
617 char string[VM_STRINGTEMP_LENGTH];
618 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
619 VM_VarString(0, string, sizeof(string));
620 VM_CheckEmptyString(string);
621 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
628 float cvar_type (string)
629 float CVAR_TYPEFLAG_EXISTS = 1;
630 float CVAR_TYPEFLAG_SAVED = 2;
631 float CVAR_TYPEFLAG_PRIVATE = 4;
632 float CVAR_TYPEFLAG_ENGINE = 8;
633 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
634 float CVAR_TYPEFLAG_READONLY = 32;
637 void VM_cvar_type (void)
639 char string[VM_STRINGTEMP_LENGTH];
643 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
644 VM_VarString(0, string, sizeof(string));
645 VM_CheckEmptyString(string);
646 cvar = Cvar_FindVar(string);
651 PRVM_G_FLOAT(OFS_RETURN) = 0;
652 return; // CVAR_TYPE_NONE
655 ret = 1; // CVAR_EXISTS
656 if(cvar->flags & CVAR_SAVE)
657 ret |= 2; // CVAR_TYPE_SAVED
658 if(cvar->flags & CVAR_PRIVATE)
659 ret |= 4; // CVAR_TYPE_PRIVATE
660 if(!(cvar->flags & CVAR_ALLOCATED))
661 ret |= 8; // CVAR_TYPE_ENGINE
662 if(cvar->description != cvar_dummy_description)
663 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
664 if(cvar->flags & CVAR_READONLY)
665 ret |= 32; // CVAR_TYPE_READONLY
667 PRVM_G_FLOAT(OFS_RETURN) = ret;
674 const string VM_cvar_string (string, ...)
677 void VM_cvar_string(void)
679 char string[VM_STRINGTEMP_LENGTH];
680 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
681 VM_VarString(0, string, sizeof(string));
682 VM_CheckEmptyString(string);
683 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
688 ========================
691 const string VM_cvar_defstring (string, ...)
692 ========================
694 void VM_cvar_defstring (void)
696 char string[VM_STRINGTEMP_LENGTH];
697 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
698 VM_VarString(0, string, sizeof(string));
699 VM_CheckEmptyString(string);
700 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
704 ========================
707 const string VM_cvar_description (string, ...)
708 ========================
710 void VM_cvar_description (void)
712 char string[VM_STRINGTEMP_LENGTH];
713 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
714 VM_VarString(0, string, sizeof(string));
715 VM_CheckEmptyString(string);
716 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
722 void cvar_set (string,string, ...)
725 void VM_cvar_set (void)
728 char string[VM_STRINGTEMP_LENGTH];
729 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
730 VM_VarString(1, string, sizeof(string));
731 name = PRVM_G_STRING(OFS_PARM0);
732 VM_CheckEmptyString(name);
733 Cvar_Set(name, string);
743 void VM_dprint (void)
745 char string[VM_STRINGTEMP_LENGTH];
746 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
747 VM_VarString(0, string, sizeof(string));
749 Con_DPrintf("%s", string);
751 Con_DPrintf("%s: %s", PRVM_NAME, string);
768 VM_SAFEPARMCOUNT(1, VM_ftos);
770 v = PRVM_G_FLOAT(OFS_PARM0);
772 if ((float)((int)v) == v)
773 dpsnprintf(s, sizeof(s), "%i", (int)v);
775 dpsnprintf(s, sizeof(s), "%f", v);
776 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
791 VM_SAFEPARMCOUNT(1,VM_fabs);
793 v = PRVM_G_FLOAT(OFS_PARM0);
794 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
809 VM_SAFEPARMCOUNT(1,VM_vtos);
811 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]);
812 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
827 VM_SAFEPARMCOUNT(1, VM_etos);
829 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
830 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
837 float stof(...[string])
842 char string[VM_STRINGTEMP_LENGTH];
843 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
844 VM_VarString(0, string, sizeof(string));
845 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
849 ========================
853 ========================
857 VM_SAFEPARMCOUNT(1, VM_itof);
858 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
862 ========================
865 entity ftoe(float num)
866 ========================
871 VM_SAFEPARMCOUNT(1, VM_ftoe);
873 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
874 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
875 ent = 0; // return world instead of a free or invalid entity
877 PRVM_G_INT(OFS_RETURN) = ent;
881 ========================
884 float etof(entity ent)
885 ========================
889 VM_SAFEPARMCOUNT(1, VM_etof);
890 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
897 string strftime(float uselocaltime, string[, string ...])
900 void VM_strftime(void)
909 char fmt[VM_STRINGTEMP_LENGTH];
910 char result[VM_STRINGTEMP_LENGTH];
911 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
912 VM_VarString(1, fmt, sizeof(fmt));
915 if (PRVM_G_FLOAT(OFS_PARM0))
916 tmresult = localtime_s(&tm, &t);
918 tmresult = gmtime_s(&tm, &t);
921 if (PRVM_G_FLOAT(OFS_PARM0))
928 PRVM_G_INT(OFS_RETURN) = 0;
932 strftime(result, sizeof(result), fmt, &tm);
934 strftime(result, sizeof(result), fmt, tm);
936 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
950 VM_SAFEPARMCOUNT(0, VM_spawn);
951 prog->xfunction->builtinsprofile += 20;
952 ed = PRVM_ED_Alloc();
964 void VM_remove (void)
967 prog->xfunction->builtinsprofile += 20;
969 VM_SAFEPARMCOUNT(1, VM_remove);
971 ed = PRVM_G_EDICT(OFS_PARM0);
972 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
974 if (developer.integer)
975 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
977 else if( ed->priv.required->free )
979 if (developer.integer)
980 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
990 entity find(entity start, .string field, string match)
1001 VM_SAFEPARMCOUNT(3,VM_find);
1003 e = PRVM_G_EDICTNUM(OFS_PARM0);
1004 f = PRVM_G_INT(OFS_PARM1);
1005 s = PRVM_G_STRING(OFS_PARM2);
1007 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1008 // expects it to find all the monsters, so we must be careful to support
1011 for (e++ ; e < prog->num_edicts ; e++)
1013 prog->xfunction->builtinsprofile++;
1014 ed = PRVM_EDICT_NUM(e);
1015 if (ed->priv.required->free)
1017 t = PRVM_E_STRING(ed,f);
1022 VM_RETURN_EDICT(ed);
1027 VM_RETURN_EDICT(prog->edicts);
1034 entity findfloat(entity start, .float field, float match)
1035 entity findentity(entity start, .entity field, entity match)
1038 // LordHavoc: added this for searching float, int, and entity reference fields
1039 void VM_findfloat (void)
1046 VM_SAFEPARMCOUNT(3,VM_findfloat);
1048 e = PRVM_G_EDICTNUM(OFS_PARM0);
1049 f = PRVM_G_INT(OFS_PARM1);
1050 s = PRVM_G_FLOAT(OFS_PARM2);
1052 for (e++ ; e < prog->num_edicts ; e++)
1054 prog->xfunction->builtinsprofile++;
1055 ed = PRVM_EDICT_NUM(e);
1056 if (ed->priv.required->free)
1058 if (PRVM_E_FLOAT(ed,f) == s)
1060 VM_RETURN_EDICT(ed);
1065 VM_RETURN_EDICT(prog->edicts);
1072 entity findchain(.string field, string match)
1075 // chained search for strings in entity fields
1076 // entity(.string field, string match) findchain = #402;
1077 void VM_findchain (void)
1082 prvm_edict_t *ent, *chain;
1085 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1088 chainfield = PRVM_G_INT(OFS_PARM2);
1090 chainfield = prog->fieldoffsets.chain;
1092 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1094 chain = prog->edicts;
1096 f = PRVM_G_INT(OFS_PARM0);
1097 s = PRVM_G_STRING(OFS_PARM1);
1099 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1100 // expects it to find all the monsters, so we must be careful to support
1103 ent = PRVM_NEXT_EDICT(prog->edicts);
1104 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1106 prog->xfunction->builtinsprofile++;
1107 if (ent->priv.required->free)
1109 t = PRVM_E_STRING(ent,f);
1115 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_NUM_FOR_EDICT(chain);
1119 VM_RETURN_EDICT(chain);
1126 entity findchainfloat(.string field, float match)
1127 entity findchainentity(.string field, entity match)
1130 // LordHavoc: chained search for float, int, and entity reference fields
1131 // entity(.string field, float match) findchainfloat = #403;
1132 void VM_findchainfloat (void)
1137 prvm_edict_t *ent, *chain;
1140 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1143 chainfield = PRVM_G_INT(OFS_PARM2);
1145 chainfield = prog->fieldoffsets.chain;
1147 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1149 chain = (prvm_edict_t *)prog->edicts;
1151 f = PRVM_G_INT(OFS_PARM0);
1152 s = PRVM_G_FLOAT(OFS_PARM1);
1154 ent = PRVM_NEXT_EDICT(prog->edicts);
1155 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1157 prog->xfunction->builtinsprofile++;
1158 if (ent->priv.required->free)
1160 if (PRVM_E_FLOAT(ent,f) != s)
1163 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1167 VM_RETURN_EDICT(chain);
1171 ========================
1174 entity findflags(entity start, .float field, float match)
1175 ========================
1177 // LordHavoc: search for flags in float fields
1178 void VM_findflags (void)
1185 VM_SAFEPARMCOUNT(3, VM_findflags);
1188 e = PRVM_G_EDICTNUM(OFS_PARM0);
1189 f = PRVM_G_INT(OFS_PARM1);
1190 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1192 for (e++ ; e < prog->num_edicts ; e++)
1194 prog->xfunction->builtinsprofile++;
1195 ed = PRVM_EDICT_NUM(e);
1196 if (ed->priv.required->free)
1198 if (!PRVM_E_FLOAT(ed,f))
1200 if ((int)PRVM_E_FLOAT(ed,f) & s)
1202 VM_RETURN_EDICT(ed);
1207 VM_RETURN_EDICT(prog->edicts);
1211 ========================
1214 entity findchainflags(.float field, float match)
1215 ========================
1217 // LordHavoc: chained search for flags in float fields
1218 void VM_findchainflags (void)
1223 prvm_edict_t *ent, *chain;
1226 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1229 chainfield = PRVM_G_INT(OFS_PARM2);
1231 chainfield = prog->fieldoffsets.chain;
1233 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1235 chain = (prvm_edict_t *)prog->edicts;
1237 f = PRVM_G_INT(OFS_PARM0);
1238 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1240 ent = PRVM_NEXT_EDICT(prog->edicts);
1241 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1243 prog->xfunction->builtinsprofile++;
1244 if (ent->priv.required->free)
1246 if (!PRVM_E_FLOAT(ent,f))
1248 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1251 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1255 VM_RETURN_EDICT(chain);
1262 string precache_sound (string sample)
1265 void VM_precache_sound (void)
1269 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1271 s = PRVM_G_STRING(OFS_PARM0);
1272 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1273 //VM_CheckEmptyString(s);
1275 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1277 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1286 returns the same string as output
1288 does nothing, only used by qcc to build .pak archives
1291 void VM_precache_file (void)
1293 VM_SAFEPARMCOUNT(1,VM_precache_file);
1294 // precache_file is only used to copy files with qcc, it does nothing
1295 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1305 void VM_coredump (void)
1307 VM_SAFEPARMCOUNT(0,VM_coredump);
1309 Cbuf_AddText("prvm_edicts ");
1310 Cbuf_AddText(PRVM_NAME);
1321 void PRVM_StackTrace(void);
1322 void VM_stackdump (void)
1324 VM_SAFEPARMCOUNT(0, VM_stackdump);
1339 VM_SAFEPARMCOUNT(0, VM_crash);
1341 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1351 void VM_traceon (void)
1353 VM_SAFEPARMCOUNT(0,VM_traceon);
1365 void VM_traceoff (void)
1367 VM_SAFEPARMCOUNT(0,VM_traceoff);
1369 prog->trace = false;
1379 void VM_eprint (void)
1381 VM_SAFEPARMCOUNT(1,VM_eprint);
1383 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1396 VM_SAFEPARMCOUNT(1,VM_rint);
1398 f = PRVM_G_FLOAT(OFS_PARM0);
1400 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1402 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1412 void VM_floor (void)
1414 VM_SAFEPARMCOUNT(1,VM_floor);
1416 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1428 VM_SAFEPARMCOUNT(1,VM_ceil);
1430 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1438 entity nextent(entity)
1441 void VM_nextent (void)
1446 VM_SAFEPARMCOUNT(1, VM_nextent);
1448 i = PRVM_G_EDICTNUM(OFS_PARM0);
1451 prog->xfunction->builtinsprofile++;
1453 if (i == prog->num_edicts)
1455 VM_RETURN_EDICT(prog->edicts);
1458 ent = PRVM_EDICT_NUM(i);
1459 if (!ent->priv.required->free)
1461 VM_RETURN_EDICT(ent);
1467 //=============================================================================
1474 changelevel(string map)
1477 void VM_changelevel (void)
1479 VM_SAFEPARMCOUNT(1, VM_changelevel);
1483 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1487 // make sure we don't issue two changelevels
1488 if (svs.changelevel_issued)
1490 svs.changelevel_issued = true;
1492 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1504 VM_SAFEPARMCOUNT(1,VM_sin);
1505 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1516 VM_SAFEPARMCOUNT(1,VM_cos);
1517 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1529 VM_SAFEPARMCOUNT(1,VM_sqrt);
1530 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1542 VM_SAFEPARMCOUNT(1,VM_asin);
1543 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1554 VM_SAFEPARMCOUNT(1,VM_acos);
1555 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1566 VM_SAFEPARMCOUNT(1,VM_atan);
1567 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1573 float atan2(float,float)
1576 void VM_atan2 (void)
1578 VM_SAFEPARMCOUNT(2,VM_atan2);
1579 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1590 VM_SAFEPARMCOUNT(1,VM_tan);
1591 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1598 Returns a vector of length < 1 and > 0
1603 void VM_randomvec (void)
1608 VM_SAFEPARMCOUNT(0, VM_randomvec);
1613 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1614 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1615 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1617 while (DotProduct(temp, temp) >= 1);
1618 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1621 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1622 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1623 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1624 // length returned always > 0
1625 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1626 VectorScale(temp,length, temp);*/
1627 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1630 //=============================================================================
1636 float registercvar (string name, string value[, float flags])
1639 void VM_registercvar (void)
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("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])
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])
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 (void)
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)
1749 VM_SAFEPARMCOUNT(2,VM_pow);
1750 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1755 VM_SAFEPARMCOUNT(1,VM_log);
1756 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1759 void VM_Files_Init(void)
1762 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1763 prog->openfiles[i] = NULL;
1766 void VM_Files_CloseAll(void)
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( int index )
1779 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1781 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_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, PRVM_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
1805 const char *modestring, *filename;
1807 VM_SAFEPARMCOUNT(2,VM_fopen);
1809 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1810 if (prog->openfiles[filenum] == NULL)
1812 if (filenum >= PRVM_MAX_OPENFILES)
1814 PRVM_G_FLOAT(OFS_RETURN) = -2;
1815 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1818 filename = PRVM_G_STRING(OFS_PARM0);
1819 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1822 case 0: // FILE_READ
1824 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1825 if (prog->openfiles[filenum] == NULL)
1826 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1828 case 1: // FILE_APPEND
1830 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1832 case 2: // FILE_WRITE
1834 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1837 PRVM_G_FLOAT(OFS_RETURN) = -3;
1838 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1842 if (prog->openfiles[filenum] == NULL)
1844 PRVM_G_FLOAT(OFS_RETURN) = -1;
1845 if (developer_extra.integer)
1846 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1850 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1851 if (developer_extra.integer)
1852 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1853 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1861 fclose(float fhandle)
1864 //void(float fhandle) fclose = #111; // closes a file
1865 void VM_fclose(void)
1869 VM_SAFEPARMCOUNT(1,VM_fclose);
1871 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1872 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1874 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1877 if (prog->openfiles[filenum] == NULL)
1879 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1882 FS_Close(prog->openfiles[filenum]);
1883 prog->openfiles[filenum] = NULL;
1884 if(prog->openfiles_origin[filenum])
1885 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1886 if (developer_extra.integer)
1887 Con_DPrintf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1894 string fgets(float fhandle)
1897 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1901 char string[VM_STRINGTEMP_LENGTH];
1904 VM_SAFEPARMCOUNT(1,VM_fgets);
1906 // set the return value regardless of any possible errors
1907 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1909 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1910 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1912 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1915 if (prog->openfiles[filenum] == NULL)
1917 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1923 c = FS_Getc(prog->openfiles[filenum]);
1924 if (c == '\r' || c == '\n' || c < 0)
1926 if (end < VM_STRINGTEMP_LENGTH - 1)
1930 // remove \n following \r
1933 c = FS_Getc(prog->openfiles[filenum]);
1935 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1937 if (developer_extra.integer)
1938 Con_DPrintf("fgets: %s: %s\n", PRVM_NAME, string);
1940 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1947 fputs(float fhandle, string s)
1950 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1954 char string[VM_STRINGTEMP_LENGTH];
1957 VM_SAFEPARMCOUNT(2,VM_fputs);
1959 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1960 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1962 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1965 if (prog->openfiles[filenum] == NULL)
1967 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1970 VM_VarString(1, string, sizeof(string));
1971 if ((stringlength = (int)strlen(string)))
1972 FS_Write(prog->openfiles[filenum], string, stringlength);
1973 if (developer_extra.integer)
1974 Con_DPrintf("fputs: %s: %s\n", PRVM_NAME, string);
1981 writetofile(float fhandle, entity ent)
1984 void VM_writetofile(void)
1989 VM_SAFEPARMCOUNT(2, VM_writetofile);
1991 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1994 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1998 ent = PRVM_G_EDICT(OFS_PARM1);
1999 if(ent->priv.required->free)
2001 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2005 PRVM_ED_Write (file, ent);
2008 // KrimZon - DP_QC_ENTITYDATA
2013 float() numentityfields
2014 Return the number of entity fields - NOT offsets
2017 void VM_numentityfields(void)
2019 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
2022 // KrimZon - DP_QC_ENTITYDATA
2027 string(float fieldnum) entityfieldname
2028 Return name of the specified field as a string, or empty if the field is invalid (warning)
2031 void VM_entityfieldname(void)
2034 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2036 if (i < 0 || i >= prog->progs->numfielddefs)
2038 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
2039 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2043 d = &prog->fielddefs[i];
2044 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2047 // KrimZon - DP_QC_ENTITYDATA
2052 float(float fieldnum) entityfieldtype
2055 void VM_entityfieldtype(void)
2058 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2060 if (i < 0 || i >= prog->progs->numfielddefs)
2062 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
2063 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2067 d = &prog->fielddefs[i];
2068 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2071 // KrimZon - DP_QC_ENTITYDATA
2074 VM_getentityfieldstring
2076 string(float fieldnum, entity ent) getentityfieldstring
2079 void VM_getentityfieldstring(void)
2081 // put the data into a string
2086 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2088 if (i < 0 || i >= prog->progs->numfielddefs)
2090 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2091 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2095 d = &prog->fielddefs[i];
2098 ent = PRVM_G_EDICT(OFS_PARM1);
2099 if(ent->priv.required->free)
2101 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2102 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2105 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2107 // if it's 0 or blank, return an empty string
2108 type = d->type & ~DEF_SAVEGLOBAL;
2109 for (j=0 ; j<prvm_type_size[type] ; j++)
2112 if (j == prvm_type_size[type])
2114 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2118 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
2121 // KrimZon - DP_QC_ENTITYDATA
2124 VM_putentityfieldstring
2126 float(float fieldnum, entity ent, string s) putentityfieldstring
2129 void VM_putentityfieldstring(void)
2133 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2135 if (i < 0 || i >= prog->progs->numfielddefs)
2137 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2138 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2142 d = &prog->fielddefs[i];
2145 ent = PRVM_G_EDICT(OFS_PARM1);
2146 if(ent->priv.required->free)
2148 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2149 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2153 // parse the string into the value
2154 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2161 float strlen(string s)
2164 //float(string s) strlen = #114; // returns how many characters are in a string
2165 void VM_strlen(void)
2167 VM_SAFEPARMCOUNT(1,VM_strlen);
2169 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2170 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2173 // DRESK - Decolorized String
2178 string strdecolorize(string s)
2181 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2182 void VM_strdecolorize(void)
2184 char szNewString[VM_STRINGTEMP_LENGTH];
2185 const char *szString;
2188 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2189 szString = PRVM_G_STRING(OFS_PARM0);
2190 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2191 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2194 // DRESK - String Length (not counting color codes)
2199 float strlennocol(string s)
2202 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2203 // For example, ^2Dresk returns a length of 5
2204 void VM_strlennocol(void)
2206 const char *szString;
2209 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2211 szString = PRVM_G_STRING(OFS_PARM0);
2213 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2214 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2216 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2219 // DRESK - String to Uppercase and Lowercase
2224 string strtolower(string s)
2227 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2228 void VM_strtolower(void)
2230 char szNewString[VM_STRINGTEMP_LENGTH];
2231 const char *szString;
2234 VM_SAFEPARMCOUNT(1,VM_strtolower);
2235 szString = PRVM_G_STRING(OFS_PARM0);
2237 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2239 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2246 string strtoupper(string s)
2249 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2250 void VM_strtoupper(void)
2252 char szNewString[VM_STRINGTEMP_LENGTH];
2253 const char *szString;
2256 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2257 szString = PRVM_G_STRING(OFS_PARM0);
2259 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2261 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2268 string strcat(string,string,...[string])
2271 //string(string s1, string s2) strcat = #115;
2272 // concatenates two strings (for example "abc", "def" would return "abcdef")
2273 // and returns as a tempstring
2274 void VM_strcat(void)
2276 char s[VM_STRINGTEMP_LENGTH];
2277 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2279 VM_VarString(0, s, sizeof(s));
2280 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2287 string substring(string s, float start, float length)
2290 // string(string s, float start, float length) substring = #116;
2291 // returns a section of a string as a tempstring
2292 void VM_substring(void)
2295 int u_slength = 0, u_start;
2298 char string[VM_STRINGTEMP_LENGTH];
2300 VM_SAFEPARMCOUNT(3,VM_substring);
2303 s = PRVM_G_STRING(OFS_PARM0);
2304 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2305 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2306 slength = strlen(s);
2308 if (start < 0) // FTE_STRINGS feature
2310 start = bound(0, start, slength);
2312 if (length < 0) // FTE_STRINGS feature
2313 length += slength - start + 1;
2314 maxlen = min((int)sizeof(string) - 1, slength - start);
2315 length = bound(0, length, maxlen);
2317 memcpy(string, s + start, length);
2319 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2322 s = PRVM_G_STRING(OFS_PARM0);
2323 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2324 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2326 if (start < 0) // FTE_STRINGS feature
2328 u_slength = u8_strlen(s);
2330 start = bound(0, start, u_slength);
2333 if (length < 0) // FTE_STRINGS feature
2335 if (!u_slength) // it's not calculated when it's not needed above
2336 u_slength = u8_strlen(s);
2337 length += u_slength - start + 1;
2340 // positive start, positive length
2341 u_start = u8_byteofs(s, start, NULL);
2344 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2347 u_length = u8_bytelen(s + u_start, length);
2348 if (u_length >= sizeof(string)-1)
2349 u_length = sizeof(string)-1;
2351 memcpy(string, s + u_start, u_length);
2352 string[u_length] = 0;
2353 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2360 string(string search, string replace, string subject) strreplace = #484;
2363 // replaces all occurrences of search with replace in the string subject, and returns the result
2364 void VM_strreplace(void)
2367 const char *search, *replace, *subject;
2368 char string[VM_STRINGTEMP_LENGTH];
2369 int search_len, replace_len, subject_len;
2371 VM_SAFEPARMCOUNT(3,VM_strreplace);
2373 search = PRVM_G_STRING(OFS_PARM0);
2374 replace = PRVM_G_STRING(OFS_PARM1);
2375 subject = PRVM_G_STRING(OFS_PARM2);
2377 search_len = (int)strlen(search);
2378 replace_len = (int)strlen(replace);
2379 subject_len = (int)strlen(subject);
2382 for (i = 0; i < subject_len; i++)
2384 for (j = 0; j < search_len && i+j < subject_len; j++)
2385 if (subject[i+j] != search[j])
2387 if (j == search_len || i+j == subject_len)
2389 // found it at offset 'i'
2390 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2391 string[si++] = replace[j];
2392 i += search_len - 1;
2397 if (si < (int)sizeof(string) - 1)
2398 string[si++] = subject[i];
2403 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2410 string(string search, string replace, string subject) strireplace = #485;
2413 // case-insensitive version of strreplace
2414 void VM_strireplace(void)
2417 const char *search, *replace, *subject;
2418 char string[VM_STRINGTEMP_LENGTH];
2419 int search_len, replace_len, subject_len;
2421 VM_SAFEPARMCOUNT(3,VM_strreplace);
2423 search = PRVM_G_STRING(OFS_PARM0);
2424 replace = PRVM_G_STRING(OFS_PARM1);
2425 subject = PRVM_G_STRING(OFS_PARM2);
2427 search_len = (int)strlen(search);
2428 replace_len = (int)strlen(replace);
2429 subject_len = (int)strlen(subject);
2432 for (i = 0; i < subject_len; i++)
2434 for (j = 0; j < search_len && i+j < subject_len; j++)
2435 if (tolower(subject[i+j]) != tolower(search[j]))
2437 if (j == search_len || i+j == subject_len)
2439 // found it at offset 'i'
2440 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2441 string[si++] = replace[j];
2442 i += search_len - 1;
2447 if (si < (int)sizeof(string) - 1)
2448 string[si++] = subject[i];
2453 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2460 vector stov(string s)
2463 //vector(string s) stov = #117; // returns vector value from a string
2466 char string[VM_STRINGTEMP_LENGTH];
2468 VM_SAFEPARMCOUNT(1,VM_stov);
2470 VM_VarString(0, string, sizeof(string));
2471 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2478 string strzone(string s)
2481 //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)
2482 void VM_strzone(void)
2485 char string[VM_STRINGTEMP_LENGTH];
2488 VM_SAFEPARMCOUNT(1,VM_strzone);
2490 VM_VarString(0, string, sizeof(string));
2491 alloclen = strlen(string) + 1;
2492 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2493 memcpy(out, string, alloclen);
2503 //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!!!)
2504 void VM_strunzone(void)
2506 VM_SAFEPARMCOUNT(1,VM_strunzone);
2507 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2512 VM_command (used by client and menu)
2514 clientcommand(float client, string s) (for client and menu)
2517 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2518 //this function originally written by KrimZon, made shorter by LordHavoc
2519 void VM_clcommand (void)
2521 client_t *temp_client;
2524 VM_SAFEPARMCOUNT(2,VM_clcommand);
2526 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2527 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2529 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2533 temp_client = host_client;
2534 host_client = svs.clients + i;
2535 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2536 host_client = temp_client;
2544 float tokenize(string s)
2547 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2548 //this function originally written by KrimZon, made shorter by LordHavoc
2549 //20040203: rewritten by LordHavoc (no longer uses allocations)
2550 static int num_tokens = 0;
2551 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2552 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2553 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2554 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2555 void VM_tokenize (void)
2559 VM_SAFEPARMCOUNT(1,VM_tokenize);
2561 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2562 p = tokenize_string;
2567 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2570 // skip whitespace here to find token start pos
2571 while(*p && ISWHITESPACE(*p))
2574 tokens_startpos[num_tokens] = p - tokenize_string;
2575 if(!COM_ParseToken_VM_Tokenize(&p, false))
2577 tokens_endpos[num_tokens] = p - tokenize_string;
2578 tokens[num_tokens] = PRVM_SetTempString(com_token);
2582 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2585 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2586 void VM_tokenize_console (void)
2590 VM_SAFEPARMCOUNT(1,VM_tokenize);
2592 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2593 p = tokenize_string;
2598 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2601 // skip whitespace here to find token start pos
2602 while(*p && ISWHITESPACE(*p))
2605 tokens_startpos[num_tokens] = p - tokenize_string;
2606 if(!COM_ParseToken_Console(&p))
2608 tokens_endpos[num_tokens] = p - tokenize_string;
2609 tokens[num_tokens] = PRVM_SetTempString(com_token);
2613 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2618 VM_tokenizebyseparator
2620 float tokenizebyseparator(string s, string separator1, ...)
2623 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2624 //this function returns the token preceding each instance of a separator (of
2625 //which there can be multiple), and the text following the last separator
2626 //useful for parsing certain kinds of data like IP addresses
2628 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2629 //returns 4 and the tokens "10" "1" "2" "3".
2630 void VM_tokenizebyseparator (void)
2634 int separatorlen[7];
2635 const char *separators[7];
2638 char tokentext[MAX_INPUTLINE];
2640 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2642 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2643 p = tokenize_string;
2646 for (j = 1;j < prog->argc;j++)
2648 // skip any blank separator strings
2649 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2652 separators[numseparators] = s;
2653 separatorlen[numseparators] = strlen(s);
2660 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2662 token = tokentext + j;
2663 tokens_startpos[num_tokens] = p - tokenize_string;
2667 for (k = 0;k < numseparators;k++)
2669 if (!strncmp(p, separators[k], separatorlen[k]))
2671 p += separatorlen[k];
2675 if (k < numseparators)
2677 if (j < (int)sizeof(tokentext)-1)
2678 tokentext[j++] = *p;
2682 tokens_endpos[num_tokens] = p0 - tokenize_string;
2683 if (j >= (int)sizeof(tokentext))
2686 tokens[num_tokens++] = PRVM_SetTempString(token);
2691 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2694 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2695 //this function originally written by KrimZon, made shorter by LordHavoc
2700 VM_SAFEPARMCOUNT(1,VM_argv);
2702 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2705 token_num += num_tokens;
2707 if (token_num >= 0 && token_num < num_tokens)
2708 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2710 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2713 //float(float n) argv_start_index = #515; // returns the start index of a token
2714 void VM_argv_start_index (void)
2718 VM_SAFEPARMCOUNT(1,VM_argv);
2720 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2723 token_num += num_tokens;
2725 if (token_num >= 0 && token_num < num_tokens)
2726 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2728 PRVM_G_FLOAT(OFS_RETURN) = -1;
2731 //float(float n) argv_end_index = #516; // returns the end index of a token
2732 void VM_argv_end_index (void)
2736 VM_SAFEPARMCOUNT(1,VM_argv);
2738 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2741 token_num += num_tokens;
2743 if (token_num >= 0 && token_num < num_tokens)
2744 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2746 PRVM_G_FLOAT(OFS_RETURN) = -1;
2756 void VM_isserver(void)
2758 VM_SAFEPARMCOUNT(0,VM_serverstate);
2760 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2770 void VM_clientcount(void)
2772 VM_SAFEPARMCOUNT(0,VM_clientcount);
2774 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2784 void VM_clientstate(void)
2786 VM_SAFEPARMCOUNT(0,VM_clientstate);
2789 switch( cls.state ) {
2790 case ca_uninitialized:
2792 PRVM_G_FLOAT(OFS_RETURN) = 0;
2794 case ca_disconnected:
2795 PRVM_G_FLOAT(OFS_RETURN) = 1;
2798 PRVM_G_FLOAT(OFS_RETURN) = 2;
2801 // should never be reached!
2810 float getostype(void)
2812 */ // not used at the moment -> not included in the common list
2813 void VM_getostype(void)
2815 VM_SAFEPARMCOUNT(0,VM_getostype);
2820 OS_MAC - not supported
2824 PRVM_G_FLOAT(OFS_RETURN) = 0;
2825 #elif defined(MACOSX)
2826 PRVM_G_FLOAT(OFS_RETURN) = 2;
2828 PRVM_G_FLOAT(OFS_RETURN) = 1;
2839 extern double host_starttime;
2840 float CDAudio_GetPosition(void);
2841 void VM_gettime(void)
2845 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2849 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2853 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2856 case 0: // GETTIME_FRAMESTART
2857 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2859 case 1: // GETTIME_REALTIME
2860 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2862 case 2: // GETTIME_HIRES
2863 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2865 case 3: // GETTIME_UPTIME
2866 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2868 case 4: // GETTIME_CDTRACK
2869 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2872 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2873 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2883 loadfromdata(string data)
2886 void VM_loadfromdata(void)
2888 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2890 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2894 ========================
2897 parseentitydata(entity ent, string data)
2898 ========================
2900 void VM_parseentitydata(void)
2905 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2907 // get edict and test it
2908 ent = PRVM_G_EDICT(OFS_PARM0);
2909 if (ent->priv.required->free)
2910 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2912 data = PRVM_G_STRING(OFS_PARM1);
2914 // parse the opening brace
2915 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2916 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2918 PRVM_ED_ParseEdict (data, ent);
2925 loadfromfile(string file)
2928 void VM_loadfromfile(void)
2930 const char *filename;
2933 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2935 filename = PRVM_G_STRING(OFS_PARM0);
2936 if (FS_CheckNastyPath(filename, false))
2938 PRVM_G_FLOAT(OFS_RETURN) = -4;
2939 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2943 // not conform with VM_fopen
2944 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2946 PRVM_G_FLOAT(OFS_RETURN) = -1;
2948 PRVM_ED_LoadFromFile(data);
2959 float mod(float val, float m)
2962 void VM_modulo(void)
2965 VM_SAFEPARMCOUNT(2,VM_module);
2967 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2968 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2970 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2973 void VM_Search_Init(void)
2976 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2977 prog->opensearches[i] = NULL;
2980 void VM_Search_Reset(void)
2983 // reset the fssearch list
2984 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2986 if(prog->opensearches[i])
2987 FS_FreeSearch(prog->opensearches[i]);
2988 prog->opensearches[i] = NULL;
2996 float search_begin(string pattern, float caseinsensitive, float quiet)
2999 void VM_search_begin(void)
3002 const char *pattern;
3003 int caseinsens, quiet;
3005 VM_SAFEPARMCOUNT(3, VM_search_begin);
3007 pattern = PRVM_G_STRING(OFS_PARM0);
3009 VM_CheckEmptyString(pattern);
3011 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3012 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3014 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3015 if(!prog->opensearches[handle])
3018 if(handle >= PRVM_MAX_OPENSEARCHES)
3020 PRVM_G_FLOAT(OFS_RETURN) = -2;
3021 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
3025 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3026 PRVM_G_FLOAT(OFS_RETURN) = -1;
3029 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
3030 PRVM_G_FLOAT(OFS_RETURN) = handle;
3038 void search_end(float handle)
3041 void VM_search_end(void)
3044 VM_SAFEPARMCOUNT(1, VM_search_end);
3046 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3048 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3050 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
3053 if(prog->opensearches[handle] == NULL)
3055 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
3059 FS_FreeSearch(prog->opensearches[handle]);
3060 prog->opensearches[handle] = NULL;
3061 if(prog->opensearches_origin[handle])
3062 PRVM_Free((char *)prog->opensearches_origin[handle]);
3069 float search_getsize(float handle)
3072 void VM_search_getsize(void)
3075 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3077 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3079 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3081 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
3084 if(prog->opensearches[handle] == NULL)
3086 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
3090 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3095 VM_search_getfilename
3097 string search_getfilename(float handle, float num)
3100 void VM_search_getfilename(void)
3102 int handle, filenum;
3103 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3105 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3106 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3108 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3110 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
3113 if(prog->opensearches[handle] == NULL)
3115 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
3118 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3120 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
3124 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
3131 string chr(float ascii)
3138 VM_SAFEPARMCOUNT(1, VM_chr);
3140 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3143 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3148 VM_SAFEPARMCOUNT(1, VM_chr);
3150 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3152 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3155 //=============================================================================
3156 // Draw builtins (client & menu)
3162 float iscachedpic(string pic)
3165 void VM_iscachedpic(void)
3167 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3169 // drawq hasnt such a function, thus always return true
3170 PRVM_G_FLOAT(OFS_RETURN) = false;
3177 string precache_pic(string pic)
3180 void VM_precache_pic(void)
3184 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3186 s = PRVM_G_STRING(OFS_PARM0);
3187 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3188 VM_CheckEmptyString (s);
3190 // AK Draw_CachePic is supposed to always return a valid pointer
3191 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
3192 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3202 void VM_freepic(void)
3206 VM_SAFEPARMCOUNT(1,VM_freepic);
3208 s = PRVM_G_STRING(OFS_PARM0);
3209 VM_CheckEmptyString (s);
3214 void getdrawfontscale(float *sx, float *sy)
3218 if(prog->globaloffsets.drawfontscale >= 0)
3220 VectorCopy(PRVM_G_VECTOR(prog->globaloffsets.drawfontscale), v);
3221 if(VectorLength2(v) > 0)
3229 dp_font_t *getdrawfont(void)
3231 if(prog->globaloffsets.drawfont >= 0)
3233 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
3234 if(f < 0 || f >= MAX_FONTS)
3235 return FONT_DEFAULT;
3236 return &dp_fonts[f];
3239 return FONT_DEFAULT;
3246 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3249 void VM_drawcharacter(void)
3251 float *pos,*scale,*rgb;
3255 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3257 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3260 PRVM_G_FLOAT(OFS_RETURN) = -1;
3261 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3265 pos = PRVM_G_VECTOR(OFS_PARM0);
3266 scale = PRVM_G_VECTOR(OFS_PARM2);
3267 rgb = PRVM_G_VECTOR(OFS_PARM3);
3268 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3270 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3272 PRVM_G_FLOAT(OFS_RETURN) = -2;
3273 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3277 if(pos[2] || scale[2])
3278 Con_Printf("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")));
3280 if(!scale[0] || !scale[1])
3282 PRVM_G_FLOAT(OFS_RETURN) = -3;
3283 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3287 getdrawfontscale(&sx, &sy);
3288 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());
3289 PRVM_G_FLOAT(OFS_RETURN) = 1;
3296 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3299 void VM_drawstring(void)
3301 float *pos,*scale,*rgb;
3305 VM_SAFEPARMCOUNT(6,VM_drawstring);
3307 string = PRVM_G_STRING(OFS_PARM1);
3308 pos = PRVM_G_VECTOR(OFS_PARM0);
3309 scale = PRVM_G_VECTOR(OFS_PARM2);
3310 rgb = PRVM_G_VECTOR(OFS_PARM3);
3311 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3313 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3315 PRVM_G_FLOAT(OFS_RETURN) = -2;
3316 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3320 if(!scale[0] || !scale[1])
3322 PRVM_G_FLOAT(OFS_RETURN) = -3;
3323 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3327 if(pos[2] || scale[2])
3328 Con_Printf("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")));
3330 getdrawfontscale(&sx, &sy);
3331 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());
3332 //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);
3333 PRVM_G_FLOAT(OFS_RETURN) = 1;
3338 VM_drawcolorcodedstring
3340 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3343 void VM_drawcolorcodedstring(void)
3349 VM_SAFEPARMCOUNT(5,VM_drawstring);
3351 string = PRVM_G_STRING(OFS_PARM1);
3352 pos = PRVM_G_VECTOR(OFS_PARM0);
3353 scale = PRVM_G_VECTOR(OFS_PARM2);
3354 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3356 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3358 PRVM_G_FLOAT(OFS_RETURN) = -2;
3359 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3363 if(!scale[0] || !scale[1])
3365 PRVM_G_FLOAT(OFS_RETURN) = -3;
3366 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3370 if(pos[2] || scale[2])
3371 Con_Printf("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")));
3374 getdrawfontscale(&sx, &sy);
3375 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false, getdrawfont());
3376 PRVM_G_FLOAT(OFS_RETURN) = 1;
3382 float stringwidth(string text, float allowColorCodes, float size)
3385 void VM_stringwidth(void)
3389 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3393 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3395 getdrawfontscale(&sx, &sy);
3398 szv = PRVM_G_VECTOR(OFS_PARM2);
3403 // we want the width for 8x8 font size, divided by 8
3404 static float defsize[] = {8, 8};
3407 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3408 if(sx >= 0.9 && sx <= 1.1)
3416 string = PRVM_G_STRING(OFS_PARM0);
3417 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3419 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(), 1000000000) * mult;
3423 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3431 string = PRVM_G_STRING(OFS_PARM0);
3432 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3434 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3442 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3445 void VM_drawpic(void)
3447 const char *picname;
3448 float *size, *pos, *rgb;
3451 VM_SAFEPARMCOUNT(6,VM_drawpic);
3453 picname = PRVM_G_STRING(OFS_PARM1);
3454 VM_CheckEmptyString (picname);
3456 // is pic cached ? no function yet for that
3459 PRVM_G_FLOAT(OFS_RETURN) = -4;
3460 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3464 pos = PRVM_G_VECTOR(OFS_PARM0);
3465 size = PRVM_G_VECTOR(OFS_PARM2);
3466 rgb = PRVM_G_VECTOR(OFS_PARM3);
3467 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3469 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3471 PRVM_G_FLOAT(OFS_RETURN) = -2;
3472 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3476 if(pos[2] || size[2])
3477 Con_Printf("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")));
3479 DrawQ_Pic(pos[0], pos[1], Draw_CachePic (picname), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
3480 PRVM_G_FLOAT(OFS_RETURN) = 1;
3486 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3489 void VM_drawrotpic(void)
3491 const char *picname;
3492 float *size, *pos, *org, *rgb;
3495 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3497 picname = PRVM_G_STRING(OFS_PARM1);
3498 VM_CheckEmptyString (picname);
3500 // is pic cached ? no function yet for that
3503 PRVM_G_FLOAT(OFS_RETURN) = -4;
3504 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3508 pos = PRVM_G_VECTOR(OFS_PARM0);
3509 size = PRVM_G_VECTOR(OFS_PARM2);
3510 org = PRVM_G_VECTOR(OFS_PARM3);
3511 rgb = PRVM_G_VECTOR(OFS_PARM5);
3512 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3514 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3516 PRVM_G_FLOAT(OFS_RETURN) = -2;
3517 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3521 if(pos[2] || size[2] || org[2])
3522 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3524 DrawQ_RotPic(pos[0], pos[1], Draw_CachePic(picname), 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);
3525 PRVM_G_FLOAT(OFS_RETURN) = 1;
3531 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3535 void VM_drawsubpic(void)
3537 const char *picname;
3538 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3541 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3543 picname = PRVM_G_STRING(OFS_PARM2);
3544 VM_CheckEmptyString (picname);
3546 // is pic cached ? no function yet for that
3549 PRVM_G_FLOAT(OFS_RETURN) = -4;
3550 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3554 pos = PRVM_G_VECTOR(OFS_PARM0);
3555 size = PRVM_G_VECTOR(OFS_PARM1);
3556 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3557 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3558 rgb = PRVM_G_VECTOR(OFS_PARM5);
3559 alpha = PRVM_G_FLOAT(OFS_PARM6);
3560 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3562 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3564 PRVM_G_FLOAT(OFS_RETURN) = -2;
3565 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3569 if(pos[2] || size[2])
3570 Con_Printf("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")));
3572 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3574 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3575 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3576 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3577 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3579 PRVM_G_FLOAT(OFS_RETURN) = 1;
3586 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3589 void VM_drawfill(void)
3591 float *size, *pos, *rgb;
3594 VM_SAFEPARMCOUNT(5,VM_drawfill);
3597 pos = PRVM_G_VECTOR(OFS_PARM0);
3598 size = PRVM_G_VECTOR(OFS_PARM1);
3599 rgb = PRVM_G_VECTOR(OFS_PARM2);
3600 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3602 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3604 PRVM_G_FLOAT(OFS_RETURN) = -2;
3605 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3609 if(pos[2] || size[2])
3610 Con_Printf("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")));
3612 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3613 PRVM_G_FLOAT(OFS_RETURN) = 1;
3620 drawsetcliparea(float x, float y, float width, float height)
3623 void VM_drawsetcliparea(void)
3626 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3628 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3629 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3630 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3631 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3633 DrawQ_SetClipArea(x, y, w, h);
3638 VM_drawresetcliparea
3643 void VM_drawresetcliparea(void)
3645 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3647 DrawQ_ResetClipArea();
3654 vector getimagesize(string pic)
3657 void VM_getimagesize(void)
3662 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3664 p = PRVM_G_STRING(OFS_PARM0);
3665 VM_CheckEmptyString (p);
3667 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3669 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3670 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3671 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3678 string keynumtostring(float keynum)
3681 void VM_keynumtostring (void)
3683 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3685 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3690 VM_findkeysforcommand
3692 string findkeysforcommand(string command)
3694 the returned string is an altstring
3697 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3699 void M_FindKeysForCommand(const char *command, int *keys);
3700 void VM_findkeysforcommand(void)
3703 char ret[VM_STRINGTEMP_LENGTH];
3707 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3709 cmd = PRVM_G_STRING(OFS_PARM0);
3711 VM_CheckEmptyString(cmd);
3713 M_FindKeysForCommand(cmd, keys);
3716 for(i = 0; i < NUMKEYS; i++)
3717 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3719 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3726 float stringtokeynum(string key)
3729 void VM_stringtokeynum (void)
3731 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3733 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3736 // CL_Video interface functions
3739 ========================
3742 float cin_open(string file, string name)
3743 ========================
3745 void VM_cin_open( void )
3750 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3752 file = PRVM_G_STRING( OFS_PARM0 );
3753 name = PRVM_G_STRING( OFS_PARM1 );
3755 VM_CheckEmptyString( file );
3756 VM_CheckEmptyString( name );
3758 if( CL_OpenVideo( file, name, MENUOWNER ) )
3759 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3761 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3765 ========================
3768 void cin_close(string name)
3769 ========================
3771 void VM_cin_close( void )
3775 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3777 name = PRVM_G_STRING( OFS_PARM0 );
3778 VM_CheckEmptyString( name );
3780 CL_CloseVideo( CL_GetVideoByName( name ) );
3784 ========================
3786 void cin_setstate(string name, float type)
3787 ========================
3789 void VM_cin_setstate( void )
3792 clvideostate_t state;
3795 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3797 name = PRVM_G_STRING( OFS_PARM0 );
3798 VM_CheckEmptyString( name );
3800 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3802 video = CL_GetVideoByName( name );
3803 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3804 CL_SetVideoState( video, state );
3808 ========================
3811 float cin_getstate(string name)
3812 ========================
3814 void VM_cin_getstate( void )
3819 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3821 name = PRVM_G_STRING( OFS_PARM0 );
3822 VM_CheckEmptyString( name );
3824 video = CL_GetVideoByName( name );
3826 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3828 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3832 ========================
3835 void cin_restart(string name)
3836 ========================
3838 void VM_cin_restart( void )
3843 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3845 name = PRVM_G_STRING( OFS_PARM0 );
3846 VM_CheckEmptyString( name );
3848 video = CL_GetVideoByName( name );
3850 CL_RestartVideo( video );
3854 ========================
3856 ========================
3858 void VM_Gecko_Init( void ) {
3859 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3860 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3864 ========================
3866 ========================
3868 void VM_Gecko_Destroy( void ) {
3870 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3871 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3873 CL_Gecko_DestroyBrowser( *instance );
3880 ========================
3883 float[bool] gecko_create( string name )
3884 ========================
3886 void VM_gecko_create( void ) {
3889 clgecko_t *instance;
3891 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3893 name = PRVM_G_STRING( OFS_PARM0 );
3894 VM_CheckEmptyString( name );
3896 // find an empty slot for this gecko browser..
3897 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3898 if( prog->opengeckoinstances[ i ] == NULL ) {
3902 if( i == PRVM_MAX_GECKOINSTANCES ) {
3903 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3904 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3908 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3910 // TODO: error handling [12/3/2007 Black]
3911 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3914 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3918 ========================
3921 void gecko_destroy( string name )
3922 ========================
3924 void VM_gecko_destroy( void ) {
3926 clgecko_t *instance;
3928 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3930 name = PRVM_G_STRING( OFS_PARM0 );
3931 VM_CheckEmptyString( name );
3932 instance = CL_Gecko_FindBrowser( name );
3936 CL_Gecko_DestroyBrowser( instance );
3940 ========================
3943 void gecko_navigate( string name, string URI )
3944 ========================
3946 void VM_gecko_navigate( void ) {
3949 clgecko_t *instance;
3951 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3953 name = PRVM_G_STRING( OFS_PARM0 );
3954 URI = PRVM_G_STRING( OFS_PARM1 );
3955 VM_CheckEmptyString( name );
3956 VM_CheckEmptyString( URI );
3958 instance = CL_Gecko_FindBrowser( name );
3962 CL_Gecko_NavigateToURI( instance, URI );
3966 ========================
3969 float[bool] gecko_keyevent( string name, float key, float eventtype )
3970 ========================
3972 void VM_gecko_keyevent( void ) {
3975 clgecko_buttoneventtype_t eventtype;
3976 clgecko_t *instance;
3978 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3980 name = PRVM_G_STRING( OFS_PARM0 );
3981 VM_CheckEmptyString( name );
3982 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3983 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3985 eventtype = CLG_BET_DOWN;
3988 eventtype = CLG_BET_UP;
3991 eventtype = CLG_BET_PRESS;
3994 eventtype = CLG_BET_DOUBLECLICK;
3997 // TODO: console printf? [12/3/2007 Black]
3998 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4002 instance = CL_Gecko_FindBrowser( name );
4004 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4008 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
4012 ========================
4015 void gecko_mousemove( string name, float x, float y )
4016 ========================
4018 void VM_gecko_movemouse( void ) {
4021 clgecko_t *instance;
4023 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4025 name = PRVM_G_STRING( OFS_PARM0 );
4026 VM_CheckEmptyString( name );
4027 x = PRVM_G_FLOAT( OFS_PARM1 );
4028 y = PRVM_G_FLOAT( OFS_PARM2 );
4030 instance = CL_Gecko_FindBrowser( name );
4034 CL_Gecko_Event_CursorMove( instance, x, y );
4039 ========================
4042 void gecko_resize( string name, float w, float h )
4043 ========================
4045 void VM_gecko_resize( void ) {
4048 clgecko_t *instance;
4050 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4052 name = PRVM_G_STRING( OFS_PARM0 );
4053 VM_CheckEmptyString( name );
4054 w = PRVM_G_FLOAT( OFS_PARM1 );
4055 h = PRVM_G_FLOAT( OFS_PARM2 );
4057 instance = CL_Gecko_FindBrowser( name );
4061 CL_Gecko_Resize( instance, (int) w, (int) h );
4066 ========================
4067 VM_gecko_get_texture_extent
4069 vector gecko_get_texture_extent( string name )
4070 ========================
4072 void VM_gecko_get_texture_extent( void ) {
4074 clgecko_t *instance;
4076 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
4078 name = PRVM_G_STRING( OFS_PARM0 );
4079 VM_CheckEmptyString( name );
4081 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4082 instance = CL_Gecko_FindBrowser( name );
4084 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4085 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4088 CL_Gecko_GetTextureExtent( instance,
4089 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
4098 Writes new values for v_forward, v_up, and v_right based on angles
4099 void makevectors(vector angle)
4102 void VM_makevectors (void)
4104 prvm_eval_t *valforward, *valright, *valup;
4105 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4106 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4107 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4108 if (!valforward || !valright || !valup)
4110 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
4113 VM_SAFEPARMCOUNT(1, VM_makevectors);
4114 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
4121 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4122 vectorvectors(vector)
4125 void VM_vectorvectors (void)
4127 prvm_eval_t *valforward, *valright, *valup;
4128 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4129 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4130 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4131 if (!valforward || !valright || !valup)
4133 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
4136 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4137 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
4138 VectorVectors(valforward->vector, valright->vector, valup->vector);
4142 ========================
4145 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4146 ========================
4148 void VM_drawline (void)
4150 float *c1, *c2, *rgb;
4152 unsigned char flags;
4154 VM_SAFEPARMCOUNT(6, VM_drawline);
4155 width = PRVM_G_FLOAT(OFS_PARM0);
4156 c1 = PRVM_G_VECTOR(OFS_PARM1);
4157 c2 = PRVM_G_VECTOR(OFS_PARM2);
4158 rgb = PRVM_G_VECTOR(OFS_PARM3);
4159 alpha = PRVM_G_FLOAT(OFS_PARM4);
4160 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4161 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4164 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4165 void VM_bitshift (void)
4168 VM_SAFEPARMCOUNT(2, VM_bitshift);
4170 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4171 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4173 PRVM_G_FLOAT(OFS_RETURN) = n1;
4176 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4178 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4181 ////////////////////////////////////////
4182 // AltString functions
4183 ////////////////////////////////////////
4186 ========================
4189 float altstr_count(string)
4190 ========================
4192 void VM_altstr_count( void )
4194 const char *altstr, *pos;
4197 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4199 altstr = PRVM_G_STRING( OFS_PARM0 );
4200 //VM_CheckEmptyString( altstr );
4202 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4203 if( *pos == '\\' ) {
4207 } else if( *pos == '\'' ) {
4212 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4216 ========================
4219 string altstr_prepare(string)
4220 ========================
4222 void VM_altstr_prepare( void )
4225 const char *instr, *in;
4227 char outstr[VM_STRINGTEMP_LENGTH];
4229 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4231 instr = PRVM_G_STRING( OFS_PARM0 );
4233 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4242 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4246 ========================
4249 string altstr_get(string, float)
4250 ========================
4252 void VM_altstr_get( void )
4254 const char *altstr, *pos;
4257 char outstr[VM_STRINGTEMP_LENGTH];
4259 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4261 altstr = PRVM_G_STRING( OFS_PARM0 );
4263 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4264 count = count * 2 + 1;
4266 for( pos = altstr ; *pos && count ; pos++ )
4267 if( *pos == '\\' ) {
4270 } else if( *pos == '\'' )
4274 PRVM_G_INT( OFS_RETURN ) = 0;
4278 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4279 if( *pos == '\\' ) {
4284 } else if( *pos == '\'' )
4290 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4294 ========================
4297 string altstr_set(string altstr, float num, string set)
4298 ========================
4300 void VM_altstr_set( void )
4303 const char *altstr, *str;
4306 char outstr[VM_STRINGTEMP_LENGTH];
4308 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4310 altstr = PRVM_G_STRING( OFS_PARM0 );
4312 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4314 str = PRVM_G_STRING( OFS_PARM2 );
4317 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4322 } else if( *in == '\'' ) {
4327 for( ; *str; *out++ = *str++ );
4328 // now jump over the old content
4330 if( *in == '\'' || (*in == '\\' && !*++in) )
4333 strlcpy(out, in, outstr + sizeof(outstr) - out);
4334 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4338 ========================
4341 string altstr_ins(string altstr, float num, string set)
4342 ========================
4344 void VM_altstr_ins(void)
4352 char outstr[VM_STRINGTEMP_LENGTH];
4354 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4356 in = instr = PRVM_G_STRING( OFS_PARM0 );
4357 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4358 set = setstr = PRVM_G_STRING( OFS_PARM2 );
4361 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4366 } else if( *in == '\'' ) {
4371 for( ; *set ; *out++ = *set++ );
4374 strlcpy(out, in, outstr + sizeof(outstr) - out);
4375 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4379 ////////////////////////////////////////
4380 // BufString functions
4381 ////////////////////////////////////////
4382 //[515]: string buffers support
4384 static size_t stringbuffers_sortlength;
4386 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4388 if (stringbuffer->max_strings <= strindex)
4390 char **oldstrings = stringbuffer->strings;
4391 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4392 while (stringbuffer->max_strings <= strindex)
4393 stringbuffer->max_strings *= 2;
4394 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4395 if (stringbuffer->num_strings > 0)
4396 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4398 Mem_Free(oldstrings);
4402 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4404 // reduce num_strings if there are empty string slots at the end
4405 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4406 stringbuffer->num_strings--;
4408 // if empty, free the string pointer array
4409 if (stringbuffer->num_strings == 0)
4411 stringbuffer->max_strings = 0;
4412 if (stringbuffer->strings)
4413 Mem_Free(stringbuffer->strings);
4414 stringbuffer->strings = NULL;
4418 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4421 a = *((const char **) in1);
4422 b = *((const char **) in2);
4424 if(!b[0]) return -1;
4425 return strncmp(a, b, stringbuffers_sortlength);
4428 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4431 a = *((const char **) in1);
4432 b = *((const char **) in2);
4434 if(!b[0]) return -1;
4435 return strncmp(b, a, stringbuffers_sortlength);
4439 ========================
4441 creates new buffer, and returns it's index, returns -1 if failed
4442 float buf_create(void) = #460;
4443 ========================
4445 void VM_buf_create (void)
4447 prvm_stringbuffer_t *stringbuffer;
4449 VM_SAFEPARMCOUNT(0, VM_buf_create);
4450 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4451 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4452 stringbuffer->origin = PRVM_AllocationOrigin();
4453 PRVM_G_FLOAT(OFS_RETURN) = i;
4457 ========================
4459 deletes buffer and all strings in it
4460 void buf_del(float bufhandle) = #461;
4461 ========================
4463 void VM_buf_del (void)
4465 prvm_stringbuffer_t *stringbuffer;
4466 VM_SAFEPARMCOUNT(1, VM_buf_del);
4467 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4471 for (i = 0;i < stringbuffer->num_strings;i++)
4472 if (stringbuffer->strings[i])
4473 Mem_Free(stringbuffer->strings[i]);
4474 if (stringbuffer->strings)
4475 Mem_Free(stringbuffer->strings);
4476 if(stringbuffer->origin)
4477 PRVM_Free((char *)stringbuffer->origin);
4478 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4482 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4488 ========================
4490 how many strings are stored in buffer
4491 float buf_getsize(float bufhandle) = #462;
4492 ========================
4494 void VM_buf_getsize (void)
4496 prvm_stringbuffer_t *stringbuffer;
4497 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4499 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4502 PRVM_G_FLOAT(OFS_RETURN) = -1;
4503 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4507 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4511 ========================
4513 copy all content from one buffer to another, make sure it exists
4514 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4515 ========================
4517 void VM_buf_copy (void)
4519 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4521 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4523 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4524 if(!srcstringbuffer)
4526 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4529 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4530 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4532 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4535 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4536 if(!dststringbuffer)
4538 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4542 for (i = 0;i < dststringbuffer->num_strings;i++)
4543 if (dststringbuffer->strings[i])
4544 Mem_Free(dststringbuffer->strings[i]);
4545 if (dststringbuffer->strings)
4546 Mem_Free(dststringbuffer->strings);
4547 *dststringbuffer = *srcstringbuffer;
4548 if (dststringbuffer->max_strings)
4549 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4551 for (i = 0;i < dststringbuffer->num_strings;i++)
4553 if (srcstringbuffer->strings[i])
4556 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4557 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4558 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4564 ========================
4566 sort buffer by beginnings of strings (cmplength defaults it's length)
4567 "backward == TRUE" means that sorting goes upside-down
4568 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4569 ========================
4571 void VM_buf_sort (void)
4573 prvm_stringbuffer_t *stringbuffer;
4574 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4576 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4579 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4582 if(stringbuffer->num_strings <= 0)
4584 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4587 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4588 if(stringbuffers_sortlength <= 0)
4589 stringbuffers_sortlength = 0x7FFFFFFF;
4591 if(!PRVM_G_FLOAT(OFS_PARM2))
4592 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4594 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4596 BufStr_Shrink(stringbuffer);
4600 ========================
4602 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4603 string buf_implode(float bufhandle, string glue) = #465;
4604 ========================
4606 void VM_buf_implode (void)
4608 prvm_stringbuffer_t *stringbuffer;
4609 char k[VM_STRINGTEMP_LENGTH];
4613 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4615 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4616 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4619 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4622 if(!stringbuffer->num_strings)
4624 sep = PRVM_G_STRING(OFS_PARM1);
4626 for(l = i = 0;i < stringbuffer->num_strings;i++)
4628 if(stringbuffer->strings[i])
4630 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4631 if (l >= sizeof(k) - 1)
4633 strlcat(k, sep, sizeof(k));
4634 strlcat(k, stringbuffer->strings[i], sizeof(k));
4637 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4641 ========================
4643 get a string from buffer, returns tempstring, dont str_unzone it!
4644 string bufstr_get(float bufhandle, float string_index) = #465;
4645 ========================
4647 void VM_bufstr_get (void)
4649 prvm_stringbuffer_t *stringbuffer;
4651 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4653 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4654 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4657 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4660 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4663 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4666 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4667 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4671 ========================
4673 copies a string into selected slot of buffer
4674 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4675 ========================
4677 void VM_bufstr_set (void)
4680 prvm_stringbuffer_t *stringbuffer;
4683 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4685 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4688 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4691 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4692 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4694 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4698 BufStr_Expand(stringbuffer, strindex);
4699 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4701 if(stringbuffer->strings[strindex])
4702 Mem_Free(stringbuffer->strings[strindex]);
4703 stringbuffer->strings[strindex] = NULL;
4705 news = PRVM_G_STRING(OFS_PARM2);
4706 if (news && news[0])
4708 size_t alloclen = strlen(news) + 1;
4709 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4710 memcpy(stringbuffer->strings[strindex], news, alloclen);
4713 BufStr_Shrink(stringbuffer);
4717 ========================
4719 adds string to buffer in first free slot and returns its index
4720 "order == TRUE" means that string will be added after last "full" slot
4721 float bufstr_add(float bufhandle, string str, float order) = #467;
4722 ========================
4724 void VM_bufstr_add (void)
4726 int order, strindex;
4727 prvm_stringbuffer_t *stringbuffer;
4731 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4733 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4734 PRVM_G_FLOAT(OFS_RETURN) = -1;
4737 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4740 string = PRVM_G_STRING(OFS_PARM1);
4741 if(!string || !string[0])
4743 VM_Warning("VM_bufstr_add: can not add an empty string to buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4746 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4748 strindex = stringbuffer->num_strings;
4750 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4751 if (stringbuffer->strings[strindex] == NULL)
4754 BufStr_Expand(stringbuffer, strindex);
4756 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4757 alloclen = strlen(string) + 1;
4758 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4759 memcpy(stringbuffer->strings[strindex], string, alloclen);
4761 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4765 ========================
4767 delete string from buffer
4768 void bufstr_free(float bufhandle, float string_index) = #468;
4769 ========================
4771 void VM_bufstr_free (void)
4774 prvm_stringbuffer_t *stringbuffer;
4775 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4777 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4780 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4783 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4786 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4790 if (i < stringbuffer->num_strings)
4792 if(stringbuffer->strings[i])
4793 Mem_Free(stringbuffer->strings[i]);
4794 stringbuffer->strings[i] = NULL;
4797 BufStr_Shrink(stringbuffer);
4806 void VM_buf_cvarlist(void)
4809 const char *partial, *antipartial;
4810 size_t len, antilen;
4812 qboolean ispattern, antiispattern;
4814 prvm_stringbuffer_t *stringbuffer;
4815 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4817 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4820 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4824 partial = PRVM_G_STRING(OFS_PARM1);
4828 len = strlen(partial);
4831 antipartial = PRVM_G_STRING(OFS_PARM2);
4837 antilen = strlen(antipartial);
4839 for (n = 0;n < stringbuffer->num_strings;n++)
4840 if (stringbuffer->strings[n])
4841 Mem_Free(stringbuffer->strings[n]);
4842 if (stringbuffer->strings)
4843 Mem_Free(stringbuffer->strings);
4844 stringbuffer->strings = NULL;
4846 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4847 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4850 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4852 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4855 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4861 stringbuffer->max_strings = stringbuffer->num_strings = n;
4862 if (stringbuffer->max_strings)
4863 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4866 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4868 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4871 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4874 alloclen = strlen(cvar->name) + 1;
4875 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4876 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4891 This was a major timewaster in progs, so it was converted to C
4894 void VM_changeyaw (void)
4897 float ideal, current, move, speed;
4899 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4900 // parameters because they are the parameters to SV_MoveToGoal, not this
4901 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4903 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4904 if (ent == prog->edicts)
4906 VM_Warning("changeyaw: can not modify world entity\n");
4909 if (ent->priv.server->free)
4911 VM_Warning("changeyaw: can not modify free entity\n");
4914 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4916 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4919 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4920 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4921 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4923 if (current == ideal)
4925 move = ideal - current;
4926 if (ideal > current)
4947 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4955 void VM_changepitch (void)
4958 float ideal, current, move, speed;
4960 VM_SAFEPARMCOUNT(1, VM_changepitch);
4962 ent = PRVM_G_EDICT(OFS_PARM0);
4963 if (ent == prog->edicts)
4965 VM_Warning("changepitch: can not modify world entity\n");
4968 if (ent->priv.server->free)
4970 VM_Warning("changepitch: can not modify free entity\n");
4973 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4975 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4978 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4979 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4980 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4982 if (current == ideal)
4984 move = ideal - current;
4985 if (ideal > current)
5006 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
5010 void VM_uncolorstring (void)
5012 char szNewString[VM_STRINGTEMP_LENGTH];
5013 const char *szString;
5016 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5017 szString = PRVM_G_STRING(OFS_PARM0);
5018 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5019 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
5023 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5024 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5025 void VM_strstrofs (void)
5027 const char *instr, *match;
5029 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5030 instr = PRVM_G_STRING(OFS_PARM0);
5031 match = PRVM_G_STRING(OFS_PARM1);
5032 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5033 firstofs = u8_bytelen(instr, firstofs);
5035 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5037 PRVM_G_FLOAT(OFS_RETURN) = -1;
5041 match = strstr(instr+firstofs, match);
5043 PRVM_G_FLOAT(OFS_RETURN) = -1;
5045 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
5048 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5049 void VM_str2chr (void)
5054 VM_SAFEPARMCOUNT(2, VM_str2chr);
5055 s = PRVM_G_STRING(OFS_PARM0);
5056 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5058 if((unsigned)index < strlen(s))
5060 if (utf8_enable.integer)
5061 ch = u8_getchar_noendptr(s + index);
5063 ch = (unsigned char)s[index];
5064 PRVM_G_FLOAT(OFS_RETURN) = ch;
5067 PRVM_G_FLOAT(OFS_RETURN) = 0;
5070 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5071 void VM_chr2str (void)
5076 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5077 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5078 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5080 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5085 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5086 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5087 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5089 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5092 static int chrconv_number(int i, int base, int conv)
5117 static int chrconv_punct(int i, int base, int conv)
5135 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5137 //convert case and colour seperatly...
5154 baset = 128*((charnum&1) == (convt-5));
5170 return i + basec + baset;
5172 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5173 //bulk convert a string. change case or colouring.
5174 void VM_strconv (void)
5176 int ccase, redalpha, rednum, len, i;
5177 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5178 unsigned char *result = resbuf;
5180 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5182 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5183 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5184 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5185 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
5186 len = strlen((char *) resbuf);
5188 for (i = 0; i < len; i++, result++) //should this be done backwards?
5190 if (*result >= '0' && *result <= '9') //normal numbers...
5191 *result = chrconv_number(*result, '0', rednum);
5192 else if (*result >= '0'+128 && *result <= '9'+128)
5193 *result = chrconv_number(*result, '0'+128, rednum);
5194 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5195 *result = chrconv_number(*result, '0'+128-30, rednum);
5196 else if (*result >= '0'-30 && *result <= '9'-30)
5197 *result = chrconv_number(*result, '0'-30, rednum);
5199 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5200 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5201 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5202 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5203 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5204 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5205 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5206 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5208 else if ((*result & 127) < 16 || !redalpha) //special chars..
5210 else if (*result < 128)
5211 *result = chrconv_punct(*result, 0, redalpha);
5213 *result = chrconv_punct(*result, 128, redalpha);
5217 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5220 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5221 void VM_strpad (void)
5223 char src[VM_STRINGTEMP_LENGTH];
5224 char destbuf[VM_STRINGTEMP_LENGTH];
5226 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5227 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5228 VM_VarString(1, src, sizeof(src));
5230 // note: < 0 = left padding, > 0 = right padding,
5231 // this is reverse logic of printf!
5232 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5234 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5237 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5238 //uses qw style \key\value strings
5239 void VM_infoadd (void)
5241 const char *info, *key;
5242 char value[VM_STRINGTEMP_LENGTH];
5243 char temp[VM_STRINGTEMP_LENGTH];
5245 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5246 info = PRVM_G_STRING(OFS_PARM0);
5247 key = PRVM_G_STRING(OFS_PARM1);
5248 VM_VarString(2, value, sizeof(value));
5250 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5252 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5254 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5257 // #227 string(string info, string key) infoget (FTE_STRINGS)
5258 //uses qw style \key\value strings
5259 void VM_infoget (void)
5263 char value[VM_STRINGTEMP_LENGTH];
5265 VM_SAFEPARMCOUNT(2, VM_infoget);
5266 info = PRVM_G_STRING(OFS_PARM0);
5267 key = PRVM_G_STRING(OFS_PARM1);
5269 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5271 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5274 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5275 // also float(string s1, string s2) strcmp (FRIK_FILE)
5276 void VM_strncmp (void)
5278 const char *s1, *s2;
5279 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5280 s1 = PRVM_G_STRING(OFS_PARM0);
5281 s2 = PRVM_G_STRING(OFS_PARM1);
5284 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5288 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5292 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5293 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5294 void VM_strncasecmp (void)
5296 const char *s1, *s2;
5297 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5298 s1 = PRVM_G_STRING(OFS_PARM0);
5299 s2 = PRVM_G_STRING(OFS_PARM1);
5302 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5306 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5310 // #494 float(float caseinsensitive, string s, ...) crc16
5314 static char s[VM_STRINGTEMP_LENGTH];
5315 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5316 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5317 VM_VarString(1, s, sizeof(s));
5318 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5321 void VM_wasfreed (void)
5323 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5324 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5327 void VM_SetTraceGlobals(const trace_t *trace)
5330 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5331 val->_float = trace->allsolid;
5332 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5333 val->_float = trace->startsolid;
5334 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5335 val->_float = trace->fraction;
5336 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5337 val->_float = trace->inwater;
5338 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5339 val->_float = trace->inopen;
5340 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5341 VectorCopy(trace->endpos, val->vector);
5342 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5343 VectorCopy(trace->plane.normal, val->vector);
5344 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5345 val->_float = trace->plane.dist;
5346 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5347 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5348 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5349 val->_float = trace->startsupercontents;
5350 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5351 val->_float = trace->hitsupercontents;
5352 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5353 val->_float = trace->hitq3surfaceflags;
5354 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5355 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5358 void VM_ClearTraceGlobals(void)
5360 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5362 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5364 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5366 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5368 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5370 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5372 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5373 VectorClear(val->vector);
5374 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5375 VectorClear(val->vector);
5376 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5378 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5379 val->edict = PRVM_EDICT_TO_PROG(prog->edicts);
5380 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5382 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5384 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5386 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5392 void VM_Cmd_Init(void)
5394 // only init the stuff for the current prog
5398 // VM_BufStr_Init();
5401 void VM_Cmd_Reset(void)
5403 CL_PurgeOwner( MENUOWNER );
5405 VM_Files_CloseAll();
5407 // VM_BufStr_ShutDown();
5410 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5411 // does URI escaping on a string (replace evil stuff by %AB escapes)
5412 void VM_uri_escape (void)
5414 char src[VM_STRINGTEMP_LENGTH];
5415 char dest[VM_STRINGTEMP_LENGTH];
5417 static const char *hex = "0123456789ABCDEF";
5419 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5420 VM_VarString(0, src, sizeof(src));
5422 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5424 if((*p >= 'A' && *p <= 'Z')
5425 || (*p >= 'a' && *p <= 'z')
5426 || (*p >= '0' && *p <= '9')
5427 || (*p == '-') || (*p == '_') || (*p == '.')
5428 || (*p == '!') || (*p == '~') || (*p == '*')
5429 || (*p == '\'') || (*p == '(') || (*p == ')'))
5434 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5435 *q++ = hex[ *(unsigned char *)p & 0xF];
5440 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5443 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5444 // does URI unescaping on a string (get back the evil stuff)
5445 void VM_uri_unescape (void)
5447 char src[VM_STRINGTEMP_LENGTH];
5448 char dest[VM_STRINGTEMP_LENGTH];
5452 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5453 VM_VarString(0, src, sizeof(src));
5455 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5459 if(p[1] >= '0' && p[1] <= '9')
5461 else if(p[1] >= 'a' && p[1] <= 'f')
5462 hi = p[1] - 'a' + 10;
5463 else if(p[1] >= 'A' && p[1] <= 'F')
5464 hi = p[1] - 'A' + 10;
5467 if(p[2] >= '0' && p[2] <= '9')
5469 else if(p[2] >= 'a' && p[2] <= 'f')
5470 lo = p[2] - 'a' + 10;
5471 else if(p[2] >= 'A' && p[2] <= 'F')
5472 lo = p[2] - 'A' + 10;
5475 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5476 *q++ = (char) (hi * 0x10 + lo);
5487 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5490 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5491 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5492 void VM_whichpack (void)
5494 const char *fn, *pack;
5496 VM_SAFEPARMCOUNT(1, VM_whichpack);
5497 fn = PRVM_G_STRING(OFS_PARM0);
5498 pack = FS_WhichPack(fn);
5500 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5508 char buffer[MAX_INPUTLINE];
5512 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5514 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5516 if(!PRVM_ProgLoaded(handle->prognr))
5518 // curl reply came too late... so just drop it
5523 PRVM_SetProg(handle->prognr);
5525 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5527 if(length_received >= sizeof(handle->buffer))
5528 length_received = sizeof(handle->buffer) - 1;
5529 handle->buffer[length_received] = 0;
5531 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5532 PRVM_G_FLOAT(OFS_PARM1) = status;
5533 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5534 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5541 // 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
5542 // 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
5543 void VM_uri_get (void)
5548 uri_to_prog_t *handle;
5550 if(!prog->funcoffsets.URI_Get_Callback)
5551 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5553 VM_SAFEPARMCOUNT(2, VM_uri_get);
5555 url = PRVM_G_STRING(OFS_PARM0);
5556 id = PRVM_G_FLOAT(OFS_PARM1);
5557 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!
5559 handle->prognr = PRVM_GetProgNr();
5560 handle->starttime = prog->starttime;
5562 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5565 PRVM_G_INT(OFS_RETURN) = 1;
5570 PRVM_G_INT(OFS_RETURN) = 0;
5574 void VM_netaddress_resolve (void)
5577 char normalized[128];
5579 lhnetaddress_t addr;
5581 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5583 ip = PRVM_G_STRING(OFS_PARM0);
5586 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5588 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5589 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5591 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5594 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5595 void VM_CL_getextresponse (void)
5597 VM_SAFEPARMCOUNT(0,VM_argv);
5599 if (cl_net_extresponse_count <= 0)
5600 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5604 --cl_net_extresponse_count;
5605 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5606 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(cl_net_extresponse[first]);
5610 void VM_SV_getextresponse (void)
5612 VM_SAFEPARMCOUNT(0,VM_argv);
5614 if (sv_net_extresponse_count <= 0)
5615 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5619 --sv_net_extresponse_count;
5620 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5621 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(sv_net_extresponse[first]);
5629 callfunction(...,string function_name)
5633 mfunction_t *PRVM_ED_FindFunction (const char *name);
5634 void VM_callfunction(void)
5639 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
5641 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
5643 VM_CheckEmptyString(s);
5645 func = PRVM_ED_FindFunction(s);
5648 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
5649 else if (func->first_statement < 0)
5651 // negative statements are built in functions
5652 int builtinnumber = -func->first_statement;
5653 prog->xfunction->builtinsprofile++;
5654 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
5655 prog->builtins[builtinnumber]();
5657 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
5659 else if(func - prog->functions > 0)
5662 PRVM_ExecuteProgram(func - prog->functions,"");
5671 float isfunction(string function_name)
5674 mfunction_t *PRVM_ED_FindFunction (const char *name);
5675 void VM_isfunction(void)
5680 VM_SAFEPARMCOUNT(1, VM_isfunction);
5682 s = PRVM_G_STRING(OFS_PARM0);
5684 VM_CheckEmptyString(s);
5686 func = PRVM_ED_FindFunction(s);
5689 PRVM_G_FLOAT(OFS_RETURN) = false;
5691 PRVM_G_FLOAT(OFS_RETURN) = true;
5698 string sprintf(string format, ...)
5702 void VM_sprintf(void)
5705 char outbuf[MAX_INPUTLINE];
5706 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
5708 int width, precision, thisarg, flags;
5712 #define PRINTF_ALTERNATE 1
5713 #define PRINTF_ZEROPAD 2
5714 #define PRINTF_LEFT 4
5715 #define PRINTF_SPACEPOSITIVE 8
5716 #define PRINTF_SIGNPOSITIVE 16
5720 s = PRVM_G_STRING(OFS_PARM0);
5722 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
5723 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
5724 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
5740 // complete directive format:
5748 // is number following?
5749 if(*s >= '0' && *s <= '9')
5751 width = strtol(s, &err, 10);
5754 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
5767 flags |= PRINTF_ZEROPAD;
5769 width = -1; // it was just a flag
5781 case '#': flags |= PRINTF_ALTERNATE; break;
5782 case '0': flags |= PRINTF_ZEROPAD; break;
5783 case '-': flags |= PRINTF_LEFT; break;
5784 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
5785 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
5795 if(*s >= '0' && *s <= '9')
5797 width = strtol(s, &err, 10);
5798 if(!err || *err != '$')
5800 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
5807 width = GETARG_FLOAT(width);
5809 else if(*s >= '0' && *s <= '9')
5811 width = strtol(s, &err, 10);
5814 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
5821 flags |= PRINTF_LEFT;
5832 if(*s >= '0' && *s <= '9')
5834 precision = strtol(s, &err, 10);
5835 if(!err || *err != '$')
5837 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
5843 precision = argpos++;
5844 precision = GETARG_FLOAT(precision);
5846 else if(*s >= '0' && *s <= '9')
5848 precision = strtol(s, &err, 10);
5851 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
5858 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
5868 case 'h': isfloat = true; break;
5869 case 'l': isfloat = false; break;
5870 case 'L': isfloat = false; break;
5887 if(*s != 's' && *s != 'c')
5888 if(flags & PRINTF_ALTERNATE) *f++ = '#';
5889 if(flags & PRINTF_ZEROPAD) *f++ = '0';
5890 if(flags & PRINTF_LEFT) *f++ = '-';
5891 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
5892 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
5905 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
5907 case 'o': case 'u': case 'x': case 'X':
5908 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
5910 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
5913 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
5917 precision = end - o - 1;
5918 if(flags & PRINTF_ALTERNATE)
5919 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
5922 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
5923 const char *buf = u8_encodech(c, NULL);
5926 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
5931 precision = end - o - 1;
5932 if(flags & PRINTF_ALTERNATE)
5933 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
5935 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
5938 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
5953 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outbuf);