2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
17 extern cvar_t prvm_backtraceforwarnings;
19 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
20 void VM_Warning(const char *fmt, ...)
23 char msg[MAX_INPUTLINE];
24 static double recursive = -1;
27 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
32 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
33 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
42 //============================================================================
45 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
46 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
47 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
48 // TODO: will this war ever end? [2007-01-23 LordHavoc]
50 void VM_CheckEmptyString (const char *s)
52 if (ISWHITESPACE(s[0]))
53 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
56 void VM_GenerateFrameGroupBlend(framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
58 // self.frame is the interpolation target (new frame)
59 // self.frame1time is the animation base time for the interpolation target
60 // self.frame2 is the interpolation start (previous frame)
61 // self.frame2time is the animation base time for the interpolation start
62 // self.lerpfrac is the interpolation strength for self.frame2
63 // self.lerpfrac3 is the interpolation strength for self.frame3
64 // self.lerpfrac4 is the interpolation strength for self.frame4
65 // pitch angle on a player model where the animator set up 5 sets of
66 // animations and the csqc simply lerps between sets)
67 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
68 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
69 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
70 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
71 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
72 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
73 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
74 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
75 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
76 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
77 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
78 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
79 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
82 // LordHavoc: quite tempting to break apart this function to reuse the
83 // duplicated code, but I suspect it is better for performance
85 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model)
87 int sub2, numframes, f, i, k;
88 int isfirstframegroup = true;
90 double sublerp, lerp, d;
91 const animscene_t *scene;
92 const framegroupblend_t *g;
93 frameblend_t *blend = frameblend;
95 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
97 if (!model || !model->surfmesh.isanimated)
103 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
104 numframes = model->numframes;
105 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
108 if ((unsigned int)f >= (unsigned int)numframes)
110 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
118 if (isfirstframegroup)
121 isfirstframegroup = false;
126 if (model->animscenes)
128 scene = model->animscenes + f;
129 f = scene->firstframe;
130 if (scene->framecount > 1)
132 // this code path is only used on .zym models and torches
133 sublerp = scene->framerate * (cl.time - g->start);
134 f = (int) floor(sublerp);
137 if (sublerp < (1.0 / 65536.0f))
139 if (sublerp > (65535.0f / 65536.0f))
145 f = (f % scene->framecount);
146 sub2 = (sub2 % scene->framecount);
148 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
149 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
151 // two framelerps produced from one animation
154 for (i = 0;i < MAX_FRAMEBLENDS;i++)
156 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
158 blend[i].subframe = sub2;
164 d = (1 - sublerp) * lerp;
169 for (i = 0;i < MAX_FRAMEBLENDS;i++)
171 if (blend[i].lerp <= 0 || blend[i].subframe == f)
173 blend[i].subframe = f;
182 void VM_UpdateEdictSkeleton(prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
184 if (ed->priv.server->skeleton.model != edmodel)
186 VM_RemoveEdictSkeleton(ed);
187 ed->priv.server->skeleton.model = edmodel;
189 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
191 if(ed->priv.server->skeleton.relativetransforms)
192 Mem_Free(ed->priv.server->skeleton.relativetransforms);
193 ed->priv.server->skeleton.relativetransforms = NULL;
198 int skeletonindex = -1;
199 skeleton_t *skeleton;
200 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
201 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
203 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
204 if (!ed->priv.server->skeleton.relativetransforms)
205 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
206 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
210 if(ed->priv.server->skeleton.relativetransforms)
211 Mem_Free(ed->priv.server->skeleton.relativetransforms);
212 ed->priv.server->skeleton.relativetransforms = NULL;
217 void VM_RemoveEdictSkeleton(prvm_edict_t *ed)
219 if (ed->priv.server->skeleton.relativetransforms)
220 Mem_Free(ed->priv.server->skeleton.relativetransforms);
221 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
227 //============================================================================
230 void VM_VarString(int first, char *out, int outlength)
236 outend = out + outlength - 1;
237 for (i = first;i < prog->argc && out < outend;i++)
239 s = PRVM_G_STRING((OFS_PARM0+i*3));
240 while (out < outend && *s)
250 returns true if the extension is supported by the server
252 checkextension(extensionname)
256 // kind of helper function
257 static qboolean checkextension(const char *name)
260 const char *e, *start;
261 len = (int)strlen(name);
263 for (e = prog->extensionstring;*e;e++)
270 while (*e && *e != ' ')
272 if ((e - start) == len && !strncasecmp(start, name, len))
274 // special sheck for ODE
275 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
278 return ode_dll ? true : false;
284 // special sheck for d0_blind_id
285 if (!strcasecmp("DP_CRYPTO", name))
286 return Crypto_Available();
287 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
288 return Crypto_Available();
296 void VM_checkextension (void)
298 VM_SAFEPARMCOUNT(1,VM_checkextension);
300 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
307 This is a TERMINAL error, which will kill off the entire prog.
316 char string[VM_STRINGTEMP_LENGTH];
318 VM_VarString(0, string, sizeof(string));
319 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
320 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
321 PRVM_ED_Print(ed, NULL);
323 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);
330 Dumps out self, then an error message. The program is aborted and self is
331 removed, but the level can continue.
336 void VM_objerror (void)
339 char string[VM_STRINGTEMP_LENGTH];
341 VM_VarString(0, string, sizeof(string));
342 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
343 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
344 PRVM_ED_Print(ed, NULL);
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 VM_VarString(0, string, sizeof(string));
747 Con_DPrintf("%s", string);
749 Con_DPrintf("%s: %s", PRVM_NAME, string);
766 VM_SAFEPARMCOUNT(1, VM_ftos);
768 v = PRVM_G_FLOAT(OFS_PARM0);
770 if ((float)((int)v) == v)
771 dpsnprintf(s, sizeof(s), "%i", (int)v);
773 dpsnprintf(s, sizeof(s), "%f", v);
774 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
789 VM_SAFEPARMCOUNT(1,VM_fabs);
791 v = PRVM_G_FLOAT(OFS_PARM0);
792 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
807 VM_SAFEPARMCOUNT(1,VM_vtos);
809 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]);
810 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
825 VM_SAFEPARMCOUNT(1, VM_etos);
827 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
828 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
835 float stof(...[string])
840 char string[VM_STRINGTEMP_LENGTH];
841 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
842 VM_VarString(0, string, sizeof(string));
843 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
847 ========================
851 ========================
855 VM_SAFEPARMCOUNT(1, VM_itof);
856 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
860 ========================
863 entity ftoe(float num)
864 ========================
869 VM_SAFEPARMCOUNT(1, VM_ftoe);
871 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
872 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
873 ent = 0; // return world instead of a free or invalid entity
875 PRVM_G_INT(OFS_RETURN) = ent;
879 ========================
882 float etof(entity ent)
883 ========================
887 VM_SAFEPARMCOUNT(1, VM_etof);
888 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
895 string strftime(float uselocaltime, string[, string ...])
898 void VM_strftime(void)
907 char fmt[VM_STRINGTEMP_LENGTH];
908 char result[VM_STRINGTEMP_LENGTH];
909 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
910 VM_VarString(1, fmt, sizeof(fmt));
913 if (PRVM_G_FLOAT(OFS_PARM0))
914 tmresult = localtime_s(&tm, &t);
916 tmresult = gmtime_s(&tm, &t);
919 if (PRVM_G_FLOAT(OFS_PARM0))
926 PRVM_G_INT(OFS_RETURN) = 0;
930 strftime(result, sizeof(result), fmt, &tm);
932 strftime(result, sizeof(result), fmt, tm);
934 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
948 VM_SAFEPARMCOUNT(0, VM_spawn);
949 prog->xfunction->builtinsprofile += 20;
950 ed = PRVM_ED_Alloc();
962 void VM_remove (void)
965 prog->xfunction->builtinsprofile += 20;
967 VM_SAFEPARMCOUNT(1, VM_remove);
969 ed = PRVM_G_EDICT(OFS_PARM0);
970 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
972 if (developer.integer > 0)
973 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
975 else if( ed->priv.required->free )
977 if (developer.integer > 0)
978 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
988 entity find(entity start, .string field, string match)
999 VM_SAFEPARMCOUNT(3,VM_find);
1001 e = PRVM_G_EDICTNUM(OFS_PARM0);
1002 f = PRVM_G_INT(OFS_PARM1);
1003 s = PRVM_G_STRING(OFS_PARM2);
1005 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1006 // expects it to find all the monsters, so we must be careful to support
1009 for (e++ ; e < prog->num_edicts ; e++)
1011 prog->xfunction->builtinsprofile++;
1012 ed = PRVM_EDICT_NUM(e);
1013 if (ed->priv.required->free)
1015 t = PRVM_E_STRING(ed,f);
1020 VM_RETURN_EDICT(ed);
1025 VM_RETURN_EDICT(prog->edicts);
1032 entity findfloat(entity start, .float field, float match)
1033 entity findentity(entity start, .entity field, entity match)
1036 // LordHavoc: added this for searching float, int, and entity reference fields
1037 void VM_findfloat (void)
1044 VM_SAFEPARMCOUNT(3,VM_findfloat);
1046 e = PRVM_G_EDICTNUM(OFS_PARM0);
1047 f = PRVM_G_INT(OFS_PARM1);
1048 s = PRVM_G_FLOAT(OFS_PARM2);
1050 for (e++ ; e < prog->num_edicts ; e++)
1052 prog->xfunction->builtinsprofile++;
1053 ed = PRVM_EDICT_NUM(e);
1054 if (ed->priv.required->free)
1056 if (PRVM_E_FLOAT(ed,f) == s)
1058 VM_RETURN_EDICT(ed);
1063 VM_RETURN_EDICT(prog->edicts);
1070 entity findchain(.string field, string match)
1073 // chained search for strings in entity fields
1074 // entity(.string field, string match) findchain = #402;
1075 void VM_findchain (void)
1080 prvm_edict_t *ent, *chain;
1083 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1086 chainfield = PRVM_G_INT(OFS_PARM2);
1088 chainfield = prog->fieldoffsets.chain;
1090 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1092 chain = prog->edicts;
1094 f = PRVM_G_INT(OFS_PARM0);
1095 s = PRVM_G_STRING(OFS_PARM1);
1097 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1098 // expects it to find all the monsters, so we must be careful to support
1101 ent = PRVM_NEXT_EDICT(prog->edicts);
1102 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1104 prog->xfunction->builtinsprofile++;
1105 if (ent->priv.required->free)
1107 t = PRVM_E_STRING(ent,f);
1113 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1117 VM_RETURN_EDICT(chain);
1124 entity findchainfloat(.string field, float match)
1125 entity findchainentity(.string field, entity match)
1128 // LordHavoc: chained search for float, int, and entity reference fields
1129 // entity(.string field, float match) findchainfloat = #403;
1130 void VM_findchainfloat (void)
1135 prvm_edict_t *ent, *chain;
1138 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1141 chainfield = PRVM_G_INT(OFS_PARM2);
1143 chainfield = prog->fieldoffsets.chain;
1145 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1147 chain = (prvm_edict_t *)prog->edicts;
1149 f = PRVM_G_INT(OFS_PARM0);
1150 s = PRVM_G_FLOAT(OFS_PARM1);
1152 ent = PRVM_NEXT_EDICT(prog->edicts);
1153 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1155 prog->xfunction->builtinsprofile++;
1156 if (ent->priv.required->free)
1158 if (PRVM_E_FLOAT(ent,f) != s)
1161 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1165 VM_RETURN_EDICT(chain);
1169 ========================
1172 entity findflags(entity start, .float field, float match)
1173 ========================
1175 // LordHavoc: search for flags in float fields
1176 void VM_findflags (void)
1183 VM_SAFEPARMCOUNT(3, VM_findflags);
1186 e = PRVM_G_EDICTNUM(OFS_PARM0);
1187 f = PRVM_G_INT(OFS_PARM1);
1188 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1190 for (e++ ; e < prog->num_edicts ; e++)
1192 prog->xfunction->builtinsprofile++;
1193 ed = PRVM_EDICT_NUM(e);
1194 if (ed->priv.required->free)
1196 if (!PRVM_E_FLOAT(ed,f))
1198 if ((int)PRVM_E_FLOAT(ed,f) & s)
1200 VM_RETURN_EDICT(ed);
1205 VM_RETURN_EDICT(prog->edicts);
1209 ========================
1212 entity findchainflags(.float field, float match)
1213 ========================
1215 // LordHavoc: chained search for flags in float fields
1216 void VM_findchainflags (void)
1221 prvm_edict_t *ent, *chain;
1224 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1227 chainfield = PRVM_G_INT(OFS_PARM2);
1229 chainfield = prog->fieldoffsets.chain;
1231 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1233 chain = (prvm_edict_t *)prog->edicts;
1235 f = PRVM_G_INT(OFS_PARM0);
1236 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1238 ent = PRVM_NEXT_EDICT(prog->edicts);
1239 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1241 prog->xfunction->builtinsprofile++;
1242 if (ent->priv.required->free)
1244 if (!PRVM_E_FLOAT(ent,f))
1246 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1249 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1253 VM_RETURN_EDICT(chain);
1260 string precache_sound (string sample)
1263 void VM_precache_sound (void)
1267 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1269 s = PRVM_G_STRING(OFS_PARM0);
1270 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1271 //VM_CheckEmptyString(s);
1273 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1275 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1284 returns the same string as output
1286 does nothing, only used by qcc to build .pak archives
1289 void VM_precache_file (void)
1291 VM_SAFEPARMCOUNT(1,VM_precache_file);
1292 // precache_file is only used to copy files with qcc, it does nothing
1293 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1303 void VM_coredump (void)
1305 VM_SAFEPARMCOUNT(0,VM_coredump);
1307 Cbuf_AddText("prvm_edicts ");
1308 Cbuf_AddText(PRVM_NAME);
1319 void PRVM_StackTrace(void);
1320 void VM_stackdump (void)
1322 VM_SAFEPARMCOUNT(0, VM_stackdump);
1337 VM_SAFEPARMCOUNT(0, VM_crash);
1339 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1349 void VM_traceon (void)
1351 VM_SAFEPARMCOUNT(0,VM_traceon);
1363 void VM_traceoff (void)
1365 VM_SAFEPARMCOUNT(0,VM_traceoff);
1367 prog->trace = false;
1377 void VM_eprint (void)
1379 VM_SAFEPARMCOUNT(1,VM_eprint);
1381 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1394 VM_SAFEPARMCOUNT(1,VM_rint);
1396 f = PRVM_G_FLOAT(OFS_PARM0);
1398 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1400 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1410 void VM_floor (void)
1412 VM_SAFEPARMCOUNT(1,VM_floor);
1414 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1426 VM_SAFEPARMCOUNT(1,VM_ceil);
1428 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1436 entity nextent(entity)
1439 void VM_nextent (void)
1444 VM_SAFEPARMCOUNT(1, VM_nextent);
1446 i = PRVM_G_EDICTNUM(OFS_PARM0);
1449 prog->xfunction->builtinsprofile++;
1451 if (i == prog->num_edicts)
1453 VM_RETURN_EDICT(prog->edicts);
1456 ent = PRVM_EDICT_NUM(i);
1457 if (!ent->priv.required->free)
1459 VM_RETURN_EDICT(ent);
1465 //=============================================================================
1472 changelevel(string map)
1475 void VM_changelevel (void)
1477 VM_SAFEPARMCOUNT(1, VM_changelevel);
1481 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1485 // make sure we don't issue two changelevels
1486 if (svs.changelevel_issued)
1488 svs.changelevel_issued = true;
1490 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1502 VM_SAFEPARMCOUNT(1,VM_sin);
1503 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1514 VM_SAFEPARMCOUNT(1,VM_cos);
1515 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1527 VM_SAFEPARMCOUNT(1,VM_sqrt);
1528 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1540 VM_SAFEPARMCOUNT(1,VM_asin);
1541 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1552 VM_SAFEPARMCOUNT(1,VM_acos);
1553 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1564 VM_SAFEPARMCOUNT(1,VM_atan);
1565 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1571 float atan2(float,float)
1574 void VM_atan2 (void)
1576 VM_SAFEPARMCOUNT(2,VM_atan2);
1577 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1588 VM_SAFEPARMCOUNT(1,VM_tan);
1589 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1596 Returns a vector of length < 1 and > 0
1601 void VM_randomvec (void)
1606 VM_SAFEPARMCOUNT(0, VM_randomvec);
1611 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1612 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1613 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1615 while (DotProduct(temp, temp) >= 1);
1616 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1619 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1620 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1621 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1622 // length returned always > 0
1623 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1624 VectorScale(temp,length, temp);*/
1625 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1628 //=============================================================================
1634 float registercvar (string name, string value[, float flags])
1637 void VM_registercvar (void)
1639 const char *name, *value;
1642 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1644 name = PRVM_G_STRING(OFS_PARM0);
1645 value = PRVM_G_STRING(OFS_PARM1);
1646 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1647 PRVM_G_FLOAT(OFS_RETURN) = 0;
1649 if(flags > CVAR_MAXFLAGSVAL)
1652 // first check to see if it has already been defined
1653 if (Cvar_FindVar (name))
1656 // check for overlap with a command
1657 if (Cmd_Exists (name))
1659 VM_Warning("VM_registercvar: %s is a command\n", name);
1663 Cvar_Get(name, value, flags, NULL);
1665 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1673 returns the minimum of two supplied floats
1675 float min(float a, float b, ...[float])
1680 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1681 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1682 if (prog->argc >= 3)
1685 float f = PRVM_G_FLOAT(OFS_PARM0);
1686 for (i = 1;i < prog->argc;i++)
1687 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1688 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1689 PRVM_G_FLOAT(OFS_RETURN) = f;
1692 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1699 returns the maximum of two supplied floats
1701 float max(float a, float b, ...[float])
1706 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1707 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1708 if (prog->argc >= 3)
1711 float f = PRVM_G_FLOAT(OFS_PARM0);
1712 for (i = 1;i < prog->argc;i++)
1713 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1714 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1715 PRVM_G_FLOAT(OFS_RETURN) = f;
1718 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1725 returns number bounded by supplied range
1727 float bound(float min, float value, float max)
1730 void VM_bound (void)
1732 VM_SAFEPARMCOUNT(3,VM_bound);
1733 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1740 returns a raised to power b
1742 float pow(float a, float b)
1747 VM_SAFEPARMCOUNT(2,VM_pow);
1748 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1753 VM_SAFEPARMCOUNT(1,VM_log);
1754 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1757 void VM_Files_Init(void)
1760 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1761 prog->openfiles[i] = NULL;
1764 void VM_Files_CloseAll(void)
1767 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1769 if (prog->openfiles[i])
1770 FS_Close(prog->openfiles[i]);
1771 prog->openfiles[i] = NULL;
1775 static qfile_t *VM_GetFileHandle( int index )
1777 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1779 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1782 if (prog->openfiles[index] == NULL)
1784 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1787 return prog->openfiles[index];
1794 float fopen(string filename, float mode)
1797 // float(string filename, float mode) fopen = #110;
1798 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1799 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1803 const char *modestring, *filename;
1805 VM_SAFEPARMCOUNT(2,VM_fopen);
1807 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1808 if (prog->openfiles[filenum] == NULL)
1810 if (filenum >= PRVM_MAX_OPENFILES)
1812 PRVM_G_FLOAT(OFS_RETURN) = -2;
1813 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1816 filename = PRVM_G_STRING(OFS_PARM0);
1817 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1820 case 0: // FILE_READ
1822 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1823 if (prog->openfiles[filenum] == NULL)
1824 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1826 case 1: // FILE_APPEND
1828 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1830 case 2: // FILE_WRITE
1832 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1835 PRVM_G_FLOAT(OFS_RETURN) = -3;
1836 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1840 if (prog->openfiles[filenum] == NULL)
1842 PRVM_G_FLOAT(OFS_RETURN) = -1;
1843 if (developer_extra.integer)
1844 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1848 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1849 if (developer_extra.integer)
1850 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1851 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1859 fclose(float fhandle)
1862 //void(float fhandle) fclose = #111; // closes a file
1863 void VM_fclose(void)
1867 VM_SAFEPARMCOUNT(1,VM_fclose);
1869 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1870 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1872 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1875 if (prog->openfiles[filenum] == NULL)
1877 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1880 FS_Close(prog->openfiles[filenum]);
1881 prog->openfiles[filenum] = NULL;
1882 if(prog->openfiles_origin[filenum])
1883 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1884 if (developer_extra.integer)
1885 Con_DPrintf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1892 string fgets(float fhandle)
1895 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1899 char string[VM_STRINGTEMP_LENGTH];
1902 VM_SAFEPARMCOUNT(1,VM_fgets);
1904 // set the return value regardless of any possible errors
1905 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1907 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1908 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1910 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1913 if (prog->openfiles[filenum] == NULL)
1915 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1921 c = FS_Getc(prog->openfiles[filenum]);
1922 if (c == '\r' || c == '\n' || c < 0)
1924 if (end < VM_STRINGTEMP_LENGTH - 1)
1928 // remove \n following \r
1931 c = FS_Getc(prog->openfiles[filenum]);
1933 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1935 if (developer_extra.integer)
1936 Con_DPrintf("fgets: %s: %s\n", PRVM_NAME, string);
1938 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1945 fputs(float fhandle, string s)
1948 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1952 char string[VM_STRINGTEMP_LENGTH];
1955 VM_SAFEPARMCOUNT(2,VM_fputs);
1957 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1958 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1960 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1963 if (prog->openfiles[filenum] == NULL)
1965 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1968 VM_VarString(1, string, sizeof(string));
1969 if ((stringlength = (int)strlen(string)))
1970 FS_Write(prog->openfiles[filenum], string, stringlength);
1971 if (developer_extra.integer)
1972 Con_DPrintf("fputs: %s: %s\n", PRVM_NAME, string);
1979 writetofile(float fhandle, entity ent)
1982 void VM_writetofile(void)
1987 VM_SAFEPARMCOUNT(2, VM_writetofile);
1989 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1992 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1996 ent = PRVM_G_EDICT(OFS_PARM1);
1997 if(ent->priv.required->free)
1999 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2003 PRVM_ED_Write (file, ent);
2006 // KrimZon - DP_QC_ENTITYDATA
2011 float() numentityfields
2012 Return the number of entity fields - NOT offsets
2015 void VM_numentityfields(void)
2017 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2020 // KrimZon - DP_QC_ENTITYDATA
2025 string(float fieldnum) entityfieldname
2026 Return name of the specified field as a string, or empty if the field is invalid (warning)
2029 void VM_entityfieldname(void)
2032 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2034 if (i < 0 || i >= prog->numfielddefs)
2036 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
2037 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2041 d = &prog->fielddefs[i];
2042 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2045 // KrimZon - DP_QC_ENTITYDATA
2050 float(float fieldnum) entityfieldtype
2053 void VM_entityfieldtype(void)
2056 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2058 if (i < 0 || i >= prog->numfielddefs)
2060 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
2061 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2065 d = &prog->fielddefs[i];
2066 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2069 // KrimZon - DP_QC_ENTITYDATA
2072 VM_getentityfieldstring
2074 string(float fieldnum, entity ent) getentityfieldstring
2077 void VM_getentityfieldstring(void)
2079 // put the data into a string
2084 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2086 if (i < 0 || i >= prog->numfielddefs)
2088 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2089 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2093 d = &prog->fielddefs[i];
2096 ent = PRVM_G_EDICT(OFS_PARM1);
2097 if(ent->priv.required->free)
2099 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2100 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2103 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2105 // if it's 0 or blank, return an empty string
2106 type = d->type & ~DEF_SAVEGLOBAL;
2107 for (j=0 ; j<prvm_type_size[type] ; j++)
2110 if (j == prvm_type_size[type])
2112 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2116 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
2119 // KrimZon - DP_QC_ENTITYDATA
2122 VM_putentityfieldstring
2124 float(float fieldnum, entity ent, string s) putentityfieldstring
2127 void VM_putentityfieldstring(void)
2131 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2133 if (i < 0 || i >= prog->numfielddefs)
2135 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2136 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2140 d = &prog->fielddefs[i];
2143 ent = PRVM_G_EDICT(OFS_PARM1);
2144 if(ent->priv.required->free)
2146 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2147 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2151 // parse the string into the value
2152 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2159 float strlen(string s)
2162 //float(string s) strlen = #114; // returns how many characters are in a string
2163 void VM_strlen(void)
2165 VM_SAFEPARMCOUNT(1,VM_strlen);
2167 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2168 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2171 // DRESK - Decolorized String
2176 string strdecolorize(string s)
2179 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2180 void VM_strdecolorize(void)
2182 char szNewString[VM_STRINGTEMP_LENGTH];
2183 const char *szString;
2186 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2187 szString = PRVM_G_STRING(OFS_PARM0);
2188 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2189 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2192 // DRESK - String Length (not counting color codes)
2197 float strlennocol(string s)
2200 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2201 // For example, ^2Dresk returns a length of 5
2202 void VM_strlennocol(void)
2204 const char *szString;
2207 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2209 szString = PRVM_G_STRING(OFS_PARM0);
2211 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2212 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2214 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2217 // DRESK - String to Uppercase and Lowercase
2222 string strtolower(string s)
2225 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2226 void VM_strtolower(void)
2228 char szNewString[VM_STRINGTEMP_LENGTH];
2229 const char *szString;
2232 VM_SAFEPARMCOUNT(1,VM_strtolower);
2233 szString = PRVM_G_STRING(OFS_PARM0);
2235 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2237 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2244 string strtoupper(string s)
2247 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2248 void VM_strtoupper(void)
2250 char szNewString[VM_STRINGTEMP_LENGTH];
2251 const char *szString;
2254 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2255 szString = PRVM_G_STRING(OFS_PARM0);
2257 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2259 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2266 string strcat(string,string,...[string])
2269 //string(string s1, string s2) strcat = #115;
2270 // concatenates two strings (for example "abc", "def" would return "abcdef")
2271 // and returns as a tempstring
2272 void VM_strcat(void)
2274 char s[VM_STRINGTEMP_LENGTH];
2275 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2277 VM_VarString(0, s, sizeof(s));
2278 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2285 string substring(string s, float start, float length)
2288 // string(string s, float start, float length) substring = #116;
2289 // returns a section of a string as a tempstring
2290 void VM_substring(void)
2293 int u_slength = 0, u_start;
2296 char string[VM_STRINGTEMP_LENGTH];
2298 VM_SAFEPARMCOUNT(3,VM_substring);
2301 s = PRVM_G_STRING(OFS_PARM0);
2302 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2303 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2304 slength = strlen(s);
2306 if (start < 0) // FTE_STRINGS feature
2308 start = bound(0, start, slength);
2310 if (length < 0) // FTE_STRINGS feature
2311 length += slength - start + 1;
2312 maxlen = min((int)sizeof(string) - 1, slength - start);
2313 length = bound(0, length, maxlen);
2315 memcpy(string, s + start, length);
2317 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2320 s = PRVM_G_STRING(OFS_PARM0);
2321 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2322 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2324 if (start < 0) // FTE_STRINGS feature
2326 u_slength = u8_strlen(s);
2328 start = bound(0, start, u_slength);
2331 if (length < 0) // FTE_STRINGS feature
2333 if (!u_slength) // it's not calculated when it's not needed above
2334 u_slength = u8_strlen(s);
2335 length += u_slength - start + 1;
2338 // positive start, positive length
2339 u_start = u8_byteofs(s, start, NULL);
2342 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2345 u_length = u8_bytelen(s + u_start, length);
2346 if (u_length >= sizeof(string)-1)
2347 u_length = sizeof(string)-1;
2349 memcpy(string, s + u_start, u_length);
2350 string[u_length] = 0;
2351 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2358 string(string search, string replace, string subject) strreplace = #484;
2361 // replaces all occurrences of search with replace in the string subject, and returns the result
2362 void VM_strreplace(void)
2365 const char *search, *replace, *subject;
2366 char string[VM_STRINGTEMP_LENGTH];
2367 int search_len, replace_len, subject_len;
2369 VM_SAFEPARMCOUNT(3,VM_strreplace);
2371 search = PRVM_G_STRING(OFS_PARM0);
2372 replace = PRVM_G_STRING(OFS_PARM1);
2373 subject = PRVM_G_STRING(OFS_PARM2);
2375 search_len = (int)strlen(search);
2376 replace_len = (int)strlen(replace);
2377 subject_len = (int)strlen(subject);
2380 for (i = 0; i <= subject_len - search_len; i++)
2382 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2383 if (subject[i+j] != search[j])
2385 if (j == search_len)
2387 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2388 // found it at offset 'i'
2389 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2390 string[si++] = replace[j];
2393 i += search_len - 1;
2397 // the above would subtract 1 from i... so we
2398 // don't do that, but instead output the next
2400 if (si < (int)sizeof(string) - 1)
2401 string[si++] = subject[i];
2406 // in THIS case, we know search_len > 0, thus i < subject_len
2408 if (si < (int)sizeof(string) - 1)
2409 string[si++] = subject[i];
2412 // remaining chars (these cannot match)
2413 for (; i < subject_len; i++)
2414 if (si < (int)sizeof(string) - 1)
2415 string[si++] = subject[i];
2418 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2425 string(string search, string replace, string subject) strireplace = #485;
2428 // case-insensitive version of strreplace
2429 void VM_strireplace(void)
2432 const char *search, *replace, *subject;
2433 char string[VM_STRINGTEMP_LENGTH];
2434 int search_len, replace_len, subject_len;
2436 VM_SAFEPARMCOUNT(3,VM_strreplace);
2438 search = PRVM_G_STRING(OFS_PARM0);
2439 replace = PRVM_G_STRING(OFS_PARM1);
2440 subject = PRVM_G_STRING(OFS_PARM2);
2442 search_len = (int)strlen(search);
2443 replace_len = (int)strlen(replace);
2444 subject_len = (int)strlen(subject);
2447 for (i = 0; i <= subject_len - search_len; i++)
2449 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2450 if (tolower(subject[i+j]) != tolower(search[j]))
2452 if (j == search_len)
2454 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2455 // found it at offset 'i'
2456 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2457 string[si++] = replace[j];
2460 i += search_len - 1;
2464 // the above would subtract 1 from i... so we
2465 // don't do that, but instead output the next
2467 if (si < (int)sizeof(string) - 1)
2468 string[si++] = subject[i];
2473 // in THIS case, we know search_len > 0, thus i < subject_len
2475 if (si < (int)sizeof(string) - 1)
2476 string[si++] = subject[i];
2479 // remaining chars (these cannot match)
2480 for (; i < subject_len; i++)
2481 if (si < (int)sizeof(string) - 1)
2482 string[si++] = subject[i];
2485 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2492 vector stov(string s)
2495 //vector(string s) stov = #117; // returns vector value from a string
2498 char string[VM_STRINGTEMP_LENGTH];
2500 VM_SAFEPARMCOUNT(1,VM_stov);
2502 VM_VarString(0, string, sizeof(string));
2503 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2510 string strzone(string s)
2513 //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)
2514 void VM_strzone(void)
2517 char string[VM_STRINGTEMP_LENGTH];
2520 VM_SAFEPARMCOUNT(1,VM_strzone);
2522 VM_VarString(0, string, sizeof(string));
2523 alloclen = strlen(string) + 1;
2524 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2525 memcpy(out, string, alloclen);
2535 //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!!!)
2536 void VM_strunzone(void)
2538 VM_SAFEPARMCOUNT(1,VM_strunzone);
2539 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2544 VM_command (used by client and menu)
2546 clientcommand(float client, string s) (for client and menu)
2549 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2550 //this function originally written by KrimZon, made shorter by LordHavoc
2551 void VM_clcommand (void)
2553 client_t *temp_client;
2556 VM_SAFEPARMCOUNT(2,VM_clcommand);
2558 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2559 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2561 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2565 temp_client = host_client;
2566 host_client = svs.clients + i;
2567 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2568 host_client = temp_client;
2576 float tokenize(string s)
2579 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2580 //this function originally written by KrimZon, made shorter by LordHavoc
2581 //20040203: rewritten by LordHavoc (no longer uses allocations)
2582 static int num_tokens = 0;
2583 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2584 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2585 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2586 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2587 void VM_tokenize (void)
2591 VM_SAFEPARMCOUNT(1,VM_tokenize);
2593 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2594 p = tokenize_string;
2599 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2602 // skip whitespace here to find token start pos
2603 while(*p && ISWHITESPACE(*p))
2606 tokens_startpos[num_tokens] = p - tokenize_string;
2607 if(!COM_ParseToken_VM_Tokenize(&p, false))
2609 tokens_endpos[num_tokens] = p - tokenize_string;
2610 tokens[num_tokens] = PRVM_SetTempString(com_token);
2614 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2617 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2618 void VM_tokenize_console (void)
2622 VM_SAFEPARMCOUNT(1,VM_tokenize);
2624 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2625 p = tokenize_string;
2630 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2633 // skip whitespace here to find token start pos
2634 while(*p && ISWHITESPACE(*p))
2637 tokens_startpos[num_tokens] = p - tokenize_string;
2638 if(!COM_ParseToken_Console(&p))
2640 tokens_endpos[num_tokens] = p - tokenize_string;
2641 tokens[num_tokens] = PRVM_SetTempString(com_token);
2645 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2650 VM_tokenizebyseparator
2652 float tokenizebyseparator(string s, string separator1, ...)
2655 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2656 //this function returns the token preceding each instance of a separator (of
2657 //which there can be multiple), and the text following the last separator
2658 //useful for parsing certain kinds of data like IP addresses
2660 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2661 //returns 4 and the tokens "10" "1" "2" "3".
2662 void VM_tokenizebyseparator (void)
2666 int separatorlen[7];
2667 const char *separators[7];
2670 char tokentext[MAX_INPUTLINE];
2672 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2674 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2675 p = tokenize_string;
2678 for (j = 1;j < prog->argc;j++)
2680 // skip any blank separator strings
2681 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2684 separators[numseparators] = s;
2685 separatorlen[numseparators] = strlen(s);
2692 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2694 token = tokentext + j;
2695 tokens_startpos[num_tokens] = p - tokenize_string;
2699 for (k = 0;k < numseparators;k++)
2701 if (!strncmp(p, separators[k], separatorlen[k]))
2703 p += separatorlen[k];
2707 if (k < numseparators)
2709 if (j < (int)sizeof(tokentext)-1)
2710 tokentext[j++] = *p;
2714 tokens_endpos[num_tokens] = p0 - tokenize_string;
2715 if (j >= (int)sizeof(tokentext))
2718 tokens[num_tokens++] = PRVM_SetTempString(token);
2723 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2726 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2727 //this function originally written by KrimZon, made shorter by LordHavoc
2732 VM_SAFEPARMCOUNT(1,VM_argv);
2734 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2737 token_num += num_tokens;
2739 if (token_num >= 0 && token_num < num_tokens)
2740 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2742 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2745 //float(float n) argv_start_index = #515; // returns the start index of a token
2746 void VM_argv_start_index (void)
2750 VM_SAFEPARMCOUNT(1,VM_argv);
2752 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2755 token_num += num_tokens;
2757 if (token_num >= 0 && token_num < num_tokens)
2758 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2760 PRVM_G_FLOAT(OFS_RETURN) = -1;
2763 //float(float n) argv_end_index = #516; // returns the end index of a token
2764 void VM_argv_end_index (void)
2768 VM_SAFEPARMCOUNT(1,VM_argv);
2770 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2773 token_num += num_tokens;
2775 if (token_num >= 0 && token_num < num_tokens)
2776 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2778 PRVM_G_FLOAT(OFS_RETURN) = -1;
2788 void VM_isserver(void)
2790 VM_SAFEPARMCOUNT(0,VM_serverstate);
2792 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2802 void VM_clientcount(void)
2804 VM_SAFEPARMCOUNT(0,VM_clientcount);
2806 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2816 void VM_clientstate(void)
2818 VM_SAFEPARMCOUNT(0,VM_clientstate);
2821 switch( cls.state ) {
2822 case ca_uninitialized:
2824 PRVM_G_FLOAT(OFS_RETURN) = 0;
2826 case ca_disconnected:
2827 PRVM_G_FLOAT(OFS_RETURN) = 1;
2830 PRVM_G_FLOAT(OFS_RETURN) = 2;
2833 // should never be reached!
2842 float getostype(void)
2844 */ // not used at the moment -> not included in the common list
2845 void VM_getostype(void)
2847 VM_SAFEPARMCOUNT(0,VM_getostype);
2852 OS_MAC - not supported
2856 PRVM_G_FLOAT(OFS_RETURN) = 0;
2857 #elif defined(MACOSX)
2858 PRVM_G_FLOAT(OFS_RETURN) = 2;
2860 PRVM_G_FLOAT(OFS_RETURN) = 1;
2871 extern double host_starttime;
2872 float CDAudio_GetPosition(void);
2873 void VM_gettime(void)
2877 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2881 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2885 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2888 case 0: // GETTIME_FRAMESTART
2889 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2891 case 1: // GETTIME_REALTIME
2892 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2894 case 2: // GETTIME_HIRES
2895 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2897 case 3: // GETTIME_UPTIME
2898 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2900 case 4: // GETTIME_CDTRACK
2901 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2904 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2905 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2915 float getsoundtime(void)
2919 void VM_getsoundtime (void)
2921 int entnum, entchannel, pnum;
2922 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2924 pnum = PRVM_GetProgNr();
2925 if (pnum == PRVM_MENUPROG)
2927 VM_Warning("VM_getsoundtime: %s: not supported on this progs\n", PRVM_NAME);
2928 PRVM_G_FLOAT(OFS_RETURN) = -1;
2931 entnum = ((pnum == PRVM_CLIENTPROG) ? MAX_EDICTS : 0) + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2932 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2933 entchannel = CHAN_USER2ENGINE(entchannel);
2934 if (!IS_CHAN(entchannel))
2935 VM_Warning("VM_getsoundtime: %s: bad channel %i\n", PRVM_NAME, entchannel);
2936 PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2943 string soundlength (string sample)
2946 void VM_soundlength (void)
2950 VM_SAFEPARMCOUNT(1, VM_soundlength);
2952 s = PRVM_G_STRING(OFS_PARM0);
2953 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2960 loadfromdata(string data)
2963 void VM_loadfromdata(void)
2965 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2967 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2971 ========================
2974 parseentitydata(entity ent, string data)
2975 ========================
2977 void VM_parseentitydata(void)
2982 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2984 // get edict and test it
2985 ent = PRVM_G_EDICT(OFS_PARM0);
2986 if (ent->priv.required->free)
2987 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2989 data = PRVM_G_STRING(OFS_PARM1);
2991 // parse the opening brace
2992 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2993 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2995 PRVM_ED_ParseEdict (data, ent);
3002 loadfromfile(string file)
3005 void VM_loadfromfile(void)
3007 const char *filename;
3010 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3012 filename = PRVM_G_STRING(OFS_PARM0);
3013 if (FS_CheckNastyPath(filename, false))
3015 PRVM_G_FLOAT(OFS_RETURN) = -4;
3016 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
3020 // not conform with VM_fopen
3021 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3023 PRVM_G_FLOAT(OFS_RETURN) = -1;
3025 PRVM_ED_LoadFromFile(data);
3036 float mod(float val, float m)
3039 void VM_modulo(void)
3042 VM_SAFEPARMCOUNT(2,VM_module);
3044 val = (int) PRVM_G_FLOAT(OFS_PARM0);
3045 m = (int) PRVM_G_FLOAT(OFS_PARM1);
3047 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3050 void VM_Search_Init(void)
3053 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3054 prog->opensearches[i] = NULL;
3057 void VM_Search_Reset(void)
3060 // reset the fssearch list
3061 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3063 if(prog->opensearches[i])
3064 FS_FreeSearch(prog->opensearches[i]);
3065 prog->opensearches[i] = NULL;
3073 float search_begin(string pattern, float caseinsensitive, float quiet)
3076 void VM_search_begin(void)
3079 const char *pattern;
3080 int caseinsens, quiet;
3082 VM_SAFEPARMCOUNT(3, VM_search_begin);
3084 pattern = PRVM_G_STRING(OFS_PARM0);
3086 VM_CheckEmptyString(pattern);
3088 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3089 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3091 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3092 if(!prog->opensearches[handle])
3095 if(handle >= PRVM_MAX_OPENSEARCHES)
3097 PRVM_G_FLOAT(OFS_RETURN) = -2;
3098 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
3102 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3103 PRVM_G_FLOAT(OFS_RETURN) = -1;
3106 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
3107 PRVM_G_FLOAT(OFS_RETURN) = handle;
3115 void search_end(float handle)
3118 void VM_search_end(void)
3121 VM_SAFEPARMCOUNT(1, VM_search_end);
3123 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3125 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3127 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
3130 if(prog->opensearches[handle] == NULL)
3132 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
3136 FS_FreeSearch(prog->opensearches[handle]);
3137 prog->opensearches[handle] = NULL;
3138 if(prog->opensearches_origin[handle])
3139 PRVM_Free((char *)prog->opensearches_origin[handle]);
3146 float search_getsize(float handle)
3149 void VM_search_getsize(void)
3152 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3154 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3156 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3158 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
3161 if(prog->opensearches[handle] == NULL)
3163 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
3167 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3172 VM_search_getfilename
3174 string search_getfilename(float handle, float num)
3177 void VM_search_getfilename(void)
3179 int handle, filenum;
3180 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3182 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3183 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3185 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3187 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
3190 if(prog->opensearches[handle] == NULL)
3192 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
3195 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3197 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
3201 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
3208 string chr(float ascii)
3215 VM_SAFEPARMCOUNT(1, VM_chr);
3217 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3220 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3225 VM_SAFEPARMCOUNT(1, VM_chr);
3227 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3229 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3232 //=============================================================================
3233 // Draw builtins (client & menu)
3239 float iscachedpic(string pic)
3242 void VM_iscachedpic(void)
3244 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3246 // drawq hasnt such a function, thus always return true
3247 PRVM_G_FLOAT(OFS_RETURN) = false;
3254 string precache_pic(string pic)
3257 void VM_precache_pic(void)
3261 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3263 s = PRVM_G_STRING(OFS_PARM0);
3264 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3265 VM_CheckEmptyString (s);
3267 // AK Draw_CachePic is supposed to always return a valid pointer
3268 if( Draw_CachePic_Flags(s, 0)->tex == r_texture_notexture )
3269 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3279 void VM_freepic(void)
3283 VM_SAFEPARMCOUNT(1,VM_freepic);
3285 s = PRVM_G_STRING(OFS_PARM0);
3286 VM_CheckEmptyString (s);
3291 void getdrawfontscale(float *sx, float *sy)
3295 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3296 if(VectorLength2(v) > 0)
3303 dp_font_t *getdrawfont(void)
3305 int f = (int) PRVM_drawglobalfloat(drawfont);
3306 if(f < 0 || f >= dp_fonts.maxsize)
3307 return FONT_DEFAULT;
3308 return &dp_fonts.f[f];
3315 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3318 void VM_drawcharacter(void)
3320 float *pos,*scale,*rgb;
3324 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3326 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3329 PRVM_G_FLOAT(OFS_RETURN) = -1;
3330 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3334 pos = PRVM_G_VECTOR(OFS_PARM0);
3335 scale = PRVM_G_VECTOR(OFS_PARM2);
3336 rgb = PRVM_G_VECTOR(OFS_PARM3);
3337 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3339 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3341 PRVM_G_FLOAT(OFS_RETURN) = -2;
3342 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3346 if(pos[2] || scale[2])
3347 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")));
3349 if(!scale[0] || !scale[1])
3351 PRVM_G_FLOAT(OFS_RETURN) = -3;
3352 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3356 getdrawfontscale(&sx, &sy);
3357 DrawQ_String_Scale(pos[0], pos[1], &character, 1, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3358 PRVM_G_FLOAT(OFS_RETURN) = 1;
3365 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3368 void VM_drawstring(void)
3370 float *pos,*scale,*rgb;
3374 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3376 string = PRVM_G_STRING(OFS_PARM1);
3377 pos = PRVM_G_VECTOR(OFS_PARM0);
3378 scale = PRVM_G_VECTOR(OFS_PARM2);
3379 rgb = PRVM_G_VECTOR(OFS_PARM3);
3380 if (prog->argc >= 6)
3381 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3383 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3385 PRVM_G_FLOAT(OFS_RETURN) = -2;
3386 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3390 if(!scale[0] || !scale[1])
3392 PRVM_G_FLOAT(OFS_RETURN) = -3;
3393 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3397 if(pos[2] || scale[2])
3398 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")));
3400 getdrawfontscale(&sx, &sy);
3401 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3402 //Font_DrawString(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
3403 PRVM_G_FLOAT(OFS_RETURN) = 1;
3408 VM_drawcolorcodedstring
3410 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3412 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3415 void VM_drawcolorcodedstring(void)
3421 float sx, sy, alpha;
3423 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3425 if (prog->argc == 6) // full 6 parms, like normal drawstring
3427 pos = PRVM_G_VECTOR(OFS_PARM0);
3428 string = PRVM_G_STRING(OFS_PARM1);
3429 scale = PRVM_G_VECTOR(OFS_PARM2);
3430 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3431 alpha = PRVM_G_FLOAT(OFS_PARM4);
3432 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3436 pos = PRVM_G_VECTOR(OFS_PARM0);
3437 string = PRVM_G_STRING(OFS_PARM1);
3438 scale = PRVM_G_VECTOR(OFS_PARM2);
3442 alpha = PRVM_G_FLOAT(OFS_PARM3);
3443 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3446 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3448 PRVM_G_FLOAT(OFS_RETURN) = -2;
3449 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3453 if(!scale[0] || !scale[1])
3455 PRVM_G_FLOAT(OFS_RETURN) = -3;
3456 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3460 if(pos[2] || scale[2])
3461 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")));
3463 getdrawfontscale(&sx, &sy);
3464 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], alpha, flag, NULL, false, getdrawfont());
3465 if (prog->argc == 6) // also return vector of last color
3466 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3468 PRVM_G_FLOAT(OFS_RETURN) = 1;
3474 float stringwidth(string text, float allowColorCodes, float size)
3477 void VM_stringwidth(void)
3481 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3485 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3487 getdrawfontscale(&sx, &sy);
3490 szv = PRVM_G_VECTOR(OFS_PARM2);
3495 // we want the width for 8x8 font size, divided by 8
3496 static float defsize[] = {8, 8};
3499 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3500 if(sx >= 0.9 && sx <= 1.1)
3508 string = PRVM_G_STRING(OFS_PARM0);
3509 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3511 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(), 1000000000) * mult;
3515 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3523 string = PRVM_G_STRING(OFS_PARM0);
3524 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3526 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3534 float findfont(string s)
3538 float getdrawfontnum(const char *fontname)
3542 for(i = 0; i < dp_fonts.maxsize; ++i)
3543 if(!strcmp(dp_fonts.f[i].title, fontname))
3548 void VM_findfont(void)
3550 VM_SAFEPARMCOUNT(1,VM_findfont);
3551 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3558 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3562 dp_font_t *FindFont(const char *title, qboolean allocate_new);
3563 void LoadFont(qboolean override, const char *name, dp_font_t *fnt, float scale, float voffset);
3564 void VM_loadfont(void)
3566 const char *fontname, *filelist, *sizes, *c, *cm;
3567 char mainfont[MAX_QPATH];
3569 float sz, scale, voffset;
3572 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3574 fontname = PRVM_G_STRING(OFS_PARM0);
3576 fontname = "default";
3578 filelist = PRVM_G_STRING(OFS_PARM1);
3580 filelist = "gfx/conchars";
3582 sizes = PRVM_G_STRING(OFS_PARM2);
3588 if (prog->argc >= 4)
3590 i = PRVM_G_FLOAT(OFS_PARM3);
3591 if (i >= 0 && i < dp_fonts.maxsize)
3594 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3598 f = FindFont(fontname, true);
3601 PRVM_G_FLOAT(OFS_RETURN) = -1;
3602 return; // something go wrong
3605 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3606 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3608 // first font is handled "normally"
3609 c = strchr(filelist, ':');
3610 cm = strchr(filelist, ',');
3611 if(c && (!cm || c < cm))
3612 f->req_face = atoi(c+1);
3618 if(!c || (c - filelist) > MAX_QPATH)
3619 strlcpy(mainfont, filelist, sizeof(mainfont));
3622 memcpy(mainfont, filelist, c - filelist);
3623 mainfont[c - filelist] = 0;
3627 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3629 c = strchr(filelist, ',');
3635 c = strchr(filelist, ':');
3636 cm = strchr(filelist, ',');
3637 if(c && (!cm || c < cm))
3638 f->fallback_faces[i] = atoi(c+1);
3641 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3644 if(!c || (c-filelist) > MAX_QPATH)
3646 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3650 memcpy(f->fallbacks[i], filelist, c - filelist);
3651 f->fallbacks[i][c - filelist] = 0;
3656 for(i = 0; i < MAX_FONT_SIZES; ++i)
3657 f->req_sizes[i] = -1;
3658 for (numsizes = 0,c = sizes;;)
3660 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3662 sz = atof(com_token);
3664 if (sz < 0.001f || sz > 1000.0f)
3666 VM_Warning("VM_loadfont: crap size %s", com_token);
3670 if (numsizes == MAX_FONT_SIZES)
3672 VM_Warning("VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3675 f->req_sizes[numsizes] = sz;
3679 // additional scale/hoffset parms
3682 if (prog->argc >= 5)
3684 scale = PRVM_G_FLOAT(OFS_PARM4);
3688 if (prog->argc >= 6)
3689 voffset = PRVM_G_FLOAT(OFS_PARM5);
3692 LoadFont(true, mainfont, f, scale, voffset);
3694 // return index of loaded font
3695 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3702 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3705 void VM_drawpic(void)
3707 const char *picname;
3708 float *size, *pos, *rgb;
3711 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3713 picname = PRVM_G_STRING(OFS_PARM1);
3714 VM_CheckEmptyString (picname);
3716 // is pic cached ? no function yet for that
3719 PRVM_G_FLOAT(OFS_RETURN) = -4;
3720 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3724 pos = PRVM_G_VECTOR(OFS_PARM0);
3725 size = PRVM_G_VECTOR(OFS_PARM2);
3726 rgb = PRVM_G_VECTOR(OFS_PARM3);
3727 if (prog->argc >= 6)
3728 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3730 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3732 PRVM_G_FLOAT(OFS_RETURN) = -2;
3733 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3737 if(pos[2] || size[2])
3738 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")));
3740 DrawQ_Pic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
3741 PRVM_G_FLOAT(OFS_RETURN) = 1;
3747 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3750 void VM_drawrotpic(void)
3752 const char *picname;
3753 float *size, *pos, *org, *rgb;
3756 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3758 picname = PRVM_G_STRING(OFS_PARM1);
3759 VM_CheckEmptyString (picname);
3761 // is pic cached ? no function yet for that
3764 PRVM_G_FLOAT(OFS_RETURN) = -4;
3765 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3769 pos = PRVM_G_VECTOR(OFS_PARM0);
3770 size = PRVM_G_VECTOR(OFS_PARM2);
3771 org = PRVM_G_VECTOR(OFS_PARM3);
3772 rgb = PRVM_G_VECTOR(OFS_PARM5);
3773 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3775 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3777 PRVM_G_FLOAT(OFS_RETURN) = -2;
3778 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3782 if(pos[2] || size[2] || org[2])
3783 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3785 DrawQ_RotPic(pos[0], pos[1], Draw_CachePic_Flags(picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], org[0], org[1], PRVM_G_FLOAT(OFS_PARM4), rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM6), flag);
3786 PRVM_G_FLOAT(OFS_RETURN) = 1;
3792 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3796 void VM_drawsubpic(void)
3798 const char *picname;
3799 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3802 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3804 picname = PRVM_G_STRING(OFS_PARM2);
3805 VM_CheckEmptyString (picname);
3807 // is pic cached ? no function yet for that
3810 PRVM_G_FLOAT(OFS_RETURN) = -4;
3811 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3815 pos = PRVM_G_VECTOR(OFS_PARM0);
3816 size = PRVM_G_VECTOR(OFS_PARM1);
3817 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3818 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3819 rgb = PRVM_G_VECTOR(OFS_PARM5);
3820 alpha = PRVM_G_FLOAT(OFS_PARM6);
3821 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3823 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3825 PRVM_G_FLOAT(OFS_RETURN) = -2;
3826 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3830 if(pos[2] || size[2])
3831 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")));
3833 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3835 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3836 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3837 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3838 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3840 PRVM_G_FLOAT(OFS_RETURN) = 1;
3847 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3850 void VM_drawfill(void)
3852 float *size, *pos, *rgb;
3855 VM_SAFEPARMCOUNT(5,VM_drawfill);
3858 pos = PRVM_G_VECTOR(OFS_PARM0);
3859 size = PRVM_G_VECTOR(OFS_PARM1);
3860 rgb = PRVM_G_VECTOR(OFS_PARM2);
3861 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3863 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3865 PRVM_G_FLOAT(OFS_RETURN) = -2;
3866 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3870 if(pos[2] || size[2])
3871 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")));
3873 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3874 PRVM_G_FLOAT(OFS_RETURN) = 1;
3881 drawsetcliparea(float x, float y, float width, float height)
3884 void VM_drawsetcliparea(void)
3887 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3889 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3890 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3891 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3892 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3894 DrawQ_SetClipArea(x, y, w, h);
3899 VM_drawresetcliparea
3904 void VM_drawresetcliparea(void)
3906 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3908 DrawQ_ResetClipArea();
3915 vector getimagesize(string pic)
3918 void VM_getimagesize(void)
3923 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3925 p = PRVM_G_STRING(OFS_PARM0);
3926 VM_CheckEmptyString (p);
3928 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3930 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3931 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3932 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3939 string keynumtostring(float keynum)
3942 void VM_keynumtostring (void)
3944 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3946 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3951 VM_findkeysforcommand
3953 string findkeysforcommand(string command, float bindmap)
3955 the returned string is an altstring
3958 #define FKFC_NUMKEYS 5
3959 void M_FindKeysForCommand(const char *command, int *keys);
3960 void VM_findkeysforcommand(void)
3963 char ret[VM_STRINGTEMP_LENGTH];
3964 int keys[FKFC_NUMKEYS];
3968 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3970 cmd = PRVM_G_STRING(OFS_PARM0);
3972 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3974 bindmap = 0; // consistent to "bind"
3976 VM_CheckEmptyString(cmd);
3978 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3981 for(i = 0; i < FKFC_NUMKEYS; i++)
3982 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3984 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3991 float stringtokeynum(string key)
3994 void VM_stringtokeynum (void)
3996 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3998 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
4005 string getkeybind(float key, float bindmap)
4008 void VM_getkeybind (void)
4011 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4013 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4015 bindmap = 0; // consistent to "bind"
4017 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4024 float setkeybind(float key, string cmd, float bindmap)
4027 void VM_setkeybind (void)
4030 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4032 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4034 bindmap = 0; // consistent to "bind"
4036 PRVM_G_FLOAT(OFS_RETURN) = 0;
4037 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4038 PRVM_G_FLOAT(OFS_RETURN) = 1;
4045 vector getbindmaps()
4048 void VM_getbindmaps (void)
4051 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4052 Key_GetBindMap(&fg, &bg);
4053 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4054 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4055 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4062 float setbindmaps(vector bindmap)
4065 void VM_setbindmaps (void)
4067 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4068 PRVM_G_FLOAT(OFS_RETURN) = 0;
4069 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4070 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4071 PRVM_G_FLOAT(OFS_RETURN) = 1;
4074 // CL_Video interface functions
4077 ========================
4080 float cin_open(string file, string name)
4081 ========================
4083 void VM_cin_open( void )
4088 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4090 file = PRVM_G_STRING( OFS_PARM0 );
4091 name = PRVM_G_STRING( OFS_PARM1 );
4093 VM_CheckEmptyString( file );
4094 VM_CheckEmptyString( name );
4096 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4097 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4099 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4103 ========================
4106 void cin_close(string name)
4107 ========================
4109 void VM_cin_close( void )
4113 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4115 name = PRVM_G_STRING( OFS_PARM0 );
4116 VM_CheckEmptyString( name );
4118 CL_CloseVideo( CL_GetVideoByName( name ) );
4122 ========================
4124 void cin_setstate(string name, float type)
4125 ========================
4127 void VM_cin_setstate( void )
4130 clvideostate_t state;
4133 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4135 name = PRVM_G_STRING( OFS_PARM0 );
4136 VM_CheckEmptyString( name );
4138 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4140 video = CL_GetVideoByName( name );
4141 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4142 CL_SetVideoState( video, state );
4146 ========================
4149 float cin_getstate(string name)
4150 ========================
4152 void VM_cin_getstate( void )
4157 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4159 name = PRVM_G_STRING( OFS_PARM0 );
4160 VM_CheckEmptyString( name );
4162 video = CL_GetVideoByName( name );
4164 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4166 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4170 ========================
4173 void cin_restart(string name)
4174 ========================
4176 void VM_cin_restart( void )
4181 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4183 name = PRVM_G_STRING( OFS_PARM0 );
4184 VM_CheckEmptyString( name );
4186 video = CL_GetVideoByName( name );
4188 CL_RestartVideo( video );
4192 ========================
4194 ========================
4196 void VM_Gecko_Init( void ) {
4197 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
4198 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
4202 ========================
4204 ========================
4206 void VM_Gecko_Destroy( void ) {
4208 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4209 clgecko_t **instance = &prog->opengeckoinstances[ i ];
4211 CL_Gecko_DestroyBrowser( *instance );
4218 ========================
4221 float[bool] gecko_create( string name )
4222 ========================
4224 void VM_gecko_create( void ) {
4227 clgecko_t *instance;
4229 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
4231 name = PRVM_G_STRING( OFS_PARM0 );
4232 VM_CheckEmptyString( name );
4234 // find an empty slot for this gecko browser..
4235 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4236 if( prog->opengeckoinstances[ i ] == NULL ) {
4240 if( i == PRVM_MAX_GECKOINSTANCES ) {
4241 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
4242 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4246 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
4248 // TODO: error handling [12/3/2007 Black]
4249 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4252 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4256 ========================
4259 void gecko_destroy( string name )
4260 ========================
4262 void VM_gecko_destroy( void ) {
4264 clgecko_t *instance;
4266 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
4268 name = PRVM_G_STRING( OFS_PARM0 );
4269 VM_CheckEmptyString( name );
4270 instance = CL_Gecko_FindBrowser( name );
4274 CL_Gecko_DestroyBrowser( instance );
4278 ========================
4281 void gecko_navigate( string name, string URI )
4282 ========================
4284 void VM_gecko_navigate( void ) {
4287 clgecko_t *instance;
4289 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
4291 name = PRVM_G_STRING( OFS_PARM0 );
4292 URI = PRVM_G_STRING( OFS_PARM1 );
4293 VM_CheckEmptyString( name );
4294 VM_CheckEmptyString( URI );
4296 instance = CL_Gecko_FindBrowser( name );
4300 CL_Gecko_NavigateToURI( instance, URI );
4304 ========================
4307 float[bool] gecko_keyevent( string name, float key, float eventtype )
4308 ========================
4310 void VM_gecko_keyevent( void ) {
4313 clgecko_buttoneventtype_t eventtype;
4314 clgecko_t *instance;
4316 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
4318 name = PRVM_G_STRING( OFS_PARM0 );
4319 VM_CheckEmptyString( name );
4320 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
4321 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
4323 eventtype = CLG_BET_DOWN;
4326 eventtype = CLG_BET_UP;
4329 eventtype = CLG_BET_PRESS;
4332 eventtype = CLG_BET_DOUBLECLICK;
4335 // TODO: console printf? [12/3/2007 Black]
4336 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4340 instance = CL_Gecko_FindBrowser( name );
4342 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4346 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
4350 ========================
4353 void gecko_mousemove( string name, float x, float y )
4354 ========================
4356 void VM_gecko_movemouse( void ) {
4359 clgecko_t *instance;
4361 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4363 name = PRVM_G_STRING( OFS_PARM0 );
4364 VM_CheckEmptyString( name );
4365 x = PRVM_G_FLOAT( OFS_PARM1 );
4366 y = PRVM_G_FLOAT( OFS_PARM2 );
4368 instance = CL_Gecko_FindBrowser( name );
4372 CL_Gecko_Event_CursorMove( instance, x, y );
4377 ========================
4380 void gecko_resize( string name, float w, float h )
4381 ========================
4383 void VM_gecko_resize( void ) {
4386 clgecko_t *instance;
4388 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4390 name = PRVM_G_STRING( OFS_PARM0 );
4391 VM_CheckEmptyString( name );
4392 w = PRVM_G_FLOAT( OFS_PARM1 );
4393 h = PRVM_G_FLOAT( OFS_PARM2 );
4395 instance = CL_Gecko_FindBrowser( name );
4399 CL_Gecko_Resize( instance, (int) w, (int) h );
4404 ========================
4405 VM_gecko_get_texture_extent
4407 vector gecko_get_texture_extent( string name )
4408 ========================
4410 void VM_gecko_get_texture_extent( void ) {
4412 clgecko_t *instance;
4414 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
4416 name = PRVM_G_STRING( OFS_PARM0 );
4417 VM_CheckEmptyString( name );
4419 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4420 instance = CL_Gecko_FindBrowser( name );
4422 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4423 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4426 CL_Gecko_GetTextureExtent( instance,
4427 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
4436 Writes new values for v_forward, v_up, and v_right based on angles
4437 void makevectors(vector angle)
4440 void VM_makevectors (void)
4442 VM_SAFEPARMCOUNT(1, VM_makevectors);
4443 AngleVectors(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4450 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4451 vectorvectors(vector)
4454 void VM_vectorvectors (void)
4456 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4457 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward));
4458 VectorVectors(PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4462 ========================
4465 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4466 ========================
4468 void VM_drawline (void)
4470 float *c1, *c2, *rgb;
4472 unsigned char flags;
4474 VM_SAFEPARMCOUNT(6, VM_drawline);
4475 width = PRVM_G_FLOAT(OFS_PARM0);
4476 c1 = PRVM_G_VECTOR(OFS_PARM1);
4477 c2 = PRVM_G_VECTOR(OFS_PARM2);
4478 rgb = PRVM_G_VECTOR(OFS_PARM3);
4479 alpha = PRVM_G_FLOAT(OFS_PARM4);
4480 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4481 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4484 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4485 void VM_bitshift (void)
4488 VM_SAFEPARMCOUNT(2, VM_bitshift);
4490 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4491 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4493 PRVM_G_FLOAT(OFS_RETURN) = n1;
4496 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4498 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4501 ////////////////////////////////////////
4502 // AltString functions
4503 ////////////////////////////////////////
4506 ========================
4509 float altstr_count(string)
4510 ========================
4512 void VM_altstr_count( void )
4514 const char *altstr, *pos;
4517 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4519 altstr = PRVM_G_STRING( OFS_PARM0 );
4520 //VM_CheckEmptyString( altstr );
4522 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4523 if( *pos == '\\' ) {
4527 } else if( *pos == '\'' ) {
4532 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4536 ========================
4539 string altstr_prepare(string)
4540 ========================
4542 void VM_altstr_prepare( void )
4545 const char *instr, *in;
4547 char outstr[VM_STRINGTEMP_LENGTH];
4549 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4551 instr = PRVM_G_STRING( OFS_PARM0 );
4553 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4562 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4566 ========================
4569 string altstr_get(string, float)
4570 ========================
4572 void VM_altstr_get( void )
4574 const char *altstr, *pos;
4577 char outstr[VM_STRINGTEMP_LENGTH];
4579 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4581 altstr = PRVM_G_STRING( OFS_PARM0 );
4583 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4584 count = count * 2 + 1;
4586 for( pos = altstr ; *pos && count ; pos++ )
4587 if( *pos == '\\' ) {
4590 } else if( *pos == '\'' )
4594 PRVM_G_INT( OFS_RETURN ) = 0;
4598 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4599 if( *pos == '\\' ) {
4604 } else if( *pos == '\'' )
4610 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4614 ========================
4617 string altstr_set(string altstr, float num, string set)
4618 ========================
4620 void VM_altstr_set( void )
4623 const char *altstr, *str;
4626 char outstr[VM_STRINGTEMP_LENGTH];
4628 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4630 altstr = PRVM_G_STRING( OFS_PARM0 );
4632 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4634 str = PRVM_G_STRING( OFS_PARM2 );
4637 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4642 } else if( *in == '\'' ) {
4647 for( ; *str; *out++ = *str++ );
4648 // now jump over the old content
4650 if( *in == '\'' || (*in == '\\' && !*++in) )
4653 strlcpy(out, in, outstr + sizeof(outstr) - out);
4654 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4658 ========================
4661 string altstr_ins(string altstr, float num, string set)
4662 ========================
4664 void VM_altstr_ins(void)
4670 char outstr[VM_STRINGTEMP_LENGTH];
4672 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4674 in = PRVM_G_STRING( OFS_PARM0 );
4675 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4676 set = PRVM_G_STRING( OFS_PARM2 );
4679 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4684 } else if( *in == '\'' ) {
4689 for( ; *set ; *out++ = *set++ );
4692 strlcpy(out, in, outstr + sizeof(outstr) - out);
4693 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4697 ////////////////////////////////////////
4698 // BufString functions
4699 ////////////////////////////////////////
4700 //[515]: string buffers support
4702 static size_t stringbuffers_sortlength;
4704 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4706 if (stringbuffer->max_strings <= strindex)
4708 char **oldstrings = stringbuffer->strings;
4709 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4710 while (stringbuffer->max_strings <= strindex)
4711 stringbuffer->max_strings *= 2;
4712 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4713 if (stringbuffer->num_strings > 0)
4714 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4716 Mem_Free(oldstrings);
4720 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4722 // reduce num_strings if there are empty string slots at the end
4723 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4724 stringbuffer->num_strings--;
4726 // if empty, free the string pointer array
4727 if (stringbuffer->num_strings == 0)
4729 stringbuffer->max_strings = 0;
4730 if (stringbuffer->strings)
4731 Mem_Free(stringbuffer->strings);
4732 stringbuffer->strings = NULL;
4736 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4739 a = *((const char **) in1);
4740 b = *((const char **) in2);
4741 if(!a || !a[0]) return 1;
4742 if(!b || !b[0]) return -1;
4743 return strncmp(a, b, stringbuffers_sortlength);
4746 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4749 a = *((const char **) in1);
4750 b = *((const char **) in2);
4751 if(!a || !a[0]) return 1;
4752 if(!b || !b[0]) return -1;
4753 return strncmp(b, a, stringbuffers_sortlength);
4757 ========================
4759 creates new buffer, and returns it's index, returns -1 if failed
4760 float buf_create(void) = #460;
4761 float newbuf(string format, float flags) = #460;
4762 ========================
4765 void VM_buf_create (void)
4767 prvm_stringbuffer_t *stringbuffer;
4770 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4772 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4773 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4775 PRVM_G_FLOAT(OFS_RETURN) = -1;
4778 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4779 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4780 stringbuffer->origin = PRVM_AllocationOrigin();
4781 // optional flags parm
4782 if (prog->argc >= 2)
4783 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4784 PRVM_G_FLOAT(OFS_RETURN) = i;
4790 ========================
4792 deletes buffer and all strings in it
4793 void buf_del(float bufhandle) = #461;
4794 ========================
4796 void VM_buf_del (void)
4798 prvm_stringbuffer_t *stringbuffer;
4799 VM_SAFEPARMCOUNT(1, VM_buf_del);
4800 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4804 for (i = 0;i < stringbuffer->num_strings;i++)
4805 if (stringbuffer->strings[i])
4806 Mem_Free(stringbuffer->strings[i]);
4807 if (stringbuffer->strings)
4808 Mem_Free(stringbuffer->strings);
4809 if(stringbuffer->origin)
4810 PRVM_Free((char *)stringbuffer->origin);
4811 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4815 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4821 ========================
4823 how many strings are stored in buffer
4824 float buf_getsize(float bufhandle) = #462;
4825 ========================
4827 void VM_buf_getsize (void)
4829 prvm_stringbuffer_t *stringbuffer;
4830 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4832 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4835 PRVM_G_FLOAT(OFS_RETURN) = -1;
4836 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4840 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4844 ========================
4846 copy all content from one buffer to another, make sure it exists
4847 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4848 ========================
4850 void VM_buf_copy (void)
4852 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4854 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4856 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4857 if(!srcstringbuffer)
4859 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4862 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4863 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4865 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4868 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4869 if(!dststringbuffer)
4871 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4875 for (i = 0;i < dststringbuffer->num_strings;i++)
4876 if (dststringbuffer->strings[i])
4877 Mem_Free(dststringbuffer->strings[i]);
4878 if (dststringbuffer->strings)
4879 Mem_Free(dststringbuffer->strings);
4880 *dststringbuffer = *srcstringbuffer;
4881 if (dststringbuffer->max_strings)
4882 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4884 for (i = 0;i < dststringbuffer->num_strings;i++)
4886 if (srcstringbuffer->strings[i])
4889 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4890 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4891 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4897 ========================
4899 sort buffer by beginnings of strings (cmplength defaults it's length)
4900 "backward == TRUE" means that sorting goes upside-down
4901 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4902 ========================
4904 void VM_buf_sort (void)
4906 prvm_stringbuffer_t *stringbuffer;
4907 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4909 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4912 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4915 if(stringbuffer->num_strings <= 0)
4917 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4920 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4921 if(stringbuffers_sortlength <= 0)
4922 stringbuffers_sortlength = 0x7FFFFFFF;
4924 if(!PRVM_G_FLOAT(OFS_PARM2))
4925 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4927 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4929 BufStr_Shrink(stringbuffer);
4933 ========================
4935 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4936 string buf_implode(float bufhandle, string glue) = #465;
4937 ========================
4939 void VM_buf_implode (void)
4941 prvm_stringbuffer_t *stringbuffer;
4942 char k[VM_STRINGTEMP_LENGTH];
4946 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4948 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4949 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4952 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4955 if(!stringbuffer->num_strings)
4957 sep = PRVM_G_STRING(OFS_PARM1);
4959 for(l = i = 0;i < stringbuffer->num_strings;i++)
4961 if(stringbuffer->strings[i])
4963 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4964 if (l >= sizeof(k) - 1)
4966 strlcat(k, sep, sizeof(k));
4967 strlcat(k, stringbuffer->strings[i], sizeof(k));
4970 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4974 ========================
4976 get a string from buffer, returns tempstring, dont str_unzone it!
4977 string bufstr_get(float bufhandle, float string_index) = #465;
4978 ========================
4980 void VM_bufstr_get (void)
4982 prvm_stringbuffer_t *stringbuffer;
4984 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4986 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4987 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4990 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4993 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4996 // VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4999 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
5000 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
5004 ========================
5006 copies a string into selected slot of buffer
5007 void bufstr_set(float bufhandle, float string_index, string str) = #466;
5008 ========================
5010 void VM_bufstr_set (void)
5014 prvm_stringbuffer_t *stringbuffer;
5017 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
5019 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5022 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5025 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
5026 if(strindex < 0 || strindex >= 1000000) // huge number of strings
5028 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
5032 BufStr_Expand(stringbuffer, strindex);
5033 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5035 if(stringbuffer->strings[strindex])
5036 Mem_Free(stringbuffer->strings[strindex]);
5037 stringbuffer->strings[strindex] = NULL;
5039 if(PRVM_G_INT(OFS_PARM2))
5041 // not the NULL string!
5042 news = PRVM_G_STRING(OFS_PARM2);
5043 alloclen = strlen(news) + 1;
5044 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5045 memcpy(stringbuffer->strings[strindex], news, alloclen);
5048 BufStr_Shrink(stringbuffer);
5052 ========================
5054 adds string to buffer in first free slot and returns its index
5055 "order == TRUE" means that string will be added after last "full" slot
5056 float bufstr_add(float bufhandle, string str, float order) = #467;
5057 ========================
5059 void VM_bufstr_add (void)
5061 int order, strindex;
5062 prvm_stringbuffer_t *stringbuffer;
5066 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
5068 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5069 PRVM_G_FLOAT(OFS_RETURN) = -1;
5072 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5075 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
5077 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);
5080 string = PRVM_G_STRING(OFS_PARM1);
5081 order = (int)PRVM_G_FLOAT(OFS_PARM2);
5083 strindex = stringbuffer->num_strings;
5085 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
5086 if (stringbuffer->strings[strindex] == NULL)
5089 BufStr_Expand(stringbuffer, strindex);
5091 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5092 alloclen = strlen(string) + 1;
5093 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5094 memcpy(stringbuffer->strings[strindex], string, alloclen);
5096 PRVM_G_FLOAT(OFS_RETURN) = strindex;
5100 ========================
5102 delete string from buffer
5103 void bufstr_free(float bufhandle, float string_index) = #468;
5104 ========================
5106 void VM_bufstr_free (void)
5109 prvm_stringbuffer_t *stringbuffer;
5110 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
5112 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5115 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5118 i = (int)PRVM_G_FLOAT(OFS_PARM1);
5121 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
5125 if (i < stringbuffer->num_strings)
5127 if(stringbuffer->strings[i])
5128 Mem_Free(stringbuffer->strings[i]);
5129 stringbuffer->strings[i] = NULL;
5132 BufStr_Shrink(stringbuffer);
5141 void VM_buf_cvarlist(void)
5144 const char *partial, *antipartial;
5145 size_t len, antilen;
5147 qboolean ispattern, antiispattern;
5149 prvm_stringbuffer_t *stringbuffer;
5150 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5152 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5155 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5159 partial = PRVM_G_STRING(OFS_PARM1);
5163 len = strlen(partial);
5166 antipartial = PRVM_G_STRING(OFS_PARM2);
5172 antilen = strlen(antipartial);
5174 for (n = 0;n < stringbuffer->num_strings;n++)
5175 if (stringbuffer->strings[n])
5176 Mem_Free(stringbuffer->strings[n]);
5177 if (stringbuffer->strings)
5178 Mem_Free(stringbuffer->strings);
5179 stringbuffer->strings = NULL;
5181 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5182 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5185 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5187 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5190 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5196 stringbuffer->max_strings = stringbuffer->num_strings = n;
5197 if (stringbuffer->max_strings)
5198 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5201 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5203 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5206 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5209 alloclen = strlen(cvar->name) + 1;
5210 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5211 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5226 This was a major timewaster in progs, so it was converted to C
5229 void VM_changeyaw (void)
5232 float ideal, current, move, speed;
5234 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
5235 // parameters because they are the parameters to SV_MoveToGoal, not this
5236 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5238 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5239 if (ent == prog->edicts)
5241 VM_Warning("changeyaw: can not modify world entity\n");
5244 if (ent->priv.server->free)
5246 VM_Warning("changeyaw: can not modify free entity\n");
5249 current = PRVM_gameedictvector(ent, angles)[1];
5250 current = ANGLEMOD(current);
5251 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5252 speed = PRVM_gameedictfloat(ent, yaw_speed);
5254 if (current == ideal)
5256 move = ideal - current;
5257 if (ideal > current)
5279 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5287 void VM_changepitch (void)
5290 float ideal, current, move, speed;
5292 VM_SAFEPARMCOUNT(1, VM_changepitch);
5294 ent = PRVM_G_EDICT(OFS_PARM0);
5295 if (ent == prog->edicts)
5297 VM_Warning("changepitch: can not modify world entity\n");
5300 if (ent->priv.server->free)
5302 VM_Warning("changepitch: can not modify free entity\n");
5305 current = PRVM_gameedictvector(ent, angles)[0];
5306 current = ANGLEMOD(current);
5307 ideal = PRVM_gameedictfloat(ent, idealpitch);
5308 speed = PRVM_gameedictfloat(ent, pitch_speed);
5310 if (current == ideal)
5312 move = ideal - current;
5313 if (ideal > current)
5335 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5339 void VM_uncolorstring (void)
5341 char szNewString[VM_STRINGTEMP_LENGTH];
5342 const char *szString;
5345 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5346 szString = PRVM_G_STRING(OFS_PARM0);
5347 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5348 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
5352 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5353 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5354 void VM_strstrofs (void)
5356 const char *instr, *match;
5358 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5359 instr = PRVM_G_STRING(OFS_PARM0);
5360 match = PRVM_G_STRING(OFS_PARM1);
5361 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5362 firstofs = u8_bytelen(instr, firstofs);
5364 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5366 PRVM_G_FLOAT(OFS_RETURN) = -1;
5370 match = strstr(instr+firstofs, match);
5372 PRVM_G_FLOAT(OFS_RETURN) = -1;
5374 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5377 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5378 void VM_str2chr (void)
5383 VM_SAFEPARMCOUNT(2, VM_str2chr);
5384 s = PRVM_G_STRING(OFS_PARM0);
5385 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5387 if((unsigned)index < strlen(s))
5389 if (utf8_enable.integer)
5390 ch = u8_getchar_noendptr(s + index);
5392 ch = (unsigned char)s[index];
5393 PRVM_G_FLOAT(OFS_RETURN) = ch;
5396 PRVM_G_FLOAT(OFS_RETURN) = 0;
5399 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5400 void VM_chr2str (void)
5405 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5406 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5407 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5409 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5414 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5415 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5416 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5418 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5421 static int chrconv_number(int i, int base, int conv)
5446 static int chrconv_punct(int i, int base, int conv)
5464 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5466 //convert case and colour seperatly...
5483 baset = 128*((charnum&1) == (convt-5));
5499 return i + basec + baset;
5501 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5502 //bulk convert a string. change case or colouring.
5503 void VM_strconv (void)
5505 int ccase, redalpha, rednum, len, i;
5506 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5507 unsigned char *result = resbuf;
5509 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5511 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5512 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5513 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5514 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
5515 len = strlen((char *) resbuf);
5517 for (i = 0; i < len; i++, result++) //should this be done backwards?
5519 if (*result >= '0' && *result <= '9') //normal numbers...
5520 *result = chrconv_number(*result, '0', rednum);
5521 else if (*result >= '0'+128 && *result <= '9'+128)
5522 *result = chrconv_number(*result, '0'+128, rednum);
5523 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5524 *result = chrconv_number(*result, '0'+128-30, rednum);
5525 else if (*result >= '0'-30 && *result <= '9'-30)
5526 *result = chrconv_number(*result, '0'-30, rednum);
5528 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5529 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5530 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5531 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5532 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5533 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5534 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5535 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5537 else if ((*result & 127) < 16 || !redalpha) //special chars..
5539 else if (*result < 128)
5540 *result = chrconv_punct(*result, 0, redalpha);
5542 *result = chrconv_punct(*result, 128, redalpha);
5546 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5549 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5550 void VM_strpad (void)
5552 char src[VM_STRINGTEMP_LENGTH];
5553 char destbuf[VM_STRINGTEMP_LENGTH];
5555 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5556 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5557 VM_VarString(1, src, sizeof(src));
5559 // note: < 0 = left padding, > 0 = right padding,
5560 // this is reverse logic of printf!
5561 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5563 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5566 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5567 //uses qw style \key\value strings
5568 void VM_infoadd (void)
5570 const char *info, *key;
5571 char value[VM_STRINGTEMP_LENGTH];
5572 char temp[VM_STRINGTEMP_LENGTH];
5574 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5575 info = PRVM_G_STRING(OFS_PARM0);
5576 key = PRVM_G_STRING(OFS_PARM1);
5577 VM_VarString(2, value, sizeof(value));
5579 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5581 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5583 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5586 // #227 string(string info, string key) infoget (FTE_STRINGS)
5587 //uses qw style \key\value strings
5588 void VM_infoget (void)
5592 char value[VM_STRINGTEMP_LENGTH];
5594 VM_SAFEPARMCOUNT(2, VM_infoget);
5595 info = PRVM_G_STRING(OFS_PARM0);
5596 key = PRVM_G_STRING(OFS_PARM1);
5598 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5600 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5603 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5604 // also float(string s1, string s2) strcmp (FRIK_FILE)
5605 void VM_strncmp (void)
5607 const char *s1, *s2;
5608 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5609 s1 = PRVM_G_STRING(OFS_PARM0);
5610 s2 = PRVM_G_STRING(OFS_PARM1);
5613 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5617 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5621 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5622 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5623 void VM_strncasecmp (void)
5625 const char *s1, *s2;
5626 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5627 s1 = PRVM_G_STRING(OFS_PARM0);
5628 s2 = PRVM_G_STRING(OFS_PARM1);
5631 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5635 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5639 // #494 float(float caseinsensitive, string s, ...) crc16
5643 static char s[VM_STRINGTEMP_LENGTH];
5644 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5645 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5646 VM_VarString(1, s, sizeof(s));
5647 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5650 // #639 float(string digest, string data, ...) digest_hex
5651 void VM_digest_hex(void)
5655 static char out[32];
5656 static char outhex[65];
5659 static char s[VM_STRINGTEMP_LENGTH];
5662 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5663 digest = PRVM_G_STRING(OFS_PARM0);
5666 VM_VarString(1, s, sizeof(s));
5671 if(!strcmp(digest, "MD4"))
5674 mdfour(&out, s, len);
5676 else if(!strcmp(digest, "SHA256") && Crypto_Available())
5679 sha256(&out, s, len);
5681 // no warning needed on mismatch - we return string_null to QC
5686 static const char *hexmap = "0123456789abcdef";
5687 for(i = 0; i < outlen; ++i)
5689 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
5690 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
5693 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outhex);
5696 PRVM_G_INT(OFS_RETURN) = 0;
5699 void VM_wasfreed (void)
5701 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5702 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5705 void VM_SetTraceGlobals(const trace_t *trace)
5707 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
5708 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
5709 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
5710 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
5711 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
5712 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
5713 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
5714 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
5715 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5716 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
5717 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
5718 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
5719 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5722 void VM_ClearTraceGlobals(void)
5724 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5725 PRVM_gameglobalfloat(trace_allsolid) = 0;
5726 PRVM_gameglobalfloat(trace_startsolid) = 0;
5727 PRVM_gameglobalfloat(trace_fraction) = 0;
5728 PRVM_gameglobalfloat(trace_inwater) = 0;
5729 PRVM_gameglobalfloat(trace_inopen) = 0;
5730 VectorClear(PRVM_gameglobalvector(trace_endpos));
5731 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
5732 PRVM_gameglobalfloat(trace_plane_dist) = 0;
5733 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
5734 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
5735 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
5736 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
5737 PRVM_gameglobalstring(trace_dphittexturename) = 0;
5742 void VM_Cmd_Init(void)
5744 // only init the stuff for the current prog
5748 // VM_BufStr_Init();
5751 void VM_Cmd_Reset(void)
5753 CL_PurgeOwner( MENUOWNER );
5755 VM_Files_CloseAll();
5757 // VM_BufStr_ShutDown();
5760 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5761 // does URI escaping on a string (replace evil stuff by %AB escapes)
5762 void VM_uri_escape (void)
5764 char src[VM_STRINGTEMP_LENGTH];
5765 char dest[VM_STRINGTEMP_LENGTH];
5767 static const char *hex = "0123456789ABCDEF";
5769 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5770 VM_VarString(0, src, sizeof(src));
5772 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5774 if((*p >= 'A' && *p <= 'Z')
5775 || (*p >= 'a' && *p <= 'z')
5776 || (*p >= '0' && *p <= '9')
5777 || (*p == '-') || (*p == '_') || (*p == '.')
5778 || (*p == '!') || (*p == '~')
5779 || (*p == '\'') || (*p == '(') || (*p == ')'))
5784 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5785 *q++ = hex[ *(unsigned char *)p & 0xF];
5790 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5793 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5794 // does URI unescaping on a string (get back the evil stuff)
5795 void VM_uri_unescape (void)
5797 char src[VM_STRINGTEMP_LENGTH];
5798 char dest[VM_STRINGTEMP_LENGTH];
5802 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5803 VM_VarString(0, src, sizeof(src));
5805 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5809 if(p[1] >= '0' && p[1] <= '9')
5811 else if(p[1] >= 'a' && p[1] <= 'f')
5812 hi = p[1] - 'a' + 10;
5813 else if(p[1] >= 'A' && p[1] <= 'F')
5814 hi = p[1] - 'A' + 10;
5817 if(p[2] >= '0' && p[2] <= '9')
5819 else if(p[2] >= 'a' && p[2] <= 'f')
5820 lo = p[2] - 'a' + 10;
5821 else if(p[2] >= 'A' && p[2] <= 'F')
5822 lo = p[2] - 'A' + 10;
5825 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5826 *q++ = (char) (hi * 0x10 + lo);
5837 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5840 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5841 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5842 void VM_whichpack (void)
5844 const char *fn, *pack;
5846 VM_SAFEPARMCOUNT(1, VM_whichpack);
5847 fn = PRVM_G_STRING(OFS_PARM0);
5848 pack = FS_WhichPack(fn);
5850 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5858 char buffer[MAX_INPUTLINE];
5859 unsigned char *postdata; // free when uri_to_prog_t is freed
5861 char *sigdata; // free when uri_to_prog_t is freed
5866 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5868 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5870 if(!PRVM_ProgLoaded(handle->prognr))
5872 // curl reply came too late... so just drop it
5873 if(handle->postdata)
5874 Z_Free(handle->postdata);
5876 Z_Free(handle->sigdata);
5881 PRVM_SetProg(handle->prognr);
5883 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
5885 if(length_received >= sizeof(handle->buffer))
5886 length_received = sizeof(handle->buffer) - 1;
5887 handle->buffer[length_received] = 0;
5889 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5890 PRVM_G_FLOAT(OFS_PARM1) = status;
5891 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5892 PRVM_ExecuteProgram(PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
5896 if(handle->postdata)
5897 Z_Free(handle->postdata);
5899 Z_Free(handle->sigdata);
5903 // 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
5904 // 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
5905 void VM_uri_get (void)
5910 uri_to_prog_t *handle;
5911 const char *posttype = NULL;
5912 const char *postseparator = NULL;
5913 int poststringbuffer = -1;
5915 const char *query_string = NULL;
5918 if(!PRVM_allfunction(URI_Get_Callback))
5919 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5921 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
5923 url = PRVM_G_STRING(OFS_PARM0);
5924 id = PRVM_G_FLOAT(OFS_PARM1);
5926 posttype = PRVM_G_STRING(OFS_PARM2);
5928 postseparator = PRVM_G_STRING(OFS_PARM3);
5930 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
5932 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
5933 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!
5935 query_string = strchr(url, '?');
5938 lq = query_string ? strlen(query_string) : 0;
5940 handle->prognr = PRVM_GetProgNr();
5941 handle->starttime = prog->starttime;
5943 if(postseparator && posttype && *posttype)
5945 size_t l = strlen(postseparator);
5946 if(poststringbuffer >= 0)
5951 prvm_stringbuffer_t *stringbuffer;
5952 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
5955 VM_Warning("uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5959 for(i = 0;i < stringbuffer->num_strings;i++)
5963 if(stringbuffer->strings[i])
5964 ltotal += strlen(stringbuffer->strings[i]);
5966 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
5967 handle->postlen = ltotal;
5969 for(i = 0;i < stringbuffer->num_strings;i++)
5973 memcpy(handle->postdata + ltotal, postseparator, l);
5976 if(stringbuffer->strings[i])
5978 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
5979 ltotal += strlen(stringbuffer->strings[i]);
5982 if(ltotal != handle->postlen)
5983 PRVM_ERROR ("%s: string buffer content size mismatch, possible overrun", PRVM_NAME);
5987 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
5988 handle->postlen = l;
5989 memcpy(handle->postdata, postseparator, l);
5991 handle->postdata[handle->postlen] = 0;
5993 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
5996 // POST: we sign postdata \0 query string
5998 handle->sigdata = (char *)Z_Malloc(8192);
5999 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6000 l = strlen(handle->sigdata);
6001 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
6004 Z_Free(handle->sigdata);
6005 handle->sigdata = NULL;
6008 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6011 Z_Free(handle->sigdata);
6012 handle->sigdata = NULL;
6015 handle->siglen = l + ll;
6016 handle->sigdata[handle->siglen] = 0;
6019 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, posttype, handle->postdata, handle->postlen, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6023 if(postkeyid >= 0 && query_string)
6025 // GET: we sign JUST the query string
6027 handle->sigdata = (char *)Z_Malloc(8192);
6028 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6029 l = strlen(handle->sigdata);
6030 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
6033 Z_Free(handle->sigdata);
6034 handle->sigdata = NULL;
6037 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6040 Z_Free(handle->sigdata);
6041 handle->sigdata = NULL;
6044 handle->siglen = l + ll;
6045 handle->sigdata[handle->siglen] = 0;
6048 handle->postdata = NULL;
6049 handle->postlen = 0;
6050 ret = Curl_Begin_ToMemory(url, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6054 PRVM_G_INT(OFS_RETURN) = 1;
6058 if(handle->postdata)
6059 Z_Free(handle->postdata);
6061 Z_Free(handle->sigdata);
6063 PRVM_G_INT(OFS_RETURN) = 0;
6067 void VM_netaddress_resolve (void)
6070 char normalized[128];
6072 lhnetaddress_t addr;
6074 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
6076 ip = PRVM_G_STRING(OFS_PARM0);
6079 port = (int) PRVM_G_FLOAT(OFS_PARM1);
6081 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
6082 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
6084 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
6087 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
6088 void VM_CL_getextresponse (void)
6090 VM_SAFEPARMCOUNT(0,VM_argv);
6092 if (cl_net_extresponse_count <= 0)
6093 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6097 --cl_net_extresponse_count;
6098 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6099 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(cl_net_extresponse[first]);
6103 void VM_SV_getextresponse (void)
6105 VM_SAFEPARMCOUNT(0,VM_argv);
6107 if (sv_net_extresponse_count <= 0)
6108 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6112 --sv_net_extresponse_count;
6113 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6114 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(sv_net_extresponse[first]);
6120 Common functions between menu.dat and clsprogs
6124 //#349 float() isdemo
6125 void VM_CL_isdemo (void)
6127 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
6128 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
6131 //#355 float() videoplaying
6132 void VM_CL_videoplaying (void)
6134 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
6135 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6142 callfunction(...,string function_name)
6146 mfunction_t *PRVM_ED_FindFunction (const char *name);
6147 void VM_callfunction(void)
6152 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6154 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6156 VM_CheckEmptyString(s);
6158 func = PRVM_ED_FindFunction(s);
6161 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
6162 else if (func->first_statement < 0)
6164 // negative statements are built in functions
6165 int builtinnumber = -func->first_statement;
6166 prog->xfunction->builtinsprofile++;
6167 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6168 prog->builtins[builtinnumber]();
6170 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
6172 else if(func - prog->functions > 0)
6175 PRVM_ExecuteProgram(func - prog->functions,"");
6184 float isfunction(string function_name)
6187 mfunction_t *PRVM_ED_FindFunction (const char *name);
6188 void VM_isfunction(void)
6193 VM_SAFEPARMCOUNT(1, VM_isfunction);
6195 s = PRVM_G_STRING(OFS_PARM0);
6197 VM_CheckEmptyString(s);
6199 func = PRVM_ED_FindFunction(s);
6202 PRVM_G_FLOAT(OFS_RETURN) = false;
6204 PRVM_G_FLOAT(OFS_RETURN) = true;
6211 string sprintf(string format, ...)
6215 void VM_sprintf(void)
6218 char outbuf[MAX_INPUTLINE];
6219 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6221 int width, precision, thisarg, flags;
6225 static int dummyivec[3] = {0, 0, 0};
6226 static float dummyvec[3] = {0, 0, 0};
6228 #define PRINTF_ALTERNATE 1
6229 #define PRINTF_ZEROPAD 2
6230 #define PRINTF_LEFT 4
6231 #define PRINTF_SPACEPOSITIVE 8
6232 #define PRINTF_SIGNPOSITIVE 16
6236 s = PRVM_G_STRING(OFS_PARM0);
6238 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6239 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6240 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6241 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((int*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6242 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6257 // complete directive format:
6266 // is number following?
6267 if(*s >= '0' && *s <= '9')
6269 width = strtol(s, &err, 10);
6272 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6285 flags |= PRINTF_ZEROPAD;
6287 width = -1; // it was just a flag
6299 case '#': flags |= PRINTF_ALTERNATE; break;
6300 case '0': flags |= PRINTF_ZEROPAD; break;
6301 case '-': flags |= PRINTF_LEFT; break;
6302 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6303 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6313 if(*s >= '0' && *s <= '9')
6315 width = strtol(s, &err, 10);
6316 if(!err || *err != '$')
6318 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6325 width = GETARG_FLOAT(width);
6328 flags |= PRINTF_LEFT;
6332 else if(*s >= '0' && *s <= '9')
6334 width = strtol(s, &err, 10);
6337 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6343 flags |= PRINTF_LEFT;
6347 // otherwise width stays -1
6356 if(*s >= '0' && *s <= '9')
6358 precision = strtol(s, &err, 10);
6359 if(!err || *err != '$')
6361 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6367 precision = argpos++;
6368 precision = GETARG_FLOAT(precision);
6370 else if(*s >= '0' && *s <= '9')
6372 precision = strtol(s, &err, 10);
6375 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6382 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6391 case 'h': isfloat = 1; break;
6392 case 'l': isfloat = 0; break;
6393 case 'L': isfloat = 0; break;
6404 // now s points to the final directive char and is no longer changed
6419 if(*s != 's' && *s != 'c')
6420 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6421 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6422 if(flags & PRINTF_LEFT) *f++ = '-';
6423 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6424 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6434 if(width < 0) // not set
6440 if(precision < 0) // not set
6441 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6443 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6445 case 'o': case 'u': case 'x': case 'X':
6446 if(precision < 0) // not set
6447 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6449 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6451 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6452 if(precision < 0) // not set
6453 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6455 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6459 if(precision < 0) // not set
6460 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6461 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6462 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6463 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6466 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6467 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6468 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6469 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6473 if(flags & PRINTF_ALTERNATE)
6475 if(precision < 0) // not set
6476 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6478 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6482 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6483 const char *buf = u8_encodech(c, NULL);
6486 if(precision < 0) // not set
6487 precision = end - o - 1;
6488 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6492 if(flags & PRINTF_ALTERNATE)
6494 if(precision < 0) // not set
6495 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6497 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6501 if(precision < 0) // not set
6502 precision = end - o - 1;
6503 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6507 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6522 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outbuf);
6528 static dp_model_t *getmodel(prvm_edict_t *ed)
6530 switch(PRVM_GetProgNr())
6532 case PRVM_SERVERPROG:
6533 return SV_GetModelFromEdict(ed);
6534 case PRVM_CLIENTPROG:
6535 return CL_GetModelFromEdict(ed);
6543 unsigned int progid;
6545 frameblend_t frameblend[MAX_FRAMEBLENDS];
6546 skeleton_t *skeleton_p;
6547 skeleton_t skeleton;
6548 float *data_vertex3f;
6549 float *data_svector3f;
6550 float *data_tvector3f;
6551 float *data_normal3f;
6553 float *buf_vertex3f;
6554 float *buf_svector3f;
6555 float *buf_tvector3f;
6556 float *buf_normal3f;
6558 animatemodel_cache_t;
6559 static animatemodel_cache_t animatemodel_cache;
6561 void animatemodel(dp_model_t *model, prvm_edict_t *ed)
6563 skeleton_t *skeleton;
6564 int skeletonindex = -1;
6565 qboolean need = false;
6566 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6568 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6569 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6570 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6571 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6574 if(animatemodel_cache.progid != prog->id)
6575 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6576 need |= (animatemodel_cache.model != model);
6577 VM_GenerateFrameGroupBlend(ed->priv.server->framegroupblend, ed);
6578 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
6579 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6580 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6581 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6583 need |= (animatemodel_cache.skeleton_p != skeleton);
6585 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6588 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6590 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6591 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6592 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6593 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6594 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6595 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6596 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6597 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6598 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6600 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6601 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6602 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6603 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6604 VM_UpdateEdictSkeleton(ed, model, ed->priv.server->frameblend);
6605 model->AnimateVertices(model, ed->priv.server->frameblend, &ed->priv.server->skeleton, animatemodel_cache.data_vertex3f, animatemodel_cache.data_normal3f, animatemodel_cache.data_svector3f, animatemodel_cache.data_tvector3f);
6606 animatemodel_cache.progid = prog->id;
6607 animatemodel_cache.model = model;
6608 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6609 animatemodel_cache.skeleton_p = skeleton;
6611 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6614 static void getmatrix(prvm_edict_t *ed, matrix4x4_t *out)
6616 switch(PRVM_GetProgNr())
6618 case PRVM_SERVERPROG:
6619 SV_GetEntityMatrix(ed, out, false);
6621 case PRVM_CLIENTPROG:
6622 CL_GetEntityMatrix(ed, out, false);
6625 *out = identitymatrix;
6630 static void applytransform_forward(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6634 Matrix4x4_Transform(&m, in, out);
6637 static void applytransform_forward_direction(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6641 Matrix4x4_Transform3x3(&m, in, out);
6644 static void applytransform_inverted(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6648 Matrix4x4_Invert_Full(&n, &m);
6649 Matrix4x4_Transform3x3(&n, in, out);
6652 static void applytransform_forward_normal(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6657 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6661 static void clippointtosurface(prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6664 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6666 animatemodel(model, ed);
6667 bestdist = 1000000000;
6669 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6671 // clip original point to each triangle of the surface and find the
6672 // triangle that is closest
6673 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6674 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6675 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6676 TriangleNormal(v[0], v[1], v[2], facenormal);
6677 VectorNormalize(facenormal);
6678 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6679 VectorMA(p, offsetdist, facenormal, temp);
6680 for (j = 0, k = 2;j < 3;k = j, j++)
6682 VectorSubtract(v[k], v[j], edgenormal);
6683 CrossProduct(edgenormal, facenormal, sidenormal);
6684 VectorNormalize(sidenormal);
6685 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6687 VectorMA(temp, offsetdist, sidenormal, temp);
6689 dist = VectorDistance2(temp, p);
6690 if (bestdist > dist)
6693 VectorCopy(temp, out);
6698 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6700 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6702 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6706 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6707 void VM_getsurfacenumpoints(void)
6710 msurface_t *surface;
6711 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6712 // return 0 if no such surface
6713 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6715 PRVM_G_FLOAT(OFS_RETURN) = 0;
6719 // note: this (incorrectly) assumes it is a simple polygon
6720 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6722 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6723 void VM_getsurfacepoint(void)
6727 msurface_t *surface;
6729 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6730 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6731 ed = PRVM_G_EDICT(OFS_PARM0);
6732 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6734 // note: this (incorrectly) assumes it is a simple polygon
6735 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6736 if (pointnum < 0 || pointnum >= surface->num_vertices)
6738 animatemodel(model, ed);
6739 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6741 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6742 // float SPA_POSITION = 0;
6743 // float SPA_S_AXIS = 1;
6744 // float SPA_T_AXIS = 2;
6745 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6746 // float SPA_TEXCOORDS0 = 4;
6747 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6748 // float SPA_LIGHTMAP0_COLOR = 6;
6749 void VM_getsurfacepointattribute(void)
6753 msurface_t *surface;
6757 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6758 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6759 ed = PRVM_G_EDICT(OFS_PARM0);
6760 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6762 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6763 if (pointnum < 0 || pointnum >= surface->num_vertices)
6765 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6767 animatemodel(model, ed);
6769 switch( attributetype ) {
6770 // float SPA_POSITION = 0;
6772 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6774 // float SPA_S_AXIS = 1;
6776 applytransform_forward_direction(&(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6778 // float SPA_T_AXIS = 2;
6780 applytransform_forward_direction(&(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6782 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6784 applytransform_forward_direction(&(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6786 // float SPA_TEXCOORDS0 = 4;
6788 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6789 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6790 ret[0] = texcoord[0];
6791 ret[1] = texcoord[1];
6795 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6797 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6798 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6799 ret[0] = texcoord[0];
6800 ret[1] = texcoord[1];
6804 // float SPA_LIGHTMAP0_COLOR = 6;
6806 // ignore alpha for now..
6807 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
6810 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
6814 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
6815 void VM_getsurfacenormal(void)
6818 msurface_t *surface;
6820 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
6821 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6822 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6824 // note: this only returns the first triangle, so it doesn't work very
6825 // well for curved surfaces or arbitrary meshes
6826 animatemodel(model, PRVM_G_EDICT(OFS_PARM0));
6827 TriangleNormal((animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex), (animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex) + 3, (animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex) + 6, normal);
6828 applytransform_forward_normal(normal, PRVM_G_EDICT(OFS_PARM0), PRVM_G_VECTOR(OFS_RETURN));
6829 VectorNormalize(PRVM_G_VECTOR(OFS_RETURN));
6831 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
6832 void VM_getsurfacetexture(void)
6835 msurface_t *surface;
6836 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
6837 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6838 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6840 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(surface->texture->name);
6842 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
6843 void VM_getsurfacenearpoint(void)
6845 int surfacenum, best;
6847 vec_t dist, bestdist;
6850 msurface_t *surface;
6852 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
6853 PRVM_G_FLOAT(OFS_RETURN) = -1;
6854 ed = PRVM_G_EDICT(OFS_PARM0);
6855 point = PRVM_G_VECTOR(OFS_PARM1);
6857 if (!ed || ed->priv.server->free)
6859 model = getmodel(ed);
6860 if (!model || !model->num_surfaces)
6863 animatemodel(model, ed);
6865 applytransform_inverted(point, ed, p);
6867 bestdist = 1000000000;
6868 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
6870 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
6871 // first see if the nearest point on the surface's box is closer than the previous match
6872 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
6873 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
6874 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
6875 dist = VectorLength2(clipped);
6876 if (dist < bestdist)
6878 // it is, check the nearest point on the actual geometry
6879 clippointtosurface(ed, model, surface, p, clipped);
6880 VectorSubtract(clipped, p, clipped);
6881 dist += VectorLength2(clipped);
6882 if (dist < bestdist)
6884 // that's closer too, store it as the best match
6890 PRVM_G_FLOAT(OFS_RETURN) = best;
6892 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
6893 void VM_getsurfaceclippedpoint(void)
6897 msurface_t *surface;
6899 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
6900 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6901 ed = PRVM_G_EDICT(OFS_PARM0);
6902 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6904 animatemodel(model, ed);
6905 applytransform_inverted(PRVM_G_VECTOR(OFS_PARM2), ed, p);
6906 clippointtosurface(ed, model, surface, p, out);
6907 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
6910 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
6911 void VM_getsurfacenumtriangles(void)
6914 msurface_t *surface;
6915 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
6916 // return 0 if no such surface
6917 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6919 PRVM_G_FLOAT(OFS_RETURN) = 0;
6923 // note: this (incorrectly) assumes it is a simple polygon
6924 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
6926 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
6927 void VM_getsurfacetriangle(void)
6929 const vec3_t d = {-1, -1, -1};
6932 msurface_t *surface;
6934 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
6935 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6936 ed = PRVM_G_EDICT(OFS_PARM0);
6937 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6939 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
6940 if (trinum < 0 || trinum >= surface->num_triangles)
6942 // FIXME: implement rotation/scaling
6943 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
6950 void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f);
6952 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(ed, f); else World_Physics_ApplyCmd(ed, f)
6954 edict_odefunc_t *VM_physics_newstackfunction(prvm_edict_t *ed, edict_odefunc_t *f)
6956 edict_odefunc_t *newfunc, *func;
6958 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
6959 memcpy(newfunc, f, sizeof(edict_odefunc_t));
6960 newfunc->next = NULL;
6961 if (!ed->priv.server->ode_func)
6962 ed->priv.server->ode_func = newfunc;
6965 for (func = ed->priv.server->ode_func; func->next; func = func->next);
6966 func->next = newfunc;
6971 // void(entity e, float physics_enabled) physics_enable = #;
6972 void VM_physics_enable(void)
6977 VM_SAFEPARMCOUNT(2, VM_physics_enable);
6978 ed = PRVM_G_EDICT(OFS_PARM0);
6981 if (developer.integer > 0)
6982 VM_Warning("VM_physics_enable: null entity!\n");
6985 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6986 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6988 VM_Warning("VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
6991 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
6992 VM_physics_ApplyCmd(ed, &f);
6995 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
6996 void VM_physics_addforce(void)
7001 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
7002 ed = PRVM_G_EDICT(OFS_PARM0);
7005 if (developer.integer > 0)
7006 VM_Warning("VM_physics_addforce: null entity!\n");
7009 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7010 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7012 VM_Warning("VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
7015 f.type = ODEFUNC_RELFORCEATPOS;
7016 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7017 VectorSubtract(PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_PARM2), f.v2);
7018 VM_physics_ApplyCmd(ed, &f);
7021 // void(entity e, vector torque) physics_addtorque = #;
7022 void VM_physics_addtorque(void)
7027 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
7028 ed = PRVM_G_EDICT(OFS_PARM0);
7031 if (developer.integer > 0)
7032 VM_Warning("VM_physics_addtorque: null entity!\n");
7035 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7036 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7038 VM_Warning("VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
7041 f.type = ODEFUNC_RELTORQUE;
7042 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7043 VM_physics_ApplyCmd(ed, &f);