2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 extern cvar_t prvm_backtraceforwarnings;
15 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
16 void VM_Warning(const char *fmt, ...)
19 char msg[MAX_INPUTLINE];
20 static double recursive = -1;
23 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
28 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
29 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
38 //============================================================================
41 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
42 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
43 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
44 // TODO: will this war ever end? [2007-01-23 LordHavoc]
46 void VM_CheckEmptyString (const char *s)
48 if (ISWHITESPACE(s[0]))
49 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
52 void VM_GenerateFrameGroupBlend(framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
55 // self.frame is the interpolation target (new frame)
56 // self.frame1time is the animation base time for the interpolation target
57 // self.frame2 is the interpolation start (previous frame)
58 // self.frame2time is the animation base time for the interpolation start
59 // self.lerpfrac is the interpolation strength for self.frame2
60 // self.lerpfrac3 is the interpolation strength for self.frame3
61 // self.lerpfrac4 is the interpolation strength for self.frame4
62 // pitch angle on a player model where the animator set up 5 sets of
63 // animations and the csqc simply lerps between sets)
64 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame))) framegroupblend[0].frame = (int) val->_float;
65 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) framegroupblend[1].frame = (int) val->_float;
66 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame3))) framegroupblend[2].frame = (int) val->_float;
67 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame4))) framegroupblend[3].frame = (int) val->_float;
68 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) framegroupblend[0].start = val->_float;
69 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) framegroupblend[1].start = val->_float;
70 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame3time))) framegroupblend[2].start = val->_float;
71 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame4time))) framegroupblend[3].start = val->_float;
72 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) framegroupblend[1].lerp = val->_float;
73 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac3))) framegroupblend[2].lerp = val->_float;
74 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac4))) framegroupblend[3].lerp = val->_float;
75 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
76 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
79 // LordHavoc: quite tempting to break apart this function to reuse the
80 // duplicated code, but I suspect it is better for performance
82 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model)
84 int sub2, numframes, f, i, k;
85 int isfirstframegroup = true;
87 double sublerp, lerp, d;
88 const animscene_t *scene;
89 const framegroupblend_t *g;
90 frameblend_t *blend = frameblend;
92 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
94 if (!model || !model->surfmesh.isanimated)
100 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
101 numframes = model->numframes;
102 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
105 if ((unsigned int)f >= (unsigned int)numframes)
107 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
115 if (isfirstframegroup)
118 isfirstframegroup = false;
123 if (model->animscenes)
125 scene = model->animscenes + f;
126 f = scene->firstframe;
127 if (scene->framecount > 1)
129 // this code path is only used on .zym models and torches
130 sublerp = scene->framerate * (cl.time - g->start);
131 f = (int) floor(sublerp);
134 if (sublerp < (1.0 / 65536.0f))
136 if (sublerp > (65535.0f / 65536.0f))
142 f = (f % scene->framecount);
143 sub2 = (sub2 % scene->framecount);
145 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
146 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
148 // two framelerps produced from one animation
151 for (i = 0;i < MAX_FRAMEBLENDS;i++)
153 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
155 blend[i].subframe = sub2;
161 d = (1 - sublerp) * lerp;
166 for (i = 0;i < MAX_FRAMEBLENDS;i++)
168 if (blend[i].lerp <= 0 || blend[i].subframe == f)
170 blend[i].subframe = f;
179 void VM_UpdateEdictSkeleton(prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
181 if (ed->priv.server->skeleton.model != edmodel)
183 VM_RemoveEdictSkeleton(ed);
184 ed->priv.server->skeleton.model = edmodel;
186 if (!ed->priv.server->skeleton.relativetransforms && ed->priv.server->skeleton.model && ed->priv.server->skeleton.model->num_bones)
187 ed->priv.server->skeleton.relativetransforms = Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
188 if (ed->priv.server->skeleton.relativetransforms)
190 int skeletonindex = 0;
191 skeleton_t *skeleton;
193 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.skeletonindex))) skeletonindex = (int)val->_float;
194 if (skeletonindex > 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
196 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
197 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
201 // generated skeleton from frame animation
204 int numbones = ed->priv.server->skeleton.model->num_bones;
205 const float *poses = ed->priv.server->skeleton.model->data_poses;
206 const float *framebones;
208 matrix4x4_t *relativetransforms = ed->priv.server->skeleton.relativetransforms;
210 memset(relativetransforms, 0, numbones * sizeof(matrix4x4_t));
211 for (blendindex = 0;blendindex < MAX_FRAMEBLENDS && frameblend[blendindex].lerp > 0;blendindex++)
213 lerp = frameblend[blendindex].lerp;
214 framebones = poses + 12 * frameblend[blendindex].subframe * numbones;
215 for (bonenum = 0;bonenum < numbones;bonenum++)
217 Matrix4x4_FromArray12FloatD3D(&matrix, framebones + 12 * bonenum);
218 Matrix4x4_Accumulate(&ed->priv.server->skeleton.relativetransforms[bonenum], &matrix, lerp);
225 void VM_RemoveEdictSkeleton(prvm_edict_t *ed)
227 if (ed->priv.server->skeleton.relativetransforms)
228 Mem_Free(ed->priv.server->skeleton.relativetransforms);
229 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
235 //============================================================================
238 void VM_VarString(int first, char *out, int outlength)
244 outend = out + outlength - 1;
245 for (i = first;i < prog->argc && out < outend;i++)
247 s = PRVM_G_STRING((OFS_PARM0+i*3));
248 while (out < outend && *s)
258 returns true if the extension is supported by the server
260 checkextension(extensionname)
264 // kind of helper function
265 static qboolean checkextension(const char *name)
269 len = (int)strlen(name);
271 for (e = prog->extensionstring;*e;e++)
278 while (*e && *e != ' ')
280 if ((e - start) == len && !strncasecmp(start, name, len))
286 void VM_checkextension (void)
288 VM_SAFEPARMCOUNT(1,VM_checkextension);
290 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
297 This is a TERMINAL error, which will kill off the entire prog.
306 char string[VM_STRINGTEMP_LENGTH];
308 VM_VarString(0, string, sizeof(string));
309 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
310 if (prog->globaloffsets.self >= 0)
312 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
313 PRVM_ED_Print(ed, NULL);
316 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);
323 Dumps out self, then an error message. The program is aborted and self is
324 removed, but the level can continue.
329 void VM_objerror (void)
332 char string[VM_STRINGTEMP_LENGTH];
334 VM_VarString(0, string, sizeof(string));
335 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
336 if (prog->globaloffsets.self >= 0)
338 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
339 PRVM_ED_Print(ed, NULL);
344 // objerror has to display the object fields -> else call
345 PRVM_ERROR ("VM_objecterror: self not defined !");
346 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);
360 char string[VM_STRINGTEMP_LENGTH];
362 VM_VarString(0, string, sizeof(string));
370 broadcast print to everyone on server
375 void VM_bprint (void)
377 char string[VM_STRINGTEMP_LENGTH];
381 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
385 VM_VarString(0, string, sizeof(string));
386 SV_BroadcastPrint(string);
391 VM_sprint (menu & client but only if server.active == true)
393 single print to a specific client
395 sprint(float clientnum,...[string])
398 void VM_sprint (void)
402 char string[VM_STRINGTEMP_LENGTH];
404 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
406 //find client for this entity
407 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
408 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
410 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
414 client = svs.clients + clientnum;
415 if (!client->netconnection)
418 VM_VarString(1, string, sizeof(string));
419 MSG_WriteChar(&client->netconnection->message,svc_print);
420 MSG_WriteString(&client->netconnection->message, string);
427 single print to the screen
432 void VM_centerprint (void)
434 char string[VM_STRINGTEMP_LENGTH];
436 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
437 VM_VarString(0, string, sizeof(string));
438 SCR_CenterPrint(string);
445 vector normalize(vector)
448 void VM_normalize (void)
454 VM_SAFEPARMCOUNT(1,VM_normalize);
456 value1 = PRVM_G_VECTOR(OFS_PARM0);
458 f = VectorLength2(value1);
462 VectorScale(value1, f, newvalue);
465 VectorClear(newvalue);
467 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
479 VM_SAFEPARMCOUNT(1,VM_vlen);
480 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
487 float vectoyaw(vector)
490 void VM_vectoyaw (void)
495 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
497 value1 = PRVM_G_VECTOR(OFS_PARM0);
499 if (value1[1] == 0 && value1[0] == 0)
503 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
508 PRVM_G_FLOAT(OFS_RETURN) = yaw;
516 vector vectoangles(vector[, vector])
519 void VM_vectoangles (void)
521 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
523 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
530 Returns a number from 0<= num < 1
535 void VM_random (void)
537 VM_SAFEPARMCOUNT(0,VM_random);
539 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
546 localsound(string sample)
549 void VM_localsound(void)
553 VM_SAFEPARMCOUNT(1,VM_localsound);
555 s = PRVM_G_STRING(OFS_PARM0);
557 if(!S_LocalSound (s))
559 PRVM_G_FLOAT(OFS_RETURN) = -4;
560 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
564 PRVM_G_FLOAT(OFS_RETURN) = 1;
576 PRVM_ERROR ("%s: break statement", PRVM_NAME);
579 //============================================================================
585 Sends text over to the client's execution buffer
587 [localcmd (string, ...) or]
591 void VM_localcmd (void)
593 char string[VM_STRINGTEMP_LENGTH];
594 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
595 VM_VarString(0, string, sizeof(string));
596 Cbuf_AddText(string);
599 static qboolean PRVM_Cvar_ReadOk(const char *string)
602 cvar = Cvar_FindVar(string);
603 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
615 char string[VM_STRINGTEMP_LENGTH];
616 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
617 VM_VarString(0, string, sizeof(string));
618 VM_CheckEmptyString(string);
619 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
626 float cvar_type (string)
627 float CVAR_TYPEFLAG_EXISTS = 1;
628 float CVAR_TYPEFLAG_SAVED = 2;
629 float CVAR_TYPEFLAG_PRIVATE = 4;
630 float CVAR_TYPEFLAG_ENGINE = 8;
631 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
632 float CVAR_TYPEFLAG_READONLY = 32;
635 void VM_cvar_type (void)
637 char string[VM_STRINGTEMP_LENGTH];
641 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
642 VM_VarString(0, string, sizeof(string));
643 VM_CheckEmptyString(string);
644 cvar = Cvar_FindVar(string);
649 PRVM_G_FLOAT(OFS_RETURN) = 0;
650 return; // CVAR_TYPE_NONE
653 ret = 1; // CVAR_EXISTS
654 if(cvar->flags & CVAR_SAVE)
655 ret |= 2; // CVAR_TYPE_SAVED
656 if(cvar->flags & CVAR_PRIVATE)
657 ret |= 4; // CVAR_TYPE_PRIVATE
658 if(!(cvar->flags & CVAR_ALLOCATED))
659 ret |= 8; // CVAR_TYPE_ENGINE
660 if(cvar->description != cvar_dummy_description)
661 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
662 if(cvar->flags & CVAR_READONLY)
663 ret |= 32; // CVAR_TYPE_READONLY
665 PRVM_G_FLOAT(OFS_RETURN) = ret;
672 const string VM_cvar_string (string, ...)
675 void VM_cvar_string(void)
677 char string[VM_STRINGTEMP_LENGTH];
678 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
679 VM_VarString(0, string, sizeof(string));
680 VM_CheckEmptyString(string);
681 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
686 ========================
689 const string VM_cvar_defstring (string, ...)
690 ========================
692 void VM_cvar_defstring (void)
694 char string[VM_STRINGTEMP_LENGTH];
695 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
696 VM_VarString(0, string, sizeof(string));
697 VM_CheckEmptyString(string);
698 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
702 ========================
705 const string VM_cvar_description (string, ...)
706 ========================
708 void VM_cvar_description (void)
710 char string[VM_STRINGTEMP_LENGTH];
711 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
712 VM_VarString(0, string, sizeof(string));
713 VM_CheckEmptyString(string);
714 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
720 void cvar_set (string,string, ...)
723 void VM_cvar_set (void)
726 char string[VM_STRINGTEMP_LENGTH];
727 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
728 VM_VarString(1, string, sizeof(string));
729 name = PRVM_G_STRING(OFS_PARM0);
730 VM_CheckEmptyString(name);
731 Cvar_Set(name, string);
741 void VM_dprint (void)
743 char string[VM_STRINGTEMP_LENGTH];
744 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
745 if (developer.integer)
747 VM_VarString(0, string, sizeof(string));
749 Con_Printf("%s", string);
751 Con_Printf("%s: %s", PRVM_NAME, string);
769 VM_SAFEPARMCOUNT(1, VM_ftos);
771 v = PRVM_G_FLOAT(OFS_PARM0);
773 if ((float)((int)v) == v)
774 dpsnprintf(s, sizeof(s), "%i", (int)v);
776 dpsnprintf(s, sizeof(s), "%f", v);
777 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
792 VM_SAFEPARMCOUNT(1,VM_fabs);
794 v = PRVM_G_FLOAT(OFS_PARM0);
795 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
810 VM_SAFEPARMCOUNT(1,VM_vtos);
812 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]);
813 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
828 VM_SAFEPARMCOUNT(1, VM_etos);
830 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
831 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
838 float stof(...[string])
843 char string[VM_STRINGTEMP_LENGTH];
844 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
845 VM_VarString(0, string, sizeof(string));
846 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
850 ========================
854 ========================
858 VM_SAFEPARMCOUNT(1, VM_itof);
859 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
863 ========================
866 entity ftoe(float num)
867 ========================
872 VM_SAFEPARMCOUNT(1, VM_ftoe);
874 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
875 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
876 ent = 0; // return world instead of a free or invalid entity
878 PRVM_G_INT(OFS_RETURN) = ent;
882 ========================
885 float etof(entity ent)
886 ========================
890 VM_SAFEPARMCOUNT(1, VM_etof);
891 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
898 string strftime(float uselocaltime, string[, string ...])
901 void VM_strftime(void)
910 char fmt[VM_STRINGTEMP_LENGTH];
911 char result[VM_STRINGTEMP_LENGTH];
912 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
913 VM_VarString(1, fmt, sizeof(fmt));
916 if (PRVM_G_FLOAT(OFS_PARM0))
917 tmresult = localtime_s(&tm, &t);
919 tmresult = gmtime_s(&tm, &t);
922 if (PRVM_G_FLOAT(OFS_PARM0))
929 PRVM_G_INT(OFS_RETURN) = 0;
933 strftime(result, sizeof(result), fmt, &tm);
935 strftime(result, sizeof(result), fmt, tm);
937 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
951 VM_SAFEPARMCOUNT(0, VM_spawn);
952 prog->xfunction->builtinsprofile += 20;
953 ed = PRVM_ED_Alloc();
965 void VM_remove (void)
968 prog->xfunction->builtinsprofile += 20;
970 VM_SAFEPARMCOUNT(1, VM_remove);
972 ed = PRVM_G_EDICT(OFS_PARM0);
973 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
975 if (developer.integer >= 1)
976 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
978 else if( ed->priv.required->free )
980 if (developer.integer >= 1)
981 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
991 entity find(entity start, .string field, string match)
1002 VM_SAFEPARMCOUNT(3,VM_find);
1004 e = PRVM_G_EDICTNUM(OFS_PARM0);
1005 f = PRVM_G_INT(OFS_PARM1);
1006 s = PRVM_G_STRING(OFS_PARM2);
1008 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1009 // expects it to find all the monsters, so we must be careful to support
1012 for (e++ ; e < prog->num_edicts ; e++)
1014 prog->xfunction->builtinsprofile++;
1015 ed = PRVM_EDICT_NUM(e);
1016 if (ed->priv.required->free)
1018 t = PRVM_E_STRING(ed,f);
1023 VM_RETURN_EDICT(ed);
1028 VM_RETURN_EDICT(prog->edicts);
1035 entity findfloat(entity start, .float field, float match)
1036 entity findentity(entity start, .entity field, entity match)
1039 // LordHavoc: added this for searching float, int, and entity reference fields
1040 void VM_findfloat (void)
1047 VM_SAFEPARMCOUNT(3,VM_findfloat);
1049 e = PRVM_G_EDICTNUM(OFS_PARM0);
1050 f = PRVM_G_INT(OFS_PARM1);
1051 s = PRVM_G_FLOAT(OFS_PARM2);
1053 for (e++ ; e < prog->num_edicts ; e++)
1055 prog->xfunction->builtinsprofile++;
1056 ed = PRVM_EDICT_NUM(e);
1057 if (ed->priv.required->free)
1059 if (PRVM_E_FLOAT(ed,f) == s)
1061 VM_RETURN_EDICT(ed);
1066 VM_RETURN_EDICT(prog->edicts);
1073 entity findchain(.string field, string match)
1076 // chained search for strings in entity fields
1077 // entity(.string field, string match) findchain = #402;
1078 void VM_findchain (void)
1083 prvm_edict_t *ent, *chain;
1086 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1089 chainfield = PRVM_G_INT(OFS_PARM2);
1091 chainfield = prog->fieldoffsets.chain;
1093 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1095 chain = prog->edicts;
1097 f = PRVM_G_INT(OFS_PARM0);
1098 s = PRVM_G_STRING(OFS_PARM1);
1100 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1101 // expects it to find all the monsters, so we must be careful to support
1104 ent = PRVM_NEXT_EDICT(prog->edicts);
1105 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1107 prog->xfunction->builtinsprofile++;
1108 if (ent->priv.required->free)
1110 t = PRVM_E_STRING(ent,f);
1116 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_NUM_FOR_EDICT(chain);
1120 VM_RETURN_EDICT(chain);
1127 entity findchainfloat(.string field, float match)
1128 entity findchainentity(.string field, entity match)
1131 // LordHavoc: chained search for float, int, and entity reference fields
1132 // entity(.string field, float match) findchainfloat = #403;
1133 void VM_findchainfloat (void)
1138 prvm_edict_t *ent, *chain;
1141 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1144 chainfield = PRVM_G_INT(OFS_PARM2);
1146 chainfield = prog->fieldoffsets.chain;
1148 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1150 chain = (prvm_edict_t *)prog->edicts;
1152 f = PRVM_G_INT(OFS_PARM0);
1153 s = PRVM_G_FLOAT(OFS_PARM1);
1155 ent = PRVM_NEXT_EDICT(prog->edicts);
1156 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1158 prog->xfunction->builtinsprofile++;
1159 if (ent->priv.required->free)
1161 if (PRVM_E_FLOAT(ent,f) != s)
1164 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1168 VM_RETURN_EDICT(chain);
1172 ========================
1175 entity findflags(entity start, .float field, float match)
1176 ========================
1178 // LordHavoc: search for flags in float fields
1179 void VM_findflags (void)
1186 VM_SAFEPARMCOUNT(3, VM_findflags);
1189 e = PRVM_G_EDICTNUM(OFS_PARM0);
1190 f = PRVM_G_INT(OFS_PARM1);
1191 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1193 for (e++ ; e < prog->num_edicts ; e++)
1195 prog->xfunction->builtinsprofile++;
1196 ed = PRVM_EDICT_NUM(e);
1197 if (ed->priv.required->free)
1199 if (!PRVM_E_FLOAT(ed,f))
1201 if ((int)PRVM_E_FLOAT(ed,f) & s)
1203 VM_RETURN_EDICT(ed);
1208 VM_RETURN_EDICT(prog->edicts);
1212 ========================
1215 entity findchainflags(.float field, float match)
1216 ========================
1218 // LordHavoc: chained search for flags in float fields
1219 void VM_findchainflags (void)
1224 prvm_edict_t *ent, *chain;
1227 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1230 chainfield = PRVM_G_INT(OFS_PARM2);
1232 chainfield = prog->fieldoffsets.chain;
1234 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1236 chain = (prvm_edict_t *)prog->edicts;
1238 f = PRVM_G_INT(OFS_PARM0);
1239 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1241 ent = PRVM_NEXT_EDICT(prog->edicts);
1242 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1244 prog->xfunction->builtinsprofile++;
1245 if (ent->priv.required->free)
1247 if (!PRVM_E_FLOAT(ent,f))
1249 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1252 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1256 VM_RETURN_EDICT(chain);
1263 string precache_sound (string sample)
1266 void VM_precache_sound (void)
1270 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1272 s = PRVM_G_STRING(OFS_PARM0);
1273 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1274 //VM_CheckEmptyString(s);
1276 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1278 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1287 returns the same string as output
1289 does nothing, only used by qcc to build .pak archives
1292 void VM_precache_file (void)
1294 VM_SAFEPARMCOUNT(1,VM_precache_file);
1295 // precache_file is only used to copy files with qcc, it does nothing
1296 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1306 void VM_coredump (void)
1308 VM_SAFEPARMCOUNT(0,VM_coredump);
1310 Cbuf_AddText("prvm_edicts ");
1311 Cbuf_AddText(PRVM_NAME);
1322 void PRVM_StackTrace(void);
1323 void VM_stackdump (void)
1325 VM_SAFEPARMCOUNT(0, VM_stackdump);
1340 VM_SAFEPARMCOUNT(0, VM_crash);
1342 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1352 void VM_traceon (void)
1354 VM_SAFEPARMCOUNT(0,VM_traceon);
1366 void VM_traceoff (void)
1368 VM_SAFEPARMCOUNT(0,VM_traceoff);
1370 prog->trace = false;
1380 void VM_eprint (void)
1382 VM_SAFEPARMCOUNT(1,VM_eprint);
1384 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1397 VM_SAFEPARMCOUNT(1,VM_rint);
1399 f = PRVM_G_FLOAT(OFS_PARM0);
1401 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1403 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1413 void VM_floor (void)
1415 VM_SAFEPARMCOUNT(1,VM_floor);
1417 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1429 VM_SAFEPARMCOUNT(1,VM_ceil);
1431 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1439 entity nextent(entity)
1442 void VM_nextent (void)
1447 VM_SAFEPARMCOUNT(1, VM_nextent);
1449 i = PRVM_G_EDICTNUM(OFS_PARM0);
1452 prog->xfunction->builtinsprofile++;
1454 if (i == prog->num_edicts)
1456 VM_RETURN_EDICT(prog->edicts);
1459 ent = PRVM_EDICT_NUM(i);
1460 if (!ent->priv.required->free)
1462 VM_RETURN_EDICT(ent);
1468 //=============================================================================
1475 changelevel(string map)
1478 void VM_changelevel (void)
1480 VM_SAFEPARMCOUNT(1, VM_changelevel);
1484 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1488 // make sure we don't issue two changelevels
1489 if (svs.changelevel_issued)
1491 svs.changelevel_issued = true;
1493 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1505 VM_SAFEPARMCOUNT(1,VM_sin);
1506 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1517 VM_SAFEPARMCOUNT(1,VM_cos);
1518 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1530 VM_SAFEPARMCOUNT(1,VM_sqrt);
1531 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1543 VM_SAFEPARMCOUNT(1,VM_asin);
1544 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1555 VM_SAFEPARMCOUNT(1,VM_acos);
1556 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1567 VM_SAFEPARMCOUNT(1,VM_atan);
1568 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1574 float atan2(float,float)
1577 void VM_atan2 (void)
1579 VM_SAFEPARMCOUNT(2,VM_atan2);
1580 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1591 VM_SAFEPARMCOUNT(1,VM_tan);
1592 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1599 Returns a vector of length < 1 and > 0
1604 void VM_randomvec (void)
1609 VM_SAFEPARMCOUNT(0, VM_randomvec);
1614 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1615 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1616 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1618 while (DotProduct(temp, temp) >= 1);
1619 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1622 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1623 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1624 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1625 // length returned always > 0
1626 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1627 VectorScale(temp,length, temp);*/
1628 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1631 //=============================================================================
1637 float registercvar (string name, string value[, float flags])
1640 void VM_registercvar (void)
1642 const char *name, *value;
1645 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1647 name = PRVM_G_STRING(OFS_PARM0);
1648 value = PRVM_G_STRING(OFS_PARM1);
1649 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1650 PRVM_G_FLOAT(OFS_RETURN) = 0;
1652 if(flags > CVAR_MAXFLAGSVAL)
1655 // first check to see if it has already been defined
1656 if (Cvar_FindVar (name))
1659 // check for overlap with a command
1660 if (Cmd_Exists (name))
1662 VM_Warning("VM_registercvar: %s is a command\n", name);
1666 Cvar_Get(name, value, flags, NULL);
1668 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1676 returns the minimum of two supplied floats
1678 float min(float a, float b, ...[float])
1683 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1684 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1685 if (prog->argc >= 3)
1688 float f = PRVM_G_FLOAT(OFS_PARM0);
1689 for (i = 1;i < prog->argc;i++)
1690 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1691 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1692 PRVM_G_FLOAT(OFS_RETURN) = f;
1695 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1702 returns the maximum of two supplied floats
1704 float max(float a, float b, ...[float])
1709 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1710 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1711 if (prog->argc >= 3)
1714 float f = PRVM_G_FLOAT(OFS_PARM0);
1715 for (i = 1;i < prog->argc;i++)
1716 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1717 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1718 PRVM_G_FLOAT(OFS_RETURN) = f;
1721 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1728 returns number bounded by supplied range
1730 float bound(float min, float value, float max)
1733 void VM_bound (void)
1735 VM_SAFEPARMCOUNT(3,VM_bound);
1736 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1743 returns a raised to power b
1745 float pow(float a, float b)
1750 VM_SAFEPARMCOUNT(2,VM_pow);
1751 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1756 VM_SAFEPARMCOUNT(1,VM_log);
1757 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1760 void VM_Files_Init(void)
1763 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1764 prog->openfiles[i] = NULL;
1767 void VM_Files_CloseAll(void)
1770 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1772 if (prog->openfiles[i])
1773 FS_Close(prog->openfiles[i]);
1774 prog->openfiles[i] = NULL;
1778 static qfile_t *VM_GetFileHandle( int index )
1780 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1782 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1785 if (prog->openfiles[index] == NULL)
1787 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1790 return prog->openfiles[index];
1797 float fopen(string filename, float mode)
1800 // float(string filename, float mode) fopen = #110;
1801 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1802 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1806 const char *modestring, *filename;
1808 VM_SAFEPARMCOUNT(2,VM_fopen);
1810 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1811 if (prog->openfiles[filenum] == NULL)
1813 if (filenum >= PRVM_MAX_OPENFILES)
1815 PRVM_G_FLOAT(OFS_RETURN) = -2;
1816 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1819 filename = PRVM_G_STRING(OFS_PARM0);
1820 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1823 case 0: // FILE_READ
1825 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1826 if (prog->openfiles[filenum] == NULL)
1827 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1829 case 1: // FILE_APPEND
1831 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1833 case 2: // FILE_WRITE
1835 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1838 PRVM_G_FLOAT(OFS_RETURN) = -3;
1839 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1843 if (prog->openfiles[filenum] == NULL)
1845 PRVM_G_FLOAT(OFS_RETURN) = -1;
1846 if (developer.integer >= 100)
1847 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1851 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1852 if (developer.integer >= 100)
1853 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1854 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1862 fclose(float fhandle)
1865 //void(float fhandle) fclose = #111; // closes a file
1866 void VM_fclose(void)
1870 VM_SAFEPARMCOUNT(1,VM_fclose);
1872 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1873 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1875 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1878 if (prog->openfiles[filenum] == NULL)
1880 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1883 FS_Close(prog->openfiles[filenum]);
1884 prog->openfiles[filenum] = NULL;
1885 if(prog->openfiles_origin[filenum])
1886 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1887 if (developer.integer >= 100)
1888 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1895 string fgets(float fhandle)
1898 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1902 char string[VM_STRINGTEMP_LENGTH];
1905 VM_SAFEPARMCOUNT(1,VM_fgets);
1907 // set the return value regardless of any possible errors
1908 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1910 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1911 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1913 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1916 if (prog->openfiles[filenum] == NULL)
1918 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1924 c = FS_Getc(prog->openfiles[filenum]);
1925 if (c == '\r' || c == '\n' || c < 0)
1927 if (end < VM_STRINGTEMP_LENGTH - 1)
1931 // remove \n following \r
1934 c = FS_Getc(prog->openfiles[filenum]);
1936 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1938 if (developer.integer >= 100)
1939 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1941 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1948 fputs(float fhandle, string s)
1951 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1955 char string[VM_STRINGTEMP_LENGTH];
1958 VM_SAFEPARMCOUNT(2,VM_fputs);
1960 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1961 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1963 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1966 if (prog->openfiles[filenum] == NULL)
1968 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1971 VM_VarString(1, string, sizeof(string));
1972 if ((stringlength = (int)strlen(string)))
1973 FS_Write(prog->openfiles[filenum], string, stringlength);
1974 if (developer.integer >= 100)
1975 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1982 writetofile(float fhandle, entity ent)
1985 void VM_writetofile(void)
1990 VM_SAFEPARMCOUNT(2, VM_writetofile);
1992 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1995 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1999 ent = PRVM_G_EDICT(OFS_PARM1);
2000 if(ent->priv.required->free)
2002 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2006 PRVM_ED_Write (file, ent);
2009 // KrimZon - DP_QC_ENTITYDATA
2014 float() numentityfields
2015 Return the number of entity fields - NOT offsets
2018 void VM_numentityfields(void)
2020 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
2023 // KrimZon - DP_QC_ENTITYDATA
2028 string(float fieldnum) entityfieldname
2029 Return name of the specified field as a string, or empty if the field is invalid (warning)
2032 void VM_entityfieldname(void)
2035 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2037 if (i < 0 || i >= prog->progs->numfielddefs)
2039 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
2040 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2044 d = &prog->fielddefs[i];
2045 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2048 // KrimZon - DP_QC_ENTITYDATA
2053 float(float fieldnum) entityfieldtype
2056 void VM_entityfieldtype(void)
2059 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2061 if (i < 0 || i >= prog->progs->numfielddefs)
2063 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
2064 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2068 d = &prog->fielddefs[i];
2069 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2072 // KrimZon - DP_QC_ENTITYDATA
2075 VM_getentityfieldstring
2077 string(float fieldnum, entity ent) getentityfieldstring
2080 void VM_getentityfieldstring(void)
2082 // put the data into a string
2087 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2089 if (i < 0 || i >= prog->progs->numfielddefs)
2091 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2092 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2096 d = &prog->fielddefs[i];
2099 ent = PRVM_G_EDICT(OFS_PARM1);
2100 if(ent->priv.required->free)
2102 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2103 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2106 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2108 // if it's 0 or blank, return an empty string
2109 type = d->type & ~DEF_SAVEGLOBAL;
2110 for (j=0 ; j<prvm_type_size[type] ; j++)
2113 if (j == prvm_type_size[type])
2115 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2119 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
2122 // KrimZon - DP_QC_ENTITYDATA
2125 VM_putentityfieldstring
2127 float(float fieldnum, entity ent, string s) putentityfieldstring
2130 void VM_putentityfieldstring(void)
2134 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2136 if (i < 0 || i >= prog->progs->numfielddefs)
2138 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2139 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2143 d = &prog->fielddefs[i];
2146 ent = PRVM_G_EDICT(OFS_PARM1);
2147 if(ent->priv.required->free)
2149 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2150 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2154 // parse the string into the value
2155 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2162 float strlen(string s)
2165 //float(string s) strlen = #114; // returns how many characters are in a string
2166 void VM_strlen(void)
2168 VM_SAFEPARMCOUNT(1,VM_strlen);
2170 PRVM_G_FLOAT(OFS_RETURN) = 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);
2215 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2218 // DRESK - String to Uppercase and Lowercase
2223 string strtolower(string s)
2226 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2227 void VM_strtolower(void)
2229 char szNewString[VM_STRINGTEMP_LENGTH];
2230 const char *szString;
2233 VM_SAFEPARMCOUNT(1,VM_strtolower);
2234 szString = PRVM_G_STRING(OFS_PARM0);
2236 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2238 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2245 string strtoupper(string s)
2248 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2249 void VM_strtoupper(void)
2251 char szNewString[VM_STRINGTEMP_LENGTH];
2252 const char *szString;
2255 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2256 szString = PRVM_G_STRING(OFS_PARM0);
2258 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2260 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2267 string strcat(string,string,...[string])
2270 //string(string s1, string s2) strcat = #115;
2271 // concatenates two strings (for example "abc", "def" would return "abcdef")
2272 // and returns as a tempstring
2273 void VM_strcat(void)
2275 char s[VM_STRINGTEMP_LENGTH];
2276 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2278 VM_VarString(0, s, sizeof(s));
2279 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2286 string substring(string s, float start, float length)
2289 // string(string s, float start, float length) substring = #116;
2290 // returns a section of a string as a tempstring
2291 void VM_substring(void)
2293 int start, length, slength, maxlen;
2295 char string[VM_STRINGTEMP_LENGTH];
2297 VM_SAFEPARMCOUNT(3,VM_substring);
2299 s = PRVM_G_STRING(OFS_PARM0);
2300 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2301 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2302 slength = strlen(s);
2304 if (start < 0) // FTE_STRINGS feature
2306 start = bound(0, start, slength);
2308 if (length < 0) // FTE_STRINGS feature
2309 length += slength - start + 1;
2310 maxlen = min((int)sizeof(string) - 1, slength - start);
2311 length = bound(0, length, maxlen);
2313 memcpy(string, s + start, length);
2315 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2322 string(string search, string replace, string subject) strreplace = #484;
2325 // replaces all occurrences of search with replace in the string subject, and returns the result
2326 void VM_strreplace(void)
2329 const char *search, *replace, *subject;
2330 char string[VM_STRINGTEMP_LENGTH];
2331 int search_len, replace_len, subject_len;
2333 VM_SAFEPARMCOUNT(3,VM_strreplace);
2335 search = PRVM_G_STRING(OFS_PARM0);
2336 replace = PRVM_G_STRING(OFS_PARM1);
2337 subject = PRVM_G_STRING(OFS_PARM2);
2339 search_len = (int)strlen(search);
2340 replace_len = (int)strlen(replace);
2341 subject_len = (int)strlen(subject);
2344 for (i = 0; i < subject_len; i++)
2346 for (j = 0; j < search_len && i+j < subject_len; j++)
2347 if (subject[i+j] != search[j])
2349 if (j == search_len || i+j == subject_len)
2351 // found it at offset 'i'
2352 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2353 string[si++] = replace[j];
2354 i += search_len - 1;
2359 if (si < (int)sizeof(string) - 1)
2360 string[si++] = subject[i];
2365 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2372 string(string search, string replace, string subject) strireplace = #485;
2375 // case-insensitive version of strreplace
2376 void VM_strireplace(void)
2379 const char *search, *replace, *subject;
2380 char string[VM_STRINGTEMP_LENGTH];
2381 int search_len, replace_len, subject_len;
2383 VM_SAFEPARMCOUNT(3,VM_strreplace);
2385 search = PRVM_G_STRING(OFS_PARM0);
2386 replace = PRVM_G_STRING(OFS_PARM1);
2387 subject = PRVM_G_STRING(OFS_PARM2);
2389 search_len = (int)strlen(search);
2390 replace_len = (int)strlen(replace);
2391 subject_len = (int)strlen(subject);
2394 for (i = 0; i < subject_len; i++)
2396 for (j = 0; j < search_len && i+j < subject_len; j++)
2397 if (tolower(subject[i+j]) != tolower(search[j]))
2399 if (j == search_len || i+j == subject_len)
2401 // found it at offset 'i'
2402 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2403 string[si++] = replace[j];
2404 i += search_len - 1;
2409 if (si < (int)sizeof(string) - 1)
2410 string[si++] = subject[i];
2415 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2422 vector stov(string s)
2425 //vector(string s) stov = #117; // returns vector value from a string
2428 char string[VM_STRINGTEMP_LENGTH];
2430 VM_SAFEPARMCOUNT(1,VM_stov);
2432 VM_VarString(0, string, sizeof(string));
2433 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2440 string strzone(string s)
2443 //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)
2444 void VM_strzone(void)
2447 char string[VM_STRINGTEMP_LENGTH];
2450 VM_SAFEPARMCOUNT(1,VM_strzone);
2452 VM_VarString(0, string, sizeof(string));
2453 alloclen = strlen(string) + 1;
2454 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2455 memcpy(out, string, alloclen);
2465 //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!!!)
2466 void VM_strunzone(void)
2468 VM_SAFEPARMCOUNT(1,VM_strunzone);
2469 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2474 VM_command (used by client and menu)
2476 clientcommand(float client, string s) (for client and menu)
2479 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2480 //this function originally written by KrimZon, made shorter by LordHavoc
2481 void VM_clcommand (void)
2483 client_t *temp_client;
2486 VM_SAFEPARMCOUNT(2,VM_clcommand);
2488 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2489 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2491 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2495 temp_client = host_client;
2496 host_client = svs.clients + i;
2497 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2498 host_client = temp_client;
2506 float tokenize(string s)
2509 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2510 //this function originally written by KrimZon, made shorter by LordHavoc
2511 //20040203: rewritten by LordHavoc (no longer uses allocations)
2512 static int num_tokens = 0;
2513 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2514 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2515 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2516 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2517 void VM_tokenize (void)
2521 VM_SAFEPARMCOUNT(1,VM_tokenize);
2523 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2524 p = tokenize_string;
2529 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2532 // skip whitespace here to find token start pos
2533 while(*p && ISWHITESPACE(*p))
2536 tokens_startpos[num_tokens] = p - tokenize_string;
2537 if(!COM_ParseToken_VM_Tokenize(&p, false))
2539 tokens_endpos[num_tokens] = p - tokenize_string;
2540 tokens[num_tokens] = PRVM_SetTempString(com_token);
2544 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2547 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2548 void VM_tokenize_console (void)
2552 VM_SAFEPARMCOUNT(1,VM_tokenize);
2554 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2555 p = tokenize_string;
2560 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2563 // skip whitespace here to find token start pos
2564 while(*p && ISWHITESPACE(*p))
2567 tokens_startpos[num_tokens] = p - tokenize_string;
2568 if(!COM_ParseToken_Console(&p))
2570 tokens_endpos[num_tokens] = p - tokenize_string;
2571 tokens[num_tokens] = PRVM_SetTempString(com_token);
2575 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2580 VM_tokenizebyseparator
2582 float tokenizebyseparator(string s, string separator1, ...)
2585 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2586 //this function returns the token preceding each instance of a separator (of
2587 //which there can be multiple), and the text following the last separator
2588 //useful for parsing certain kinds of data like IP addresses
2590 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2591 //returns 4 and the tokens "10" "1" "2" "3".
2592 void VM_tokenizebyseparator (void)
2596 int separatorlen[7];
2597 const char *separators[7];
2600 char tokentext[MAX_INPUTLINE];
2602 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2604 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2605 p = tokenize_string;
2608 for (j = 1;j < prog->argc;j++)
2610 // skip any blank separator strings
2611 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2614 separators[numseparators] = s;
2615 separatorlen[numseparators] = strlen(s);
2622 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2624 token = tokentext + j;
2625 tokens_startpos[num_tokens] = p - tokenize_string;
2629 for (k = 0;k < numseparators;k++)
2631 if (!strncmp(p, separators[k], separatorlen[k]))
2633 p += separatorlen[k];
2637 if (k < numseparators)
2639 if (j < (int)sizeof(tokentext)-1)
2640 tokentext[j++] = *p;
2644 tokens_endpos[num_tokens] = p0 - tokenize_string;
2645 if (j >= (int)sizeof(tokentext))
2648 tokens[num_tokens++] = PRVM_SetTempString(token);
2653 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2656 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2657 //this function originally written by KrimZon, made shorter by LordHavoc
2662 VM_SAFEPARMCOUNT(1,VM_argv);
2664 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2667 token_num += num_tokens;
2669 if (token_num >= 0 && token_num < num_tokens)
2670 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2672 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2675 //float(float n) argv_start_index = #515; // returns the start index of a token
2676 void VM_argv_start_index (void)
2680 VM_SAFEPARMCOUNT(1,VM_argv);
2682 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2685 token_num += num_tokens;
2687 if (token_num >= 0 && token_num < num_tokens)
2688 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2690 PRVM_G_FLOAT(OFS_RETURN) = -1;
2693 //float(float n) argv_end_index = #516; // returns the end index of a token
2694 void VM_argv_end_index (void)
2698 VM_SAFEPARMCOUNT(1,VM_argv);
2700 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2703 token_num += num_tokens;
2705 if (token_num >= 0 && token_num < num_tokens)
2706 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2708 PRVM_G_FLOAT(OFS_RETURN) = -1;
2718 void VM_isserver(void)
2720 VM_SAFEPARMCOUNT(0,VM_serverstate);
2722 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2732 void VM_clientcount(void)
2734 VM_SAFEPARMCOUNT(0,VM_clientcount);
2736 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2746 void VM_clientstate(void)
2748 VM_SAFEPARMCOUNT(0,VM_clientstate);
2751 switch( cls.state ) {
2752 case ca_uninitialized:
2754 PRVM_G_FLOAT(OFS_RETURN) = 0;
2756 case ca_disconnected:
2757 PRVM_G_FLOAT(OFS_RETURN) = 1;
2760 PRVM_G_FLOAT(OFS_RETURN) = 2;
2763 // should never be reached!
2772 float getostype(void)
2774 */ // not used at the moment -> not included in the common list
2775 void VM_getostype(void)
2777 VM_SAFEPARMCOUNT(0,VM_getostype);
2782 OS_MAC - not supported
2786 PRVM_G_FLOAT(OFS_RETURN) = 0;
2787 #elif defined(MACOSX)
2788 PRVM_G_FLOAT(OFS_RETURN) = 2;
2790 PRVM_G_FLOAT(OFS_RETURN) = 1;
2801 extern double host_starttime;
2802 float CDAudio_GetPosition(void);
2803 void VM_gettime(void)
2807 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2811 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2815 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2818 case 0: // GETTIME_FRAMESTART
2819 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2821 case 1: // GETTIME_REALTIME
2822 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2824 case 2: // GETTIME_HIRES
2825 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2827 case 3: // GETTIME_UPTIME
2828 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2830 case 4: // GETTIME_CDTRACK
2831 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2834 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2835 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2845 loadfromdata(string data)
2848 void VM_loadfromdata(void)
2850 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2852 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2856 ========================
2859 parseentitydata(entity ent, string data)
2860 ========================
2862 void VM_parseentitydata(void)
2867 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2869 // get edict and test it
2870 ent = PRVM_G_EDICT(OFS_PARM0);
2871 if (ent->priv.required->free)
2872 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2874 data = PRVM_G_STRING(OFS_PARM1);
2876 // parse the opening brace
2877 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2878 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2880 PRVM_ED_ParseEdict (data, ent);
2887 loadfromfile(string file)
2890 void VM_loadfromfile(void)
2892 const char *filename;
2895 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2897 filename = PRVM_G_STRING(OFS_PARM0);
2898 if (FS_CheckNastyPath(filename, false))
2900 PRVM_G_FLOAT(OFS_RETURN) = -4;
2901 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2905 // not conform with VM_fopen
2906 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2908 PRVM_G_FLOAT(OFS_RETURN) = -1;
2910 PRVM_ED_LoadFromFile(data);
2921 float mod(float val, float m)
2924 void VM_modulo(void)
2927 VM_SAFEPARMCOUNT(2,VM_module);
2929 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2930 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2932 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2935 void VM_Search_Init(void)
2938 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2939 prog->opensearches[i] = NULL;
2942 void VM_Search_Reset(void)
2945 // reset the fssearch list
2946 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2948 if(prog->opensearches[i])
2949 FS_FreeSearch(prog->opensearches[i]);
2950 prog->opensearches[i] = NULL;
2958 float search_begin(string pattern, float caseinsensitive, float quiet)
2961 void VM_search_begin(void)
2964 const char *pattern;
2965 int caseinsens, quiet;
2967 VM_SAFEPARMCOUNT(3, VM_search_begin);
2969 pattern = PRVM_G_STRING(OFS_PARM0);
2971 VM_CheckEmptyString(pattern);
2973 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2974 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2976 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2977 if(!prog->opensearches[handle])
2980 if(handle >= PRVM_MAX_OPENSEARCHES)
2982 PRVM_G_FLOAT(OFS_RETURN) = -2;
2983 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2987 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2988 PRVM_G_FLOAT(OFS_RETURN) = -1;
2991 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2992 PRVM_G_FLOAT(OFS_RETURN) = handle;
3000 void search_end(float handle)
3003 void VM_search_end(void)
3006 VM_SAFEPARMCOUNT(1, VM_search_end);
3008 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3010 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3012 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
3015 if(prog->opensearches[handle] == NULL)
3017 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
3021 FS_FreeSearch(prog->opensearches[handle]);
3022 prog->opensearches[handle] = NULL;
3023 if(prog->opensearches_origin[handle])
3024 PRVM_Free((char *)prog->opensearches_origin[handle]);
3031 float search_getsize(float handle)
3034 void VM_search_getsize(void)
3037 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3039 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3041 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3043 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
3046 if(prog->opensearches[handle] == NULL)
3048 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
3052 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3057 VM_search_getfilename
3059 string search_getfilename(float handle, float num)
3062 void VM_search_getfilename(void)
3064 int handle, filenum;
3065 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3067 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3068 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3070 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3072 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
3075 if(prog->opensearches[handle] == NULL)
3077 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
3080 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3082 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
3086 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
3093 string chr(float ascii)
3099 VM_SAFEPARMCOUNT(1, VM_chr);
3101 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3104 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3107 //=============================================================================
3108 // Draw builtins (client & menu)
3114 float iscachedpic(string pic)
3117 void VM_iscachedpic(void)
3119 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3121 // drawq hasnt such a function, thus always return true
3122 PRVM_G_FLOAT(OFS_RETURN) = false;
3129 string precache_pic(string pic)
3132 void VM_precache_pic(void)
3136 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3138 s = PRVM_G_STRING(OFS_PARM0);
3139 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3140 VM_CheckEmptyString (s);
3142 // AK Draw_CachePic is supposed to always return a valid pointer
3143 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
3144 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3154 void VM_freepic(void)
3158 VM_SAFEPARMCOUNT(1,VM_freepic);
3160 s = PRVM_G_STRING(OFS_PARM0);
3161 VM_CheckEmptyString (s);
3166 dp_font_t *getdrawfont(void)
3168 if(prog->globaloffsets.drawfont >= 0)
3170 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
3171 if(f < 0 || f >= MAX_FONTS)
3172 return FONT_DEFAULT;
3173 return &dp_fonts[f];
3176 return FONT_DEFAULT;
3183 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3186 void VM_drawcharacter(void)
3188 float *pos,*scale,*rgb;
3191 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3193 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3196 PRVM_G_FLOAT(OFS_RETURN) = -1;
3197 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3201 pos = PRVM_G_VECTOR(OFS_PARM0);
3202 scale = PRVM_G_VECTOR(OFS_PARM2);
3203 rgb = PRVM_G_VECTOR(OFS_PARM3);
3204 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3206 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3208 PRVM_G_FLOAT(OFS_RETURN) = -2;
3209 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3213 if(pos[2] || scale[2])
3214 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")));
3216 if(!scale[0] || !scale[1])
3218 PRVM_G_FLOAT(OFS_RETURN) = -3;
3219 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3223 DrawQ_String_Font(pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3224 PRVM_G_FLOAT(OFS_RETURN) = 1;
3231 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3234 void VM_drawstring(void)
3236 float *pos,*scale,*rgb;
3239 VM_SAFEPARMCOUNT(6,VM_drawstring);
3241 string = PRVM_G_STRING(OFS_PARM1);
3242 pos = PRVM_G_VECTOR(OFS_PARM0);
3243 scale = PRVM_G_VECTOR(OFS_PARM2);
3244 rgb = PRVM_G_VECTOR(OFS_PARM3);
3245 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3247 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3249 PRVM_G_FLOAT(OFS_RETURN) = -2;
3250 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3254 if(!scale[0] || !scale[1])
3256 PRVM_G_FLOAT(OFS_RETURN) = -3;
3257 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3261 if(pos[2] || scale[2])
3262 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")));
3264 DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3265 PRVM_G_FLOAT(OFS_RETURN) = 1;
3270 VM_drawcolorcodedstring
3272 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3275 void VM_drawcolorcodedstring(void)
3280 VM_SAFEPARMCOUNT(5,VM_drawstring);
3282 string = PRVM_G_STRING(OFS_PARM1);
3283 pos = PRVM_G_VECTOR(OFS_PARM0);
3284 scale = PRVM_G_VECTOR(OFS_PARM2);
3285 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3287 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3289 PRVM_G_FLOAT(OFS_RETURN) = -2;
3290 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3294 if(!scale[0] || !scale[1])
3296 PRVM_G_FLOAT(OFS_RETURN) = -3;
3297 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3301 if(pos[2] || scale[2])
3302 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")));
3305 DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false, getdrawfont());
3306 PRVM_G_FLOAT(OFS_RETURN) = 1;
3312 float stringwidth(string text, float allowColorCodes, float size)
3315 void VM_stringwidth(void)
3318 float sz, mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3320 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3324 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3332 string = PRVM_G_STRING(OFS_PARM0);
3333 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3335 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3341 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3344 void VM_drawpic(void)
3346 const char *picname;
3347 float *size, *pos, *rgb;
3350 VM_SAFEPARMCOUNT(6,VM_drawpic);
3352 picname = PRVM_G_STRING(OFS_PARM1);
3353 VM_CheckEmptyString (picname);
3355 // is pic cached ? no function yet for that
3358 PRVM_G_FLOAT(OFS_RETURN) = -4;
3359 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3363 pos = PRVM_G_VECTOR(OFS_PARM0);
3364 size = PRVM_G_VECTOR(OFS_PARM2);
3365 rgb = PRVM_G_VECTOR(OFS_PARM3);
3366 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3368 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3370 PRVM_G_FLOAT(OFS_RETURN) = -2;
3371 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3375 if(pos[2] || size[2])
3376 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")));
3378 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);
3379 PRVM_G_FLOAT(OFS_RETURN) = 1;
3385 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3388 void VM_drawrotpic(void)
3390 const char *picname;
3391 float *size, *pos, *org, *rgb;
3394 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3396 picname = PRVM_G_STRING(OFS_PARM1);
3397 VM_CheckEmptyString (picname);
3399 // is pic cached ? no function yet for that
3402 PRVM_G_FLOAT(OFS_RETURN) = -4;
3403 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3407 pos = PRVM_G_VECTOR(OFS_PARM0);
3408 size = PRVM_G_VECTOR(OFS_PARM2);
3409 org = PRVM_G_VECTOR(OFS_PARM3);
3410 rgb = PRVM_G_VECTOR(OFS_PARM5);
3411 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3413 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3415 PRVM_G_FLOAT(OFS_RETURN) = -2;
3416 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3420 if(pos[2] || size[2] || org[2])
3421 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3423 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);
3424 PRVM_G_FLOAT(OFS_RETURN) = 1;
3430 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3434 void VM_drawsubpic(void)
3436 const char *picname;
3437 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3440 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3442 picname = PRVM_G_STRING(OFS_PARM2);
3443 VM_CheckEmptyString (picname);
3445 // is pic cached ? no function yet for that
3448 PRVM_G_FLOAT(OFS_RETURN) = -4;
3449 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3453 pos = PRVM_G_VECTOR(OFS_PARM0);
3454 size = PRVM_G_VECTOR(OFS_PARM1);
3455 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3456 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3457 rgb = PRVM_G_VECTOR(OFS_PARM5);
3458 alpha = PRVM_G_FLOAT(OFS_PARM6);
3459 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3461 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3463 PRVM_G_FLOAT(OFS_RETURN) = -2;
3464 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3468 if(pos[2] || size[2])
3469 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")));
3471 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3473 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3474 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3475 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3476 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3478 PRVM_G_FLOAT(OFS_RETURN) = 1;
3485 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3488 void VM_drawfill(void)
3490 float *size, *pos, *rgb;
3493 VM_SAFEPARMCOUNT(5,VM_drawfill);
3496 pos = PRVM_G_VECTOR(OFS_PARM0);
3497 size = PRVM_G_VECTOR(OFS_PARM1);
3498 rgb = PRVM_G_VECTOR(OFS_PARM2);
3499 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3501 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3503 PRVM_G_FLOAT(OFS_RETURN) = -2;
3504 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3508 if(pos[2] || size[2])
3509 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")));
3511 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3512 PRVM_G_FLOAT(OFS_RETURN) = 1;
3519 drawsetcliparea(float x, float y, float width, float height)
3522 void VM_drawsetcliparea(void)
3525 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3527 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3528 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3529 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3530 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3532 DrawQ_SetClipArea(x, y, w, h);
3537 VM_drawresetcliparea
3542 void VM_drawresetcliparea(void)
3544 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3546 DrawQ_ResetClipArea();
3553 vector getimagesize(string pic)
3556 void VM_getimagesize(void)
3561 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3563 p = PRVM_G_STRING(OFS_PARM0);
3564 VM_CheckEmptyString (p);
3566 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3568 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3569 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3570 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3577 string keynumtostring(float keynum)
3580 void VM_keynumtostring (void)
3582 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3584 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3589 VM_findkeysforcommand
3591 string findkeysforcommand(string command)
3593 the returned string is an altstring
3596 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3598 void M_FindKeysForCommand(const char *command, int *keys);
3599 void VM_findkeysforcommand(void)
3602 char ret[VM_STRINGTEMP_LENGTH];
3606 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3608 cmd = PRVM_G_STRING(OFS_PARM0);
3610 VM_CheckEmptyString(cmd);
3612 M_FindKeysForCommand(cmd, keys);
3615 for(i = 0; i < NUMKEYS; i++)
3616 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3618 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3625 float stringtokeynum(string key)
3628 void VM_stringtokeynum (void)
3630 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3632 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3635 // CL_Video interface functions
3638 ========================
3641 float cin_open(string file, string name)
3642 ========================
3644 void VM_cin_open( void )
3649 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3651 file = PRVM_G_STRING( OFS_PARM0 );
3652 name = PRVM_G_STRING( OFS_PARM1 );
3654 VM_CheckEmptyString( file );
3655 VM_CheckEmptyString( name );
3657 if( CL_OpenVideo( file, name, MENUOWNER ) )
3658 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3660 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3664 ========================
3667 void cin_close(string name)
3668 ========================
3670 void VM_cin_close( void )
3674 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3676 name = PRVM_G_STRING( OFS_PARM0 );
3677 VM_CheckEmptyString( name );
3679 CL_CloseVideo( CL_GetVideoByName( name ) );
3683 ========================
3685 void cin_setstate(string name, float type)
3686 ========================
3688 void VM_cin_setstate( void )
3691 clvideostate_t state;
3694 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3696 name = PRVM_G_STRING( OFS_PARM0 );
3697 VM_CheckEmptyString( name );
3699 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3701 video = CL_GetVideoByName( name );
3702 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3703 CL_SetVideoState( video, state );
3707 ========================
3710 float cin_getstate(string name)
3711 ========================
3713 void VM_cin_getstate( void )
3718 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3720 name = PRVM_G_STRING( OFS_PARM0 );
3721 VM_CheckEmptyString( name );
3723 video = CL_GetVideoByName( name );
3725 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3727 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3731 ========================
3734 void cin_restart(string name)
3735 ========================
3737 void VM_cin_restart( void )
3742 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3744 name = PRVM_G_STRING( OFS_PARM0 );
3745 VM_CheckEmptyString( name );
3747 video = CL_GetVideoByName( name );
3749 CL_RestartVideo( video );
3753 ========================
3755 ========================
3757 void VM_Gecko_Init( void ) {
3758 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3759 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3763 ========================
3765 ========================
3767 void VM_Gecko_Destroy( void ) {
3769 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3770 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3772 CL_Gecko_DestroyBrowser( *instance );
3779 ========================
3782 float[bool] gecko_create( string name )
3783 ========================
3785 void VM_gecko_create( void ) {
3788 clgecko_t *instance;
3790 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3792 name = PRVM_G_STRING( OFS_PARM0 );
3793 VM_CheckEmptyString( name );
3795 // find an empty slot for this gecko browser..
3796 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3797 if( prog->opengeckoinstances[ i ] == NULL ) {
3801 if( i == PRVM_MAX_GECKOINSTANCES ) {
3802 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3803 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3807 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3809 // TODO: error handling [12/3/2007 Black]
3810 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3813 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3817 ========================
3820 void gecko_destroy( string name )
3821 ========================
3823 void VM_gecko_destroy( void ) {
3825 clgecko_t *instance;
3827 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3829 name = PRVM_G_STRING( OFS_PARM0 );
3830 VM_CheckEmptyString( name );
3831 instance = CL_Gecko_FindBrowser( name );
3835 CL_Gecko_DestroyBrowser( instance );
3839 ========================
3842 void gecko_navigate( string name, string URI )
3843 ========================
3845 void VM_gecko_navigate( void ) {
3848 clgecko_t *instance;
3850 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3852 name = PRVM_G_STRING( OFS_PARM0 );
3853 URI = PRVM_G_STRING( OFS_PARM1 );
3854 VM_CheckEmptyString( name );
3855 VM_CheckEmptyString( URI );
3857 instance = CL_Gecko_FindBrowser( name );
3861 CL_Gecko_NavigateToURI( instance, URI );
3865 ========================
3868 float[bool] gecko_keyevent( string name, float key, float eventtype )
3869 ========================
3871 void VM_gecko_keyevent( void ) {
3874 clgecko_buttoneventtype_t eventtype;
3875 clgecko_t *instance;
3877 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3879 name = PRVM_G_STRING( OFS_PARM0 );
3880 VM_CheckEmptyString( name );
3881 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3882 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3884 eventtype = CLG_BET_DOWN;
3887 eventtype = CLG_BET_UP;
3890 eventtype = CLG_BET_PRESS;
3893 eventtype = CLG_BET_DOUBLECLICK;
3896 // TODO: console printf? [12/3/2007 Black]
3897 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3901 instance = CL_Gecko_FindBrowser( name );
3903 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3907 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
3911 ========================
3914 void gecko_mousemove( string name, float x, float y )
3915 ========================
3917 void VM_gecko_movemouse( void ) {
3920 clgecko_t *instance;
3922 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3924 name = PRVM_G_STRING( OFS_PARM0 );
3925 VM_CheckEmptyString( name );
3926 x = PRVM_G_FLOAT( OFS_PARM1 );
3927 y = PRVM_G_FLOAT( OFS_PARM2 );
3929 instance = CL_Gecko_FindBrowser( name );
3933 CL_Gecko_Event_CursorMove( instance, x, y );
3938 ========================
3941 void gecko_resize( string name, float w, float h )
3942 ========================
3944 void VM_gecko_resize( void ) {
3947 clgecko_t *instance;
3949 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3951 name = PRVM_G_STRING( OFS_PARM0 );
3952 VM_CheckEmptyString( name );
3953 w = PRVM_G_FLOAT( OFS_PARM1 );
3954 h = PRVM_G_FLOAT( OFS_PARM2 );
3956 instance = CL_Gecko_FindBrowser( name );
3960 CL_Gecko_Resize( instance, (int) w, (int) h );
3965 ========================
3966 VM_gecko_get_texture_extent
3968 vector gecko_get_texture_extent( string name )
3969 ========================
3971 void VM_gecko_get_texture_extent( void ) {
3973 clgecko_t *instance;
3975 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3977 name = PRVM_G_STRING( OFS_PARM0 );
3978 VM_CheckEmptyString( name );
3980 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3981 instance = CL_Gecko_FindBrowser( name );
3983 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3984 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3987 CL_Gecko_GetTextureExtent( instance,
3988 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3997 Writes new values for v_forward, v_up, and v_right based on angles
3998 void makevectors(vector angle)
4001 void VM_makevectors (void)
4003 prvm_eval_t *valforward, *valright, *valup;
4004 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4005 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4006 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4007 if (!valforward || !valright || !valup)
4009 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
4012 VM_SAFEPARMCOUNT(1, VM_makevectors);
4013 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
4020 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4021 vectorvectors(vector)
4024 void VM_vectorvectors (void)
4026 prvm_eval_t *valforward, *valright, *valup;
4027 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4028 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4029 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4030 if (!valforward || !valright || !valup)
4032 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
4035 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4036 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
4037 VectorVectors(valforward->vector, valright->vector, valup->vector);
4041 ========================
4044 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4045 ========================
4047 void VM_drawline (void)
4049 float *c1, *c2, *rgb;
4051 unsigned char flags;
4053 VM_SAFEPARMCOUNT(6, VM_drawline);
4054 width = PRVM_G_FLOAT(OFS_PARM0);
4055 c1 = PRVM_G_VECTOR(OFS_PARM1);
4056 c2 = PRVM_G_VECTOR(OFS_PARM2);
4057 rgb = PRVM_G_VECTOR(OFS_PARM3);
4058 alpha = PRVM_G_FLOAT(OFS_PARM4);
4059 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4060 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4063 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4064 void VM_bitshift (void)
4067 VM_SAFEPARMCOUNT(2, VM_bitshift);
4069 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4070 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4072 PRVM_G_FLOAT(OFS_RETURN) = n1;
4075 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4077 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4080 ////////////////////////////////////////
4081 // AltString functions
4082 ////////////////////////////////////////
4085 ========================
4088 float altstr_count(string)
4089 ========================
4091 void VM_altstr_count( void )
4093 const char *altstr, *pos;
4096 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4098 altstr = PRVM_G_STRING( OFS_PARM0 );
4099 //VM_CheckEmptyString( altstr );
4101 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4102 if( *pos == '\\' ) {
4106 } else if( *pos == '\'' ) {
4111 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4115 ========================
4118 string altstr_prepare(string)
4119 ========================
4121 void VM_altstr_prepare( void )
4124 const char *instr, *in;
4126 char outstr[VM_STRINGTEMP_LENGTH];
4128 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4130 instr = PRVM_G_STRING( OFS_PARM0 );
4132 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4141 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4145 ========================
4148 string altstr_get(string, float)
4149 ========================
4151 void VM_altstr_get( void )
4153 const char *altstr, *pos;
4156 char outstr[VM_STRINGTEMP_LENGTH];
4158 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4160 altstr = PRVM_G_STRING( OFS_PARM0 );
4162 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4163 count = count * 2 + 1;
4165 for( pos = altstr ; *pos && count ; pos++ )
4166 if( *pos == '\\' ) {
4169 } else if( *pos == '\'' )
4173 PRVM_G_INT( OFS_RETURN ) = 0;
4177 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4178 if( *pos == '\\' ) {
4183 } else if( *pos == '\'' )
4189 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4193 ========================
4196 string altstr_set(string altstr, float num, string set)
4197 ========================
4199 void VM_altstr_set( void )
4202 const char *altstr, *str;
4205 char outstr[VM_STRINGTEMP_LENGTH];
4207 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4209 altstr = PRVM_G_STRING( OFS_PARM0 );
4211 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4213 str = PRVM_G_STRING( OFS_PARM2 );
4216 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4221 } else if( *in == '\'' ) {
4226 for( ; *str; *out++ = *str++ );
4227 // now jump over the old content
4229 if( *in == '\'' || (*in == '\\' && !*++in) )
4232 strlcpy(out, in, outstr + sizeof(outstr) - out);
4233 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4237 ========================
4240 string altstr_ins(string altstr, float num, string set)
4241 ========================
4243 void VM_altstr_ins(void)
4251 char outstr[VM_STRINGTEMP_LENGTH];
4253 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4255 in = instr = PRVM_G_STRING( OFS_PARM0 );
4256 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4257 set = setstr = PRVM_G_STRING( OFS_PARM2 );
4260 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4265 } else if( *in == '\'' ) {
4270 for( ; *set ; *out++ = *set++ );
4273 strlcpy(out, in, outstr + sizeof(outstr) - out);
4274 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4278 ////////////////////////////////////////
4279 // BufString functions
4280 ////////////////////////////////////////
4281 //[515]: string buffers support
4283 static size_t stringbuffers_sortlength;
4285 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4287 if (stringbuffer->max_strings <= strindex)
4289 char **oldstrings = stringbuffer->strings;
4290 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4291 while (stringbuffer->max_strings <= strindex)
4292 stringbuffer->max_strings *= 2;
4293 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4294 if (stringbuffer->num_strings > 0)
4295 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4297 Mem_Free(oldstrings);
4301 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4303 // reduce num_strings if there are empty string slots at the end
4304 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4305 stringbuffer->num_strings--;
4307 // if empty, free the string pointer array
4308 if (stringbuffer->num_strings == 0)
4310 stringbuffer->max_strings = 0;
4311 if (stringbuffer->strings)
4312 Mem_Free(stringbuffer->strings);
4313 stringbuffer->strings = NULL;
4317 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4320 a = *((const char **) in1);
4321 b = *((const char **) in2);
4323 if(!b[0]) return -1;
4324 return strncmp(a, b, stringbuffers_sortlength);
4327 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4330 a = *((const char **) in1);
4331 b = *((const char **) in2);
4333 if(!b[0]) return -1;
4334 return strncmp(b, a, stringbuffers_sortlength);
4338 ========================
4340 creates new buffer, and returns it's index, returns -1 if failed
4341 float buf_create(void) = #460;
4342 ========================
4344 void VM_buf_create (void)
4346 prvm_stringbuffer_t *stringbuffer;
4348 VM_SAFEPARMCOUNT(0, VM_buf_create);
4349 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4350 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4351 stringbuffer->origin = PRVM_AllocationOrigin();
4352 PRVM_G_FLOAT(OFS_RETURN) = i;
4356 ========================
4358 deletes buffer and all strings in it
4359 void buf_del(float bufhandle) = #461;
4360 ========================
4362 void VM_buf_del (void)
4364 prvm_stringbuffer_t *stringbuffer;
4365 VM_SAFEPARMCOUNT(1, VM_buf_del);
4366 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4370 for (i = 0;i < stringbuffer->num_strings;i++)
4371 if (stringbuffer->strings[i])
4372 Mem_Free(stringbuffer->strings[i]);
4373 if (stringbuffer->strings)
4374 Mem_Free(stringbuffer->strings);
4375 if(stringbuffer->origin)
4376 PRVM_Free((char *)stringbuffer->origin);
4377 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4381 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4387 ========================
4389 how many strings are stored in buffer
4390 float buf_getsize(float bufhandle) = #462;
4391 ========================
4393 void VM_buf_getsize (void)
4395 prvm_stringbuffer_t *stringbuffer;
4396 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4398 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4401 PRVM_G_FLOAT(OFS_RETURN) = -1;
4402 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4406 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4410 ========================
4412 copy all content from one buffer to another, make sure it exists
4413 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4414 ========================
4416 void VM_buf_copy (void)
4418 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4420 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4422 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4423 if(!srcstringbuffer)
4425 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4428 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4429 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4431 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4434 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4435 if(!dststringbuffer)
4437 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4441 for (i = 0;i < dststringbuffer->num_strings;i++)
4442 if (dststringbuffer->strings[i])
4443 Mem_Free(dststringbuffer->strings[i]);
4444 if (dststringbuffer->strings)
4445 Mem_Free(dststringbuffer->strings);
4446 *dststringbuffer = *srcstringbuffer;
4447 if (dststringbuffer->max_strings)
4448 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4450 for (i = 0;i < dststringbuffer->num_strings;i++)
4452 if (srcstringbuffer->strings[i])
4455 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4456 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4457 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4463 ========================
4465 sort buffer by beginnings of strings (cmplength defaults it's length)
4466 "backward == TRUE" means that sorting goes upside-down
4467 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4468 ========================
4470 void VM_buf_sort (void)
4472 prvm_stringbuffer_t *stringbuffer;
4473 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4475 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4478 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4481 if(stringbuffer->num_strings <= 0)
4483 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4486 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4487 if(stringbuffers_sortlength <= 0)
4488 stringbuffers_sortlength = 0x7FFFFFFF;
4490 if(!PRVM_G_FLOAT(OFS_PARM2))
4491 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4493 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4495 BufStr_Shrink(stringbuffer);
4499 ========================
4501 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4502 string buf_implode(float bufhandle, string glue) = #465;
4503 ========================
4505 void VM_buf_implode (void)
4507 prvm_stringbuffer_t *stringbuffer;
4508 char k[VM_STRINGTEMP_LENGTH];
4512 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4514 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4515 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4518 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4521 if(!stringbuffer->num_strings)
4523 sep = PRVM_G_STRING(OFS_PARM1);
4525 for(l = i = 0;i < stringbuffer->num_strings;i++)
4527 if(stringbuffer->strings[i])
4529 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4530 if (l >= sizeof(k) - 1)
4532 strlcat(k, sep, sizeof(k));
4533 strlcat(k, stringbuffer->strings[i], sizeof(k));
4536 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4540 ========================
4542 get a string from buffer, returns tempstring, dont str_unzone it!
4543 string bufstr_get(float bufhandle, float string_index) = #465;
4544 ========================
4546 void VM_bufstr_get (void)
4548 prvm_stringbuffer_t *stringbuffer;
4550 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4552 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4553 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4556 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4559 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4562 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4565 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4566 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4570 ========================
4572 copies a string into selected slot of buffer
4573 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4574 ========================
4576 void VM_bufstr_set (void)
4579 prvm_stringbuffer_t *stringbuffer;
4582 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4584 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4587 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4590 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4591 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4593 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4597 BufStr_Expand(stringbuffer, strindex);
4598 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4600 if(stringbuffer->strings[strindex])
4601 Mem_Free(stringbuffer->strings[strindex]);
4602 stringbuffer->strings[strindex] = NULL;
4604 news = PRVM_G_STRING(OFS_PARM2);
4605 if (news && news[0])
4607 size_t alloclen = strlen(news) + 1;
4608 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4609 memcpy(stringbuffer->strings[strindex], news, alloclen);
4612 BufStr_Shrink(stringbuffer);
4616 ========================
4618 adds string to buffer in first free slot and returns its index
4619 "order == TRUE" means that string will be added after last "full" slot
4620 float bufstr_add(float bufhandle, string str, float order) = #467;
4621 ========================
4623 void VM_bufstr_add (void)
4625 int order, strindex;
4626 prvm_stringbuffer_t *stringbuffer;
4630 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4632 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4633 PRVM_G_FLOAT(OFS_RETURN) = -1;
4636 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4639 string = PRVM_G_STRING(OFS_PARM1);
4640 if(!string || !string[0])
4642 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);
4645 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4647 strindex = stringbuffer->num_strings;
4649 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4650 if (stringbuffer->strings[strindex] == NULL)
4653 BufStr_Expand(stringbuffer, strindex);
4655 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4656 alloclen = strlen(string) + 1;
4657 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4658 memcpy(stringbuffer->strings[strindex], string, alloclen);
4660 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4664 ========================
4666 delete string from buffer
4667 void bufstr_free(float bufhandle, float string_index) = #468;
4668 ========================
4670 void VM_bufstr_free (void)
4673 prvm_stringbuffer_t *stringbuffer;
4674 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4676 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4679 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4682 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4685 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4689 if (i < stringbuffer->num_strings)
4691 if(stringbuffer->strings[i])
4692 Mem_Free(stringbuffer->strings[i]);
4693 stringbuffer->strings[i] = NULL;
4696 BufStr_Shrink(stringbuffer);
4705 void VM_buf_cvarlist(void)
4708 const char *partial, *antipartial;
4709 size_t len, antilen;
4711 qboolean ispattern, antiispattern;
4713 prvm_stringbuffer_t *stringbuffer;
4714 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4716 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4719 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4723 partial = PRVM_G_STRING(OFS_PARM1);
4727 len = strlen(partial);
4730 antipartial = PRVM_G_STRING(OFS_PARM2);
4736 antilen = strlen(antipartial);
4738 for (n = 0;n < stringbuffer->num_strings;n++)
4739 if (stringbuffer->strings[n])
4740 Mem_Free(stringbuffer->strings[n]);
4741 if (stringbuffer->strings)
4742 Mem_Free(stringbuffer->strings);
4743 stringbuffer->strings = NULL;
4745 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4746 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4749 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4751 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4754 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4760 stringbuffer->max_strings = stringbuffer->num_strings = n;
4761 if (stringbuffer->max_strings)
4762 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4765 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4767 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4770 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4773 alloclen = strlen(cvar->name) + 1;
4774 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4775 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4790 This was a major timewaster in progs, so it was converted to C
4793 void VM_changeyaw (void)
4796 float ideal, current, move, speed;
4798 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4799 // parameters because they are the parameters to SV_MoveToGoal, not this
4800 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4802 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4803 if (ent == prog->edicts)
4805 VM_Warning("changeyaw: can not modify world entity\n");
4808 if (ent->priv.server->free)
4810 VM_Warning("changeyaw: can not modify free entity\n");
4813 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4815 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4818 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4819 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4820 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4822 if (current == ideal)
4824 move = ideal - current;
4825 if (ideal > current)
4846 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4854 void VM_changepitch (void)
4857 float ideal, current, move, speed;
4859 VM_SAFEPARMCOUNT(1, VM_changepitch);
4861 ent = PRVM_G_EDICT(OFS_PARM0);
4862 if (ent == prog->edicts)
4864 VM_Warning("changepitch: can not modify world entity\n");
4867 if (ent->priv.server->free)
4869 VM_Warning("changepitch: can not modify free entity\n");
4872 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4874 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4877 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4878 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4879 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4881 if (current == ideal)
4883 move = ideal - current;
4884 if (ideal > current)
4905 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4909 void VM_uncolorstring (void)
4911 char szNewString[VM_STRINGTEMP_LENGTH];
4912 const char *szString;
4915 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4916 szString = PRVM_G_STRING(OFS_PARM0);
4917 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
4918 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
4922 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4923 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4924 void VM_strstrofs (void)
4926 const char *instr, *match;
4928 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4929 instr = PRVM_G_STRING(OFS_PARM0);
4930 match = PRVM_G_STRING(OFS_PARM1);
4931 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
4933 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4935 PRVM_G_FLOAT(OFS_RETURN) = -1;
4939 match = strstr(instr+firstofs, match);
4941 PRVM_G_FLOAT(OFS_RETURN) = -1;
4943 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4946 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4947 void VM_str2chr (void)
4950 VM_SAFEPARMCOUNT(2, VM_str2chr);
4951 s = PRVM_G_STRING(OFS_PARM0);
4952 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4953 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4955 PRVM_G_FLOAT(OFS_RETURN) = 0;
4958 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4959 void VM_chr2str (void)
4963 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4964 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4965 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4967 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4970 static int chrconv_number(int i, int base, int conv)
4995 static int chrconv_punct(int i, int base, int conv)
5013 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5015 //convert case and colour seperatly...
5032 baset = 128*((charnum&1) == (convt-5));
5048 return i + basec + baset;
5050 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5051 //bulk convert a string. change case or colouring.
5052 void VM_strconv (void)
5054 int ccase, redalpha, rednum, len, i;
5055 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5056 unsigned char *result = resbuf;
5058 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5060 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5061 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5062 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5063 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
5064 len = strlen((char *) resbuf);
5066 for (i = 0; i < len; i++, result++) //should this be done backwards?
5068 if (*result >= '0' && *result <= '9') //normal numbers...
5069 *result = chrconv_number(*result, '0', rednum);
5070 else if (*result >= '0'+128 && *result <= '9'+128)
5071 *result = chrconv_number(*result, '0'+128, rednum);
5072 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5073 *result = chrconv_number(*result, '0'+128-30, rednum);
5074 else if (*result >= '0'-30 && *result <= '9'-30)
5075 *result = chrconv_number(*result, '0'-30, rednum);
5077 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5078 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5079 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5080 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5081 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5082 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5083 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5084 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5086 else if ((*result & 127) < 16 || !redalpha) //special chars..
5088 else if (*result < 128)
5089 *result = chrconv_punct(*result, 0, redalpha);
5091 *result = chrconv_punct(*result, 128, redalpha);
5095 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5098 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5099 void VM_strpad (void)
5101 char src[VM_STRINGTEMP_LENGTH];
5102 char destbuf[VM_STRINGTEMP_LENGTH];
5104 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5105 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5106 VM_VarString(1, src, sizeof(src));
5108 // note: < 0 = left padding, > 0 = right padding,
5109 // this is reverse logic of printf!
5110 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5112 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5115 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5116 //uses qw style \key\value strings
5117 void VM_infoadd (void)
5119 const char *info, *key;
5120 char value[VM_STRINGTEMP_LENGTH];
5121 char temp[VM_STRINGTEMP_LENGTH];
5123 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5124 info = PRVM_G_STRING(OFS_PARM0);
5125 key = PRVM_G_STRING(OFS_PARM1);
5126 VM_VarString(2, value, sizeof(value));
5128 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5130 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5132 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5135 // #227 string(string info, string key) infoget (FTE_STRINGS)
5136 //uses qw style \key\value strings
5137 void VM_infoget (void)
5141 char value[VM_STRINGTEMP_LENGTH];
5143 VM_SAFEPARMCOUNT(2, VM_infoget);
5144 info = PRVM_G_STRING(OFS_PARM0);
5145 key = PRVM_G_STRING(OFS_PARM1);
5147 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5149 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5152 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5153 // also float(string s1, string s2) strcmp (FRIK_FILE)
5154 void VM_strncmp (void)
5156 const char *s1, *s2;
5157 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5158 s1 = PRVM_G_STRING(OFS_PARM0);
5159 s2 = PRVM_G_STRING(OFS_PARM1);
5162 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5166 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5170 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5171 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5172 void VM_strncasecmp (void)
5174 const char *s1, *s2;
5175 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5176 s1 = PRVM_G_STRING(OFS_PARM0);
5177 s2 = PRVM_G_STRING(OFS_PARM1);
5180 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5184 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5188 // #494 float(float caseinsensitive, string s, ...) crc16
5192 static char s[VM_STRINGTEMP_LENGTH];
5193 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5194 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5195 VM_VarString(1, s, sizeof(s));
5196 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5199 void VM_wasfreed (void)
5201 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5202 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5205 void VM_SetTraceGlobals(const trace_t *trace)
5208 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5209 val->_float = trace->allsolid;
5210 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5211 val->_float = trace->startsolid;
5212 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5213 val->_float = trace->fraction;
5214 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5215 val->_float = trace->inwater;
5216 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5217 val->_float = trace->inopen;
5218 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5219 VectorCopy(trace->endpos, val->vector);
5220 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5221 VectorCopy(trace->plane.normal, val->vector);
5222 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5223 val->_float = trace->plane.dist;
5224 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5225 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5226 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5227 val->_float = trace->startsupercontents;
5228 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5229 val->_float = trace->hitsupercontents;
5230 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5231 val->_float = trace->hitq3surfaceflags;
5232 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5233 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5236 void VM_ClearTraceGlobals(void)
5238 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5240 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5242 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5244 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5246 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5248 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5250 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5251 VectorClear(val->vector);
5252 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5253 VectorClear(val->vector);
5254 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5256 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5257 val->edict = PRVM_EDICT_TO_PROG(prog->edicts);
5258 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5260 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5262 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5264 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5270 void VM_Cmd_Init(void)
5272 // only init the stuff for the current prog
5276 // VM_BufStr_Init();
5279 void VM_Cmd_Reset(void)
5281 CL_PurgeOwner( MENUOWNER );
5283 VM_Files_CloseAll();
5285 // VM_BufStr_ShutDown();
5288 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5289 // does URI escaping on a string (replace evil stuff by %AB escapes)
5290 void VM_uri_escape (void)
5292 char src[VM_STRINGTEMP_LENGTH];
5293 char dest[VM_STRINGTEMP_LENGTH];
5295 static const char *hex = "0123456789ABCDEF";
5297 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5298 VM_VarString(0, src, sizeof(src));
5300 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5302 if((*p >= 'A' && *p <= 'Z')
5303 || (*p >= 'a' && *p <= 'z')
5304 || (*p >= '0' && *p <= '9')
5305 || (*p == '-') || (*p == '_') || (*p == '.')
5306 || (*p == '!') || (*p == '~') || (*p == '*')
5307 || (*p == '\'') || (*p == '(') || (*p == ')'))
5312 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5313 *q++ = hex[ *(unsigned char *)p & 0xF];
5318 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5321 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5322 // does URI unescaping on a string (get back the evil stuff)
5323 void VM_uri_unescape (void)
5325 char src[VM_STRINGTEMP_LENGTH];
5326 char dest[VM_STRINGTEMP_LENGTH];
5330 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5331 VM_VarString(0, src, sizeof(src));
5333 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5337 if(p[1] >= '0' && p[1] <= '9')
5339 else if(p[1] >= 'a' && p[1] <= 'f')
5340 hi = p[1] - 'a' + 10;
5341 else if(p[1] >= 'A' && p[1] <= 'F')
5342 hi = p[1] - 'A' + 10;
5345 if(p[2] >= '0' && p[2] <= '9')
5347 else if(p[2] >= 'a' && p[2] <= 'f')
5348 lo = p[2] - 'a' + 10;
5349 else if(p[2] >= 'A' && p[2] <= 'F')
5350 lo = p[2] - 'A' + 10;
5353 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5354 *q++ = (char) (hi * 0x10 + lo);
5365 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5368 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5369 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5370 void VM_whichpack (void)
5372 const char *fn, *pack;
5374 VM_SAFEPARMCOUNT(1, VM_whichpack);
5375 fn = PRVM_G_STRING(OFS_PARM0);
5376 pack = FS_WhichPack(fn);
5378 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5386 char buffer[MAX_INPUTLINE];
5390 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5392 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5394 if(!PRVM_ProgLoaded(handle->prognr))
5396 // curl reply came too late... so just drop it
5401 PRVM_SetProg(handle->prognr);
5403 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5405 if(length_received >= sizeof(handle->buffer))
5406 length_received = sizeof(handle->buffer) - 1;
5407 handle->buffer[length_received] = 0;
5409 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5410 PRVM_G_FLOAT(OFS_PARM1) = status;
5411 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5412 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5419 // 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
5420 // 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
5421 void VM_uri_get (void)
5426 uri_to_prog_t *handle;
5428 if(!prog->funcoffsets.URI_Get_Callback)
5429 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5431 VM_SAFEPARMCOUNT(2, VM_uri_get);
5433 url = PRVM_G_STRING(OFS_PARM0);
5434 id = PRVM_G_FLOAT(OFS_PARM1);
5435 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!
5437 handle->prognr = PRVM_GetProgNr();
5438 handle->starttime = prog->starttime;
5440 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5443 PRVM_G_INT(OFS_RETURN) = 1;
5448 PRVM_G_INT(OFS_RETURN) = 0;
5452 void VM_netaddress_resolve (void)
5455 char normalized[128];
5457 lhnetaddress_t addr;
5459 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5461 ip = PRVM_G_STRING(OFS_PARM0);
5464 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5466 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5467 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5469 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5472 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5473 void VM_CL_getextresponse (void)
5475 VM_SAFEPARMCOUNT(0,VM_argv);
5477 if (cl_net_extresponse_count <= 0)
5478 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5482 --cl_net_extresponse_count;
5483 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5484 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(cl_net_extresponse[first]);
5488 void VM_SV_getextresponse (void)
5490 VM_SAFEPARMCOUNT(0,VM_argv);
5492 if (sv_net_extresponse_count <= 0)
5493 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5497 --sv_net_extresponse_count;
5498 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5499 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(sv_net_extresponse[first]);
5507 callfunction(...,string function_name)
5511 mfunction_t *PRVM_ED_FindFunction (const char *name);
5512 void VM_callfunction(void)
5517 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
5519 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
5521 VM_CheckEmptyString(s);
5523 func = PRVM_ED_FindFunction(s);
5526 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
5527 else if (func->first_statement < 0)
5529 // negative statements are built in functions
5530 int builtinnumber = -func->first_statement;
5531 prog->xfunction->builtinsprofile++;
5532 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
5533 prog->builtins[builtinnumber]();
5535 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
5537 else if(func - prog->functions > 0)
5540 PRVM_ExecuteProgram(func - prog->functions,"");
5549 float isfunction(string function_name)
5552 mfunction_t *PRVM_ED_FindFunction (const char *name);
5553 void VM_isfunction(void)
5558 VM_SAFEPARMCOUNT(1, VM_isfunction);
5560 s = PRVM_G_STRING(OFS_PARM0);
5562 VM_CheckEmptyString(s);
5564 func = PRVM_ED_FindFunction(s);
5567 PRVM_G_FLOAT(OFS_RETURN) = false;
5569 PRVM_G_FLOAT(OFS_RETURN) = true;