2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
19 extern cvar_t prvm_backtraceforwarnings;
21 extern dllhandle_t ode_dll;
24 // LadyHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
25 void VM_Warning(prvm_prog_t *prog, const char *fmt, ...)
28 char msg[MAX_INPUTLINE];
29 static double recursive = -1;
32 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
35 Con_Printf(CON_WARN "%s", msg);
37 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
38 if(prvm_backtraceforwarnings.integer && recursive != host.realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
40 recursive = host.realtime;
41 PRVM_PrintState(prog, 0);
47 //============================================================================
50 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
51 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
52 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LadyHavoc]
53 // TODO: will this war ever end? [2007-01-23 LadyHavoc]
55 void VM_CheckEmptyString(prvm_prog_t *prog, const char *s)
57 if (ISWHITESPACE(s[0]))
58 prog->error_cmd("%s: Bad string", prog->name);
61 void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
63 // self.frame is the interpolation target (new frame)
64 // self.frame1time is the animation base time for the interpolation target
65 // self.frame2 is the interpolation start (previous frame)
66 // self.frame2time is the animation base time for the interpolation start
67 // self.lerpfrac is the interpolation strength for self.frame2
68 // self.lerpfrac3 is the interpolation strength for self.frame3
69 // self.lerpfrac4 is the interpolation strength for self.frame4
70 // pitch angle on a player model where the animator set up 5 sets of
71 // animations and the csqc simply lerps between sets)
72 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
73 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
74 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
75 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
76 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
77 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
78 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
79 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
80 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
81 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
82 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
83 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
84 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
87 // LadyHavoc: quite tempting to break apart this function to reuse the
88 // duplicated code, but I suspect it is better for performance
90 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model, double curtime)
92 int sub2, numframes, f, i, k;
93 int isfirstframegroup = true;
95 double sublerp, lerp, d;
96 const animscene_t *scene;
97 const framegroupblend_t *g;
98 frameblend_t *blend = frameblend;
100 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
102 // rpolzer: Not testing isanimated here - a model might have
103 // "animations" that move no vertices (but only bones), thus rendering
104 // may assume it's not animated while processing can't.
111 nolerp = ((model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer) || (model->nolerp == true);
112 numframes = model->numframes;
113 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
116 if ((unsigned int)f >= (unsigned int)numframes)
118 if (developer_extra.integer)
119 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
127 if (isfirstframegroup)
130 isfirstframegroup = false;
135 if (model->animscenes)
137 scene = model->animscenes + f;
138 f = scene->firstframe;
139 if (scene->framecount > 1)
141 // this code path is only used on .zym models and torches
142 sublerp = scene->framerate * (curtime - g->start);
143 f = (int) floor(sublerp);
146 if (sublerp < (1.0 / 65536.0f))
148 if (sublerp > (65535.0f / 65536.0f))
154 f = (f % scene->framecount);
155 sub2 = (sub2 % scene->framecount);
157 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
158 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
160 // two framelerps produced from one animation
163 for (i = 0;i < MAX_FRAMEBLENDS;i++)
165 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
167 blend[i].subframe = sub2;
173 d = (1 - sublerp) * lerp;
178 for (i = 0;i < MAX_FRAMEBLENDS;i++)
180 if (blend[i].lerp <= 0 || blend[i].subframe == f)
182 blend[i].subframe = f;
191 void VM_UpdateEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
193 if (ed->priv.server->skeleton.model != edmodel)
195 VM_RemoveEdictSkeleton(prog, ed);
196 ed->priv.server->skeleton.model = edmodel;
198 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
200 if(ed->priv.server->skeleton.relativetransforms)
201 Mem_Free(ed->priv.server->skeleton.relativetransforms);
202 ed->priv.server->skeleton.relativetransforms = NULL;
207 int skeletonindex = -1;
208 skeleton_t *skeleton;
209 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
210 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
212 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
213 if (!ed->priv.server->skeleton.relativetransforms)
214 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
215 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
219 if(ed->priv.server->skeleton.relativetransforms)
220 Mem_Free(ed->priv.server->skeleton.relativetransforms);
221 ed->priv.server->skeleton.relativetransforms = NULL;
226 void VM_RemoveEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed)
228 if (ed->priv.server->skeleton.relativetransforms)
229 Mem_Free(ed->priv.server->skeleton.relativetransforms);
230 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
236 //============================================================================
239 void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength)
245 outend = out + outlength - 1;
246 for (i = first;i < prog->argc && out < outend;i++)
248 s = PRVM_G_STRING((OFS_PARM0+i*3));
249 while (out < outend && *s)
259 returns true if the extension is supported by the server
261 checkextension(extensionname)
265 // kind of helper function
266 static qboolean checkextension(prvm_prog_t *prog, const char *name)
270 for (e = prog->extensionstring;*e;e++)
272 if(!strcasecmp(*e, name))
275 // special sheck for ODE
276 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
278 #ifndef LINK_TO_LIBODE
279 return ode_dll ? true : false;
281 #ifdef LINK_TO_LIBODE
290 // special sheck for d0_blind_id
291 if (!strcasecmp("DP_CRYPTO", name))
292 return Crypto_Available();
293 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
294 return Crypto_Available();
302 void VM_checkextension(prvm_prog_t *prog)
304 VM_SAFEPARMCOUNT(1,VM_checkextension);
306 PRVM_G_FLOAT(OFS_RETURN) = checkextension(prog, PRVM_G_STRING(OFS_PARM0));
313 This is a TERMINAL error, which will kill off the entire prog.
319 void VM_error(prvm_prog_t *prog)
322 char string[VM_STRINGTEMP_LENGTH];
324 VM_VarString(prog, 0, string, sizeof(string));
325 Con_Printf(CON_ERROR "======%s ERROR in %s:\n%s\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
326 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
327 PRVM_ED_Print(prog, ed, NULL);
329 prog->error_cmd("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
336 Dumps out self, then an error message. The program is aborted and self is
337 removed, but the level can continue.
342 void VM_objerror(prvm_prog_t *prog)
345 char string[VM_STRINGTEMP_LENGTH];
347 VM_VarString(prog, 0, string, sizeof(string));
348 Con_Printf(CON_ERROR "======OBJECT ERROR======\n"); // , prog->name, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
349 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
350 PRVM_ED_Print(prog, ed, NULL);
351 PRVM_ED_Free (prog, ed);
352 Con_Printf(CON_ERROR "%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
364 void VM_print(prvm_prog_t *prog)
366 char string[VM_STRINGTEMP_LENGTH];
368 VM_VarString(prog, 0, string, sizeof(string));
376 broadcast print to everyone on server
381 void VM_bprint(prvm_prog_t *prog)
383 char string[VM_STRINGTEMP_LENGTH];
387 VM_Warning(prog, "VM_bprint: game is not server(%s) !\n", prog->name);
391 VM_VarString(prog, 0, string, sizeof(string));
392 SV_BroadcastPrint(string);
397 VM_sprint (menu & client but only if server.active == true)
399 single print to a specific client
401 sprint(float clientnum,...[string])
404 void VM_sprint(prvm_prog_t *prog)
408 char string[VM_STRINGTEMP_LENGTH];
410 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
412 //find client for this entity
413 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
414 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
416 VM_Warning(prog, "VM_sprint: %s: invalid client or server is not active !\n", prog->name);
420 client = svs.clients + clientnum;
421 if (!client->netconnection)
424 VM_VarString(prog, 1, string, sizeof(string));
425 MSG_WriteChar(&client->netconnection->message,svc_print);
426 MSG_WriteString(&client->netconnection->message, string);
433 single print to the screen
438 void VM_centerprint(prvm_prog_t *prog)
440 char string[VM_STRINGTEMP_LENGTH];
442 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
443 VM_VarString(prog, 0, string, sizeof(string));
444 SCR_CenterPrint(string);
451 vector normalize(vector)
454 void VM_normalize(prvm_prog_t *prog)
460 VM_SAFEPARMCOUNT(1,VM_normalize);
462 value1 = PRVM_G_VECTOR(OFS_PARM0);
464 f = VectorLength2(value1);
468 VectorScale(value1, f, newvalue);
471 VectorClear(newvalue);
473 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
483 void VM_vlen(prvm_prog_t *prog)
485 VM_SAFEPARMCOUNT(1,VM_vlen);
486 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
493 float vectoyaw(vector)
496 void VM_vectoyaw(prvm_prog_t *prog)
501 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
503 value1 = PRVM_G_VECTOR(OFS_PARM0);
505 if (value1[1] == 0 && value1[0] == 0)
509 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
514 PRVM_G_FLOAT(OFS_RETURN) = yaw;
522 vector vectoangles(vector[, vector])
525 void VM_vectoangles(prvm_prog_t *prog)
527 vec3_t result, forward, up;
528 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
530 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), forward);
533 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), up);
534 AnglesFromVectors(result, forward, up, true);
537 AnglesFromVectors(result, forward, NULL, true);
538 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
545 Returns a number from 0<= num < 1
550 void VM_random(prvm_prog_t *prog)
552 VM_SAFEPARMCOUNT(0,VM_random);
554 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
561 localsound(string sample)
564 void VM_localsound(prvm_prog_t *prog)
568 VM_SAFEPARMCOUNT(1,VM_localsound);
570 s = PRVM_G_STRING(OFS_PARM0);
572 if(!S_LocalSound (s))
574 PRVM_G_FLOAT(OFS_RETURN) = -4;
575 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
579 PRVM_G_FLOAT(OFS_RETURN) = 1;
589 void VM_break(prvm_prog_t *prog)
591 prog->error_cmd("%s: break statement", prog->name);
594 //============================================================================
600 Sends text over to the client's execution buffer
602 [localcmd (string, ...) or]
606 void VM_localcmd_client(prvm_prog_t *prog)
608 char string[VM_STRINGTEMP_LENGTH];
609 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd_client);
610 VM_VarString(prog, 0, string, sizeof(string));
611 Cbuf_AddText(&cmd_client, string);
618 Sends text over to the server's execution buffer
620 [localcmd (string, ...) or]
624 void VM_localcmd_server(prvm_prog_t *prog)
626 char string[VM_STRINGTEMP_LENGTH];
627 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd_server);
628 VM_VarString(prog, 0, string, sizeof(string));
629 Cbuf_AddText(&cmd_server, string);
632 static qboolean PRVM_Cvar_ReadOk(prvm_prog_t *prog, const char *string)
635 cvar = Cvar_FindVar(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask);
636 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
646 void VM_cvar(prvm_prog_t *prog)
648 char string[VM_STRINGTEMP_LENGTH];
649 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
650 VM_VarString(prog, 0, string, sizeof(string));
651 VM_CheckEmptyString(prog, string);
652 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(prog, string) ? Cvar_VariableValue(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask) : 0;
659 float cvar_type (string)
660 float CVAR_TYPEFLAG_EXISTS = 1;
661 float CVAR_TYPEFLAG_SAVED = 2;
662 float CVAR_TYPEFLAG_PRIVATE = 4;
663 float CVAR_TYPEFLAG_ENGINE = 8;
664 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
665 float CVAR_TYPEFLAG_READONLY = 32;
668 void VM_cvar_type(prvm_prog_t *prog)
670 char string[VM_STRINGTEMP_LENGTH];
674 VM_SAFEPARMCOUNTRANGE(1, 8, VM_cvar_type);
675 VM_VarString(prog, 0, string, sizeof(string));
676 VM_CheckEmptyString(prog, string);
677 cvar = Cvar_FindVar(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask);
682 PRVM_G_FLOAT(OFS_RETURN) = 0;
683 return; // CVAR_TYPE_NONE
686 ret = 1; // CVAR_EXISTS
687 if(cvar->flags & CVAR_SAVE)
688 ret |= 2; // CVAR_TYPE_SAVED
689 if(cvar->flags & CVAR_PRIVATE)
690 ret |= 4; // CVAR_TYPE_PRIVATE
691 if(!(cvar->flags & CVAR_ALLOCATED))
692 ret |= 8; // CVAR_TYPE_ENGINE
693 if(cvar->description != cvar_dummy_description)
694 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
695 if(cvar->flags & CVAR_READONLY)
696 ret |= 32; // CVAR_TYPE_READONLY
698 PRVM_G_FLOAT(OFS_RETURN) = ret;
705 const string VM_cvar_string (string, ...)
708 void VM_cvar_string(prvm_prog_t *prog)
710 char string[VM_STRINGTEMP_LENGTH];
711 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
712 VM_VarString(prog, 0, string, sizeof(string));
713 VM_CheckEmptyString(prog, string);
714 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_Cvar_ReadOk(prog, string) ? Cvar_VariableString(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask) : "");
719 ========================
722 const string VM_cvar_defstring (string, ...)
723 ========================
725 void VM_cvar_defstring(prvm_prog_t *prog)
727 char string[VM_STRINGTEMP_LENGTH];
728 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
729 VM_VarString(prog, 0, string, sizeof(string));
730 VM_CheckEmptyString(prog, string);
731 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDefString(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask));
735 ========================
738 const string VM_cvar_description (string, ...)
739 ========================
741 void VM_cvar_description(prvm_prog_t *prog)
743 char string[VM_STRINGTEMP_LENGTH];
744 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
745 VM_VarString(prog, 0, string, sizeof(string));
746 VM_CheckEmptyString(prog, string);
747 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDescription(prog->console_cmd->cvars, string, prog->console_cmd->cvars_flagsmask));
753 void cvar_set (string,string, ...)
756 void VM_cvar_set(prvm_prog_t *prog)
759 char string[VM_STRINGTEMP_LENGTH];
760 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
761 VM_VarString(prog, 1, string, sizeof(string));
762 name = PRVM_G_STRING(OFS_PARM0);
763 VM_CheckEmptyString(prog, name);
764 Cvar_Set(prog->console_cmd->cvars, name, string);
774 void VM_dprint(prvm_prog_t *prog)
776 char string[VM_STRINGTEMP_LENGTH];
777 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
778 VM_VarString(prog, 0, string, sizeof(string));
780 Con_DPrintf("%s", string);
782 Con_DPrintf("%s: %s", prog->name, string);
794 void VM_ftos(prvm_prog_t *prog)
799 VM_SAFEPARMCOUNT(1, VM_ftos);
801 v = PRVM_G_FLOAT(OFS_PARM0);
803 if ((prvm_vec_t)((prvm_int_t)v) == v)
804 dpsnprintf(s, sizeof(s), "%.0f", v);
806 dpsnprintf(s, sizeof(s), "%f", v);
807 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
818 void VM_fabs(prvm_prog_t *prog)
822 VM_SAFEPARMCOUNT(1,VM_fabs);
824 v = PRVM_G_FLOAT(OFS_PARM0);
825 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
836 void VM_vtos(prvm_prog_t *prog)
840 VM_SAFEPARMCOUNT(1,VM_vtos);
842 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]);
843 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
854 void VM_etos(prvm_prog_t *prog)
858 VM_SAFEPARMCOUNT(1, VM_etos);
860 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
861 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
868 float stof(...[string])
871 void VM_stof(prvm_prog_t *prog)
873 char string[VM_STRINGTEMP_LENGTH];
874 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
875 VM_VarString(prog, 0, string, sizeof(string));
876 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
880 ========================
884 ========================
886 void VM_itof(prvm_prog_t *prog)
888 VM_SAFEPARMCOUNT(1, VM_itof);
889 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
893 ========================
896 entity ftoe(float num)
897 ========================
899 void VM_ftoe(prvm_prog_t *prog)
902 VM_SAFEPARMCOUNT(1, VM_ftoe);
904 ent = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM0);
905 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
906 ent = 0; // return world instead of a free or invalid entity
908 PRVM_G_INT(OFS_RETURN) = ent;
912 ========================
915 float etof(entity ent)
916 ========================
918 void VM_etof(prvm_prog_t *prog)
920 VM_SAFEPARMCOUNT(1, VM_etof);
921 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
928 string strftime(float uselocaltime, string[, string ...])
931 void VM_strftime(prvm_prog_t *prog)
940 char fmt[VM_STRINGTEMP_LENGTH];
941 char result[VM_STRINGTEMP_LENGTH];
942 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
943 VM_VarString(prog, 1, fmt, sizeof(fmt));
946 if (PRVM_G_FLOAT(OFS_PARM0))
947 tmresult = localtime_s(&tm, &t);
949 tmresult = gmtime_s(&tm, &t);
952 if (PRVM_G_FLOAT(OFS_PARM0))
959 PRVM_G_INT(OFS_RETURN) = 0;
963 strftime(result, sizeof(result), fmt, &tm);
965 strftime(result, sizeof(result), fmt, tm);
967 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, result);
978 void VM_spawn(prvm_prog_t *prog)
981 VM_SAFEPARMCOUNT(0, VM_spawn);
982 prog->xfunction->builtinsprofile += 20;
983 ed = PRVM_ED_Alloc(prog);
995 void VM_remove(prvm_prog_t *prog)
998 prog->xfunction->builtinsprofile += 20;
1000 VM_SAFEPARMCOUNT(1, VM_remove);
1002 ed = PRVM_G_EDICT(OFS_PARM0);
1003 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
1005 if (developer.integer > 0)
1006 VM_Warning(prog, "VM_remove: tried to remove the null entity or a reserved entity!\n" );
1008 else if( ed->priv.required->free )
1010 if (developer.integer > 0)
1011 VM_Warning(prog, "VM_remove: tried to remove an already freed entity!\n" );
1014 PRVM_ED_Free (prog, ed);
1021 entity find(entity start, .string field, string match)
1025 void VM_find(prvm_prog_t *prog)
1032 VM_SAFEPARMCOUNT(3,VM_find);
1034 e = PRVM_G_EDICTNUM(OFS_PARM0);
1035 f = PRVM_G_INT(OFS_PARM1);
1036 s = PRVM_G_STRING(OFS_PARM2);
1038 // LadyHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1039 // expects it to find all the monsters, so we must be careful to support
1042 for (e++ ; e < prog->num_edicts ; e++)
1044 prog->xfunction->builtinsprofile++;
1045 ed = PRVM_EDICT_NUM(e);
1046 if (ed->priv.required->free)
1048 t = PRVM_E_STRING(ed,f);
1053 VM_RETURN_EDICT(ed);
1058 VM_RETURN_EDICT(prog->edicts);
1065 entity findfloat(entity start, .float field, float match)
1066 entity findentity(entity start, .entity field, entity match)
1069 // LadyHavoc: added this for searching float, int, and entity reference fields
1070 void VM_findfloat(prvm_prog_t *prog)
1077 VM_SAFEPARMCOUNT(3,VM_findfloat);
1079 e = PRVM_G_EDICTNUM(OFS_PARM0);
1080 f = PRVM_G_INT(OFS_PARM1);
1081 s = PRVM_G_FLOAT(OFS_PARM2);
1083 for (e++ ; e < prog->num_edicts ; e++)
1085 prog->xfunction->builtinsprofile++;
1086 ed = PRVM_EDICT_NUM(e);
1087 if (ed->priv.required->free)
1089 if (PRVM_E_FLOAT(ed,f) == s)
1091 VM_RETURN_EDICT(ed);
1096 VM_RETURN_EDICT(prog->edicts);
1103 entity findchain(.string field, string match)
1106 // chained search for strings in entity fields
1107 // entity(.string field, string match) findchain = #402;
1108 void VM_findchain(prvm_prog_t *prog)
1113 prvm_edict_t *ent, *chain;
1116 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1119 chainfield = PRVM_G_INT(OFS_PARM2);
1121 chainfield = prog->fieldoffsets.chain;
1123 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1125 chain = prog->edicts;
1127 f = PRVM_G_INT(OFS_PARM0);
1128 s = PRVM_G_STRING(OFS_PARM1);
1130 // LadyHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1131 // expects it to find all the monsters, so we must be careful to support
1134 ent = PRVM_NEXT_EDICT(prog->edicts);
1135 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1137 prog->xfunction->builtinsprofile++;
1138 if (ent->priv.required->free)
1140 t = PRVM_E_STRING(ent,f);
1146 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1150 VM_RETURN_EDICT(chain);
1157 entity findchainfloat(.string field, float match)
1158 entity findchainentity(.string field, entity match)
1161 // LadyHavoc: chained search for float, int, and entity reference fields
1162 // entity(.string field, float match) findchainfloat = #403;
1163 void VM_findchainfloat(prvm_prog_t *prog)
1168 prvm_edict_t *ent, *chain;
1171 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1174 chainfield = PRVM_G_INT(OFS_PARM2);
1176 chainfield = prog->fieldoffsets.chain;
1178 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1180 chain = (prvm_edict_t *)prog->edicts;
1182 f = PRVM_G_INT(OFS_PARM0);
1183 s = PRVM_G_FLOAT(OFS_PARM1);
1185 ent = PRVM_NEXT_EDICT(prog->edicts);
1186 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1188 prog->xfunction->builtinsprofile++;
1189 if (ent->priv.required->free)
1191 if (PRVM_E_FLOAT(ent,f) != s)
1194 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1198 VM_RETURN_EDICT(chain);
1202 ========================
1205 entity findflags(entity start, .float field, float match)
1206 ========================
1208 // LadyHavoc: search for flags in float fields
1209 void VM_findflags(prvm_prog_t *prog)
1216 VM_SAFEPARMCOUNT(3, VM_findflags);
1219 e = PRVM_G_EDICTNUM(OFS_PARM0);
1220 f = PRVM_G_INT(OFS_PARM1);
1221 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM2);
1223 for (e++ ; e < prog->num_edicts ; e++)
1225 prog->xfunction->builtinsprofile++;
1226 ed = PRVM_EDICT_NUM(e);
1227 if (ed->priv.required->free)
1229 if (!PRVM_E_FLOAT(ed,f))
1231 if ((prvm_int_t)PRVM_E_FLOAT(ed,f) & s)
1233 VM_RETURN_EDICT(ed);
1238 VM_RETURN_EDICT(prog->edicts);
1242 ========================
1245 entity findchainflags(.float field, float match)
1246 ========================
1248 // LadyHavoc: chained search for flags in float fields
1249 void VM_findchainflags(prvm_prog_t *prog)
1254 prvm_edict_t *ent, *chain;
1257 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1260 chainfield = PRVM_G_INT(OFS_PARM2);
1262 chainfield = prog->fieldoffsets.chain;
1264 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1266 chain = (prvm_edict_t *)prog->edicts;
1268 f = PRVM_G_INT(OFS_PARM0);
1269 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
1271 ent = PRVM_NEXT_EDICT(prog->edicts);
1272 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1274 prog->xfunction->builtinsprofile++;
1275 if (ent->priv.required->free)
1277 if (!PRVM_E_FLOAT(ent,f))
1279 if (!((prvm_int_t)PRVM_E_FLOAT(ent,f) & s))
1282 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1286 VM_RETURN_EDICT(chain);
1293 string precache_sound (string sample)
1296 void VM_precache_sound(prvm_prog_t *prog)
1300 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1302 s = PRVM_G_STRING(OFS_PARM0);
1303 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1304 //VM_CheckEmptyString(prog, s);
1306 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1308 VM_Warning(prog, "VM_precache_sound: Failed to load %s for %s\n", s, prog->name);
1317 returns the same string as output
1319 does nothing, only used by qcc to build .pak archives
1322 void VM_precache_file(prvm_prog_t *prog)
1324 VM_SAFEPARMCOUNT(1,VM_precache_file);
1325 // precache_file is only used to copy files with qcc, it does nothing
1326 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1336 void VM_coredump(prvm_prog_t *prog)
1338 cmd_state_t *cmd = cls.state == ca_dedicated ? &cmd_server : &cmd_client;
1339 VM_SAFEPARMCOUNT(0,VM_coredump);
1341 Cbuf_AddText(cmd, "prvm_edicts ");
1342 Cbuf_AddText(cmd, prog->name);
1343 Cbuf_AddText(cmd, "\n");
1353 void VM_stackdump(prvm_prog_t *prog)
1355 VM_SAFEPARMCOUNT(0, VM_stackdump);
1357 PRVM_StackTrace(prog);
1368 void VM_crash(prvm_prog_t *prog)
1370 VM_SAFEPARMCOUNT(0, VM_crash);
1372 prog->error_cmd("Crash called by %s",prog->name);
1382 void VM_traceon(prvm_prog_t *prog)
1384 VM_SAFEPARMCOUNT(0,VM_traceon);
1396 void VM_traceoff(prvm_prog_t *prog)
1398 VM_SAFEPARMCOUNT(0,VM_traceoff);
1400 prog->trace = false;
1410 void VM_eprint(prvm_prog_t *prog)
1412 VM_SAFEPARMCOUNT(1,VM_eprint);
1414 PRVM_ED_PrintNum (prog, PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1424 void VM_rint(prvm_prog_t *prog)
1427 VM_SAFEPARMCOUNT(1,VM_rint);
1429 f = PRVM_G_FLOAT(OFS_PARM0);
1431 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1433 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1443 void VM_floor(prvm_prog_t *prog)
1445 VM_SAFEPARMCOUNT(1,VM_floor);
1447 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1457 void VM_ceil(prvm_prog_t *prog)
1459 VM_SAFEPARMCOUNT(1,VM_ceil);
1461 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1469 entity nextent(entity)
1472 void VM_nextent(prvm_prog_t *prog)
1477 VM_SAFEPARMCOUNT(1, VM_nextent);
1479 i = PRVM_G_EDICTNUM(OFS_PARM0);
1482 prog->xfunction->builtinsprofile++;
1484 if (i == prog->num_edicts)
1486 VM_RETURN_EDICT(prog->edicts);
1489 ent = PRVM_EDICT_NUM(i);
1490 if (!ent->priv.required->free)
1492 VM_RETURN_EDICT(ent);
1498 //=============================================================================
1505 changelevel(string map)
1508 void VM_changelevel(prvm_prog_t *prog)
1511 VM_SAFEPARMCOUNT(1, VM_changelevel);
1515 VM_Warning(prog, "VM_changelevel: game is not server (%s)\n", prog->name);
1519 // make sure we don't issue two changelevels
1520 if (svs.changelevel_issued)
1522 svs.changelevel_issued = true;
1524 Cbuf_AddText(&cmd_server, va(vabuf, sizeof(vabuf), "changelevel %s\n", PRVM_G_STRING(OFS_PARM0)));
1534 void VM_sin(prvm_prog_t *prog)
1536 VM_SAFEPARMCOUNT(1,VM_sin);
1537 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1546 void VM_cos(prvm_prog_t *prog)
1548 VM_SAFEPARMCOUNT(1,VM_cos);
1549 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1559 void VM_sqrt(prvm_prog_t *prog)
1561 VM_SAFEPARMCOUNT(1,VM_sqrt);
1562 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1572 void VM_asin(prvm_prog_t *prog)
1574 VM_SAFEPARMCOUNT(1,VM_asin);
1575 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1584 void VM_acos(prvm_prog_t *prog)
1586 VM_SAFEPARMCOUNT(1,VM_acos);
1587 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1596 void VM_atan(prvm_prog_t *prog)
1598 VM_SAFEPARMCOUNT(1,VM_atan);
1599 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1605 float atan2(float,float)
1608 void VM_atan2(prvm_prog_t *prog)
1610 VM_SAFEPARMCOUNT(2,VM_atan2);
1611 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1620 void VM_tan(prvm_prog_t *prog)
1622 VM_SAFEPARMCOUNT(1,VM_tan);
1623 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1630 Returns a vector of length < 1 and > 0
1635 void VM_randomvec(prvm_prog_t *prog)
1638 VM_SAFEPARMCOUNT(0, VM_randomvec);
1640 VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1643 //=============================================================================
1649 float registercvar (string name, string value[, float flags])
1652 void VM_registercvar(prvm_prog_t *prog)
1654 const char *name, *value;
1657 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1659 name = PRVM_G_STRING(OFS_PARM0);
1660 value = PRVM_G_STRING(OFS_PARM1);
1661 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1662 PRVM_G_FLOAT(OFS_RETURN) = 0;
1664 if(flags > CVAR_MAXFLAGSVAL)
1667 // first check to see if it has already been defined
1668 if (Cvar_FindVar (prog->console_cmd->cvars, name, prog->console_cmd->cvars_flagsmask))
1671 // check for overlap with a command
1672 if (Cmd_Exists(&cmd_client, name) || Cmd_Exists(&cmd_server, name))
1674 VM_Warning(prog, "VM_registercvar: %s is a command\n", name);
1678 Cvar_Get(prog->console_cmd->cvars, name, value, prog->console_cmd->cvars_flagsmask | flags, NULL);
1680 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1688 returns the minimum of two supplied floats
1690 float min(float a, float b, ...[float])
1693 void VM_min(prvm_prog_t *prog)
1695 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1696 // LadyHavoc: 3+ argument enhancement suggested by FrikaC
1697 if (prog->argc >= 3)
1700 float f = PRVM_G_FLOAT(OFS_PARM0);
1701 for (i = 1;i < prog->argc;i++)
1702 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1703 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1704 PRVM_G_FLOAT(OFS_RETURN) = f;
1707 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1714 returns the maximum of two supplied floats
1716 float max(float a, float b, ...[float])
1719 void VM_max(prvm_prog_t *prog)
1721 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1722 // LadyHavoc: 3+ argument enhancement suggested by FrikaC
1723 if (prog->argc >= 3)
1726 float f = PRVM_G_FLOAT(OFS_PARM0);
1727 for (i = 1;i < prog->argc;i++)
1728 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1729 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1730 PRVM_G_FLOAT(OFS_RETURN) = f;
1733 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1740 returns number bounded by supplied range
1742 float bound(float min, float value, float max)
1745 void VM_bound(prvm_prog_t *prog)
1747 VM_SAFEPARMCOUNT(3,VM_bound);
1748 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1755 returns a raised to power b
1757 float pow(float a, float b)
1760 void VM_pow(prvm_prog_t *prog)
1762 VM_SAFEPARMCOUNT(2,VM_pow);
1763 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1766 void VM_log(prvm_prog_t *prog)
1768 VM_SAFEPARMCOUNT(1,VM_log);
1769 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1772 void VM_Files_Init(prvm_prog_t *prog)
1775 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1776 prog->openfiles[i] = NULL;
1779 void VM_Files_CloseAll(prvm_prog_t *prog)
1782 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1784 if (prog->openfiles[i])
1785 FS_Close(prog->openfiles[i]);
1786 prog->openfiles[i] = NULL;
1790 static qfile_t *VM_GetFileHandle(prvm_prog_t *prog, int index)
1792 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1794 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, prog->name);
1797 if (prog->openfiles[index] == NULL)
1799 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, prog->name);
1802 return prog->openfiles[index];
1809 float fopen(string filename, float mode)
1812 // float(string filename, float mode) fopen = #110;
1813 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1814 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1815 void VM_fopen(prvm_prog_t *prog)
1818 const char *modestring, *filename;
1821 VM_SAFEPARMCOUNT(2,VM_fopen);
1823 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1824 if (prog->openfiles[filenum] == NULL)
1826 if (filenum >= PRVM_MAX_OPENFILES)
1828 PRVM_G_FLOAT(OFS_RETURN) = -2;
1829 VM_Warning(prog, "VM_fopen: %s ran out of file handles (%i)\n", prog->name, PRVM_MAX_OPENFILES);
1832 filename = PRVM_G_STRING(OFS_PARM0);
1833 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1836 case 0: // FILE_READ
1838 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
1839 if (prog->openfiles[filenum] == NULL)
1840 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
1842 case 1: // FILE_APPEND
1844 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1846 case 2: // FILE_WRITE
1848 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1851 PRVM_G_FLOAT(OFS_RETURN) = -3;
1852 VM_Warning(prog, "VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", prog->name, mode);
1856 if (prog->openfiles[filenum] == NULL)
1858 PRVM_G_FLOAT(OFS_RETURN) = -1;
1859 if (developer_extra.integer)
1860 VM_Warning(prog, "VM_fopen: %s: %s mode %s failed\n", prog->name, filename, modestring);
1864 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1865 if (developer_extra.integer)
1866 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", prog->name, filename, modestring, filenum);
1867 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin(prog);
1875 fclose(float fhandle)
1878 //void(float fhandle) fclose = #111; // closes a file
1879 void VM_fclose(prvm_prog_t *prog)
1883 VM_SAFEPARMCOUNT(1,VM_fclose);
1885 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1886 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1888 VM_Warning(prog, "VM_fclose: invalid file handle %i used in %s\n", filenum, prog->name);
1891 if (prog->openfiles[filenum] == NULL)
1893 VM_Warning(prog, "VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1896 FS_Close(prog->openfiles[filenum]);
1897 prog->openfiles[filenum] = NULL;
1898 if(prog->openfiles_origin[filenum])
1899 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1900 if (developer_extra.integer)
1901 Con_DPrintf("VM_fclose: %s: #%i closed\n", prog->name, filenum);
1908 string fgets(float fhandle)
1911 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1912 void VM_fgets(prvm_prog_t *prog)
1915 char string[VM_STRINGTEMP_LENGTH];
1918 VM_SAFEPARMCOUNT(1,VM_fgets);
1920 // set the return value regardless of any possible errors
1921 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1923 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1924 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1926 VM_Warning(prog, "VM_fgets: invalid file handle %i used in %s\n", filenum, prog->name);
1929 if (prog->openfiles[filenum] == NULL)
1931 VM_Warning(prog, "VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1937 c = FS_Getc(prog->openfiles[filenum]);
1938 if (c == '\r' || c == '\n' || c < 0)
1940 if (end < VM_STRINGTEMP_LENGTH - 1)
1944 // remove \n following \r
1947 c = FS_Getc(prog->openfiles[filenum]);
1949 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1951 if (developer_extra.integer)
1952 Con_DPrintf("fgets: %s: %s\n", prog->name, string);
1954 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
1961 fputs(float fhandle, string s)
1964 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1965 void VM_fputs(prvm_prog_t *prog)
1968 char string[VM_STRINGTEMP_LENGTH];
1971 VM_SAFEPARMCOUNT(2,VM_fputs);
1973 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1974 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1976 VM_Warning(prog, "VM_fputs: invalid file handle %i used in %s\n", filenum, prog->name);
1979 if (prog->openfiles[filenum] == NULL)
1981 VM_Warning(prog, "VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1984 VM_VarString(prog, 1, string, sizeof(string));
1985 if ((stringlength = (int)strlen(string)))
1986 FS_Write(prog->openfiles[filenum], string, stringlength);
1987 if (developer_extra.integer)
1988 Con_DPrintf("fputs: %s: %s\n", prog->name, string);
1995 writetofile(float fhandle, entity ent)
1998 void VM_writetofile(prvm_prog_t *prog)
2003 VM_SAFEPARMCOUNT(2, VM_writetofile);
2005 file = VM_GetFileHandle(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
2008 VM_Warning(prog, "VM_writetofile: invalid or closed file handle\n");
2012 ent = PRVM_G_EDICT(OFS_PARM1);
2013 if(ent->priv.required->free)
2015 VM_Warning(prog, "VM_writetofile: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2019 PRVM_ED_Write (prog, file, ent);
2022 // KrimZon - DP_QC_ENTITYDATA
2027 float() numentityfields
2028 Return the number of entity fields - NOT offsets
2031 void VM_numentityfields(prvm_prog_t *prog)
2033 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2036 // KrimZon - DP_QC_ENTITYDATA
2041 string(float fieldnum) entityfieldname
2042 Return name of the specified field as a string, or empty if the field is invalid (warning)
2045 void VM_entityfieldname(prvm_prog_t *prog)
2048 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2050 if (i < 0 || i >= prog->numfielddefs)
2052 VM_Warning(prog, "VM_entityfieldname: %s: field index out of bounds\n", prog->name);
2053 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2057 d = &prog->fielddefs[i];
2058 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2061 // KrimZon - DP_QC_ENTITYDATA
2066 float(float fieldnum) entityfieldtype
2069 void VM_entityfieldtype(prvm_prog_t *prog)
2072 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2074 if (i < 0 || i >= prog->numfielddefs)
2076 VM_Warning(prog, "VM_entityfieldtype: %s: field index out of bounds\n", prog->name);
2077 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2081 d = &prog->fielddefs[i];
2082 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)d->type;
2085 // KrimZon - DP_QC_ENTITYDATA
2088 VM_getentityfieldstring
2090 string(float fieldnum, entity ent) getentityfieldstring
2093 void VM_getentityfieldstring(prvm_prog_t *prog)
2095 // put the data into a string
2100 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2101 char valuebuf[MAX_INPUTLINE];
2103 if (i < 0 || i >= prog->numfielddefs)
2105 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2106 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2110 d = &prog->fielddefs[i];
2113 ent = PRVM_G_EDICT(OFS_PARM1);
2114 if(ent->priv.required->free)
2116 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2117 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2120 val = (prvm_eval_t *)(ent->fields.fp + d->ofs);
2122 // if it's 0 or blank, return an empty string
2123 type = d->type & ~DEF_SAVEGLOBAL;
2124 for (j=0 ; j<prvm_type_size[type] ; j++)
2125 if (val->ivector[j])
2127 if (j == prvm_type_size[type])
2129 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2133 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_UglyValueString(prog, (etype_t)d->type, val, valuebuf, sizeof(valuebuf)));
2136 // KrimZon - DP_QC_ENTITYDATA
2139 VM_putentityfieldstring
2141 float(float fieldnum, entity ent, string s) putentityfieldstring
2144 void VM_putentityfieldstring(prvm_prog_t *prog)
2148 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2150 if (i < 0 || i >= prog->numfielddefs)
2152 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2153 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2157 d = &prog->fielddefs[i];
2160 ent = PRVM_G_EDICT(OFS_PARM1);
2161 if(ent->priv.required->free)
2163 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2164 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2168 // parse the string into the value
2169 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(prog, ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2176 float strlen(string s)
2179 //float(string s) strlen = #114; // returns how many characters are in a string
2180 void VM_strlen(prvm_prog_t *prog)
2182 VM_SAFEPARMCOUNT(1,VM_strlen);
2184 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2185 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2188 // DRESK - Decolorized String
2193 string strdecolorize(string s)
2196 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2197 void VM_strdecolorize(prvm_prog_t *prog)
2199 char szNewString[VM_STRINGTEMP_LENGTH];
2200 const char *szString;
2203 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2204 szString = PRVM_G_STRING(OFS_PARM0);
2205 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), true);
2206 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2209 // DRESK - String Length (not counting color codes)
2214 float strlennocol(string s)
2217 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2218 // For example, ^2Dresk returns a length of 5
2219 void VM_strlennocol(prvm_prog_t *prog)
2221 const char *szString;
2224 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2226 szString = PRVM_G_STRING(OFS_PARM0);
2228 //nCnt = (int)COM_StringLengthNoColors(szString, 0, NULL);
2229 nCnt = (int)u8_COM_StringLengthNoColors(szString, 0, NULL);
2231 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2234 // DRESK - String to Uppercase and Lowercase
2239 string strtolower(string s)
2242 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2243 void VM_strtolower(prvm_prog_t *prog)
2245 char szNewString[VM_STRINGTEMP_LENGTH];
2246 const char *szString;
2249 VM_SAFEPARMCOUNT(1,VM_strtolower);
2250 szString = PRVM_G_STRING(OFS_PARM0);
2252 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2254 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2261 string strtoupper(string s)
2264 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2265 void VM_strtoupper(prvm_prog_t *prog)
2267 char szNewString[VM_STRINGTEMP_LENGTH];
2268 const char *szString;
2271 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2272 szString = PRVM_G_STRING(OFS_PARM0);
2274 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2276 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2283 string strcat(string,string,...[string])
2286 //string(string s1, string s2) strcat = #115;
2287 // concatenates two strings (for example "abc", "def" would return "abcdef")
2288 // and returns as a tempstring
2289 void VM_strcat(prvm_prog_t *prog)
2291 char s[VM_STRINGTEMP_LENGTH];
2292 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2294 VM_VarString(prog, 0, s, sizeof(s));
2295 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
2302 string substring(string s, float start, float length)
2305 // string(string s, float start, float length) substring = #116;
2306 // returns a section of a string as a tempstring
2307 void VM_substring(prvm_prog_t *prog)
2310 int u_slength = 0, u_start;
2313 char string[VM_STRINGTEMP_LENGTH];
2315 VM_SAFEPARMCOUNT(3,VM_substring);
2318 s = PRVM_G_STRING(OFS_PARM0);
2319 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2320 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2321 slength = strlen(s);
2323 if (start < 0) // FTE_STRINGS feature
2325 start = bound(0, start, slength);
2327 if (length < 0) // FTE_STRINGS feature
2328 length += slength - start + 1;
2329 maxlen = min((int)sizeof(string) - 1, slength - start);
2330 length = bound(0, length, maxlen);
2332 memcpy(string, s + start, length);
2334 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2337 s = PRVM_G_STRING(OFS_PARM0);
2338 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2339 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2341 if (start < 0) // FTE_STRINGS feature
2343 u_slength = (int)u8_strlen(s);
2345 start = bound(0, start, u_slength);
2348 if (length < 0) // FTE_STRINGS feature
2350 if (!u_slength) // it's not calculated when it's not needed above
2351 u_slength = (int)u8_strlen(s);
2352 length += u_slength - start + 1;
2355 // positive start, positive length
2356 u_start = u8_byteofs(s, start, NULL);
2359 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2362 u_length = u8_bytelen(s + u_start, length);
2363 if (u_length >= sizeof(string)-1)
2364 u_length = sizeof(string)-1;
2366 memcpy(string, s + u_start, u_length);
2367 string[u_length] = 0;
2368 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2375 string(string search, string replace, string subject) strreplace = #484;
2378 // replaces all occurrences of search with replace in the string subject, and returns the result
2379 void VM_strreplace(prvm_prog_t *prog)
2382 const char *search, *replace, *subject;
2383 char string[VM_STRINGTEMP_LENGTH];
2384 int search_len, replace_len, subject_len;
2386 VM_SAFEPARMCOUNT(3,VM_strreplace);
2388 search = PRVM_G_STRING(OFS_PARM0);
2389 replace = PRVM_G_STRING(OFS_PARM1);
2390 subject = PRVM_G_STRING(OFS_PARM2);
2392 search_len = (int)strlen(search);
2393 replace_len = (int)strlen(replace);
2394 subject_len = (int)strlen(subject);
2397 for (i = 0; i <= subject_len - search_len; i++)
2399 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2400 if (subject[i+j] != search[j])
2402 if (j == search_len)
2404 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2405 // found it at offset 'i'
2406 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2407 string[si++] = replace[j];
2410 i += search_len - 1;
2414 // the above would subtract 1 from i... so we
2415 // don't do that, but instead output the next
2417 if (si < (int)sizeof(string) - 1)
2418 string[si++] = subject[i];
2423 // in THIS case, we know search_len > 0, thus i < subject_len
2425 if (si < (int)sizeof(string) - 1)
2426 string[si++] = subject[i];
2429 // remaining chars (these cannot match)
2430 for (; i < subject_len; i++)
2431 if (si < (int)sizeof(string) - 1)
2432 string[si++] = subject[i];
2435 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2442 string(string search, string replace, string subject) strireplace = #485;
2445 // case-insensitive version of strreplace
2446 void VM_strireplace(prvm_prog_t *prog)
2449 const char *search, *replace, *subject;
2450 char string[VM_STRINGTEMP_LENGTH];
2451 int search_len, replace_len, subject_len;
2453 VM_SAFEPARMCOUNT(3, VM_strireplace);
2455 search = PRVM_G_STRING(OFS_PARM0);
2456 replace = PRVM_G_STRING(OFS_PARM1);
2457 subject = PRVM_G_STRING(OFS_PARM2);
2459 search_len = (int)strlen(search);
2460 replace_len = (int)strlen(replace);
2461 subject_len = (int)strlen(subject);
2464 for (i = 0; i <= subject_len - search_len; i++)
2466 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2467 if (tolower(subject[i+j]) != tolower(search[j]))
2469 if (j == search_len)
2471 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2472 // found it at offset 'i'
2473 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2474 string[si++] = replace[j];
2477 i += search_len - 1;
2481 // the above would subtract 1 from i... so we
2482 // don't do that, but instead output the next
2484 if (si < (int)sizeof(string) - 1)
2485 string[si++] = subject[i];
2490 // in THIS case, we know search_len > 0, thus i < subject_len
2492 if (si < (int)sizeof(string) - 1)
2493 string[si++] = subject[i];
2496 // remaining chars (these cannot match)
2497 for (; i < subject_len; i++)
2498 if (si < (int)sizeof(string) - 1)
2499 string[si++] = subject[i];
2502 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2509 vector stov(string s)
2512 //vector(string s) stov = #117; // returns vector value from a string
2513 void VM_stov(prvm_prog_t *prog)
2515 char string[VM_STRINGTEMP_LENGTH];
2517 VM_SAFEPARMCOUNT(1,VM_stov);
2519 VM_VarString(prog, 0, string, sizeof(string));
2520 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2527 string strzone(string s)
2530 //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)
2531 void VM_strzone(prvm_prog_t *prog)
2534 char string[VM_STRINGTEMP_LENGTH];
2537 VM_SAFEPARMCOUNT(1,VM_strzone);
2539 VM_VarString(prog, 0, string, sizeof(string));
2540 alloclen = strlen(string) + 1;
2541 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(prog, alloclen, &out);
2542 memcpy(out, string, alloclen);
2552 //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!!!)
2553 void VM_strunzone(prvm_prog_t *prog)
2555 VM_SAFEPARMCOUNT(1,VM_strunzone);
2556 PRVM_FreeString(prog, PRVM_G_INT(OFS_PARM0));
2561 VM_command (used by client and menu)
2563 clientcommand(float client, string s) (for client and menu)
2566 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2567 //this function originally written by KrimZon, made shorter by LadyHavoc
2568 void VM_clcommand (prvm_prog_t *prog)
2570 client_t *temp_client;
2573 VM_SAFEPARMCOUNT(2,VM_clcommand);
2575 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2576 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2578 VM_Warning(prog, "VM_clientcommand: %s: invalid client/server is not active !\n", prog->name);
2582 temp_client = host_client;
2583 host_client = svs.clients + i;
2584 Cmd_ExecuteString (&cmd_serverfromclient, PRVM_G_STRING(OFS_PARM1), src_client, true);
2585 host_client = temp_client;
2593 float tokenize(string s)
2596 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2597 //this function originally written by KrimZon, made shorter by LadyHavoc
2598 //20040203: rewritten by LadyHavoc (no longer uses allocations)
2599 static int num_tokens = 0;
2600 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2601 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2602 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2603 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2604 void VM_tokenize (prvm_prog_t *prog)
2608 VM_SAFEPARMCOUNT(1,VM_tokenize);
2610 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2611 p = tokenize_string;
2616 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2619 // skip whitespace here to find token start pos
2620 while(*p && ISWHITESPACE(*p))
2623 tokens_startpos[num_tokens] = p - tokenize_string;
2624 if(!COM_ParseToken_VM_Tokenize(&p, false))
2626 tokens_endpos[num_tokens] = p - tokenize_string;
2627 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2631 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2634 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2635 void VM_tokenize_console (prvm_prog_t *prog)
2639 VM_SAFEPARMCOUNT(1, VM_tokenize_console);
2641 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2642 p = tokenize_string;
2647 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2650 // skip whitespace here to find token start pos
2651 while(*p && ISWHITESPACE(*p))
2654 tokens_startpos[num_tokens] = p - tokenize_string;
2655 if(!COM_ParseToken_Console(&p))
2657 tokens_endpos[num_tokens] = p - tokenize_string;
2658 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2662 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2667 VM_tokenizebyseparator
2669 float tokenizebyseparator(string s, string separator1, ...)
2672 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2673 //this function returns the token preceding each instance of a separator (of
2674 //which there can be multiple), and the text following the last separator
2675 //useful for parsing certain kinds of data like IP addresses
2677 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2678 //returns 4 and the tokens "10" "1" "2" "3".
2679 void VM_tokenizebyseparator (prvm_prog_t *prog)
2683 int separatorlen[7];
2684 const char *separators[7];
2687 char tokentext[MAX_INPUTLINE];
2689 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2691 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2692 p = tokenize_string;
2695 for (j = 1;j < prog->argc;j++)
2697 // skip any blank separator strings
2698 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2701 separators[numseparators] = s;
2702 separatorlen[numseparators] = (int)strlen(s);
2709 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2711 token = tokentext + j;
2712 tokens_startpos[num_tokens] = p - tokenize_string;
2716 for (k = 0;k < numseparators;k++)
2718 if (!strncmp(p, separators[k], separatorlen[k]))
2720 p += separatorlen[k];
2724 if (k < numseparators)
2726 if (j < (int)sizeof(tokentext)-1)
2727 tokentext[j++] = *p;
2731 tokens_endpos[num_tokens] = p0 - tokenize_string;
2732 if (j >= (int)sizeof(tokentext))
2735 tokens[num_tokens++] = PRVM_SetTempString(prog, token);
2740 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2743 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2744 //this function originally written by KrimZon, made shorter by LadyHavoc
2745 void VM_argv (prvm_prog_t *prog)
2749 VM_SAFEPARMCOUNT(1,VM_argv);
2751 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2754 token_num += num_tokens;
2756 if (token_num >= 0 && token_num < num_tokens)
2757 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2759 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2762 //float(float n) argv_start_index = #515; // returns the start index of a token
2763 void VM_argv_start_index (prvm_prog_t *prog)
2767 VM_SAFEPARMCOUNT(1,VM_argv);
2769 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2772 token_num += num_tokens;
2774 if (token_num >= 0 && token_num < num_tokens)
2775 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2777 PRVM_G_FLOAT(OFS_RETURN) = -1;
2780 //float(float n) argv_end_index = #516; // returns the end index of a token
2781 void VM_argv_end_index (prvm_prog_t *prog)
2785 VM_SAFEPARMCOUNT(1,VM_argv);
2787 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2790 token_num += num_tokens;
2792 if (token_num >= 0 && token_num < num_tokens)
2793 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2795 PRVM_G_FLOAT(OFS_RETURN) = -1;
2805 void VM_isserver(prvm_prog_t *prog)
2807 VM_SAFEPARMCOUNT(0, VM_isserver);
2809 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
2819 void VM_clientcount(prvm_prog_t *prog)
2821 VM_SAFEPARMCOUNT(0,VM_clientcount);
2823 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2833 void VM_clientstate(prvm_prog_t *prog)
2835 VM_SAFEPARMCOUNT(0,VM_clientstate);
2838 switch( cls.state ) {
2839 case ca_uninitialized:
2841 PRVM_G_FLOAT(OFS_RETURN) = 0;
2843 case ca_disconnected:
2844 PRVM_G_FLOAT(OFS_RETURN) = 1;
2847 PRVM_G_FLOAT(OFS_RETURN) = 2;
2850 // should never be reached!
2859 float getostype(prvm_prog_t *prog)
2861 */ // not used at the moment -> not included in the common list
2862 void VM_getostype(prvm_prog_t *prog)
2864 VM_SAFEPARMCOUNT(0,VM_getostype);
2869 OS_MAC - not supported
2873 PRVM_G_FLOAT(OFS_RETURN) = 0;
2874 #elif defined(MACOSX)
2875 PRVM_G_FLOAT(OFS_RETURN) = 2;
2877 PRVM_G_FLOAT(OFS_RETURN) = 1;
2885 float gettime(prvm_prog_t *prog)
2888 float CDAudio_GetPosition(void);
2889 void VM_gettime(prvm_prog_t *prog)
2893 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2897 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) host.realtime;
2901 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2904 case 0: // GETTIME_FRAMESTART
2905 PRVM_G_FLOAT(OFS_RETURN) = host.realtime;
2907 case 1: // GETTIME_REALTIME
2908 PRVM_G_FLOAT(OFS_RETURN) = Sys_DirtyTime();
2910 case 2: // GETTIME_HIRES
2911 PRVM_G_FLOAT(OFS_RETURN) = (Sys_DirtyTime() - host.dirtytime);
2913 case 3: // GETTIME_UPTIME
2914 PRVM_G_FLOAT(OFS_RETURN) = host.realtime;
2916 case 4: // GETTIME_CDTRACK
2917 PRVM_G_FLOAT(OFS_RETURN) = CDAudio_GetPosition();
2920 VM_Warning(prog, "VM_gettime: %s: unsupported timer specified, returning realtime\n", prog->name);
2921 PRVM_G_FLOAT(OFS_RETURN) = host.realtime;
2931 float getsoundtime(prvm_prog_t *prog)
2935 void VM_getsoundtime (prvm_prog_t *prog)
2937 int entnum, entchannel;
2938 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2940 if (prog == SVVM_prog)
2941 entnum = PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2942 else if (prog == CLVM_prog)
2943 entnum = MAX_EDICTS + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2946 VM_Warning(prog, "VM_getsoundtime: %s: not supported on this progs\n", prog->name);
2947 PRVM_G_FLOAT(OFS_RETURN) = -1;
2950 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2951 entchannel = CHAN_USER2ENGINE(entchannel);
2952 if (!IS_CHAN(entchannel))
2953 VM_Warning(prog, "VM_getsoundtime: %s: bad channel %i\n", prog->name, entchannel);
2954 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)S_GetEntChannelPosition(entnum, entchannel);
2961 string soundlength (string sample)
2964 void VM_soundlength (prvm_prog_t *prog)
2968 VM_SAFEPARMCOUNT(1, VM_soundlength);
2970 s = PRVM_G_STRING(OFS_PARM0);
2971 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2978 loadfromdata(string data)
2981 void VM_loadfromdata(prvm_prog_t *prog)
2983 VM_SAFEPARMCOUNT(1, VM_loadfromdata);
2985 PRVM_ED_LoadFromFile(prog, PRVM_G_STRING(OFS_PARM0));
2989 ========================
2992 parseentitydata(entity ent, string data)
2993 ========================
2995 void VM_parseentitydata(prvm_prog_t *prog)
3000 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
3002 // get edict and test it
3003 ent = PRVM_G_EDICT(OFS_PARM0);
3004 if (ent->priv.required->free)
3005 prog->error_cmd("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", prog->name, PRVM_NUM_FOR_EDICT(ent));
3007 data = PRVM_G_STRING(OFS_PARM1);
3009 // parse the opening brace
3010 if (!COM_ParseToken_Simple(&data, false, false, true) || com_token[0] != '{' )
3011 prog->error_cmd("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", prog->name, data );
3013 PRVM_ED_ParseEdict (prog, data, ent);
3020 loadfromfile(string file)
3023 void VM_loadfromfile(prvm_prog_t *prog)
3025 const char *filename;
3028 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3030 filename = PRVM_G_STRING(OFS_PARM0);
3031 if (FS_CheckNastyPath(filename, false))
3033 PRVM_G_FLOAT(OFS_RETURN) = -4;
3034 VM_Warning(prog, "VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", prog->name, filename);
3038 // not conform with VM_fopen
3039 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3041 PRVM_G_FLOAT(OFS_RETURN) = -1;
3043 PRVM_ED_LoadFromFile(prog, data);
3054 float mod(float val, float m)
3057 void VM_modulo(prvm_prog_t *prog)
3060 VM_SAFEPARMCOUNT(2, VM_modulo);
3062 val = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM0);
3063 m = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM1);
3065 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) (val % m);
3068 static void VM_Search_Init(prvm_prog_t *prog)
3071 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3072 prog->opensearches[i] = NULL;
3075 static void VM_Search_Reset(prvm_prog_t *prog)
3078 // reset the fssearch list
3079 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3081 if(prog->opensearches[i])
3082 FS_FreeSearch(prog->opensearches[i]);
3083 prog->opensearches[i] = NULL;
3091 float search_begin(string pattern, float caseinsensitive, float quiet[, string packfile])
3094 void VM_search_begin(prvm_prog_t *prog)
3097 const char *packfile = NULL, *pattern;
3098 int caseinsens, quiet;
3100 VM_SAFEPARMCOUNTRANGE(3, 4, VM_search_begin);
3102 pattern = PRVM_G_STRING(OFS_PARM0);
3104 VM_CheckEmptyString(prog, pattern);
3106 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3107 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3109 // optional packfile parameter (DP_QC_FS_SEARCH_PACKFILE)
3111 packfile = PRVM_G_STRING(OFS_PARM3);
3113 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3114 if(!prog->opensearches[handle])
3117 if(handle >= PRVM_MAX_OPENSEARCHES)
3119 PRVM_G_FLOAT(OFS_RETURN) = -2;
3120 VM_Warning(prog, "VM_search_begin: %s ran out of search handles (%i)\n", prog->name, PRVM_MAX_OPENSEARCHES);
3124 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet, packfile)))
3125 PRVM_G_FLOAT(OFS_RETURN) = -1;
3128 prog->opensearches_origin[handle] = PRVM_AllocationOrigin(prog);
3129 PRVM_G_FLOAT(OFS_RETURN) = handle;
3137 void search_end(float handle)
3140 void VM_search_end(prvm_prog_t *prog)
3143 VM_SAFEPARMCOUNT(1, VM_search_end);
3145 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3147 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3149 VM_Warning(prog, "VM_search_end: invalid handle %i used in %s\n", handle, prog->name);
3152 if(prog->opensearches[handle] == NULL)
3154 VM_Warning(prog, "VM_search_end: no such handle %i in %s\n", handle, prog->name);
3158 FS_FreeSearch(prog->opensearches[handle]);
3159 prog->opensearches[handle] = NULL;
3160 if(prog->opensearches_origin[handle])
3161 PRVM_Free((char *)prog->opensearches_origin[handle]);
3168 float search_getsize(float handle)
3171 void VM_search_getsize(prvm_prog_t *prog)
3174 VM_SAFEPARMCOUNT(1, VM_search_getsize);
3176 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3178 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3180 VM_Warning(prog, "VM_search_getsize: invalid handle %i used in %s\n", handle, prog->name);
3183 if(prog->opensearches[handle] == NULL)
3185 VM_Warning(prog, "VM_search_getsize: no such handle %i in %s\n", handle, prog->name);
3189 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3194 VM_search_getfilename
3196 string search_getfilename(float handle, float num)
3199 void VM_search_getfilename(prvm_prog_t *prog)
3201 int handle, filenum;
3202 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3204 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3205 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3207 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3209 VM_Warning(prog, "VM_search_getfilename: invalid handle %i used in %s\n", handle, prog->name);
3212 if(prog->opensearches[handle] == NULL)
3214 VM_Warning(prog, "VM_search_getfilename: no such handle %i in %s\n", handle, prog->name);
3217 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3219 VM_Warning(prog, "VM_search_getfilename: invalid filenum %i in %s\n", filenum, prog->name);
3223 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, prog->opensearches[handle]->filenames[filenum]);
3230 string chr(float ascii)
3233 void VM_chr(prvm_prog_t *prog)
3237 VM_SAFEPARMCOUNT(1, VM_chr);
3239 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3242 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3247 VM_SAFEPARMCOUNT(1, VM_chr);
3249 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3251 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3254 //=============================================================================
3255 // Draw builtins (client & menu)
3261 float iscachedpic(string pic)
3264 void VM_iscachedpic(prvm_prog_t *prog)
3266 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3268 // drawq hasnt such a function, thus always return true
3269 PRVM_G_FLOAT(OFS_RETURN) = false;
3276 string precache_pic(string pic)
3279 #define PRECACHE_PIC_FROMWAD 1 /* FTEQW, not supported here */
3280 #define PRECACHE_PIC_NOTPERSISTENT 2
3281 //#define PRECACHE_PIC_NOCLAMP 4
3282 #define PRECACHE_PIC_MIPMAP 8
3283 void VM_precache_pic(prvm_prog_t *prog)
3286 int flags = CACHEPICFLAG_FAILONMISSING;
3288 VM_SAFEPARMCOUNTRANGE(1, 2, VM_precache_pic);
3290 s = PRVM_G_STRING(OFS_PARM0);
3291 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3292 VM_CheckEmptyString(prog, s);
3296 int f = PRVM_G_FLOAT(OFS_PARM1);
3297 if(f & PRECACHE_PIC_NOTPERSISTENT)
3298 flags |= CACHEPICFLAG_NOTPERSISTENT;
3299 //if(f & PRECACHE_PIC_NOCLAMP)
3300 // flags |= CACHEPICFLAG_NOCLAMP;
3301 if(f & PRECACHE_PIC_MIPMAP)
3302 flags |= CACHEPICFLAG_MIPMAP;
3305 if( !Draw_IsPicLoaded(Draw_CachePic_Flags(s, flags | CACHEPICFLAG_QUIET)) )
3306 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3316 void VM_freepic(prvm_prog_t *prog)
3320 VM_SAFEPARMCOUNT(1,VM_freepic);
3322 s = PRVM_G_STRING(OFS_PARM0);
3323 VM_CheckEmptyString(prog, s);
3328 static void getdrawfontscale(prvm_prog_t *prog, float *sx, float *sy)
3332 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3333 if(VectorLength2(v) > 0)
3340 static dp_font_t *getdrawfont(prvm_prog_t *prog)
3342 int f = (int) PRVM_drawglobalfloat(drawfont);
3343 if(f < 0 || f >= dp_fonts.maxsize)
3344 return FONT_DEFAULT;
3345 return &dp_fonts.f[f];
3352 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3355 void VM_drawcharacter(prvm_prog_t *prog)
3357 prvm_vec_t *pos,*scale,*rgb;
3361 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3363 // polygonbegin without draw2d arg has to guess
3364 prog->polygonbegin_guess2d = true;
3366 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3369 PRVM_G_FLOAT(OFS_RETURN) = -1;
3370 VM_Warning(prog, "VM_drawcharacter: %s passed null character !\n",prog->name);
3374 pos = PRVM_G_VECTOR(OFS_PARM0);
3375 scale = PRVM_G_VECTOR(OFS_PARM2);
3376 rgb = PRVM_G_VECTOR(OFS_PARM3);
3377 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3379 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3381 PRVM_G_FLOAT(OFS_RETURN) = -2;
3382 VM_Warning(prog, "VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3386 if(pos[2] || scale[2])
3387 VM_Warning(prog, "VM_drawcharacter: z value%c from %s discarded\n",(pos[2] && scale[2]) ? 's' : 0,((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3389 if(!scale[0] || !scale[1])
3391 PRVM_G_FLOAT(OFS_RETURN) = -3;
3392 VM_Warning(prog, "VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3396 getdrawfontscale(prog, &sx, &sy);
3397 DrawQ_String_Scale(pos[0], pos[1], &character, 1, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont(prog));
3398 PRVM_G_FLOAT(OFS_RETURN) = 1;
3405 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3408 void VM_drawstring(prvm_prog_t *prog)
3410 prvm_vec_t *pos,*scale,*rgb;
3414 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3416 // polygonbegin without draw2d arg has to guess
3417 prog->polygonbegin_guess2d = true;
3419 string = PRVM_G_STRING(OFS_PARM1);
3420 pos = PRVM_G_VECTOR(OFS_PARM0);
3421 scale = PRVM_G_VECTOR(OFS_PARM2);
3422 rgb = PRVM_G_VECTOR(OFS_PARM3);
3423 if (prog->argc >= 6)
3424 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3426 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3428 PRVM_G_FLOAT(OFS_RETURN) = -2;
3429 VM_Warning(prog, "VM_drawstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3433 if(!scale[0] || !scale[1])
3435 PRVM_G_FLOAT(OFS_RETURN) = -3;
3436 VM_Warning(prog, "VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3440 if(pos[2] || scale[2])
3441 VM_Warning(prog, "VM_drawstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3443 getdrawfontscale(prog, &sx, &sy);
3444 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont(prog));
3445 //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);
3446 PRVM_G_FLOAT(OFS_RETURN) = 1;
3451 VM_drawcolorcodedstring
3453 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3455 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3458 void VM_drawcolorcodedstring(prvm_prog_t *prog)
3460 prvm_vec_t *pos, *scale;
3464 float sx, sy, alpha;
3466 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3468 // polygonbegin without draw2d arg has to guess
3469 prog->polygonbegin_guess2d = true;
3471 if (prog->argc == 6) // full 6 parms, like normal drawstring
3473 pos = PRVM_G_VECTOR(OFS_PARM0);
3474 string = PRVM_G_STRING(OFS_PARM1);
3475 scale = PRVM_G_VECTOR(OFS_PARM2);
3476 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3477 alpha = PRVM_G_FLOAT(OFS_PARM4);
3478 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3482 pos = PRVM_G_VECTOR(OFS_PARM0);
3483 string = PRVM_G_STRING(OFS_PARM1);
3484 scale = PRVM_G_VECTOR(OFS_PARM2);
3488 alpha = PRVM_G_FLOAT(OFS_PARM3);
3489 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3492 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3494 PRVM_G_FLOAT(OFS_RETURN) = -2;
3495 VM_Warning(prog, "VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3499 if(!scale[0] || !scale[1])
3501 PRVM_G_FLOAT(OFS_RETURN) = -3;
3502 VM_Warning(prog, "VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3506 if(pos[2] || scale[2])
3507 VM_Warning(prog, "VM_drawcolorcodedstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3509 getdrawfontscale(prog, &sx, &sy);
3510 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], alpha, flag, NULL, false, getdrawfont(prog));
3511 if (prog->argc == 6) // also return vector of last color
3512 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3514 PRVM_G_FLOAT(OFS_RETURN) = 1;
3520 float stringwidth(string text, float allowColorCodes, float size)
3523 void VM_stringwidth(prvm_prog_t *prog)
3527 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3531 VM_SAFEPARMCOUNTRANGE(2, 3, VM_stringwidth);
3533 getdrawfontscale(prog, &sx, &sy);
3536 Vector2Copy(PRVM_G_VECTOR(OFS_PARM2), szv);
3541 // we want the width for 8x8 font size, divided by 8
3542 Vector2Set(szv, 8, 8);
3544 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3545 if(sx >= 0.9 && sx <= 1.1)
3553 string = PRVM_G_STRING(OFS_PARM0);
3554 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3556 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(prog), 1000000000) * mult;
3560 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3568 string = PRVM_G_STRING(OFS_PARM0);
3569 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3571 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3579 float findfont(string s)
3583 static float getdrawfontnum(const char *fontname)
3587 for(i = 0; i < dp_fonts.maxsize; ++i)
3588 if(!strcmp(dp_fonts.f[i].title, fontname))
3593 void VM_findfont(prvm_prog_t *prog)
3595 VM_SAFEPARMCOUNT(1,VM_findfont);
3596 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3603 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3607 void VM_loadfont(prvm_prog_t *prog)
3609 const char *fontname, *filelist, *sizes, *c, *cm;
3610 char mainfont[MAX_QPATH];
3612 float sz, scale, voffset;
3615 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3617 fontname = PRVM_G_STRING(OFS_PARM0);
3619 fontname = "default";
3621 filelist = PRVM_G_STRING(OFS_PARM1);
3623 filelist = "gfx/conchars";
3625 sizes = PRVM_G_STRING(OFS_PARM2);
3631 if (prog->argc >= 4)
3633 i = PRVM_G_FLOAT(OFS_PARM3);
3634 if (i >= 0 && i < dp_fonts.maxsize)
3637 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3641 f = FindFont(fontname, true);
3644 PRVM_G_FLOAT(OFS_RETURN) = -1;
3645 return; // something go wrong
3648 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3649 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3651 // first font is handled "normally"
3652 c = strchr(filelist, ':');
3653 cm = strchr(filelist, ',');
3654 if(c && (!cm || c < cm))
3655 f->req_face = atoi(c+1);
3661 if(!c || (c - filelist) > MAX_QPATH)
3662 strlcpy(mainfont, filelist, sizeof(mainfont));
3665 memcpy(mainfont, filelist, c - filelist);
3666 mainfont[c - filelist] = 0;
3670 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3672 c = strchr(filelist, ',');
3678 c = strchr(filelist, ':');
3679 cm = strchr(filelist, ',');
3680 if(c && (!cm || c < cm))
3681 f->fallback_faces[i] = atoi(c+1);
3684 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3687 if(!c || (c-filelist) > MAX_QPATH)
3689 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3693 memcpy(f->fallbacks[i], filelist, c - filelist);
3694 f->fallbacks[i][c - filelist] = 0;
3699 for(i = 0; i < MAX_FONT_SIZES; ++i)
3700 f->req_sizes[i] = -1;
3701 for (numsizes = 0,c = sizes;;)
3703 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3705 sz = atof(com_token);
3707 if (sz < 0.001f || sz > 1000.0f)
3709 VM_Warning(prog, "VM_loadfont: crap size %s", com_token);
3713 if (numsizes == MAX_FONT_SIZES)
3715 VM_Warning(prog, "VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3718 f->req_sizes[numsizes] = sz;
3722 // additional scale/hoffset parms
3725 if (prog->argc >= 5)
3727 scale = PRVM_G_FLOAT(OFS_PARM4);
3731 if (prog->argc >= 6)
3732 voffset = PRVM_G_FLOAT(OFS_PARM5);
3735 LoadFont(true, mainfont, f, scale, voffset);
3737 // return index of loaded font
3738 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3745 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3748 void VM_drawpic(prvm_prog_t *prog)
3750 const char *picname;
3751 prvm_vec_t *size, *pos, *rgb;
3754 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3756 // polygonbegin without draw2d arg has to guess
3757 prog->polygonbegin_guess2d = true;
3759 picname = PRVM_G_STRING(OFS_PARM1);
3760 VM_CheckEmptyString(prog, picname);
3762 // is pic cached ? no function yet for that
3765 PRVM_G_FLOAT(OFS_RETURN) = -4;
3766 VM_Warning(prog, "VM_drawpic: %s: %s not cached !\n", prog->name, picname);
3770 pos = PRVM_G_VECTOR(OFS_PARM0);
3771 size = PRVM_G_VECTOR(OFS_PARM2);
3772 rgb = PRVM_G_VECTOR(OFS_PARM3);
3773 if (prog->argc >= 6)
3774 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3776 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3778 PRVM_G_FLOAT(OFS_RETURN) = -2;
3779 VM_Warning(prog, "VM_drawpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3783 if(pos[2] || size[2])
3784 VM_Warning(prog, "VM_drawpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3786 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);
3787 PRVM_G_FLOAT(OFS_RETURN) = 1;
3793 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3796 void VM_drawrotpic(prvm_prog_t *prog)
3798 const char *picname;
3799 prvm_vec_t *size, *pos, *org, *rgb;
3802 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3804 // polygonbegin without draw2d arg has to guess
3805 prog->polygonbegin_guess2d = true;
3807 picname = PRVM_G_STRING(OFS_PARM1);
3808 VM_CheckEmptyString(prog, picname);
3810 // is pic cached ? no function yet for that
3813 PRVM_G_FLOAT(OFS_RETURN) = -4;
3814 VM_Warning(prog, "VM_drawrotpic: %s: %s not cached !\n", prog->name, picname);
3818 pos = PRVM_G_VECTOR(OFS_PARM0);
3819 size = PRVM_G_VECTOR(OFS_PARM2);
3820 org = PRVM_G_VECTOR(OFS_PARM3);
3821 rgb = PRVM_G_VECTOR(OFS_PARM5);
3822 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3824 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3826 PRVM_G_FLOAT(OFS_RETURN) = -2;
3827 VM_Warning(prog, "VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3831 if(pos[2] || size[2] || org[2])
3832 VM_Warning(prog, "VM_drawrotpic: z value from pos/size/org discarded\n");
3834 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);
3835 PRVM_G_FLOAT(OFS_RETURN) = 1;
3841 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3845 void VM_drawsubpic(prvm_prog_t *prog)
3847 const char *picname;
3848 prvm_vec_t *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3851 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3853 // polygonbegin without draw2d arg has to guess
3854 prog->polygonbegin_guess2d = true;
3856 picname = PRVM_G_STRING(OFS_PARM2);
3857 VM_CheckEmptyString(prog, picname);
3859 // is pic cached ? no function yet for that
3862 PRVM_G_FLOAT(OFS_RETURN) = -4;
3863 VM_Warning(prog, "VM_drawsubpic: %s: %s not cached !\n", prog->name, picname);
3867 pos = PRVM_G_VECTOR(OFS_PARM0);
3868 size = PRVM_G_VECTOR(OFS_PARM1);
3869 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3870 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3871 rgb = PRVM_G_VECTOR(OFS_PARM5);
3872 alpha = PRVM_G_FLOAT(OFS_PARM6);
3873 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3875 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3877 PRVM_G_FLOAT(OFS_RETURN) = -2;
3878 VM_Warning(prog, "VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3882 if(pos[2] || size[2])
3883 VM_Warning(prog, "VM_drawsubpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3885 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3887 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3888 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3889 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3890 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3892 PRVM_G_FLOAT(OFS_RETURN) = 1;
3899 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3902 void VM_drawfill(prvm_prog_t *prog)
3904 prvm_vec_t *size, *pos, *rgb;
3907 VM_SAFEPARMCOUNT(5,VM_drawfill);
3909 // polygonbegin without draw2d arg has to guess
3910 prog->polygonbegin_guess2d = true;
3912 pos = PRVM_G_VECTOR(OFS_PARM0);
3913 size = PRVM_G_VECTOR(OFS_PARM1);
3914 rgb = PRVM_G_VECTOR(OFS_PARM2);
3915 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3917 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3919 PRVM_G_FLOAT(OFS_RETURN) = -2;
3920 VM_Warning(prog, "VM_drawfill: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3924 if(pos[2] || size[2])
3925 VM_Warning(prog, "VM_drawfill: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3927 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3928 PRVM_G_FLOAT(OFS_RETURN) = 1;
3935 drawsetcliparea(float x, float y, float width, float height)
3938 void VM_drawsetcliparea(prvm_prog_t *prog)
3941 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3943 // polygonbegin without draw2d arg has to guess
3944 prog->polygonbegin_guess2d = true;
3946 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3947 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3948 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3949 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3951 DrawQ_SetClipArea(x, y, w, h);
3956 VM_drawresetcliparea
3961 void VM_drawresetcliparea(prvm_prog_t *prog)
3963 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3965 // polygonbegin without draw2d arg has to guess
3966 prog->polygonbegin_guess2d = true;
3968 DrawQ_ResetClipArea();
3975 vector getimagesize(string pic)
3978 void VM_getimagesize(prvm_prog_t *prog)
3983 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3985 p = PRVM_G_STRING(OFS_PARM0);
3986 VM_CheckEmptyString(prog, p);
3988 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_QUIET | CACHEPICFLAG_NOTPERSISTENT);
3989 if (!Draw_IsPicLoaded(pic))
3991 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3992 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3996 PRVM_G_VECTOR(OFS_RETURN)[0] = Draw_GetPicWidth(pic);
3997 PRVM_G_VECTOR(OFS_RETURN)[1] = Draw_GetPicHeight(pic);
3999 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4006 string keynumtostring(float keynum)
4009 void VM_keynumtostring (prvm_prog_t *prog)
4012 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
4014 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, sizeof(tinystr)));
4019 VM_findkeysforcommand
4021 string findkeysforcommand(string command, float bindmap)
4023 the returned string is an altstring
4026 #define FKFC_NUMKEYS 5
4027 void M_FindKeysForCommand(const char *command, int *keys);
4028 void VM_findkeysforcommand(prvm_prog_t *prog)
4031 char ret[VM_STRINGTEMP_LENGTH];
4032 int keys[FKFC_NUMKEYS];
4037 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
4039 cmd = PRVM_G_STRING(OFS_PARM0);
4041 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4043 bindmap = 0; // consistent to "bind"
4045 VM_CheckEmptyString(prog, cmd);
4047 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
4050 for(i = 0; i < FKFC_NUMKEYS; i++)
4051 strlcat(ret, va(vabuf, sizeof(vabuf), " \'%i\'", keys[i]), sizeof(ret));
4053 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, ret);
4060 float stringtokeynum(string key)
4063 void VM_stringtokeynum (prvm_prog_t *prog)
4065 VM_SAFEPARMCOUNT( 1, VM_stringtokeynum );
4067 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
4074 string getkeybind(float key, float bindmap)
4077 void VM_getkeybind (prvm_prog_t *prog)
4080 VM_SAFEPARMCOUNTRANGE(1, 2, VM_getkeybind);
4082 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4084 bindmap = 0; // consistent to "bind"
4086 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4093 float setkeybind(float key, string cmd, float bindmap)
4096 void VM_setkeybind (prvm_prog_t *prog)
4099 VM_SAFEPARMCOUNTRANGE(2, 3, VM_setkeybind);
4101 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4103 bindmap = 0; // consistent to "bind"
4105 PRVM_G_FLOAT(OFS_RETURN) = 0;
4106 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4107 PRVM_G_FLOAT(OFS_RETURN) = 1;
4114 vector getbindmaps()
4117 void VM_getbindmaps (prvm_prog_t *prog)
4120 VM_SAFEPARMCOUNT(0, VM_getbindmaps);
4121 Key_GetBindMap(&fg, &bg);
4122 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4123 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4124 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4131 float setbindmaps(vector bindmap)
4134 void VM_setbindmaps (prvm_prog_t *prog)
4136 VM_SAFEPARMCOUNT(1, VM_setbindmaps);
4137 PRVM_G_FLOAT(OFS_RETURN) = 0;
4138 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4139 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4140 PRVM_G_FLOAT(OFS_RETURN) = 1;
4143 // CL_Video interface functions
4146 ========================
4149 float cin_open(string file, string name)
4150 ========================
4152 void VM_cin_open(prvm_prog_t *prog)
4157 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4159 file = PRVM_G_STRING( OFS_PARM0 );
4160 name = PRVM_G_STRING( OFS_PARM1 );
4162 VM_CheckEmptyString(prog, file );
4163 VM_CheckEmptyString(prog, name );
4165 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4166 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4168 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4172 ========================
4175 void cin_close(string name)
4176 ========================
4178 void VM_cin_close(prvm_prog_t *prog)
4182 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4184 name = PRVM_G_STRING( OFS_PARM0 );
4185 VM_CheckEmptyString(prog, name );
4187 CL_CloseVideo( CL_GetVideoByName( name ) );
4191 ========================
4193 void cin_setstate(string name, float type)
4194 ========================
4196 void VM_cin_setstate(prvm_prog_t *prog)
4199 clvideostate_t state;
4202 VM_SAFEPARMCOUNT( 2, VM_cin_setstate );
4204 name = PRVM_G_STRING( OFS_PARM0 );
4205 VM_CheckEmptyString(prog, name );
4207 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4209 video = CL_GetVideoByName( name );
4210 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4211 CL_SetVideoState( video, state );
4215 ========================
4218 float cin_getstate(string name)
4219 ========================
4221 void VM_cin_getstate(prvm_prog_t *prog)
4226 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4228 name = PRVM_G_STRING( OFS_PARM0 );
4229 VM_CheckEmptyString(prog, name );
4231 video = CL_GetVideoByName( name );
4233 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4235 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4239 ========================
4242 void cin_restart(string name)
4243 ========================
4245 void VM_cin_restart(prvm_prog_t *prog)
4250 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4252 name = PRVM_G_STRING( OFS_PARM0 );
4253 VM_CheckEmptyString(prog, name );
4255 video = CL_GetVideoByName( name );
4257 CL_RestartVideo( video );
4261 ========================
4264 float[bool] gecko_create( string name )
4265 ========================
4267 void VM_gecko_create(prvm_prog_t *prog) {
4269 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4273 ========================
4276 void gecko_destroy( string name )
4277 ========================
4279 void VM_gecko_destroy(prvm_prog_t *prog) {
4284 ========================
4287 void gecko_navigate( string name, string URI )
4288 ========================
4290 void VM_gecko_navigate(prvm_prog_t *prog) {
4295 ========================
4298 float[bool] gecko_keyevent( string name, float key, float eventtype )
4299 ========================
4301 void VM_gecko_keyevent(prvm_prog_t *prog) {
4303 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4307 ========================
4310 void gecko_mousemove( string name, float x, float y )
4311 ========================
4313 void VM_gecko_movemouse(prvm_prog_t *prog) {
4319 ========================
4322 void gecko_resize( string name, float w, float h )
4323 ========================
4325 void VM_gecko_resize(prvm_prog_t *prog) {
4331 ========================
4332 VM_gecko_get_texture_extent
4334 vector gecko_get_texture_extent( string name )
4335 ========================
4337 void VM_gecko_get_texture_extent(prvm_prog_t *prog) {
4339 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4340 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4349 Writes new values for v_forward, v_up, and v_right based on angles
4350 void makevectors(vector angle)
4353 void VM_makevectors (prvm_prog_t *prog)
4355 vec3_t angles, forward, right, up;
4356 VM_SAFEPARMCOUNT(1, VM_makevectors);
4357 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), angles);
4358 AngleVectors(angles, forward, right, up);
4359 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
4360 VectorCopy(right, PRVM_gameglobalvector(v_right));
4361 VectorCopy(up, PRVM_gameglobalvector(v_up));
4368 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4369 vectorvectors(vector)
4372 void VM_vectorvectors (prvm_prog_t *prog)
4374 vec3_t forward, right, up;
4375 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4376 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), forward);
4377 VectorVectors(forward, right, up);
4378 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
4379 VectorCopy(right, PRVM_gameglobalvector(v_right));
4380 VectorCopy(up, PRVM_gameglobalvector(v_up));
4384 ========================
4387 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4388 ========================
4390 void VM_drawline (prvm_prog_t *prog)
4392 prvm_vec_t *c1, *c2, *rgb;
4394 unsigned char flags;
4396 VM_SAFEPARMCOUNT(6, VM_drawline);
4398 // polygonbegin without draw2d arg has to guess
4399 prog->polygonbegin_guess2d = true;
4401 width = PRVM_G_FLOAT(OFS_PARM0);
4402 c1 = PRVM_G_VECTOR(OFS_PARM1);
4403 c2 = PRVM_G_VECTOR(OFS_PARM2);
4404 rgb = PRVM_G_VECTOR(OFS_PARM3);
4405 alpha = PRVM_G_FLOAT(OFS_PARM4);
4406 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4407 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4410 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4411 void VM_bitshift (prvm_prog_t *prog)
4414 VM_SAFEPARMCOUNT(2, VM_bitshift);
4416 n1 = (prvm_int_t)fabs((prvm_vec_t)((prvm_int_t)PRVM_G_FLOAT(OFS_PARM0)));
4417 n2 = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
4419 PRVM_G_FLOAT(OFS_RETURN) = n1;
4422 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4424 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4427 ////////////////////////////////////////
4428 // AltString functions
4429 ////////////////////////////////////////
4432 ========================
4435 float altstr_count(string)
4436 ========================
4438 void VM_altstr_count(prvm_prog_t *prog)
4440 const char *altstr, *pos;
4443 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4445 altstr = PRVM_G_STRING( OFS_PARM0 );
4446 //VM_CheckEmptyString(prog, altstr );
4448 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4449 if( *pos == '\\' ) {
4453 } else if( *pos == '\'' ) {
4458 PRVM_G_FLOAT( OFS_RETURN ) = (prvm_vec_t) (count / 2);
4462 ========================
4465 string altstr_prepare(string)
4466 ========================
4468 void VM_altstr_prepare(prvm_prog_t *prog)
4470 const char *instr, *in;
4471 char outstr[VM_STRINGTEMP_LENGTH];
4474 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4476 instr = PRVM_G_STRING( OFS_PARM0 );
4478 for (in = instr, outpos = 0; *in && outpos < sizeof(outstr) - 1; ++in)
4480 if (*in == '\'' && outpos < sizeof(outstr) - 2)
4482 outstr[outpos++] = '\\';
4483 outstr[outpos++] = '\'';
4486 outstr[outpos++] = *in;
4490 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4494 ========================
4497 string altstr_get(string, float)
4498 ========================
4500 void VM_altstr_get(prvm_prog_t *prog)
4502 const char *altstr, *pos;
4505 char outstr[VM_STRINGTEMP_LENGTH];
4507 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4509 altstr = PRVM_G_STRING( OFS_PARM0 );
4511 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4512 count = count * 2 + 1;
4514 for( pos = altstr ; *pos && count ; pos++ )
4515 if( *pos == '\\' ) {
4518 } else if( *pos == '\'' )
4522 PRVM_G_INT( OFS_RETURN ) = 0;
4526 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4527 if( *pos == '\\' ) {
4532 } else if( *pos == '\'' )
4538 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4542 ========================
4545 string altstr_set(string altstr, float num, string set)
4546 ========================
4548 void VM_altstr_set(prvm_prog_t *prog)
4551 const char *altstr, *str;
4554 char outstr[VM_STRINGTEMP_LENGTH];
4556 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4558 altstr = PRVM_G_STRING( OFS_PARM0 );
4560 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4562 str = PRVM_G_STRING( OFS_PARM2 );
4565 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4570 } else if( *in == '\'' ) {
4575 for( ; *str; *out++ = *str++ );
4576 // now jump over the old content
4578 if( *in == '\'' || (*in == '\\' && !*++in) )
4581 strlcpy(out, in, outstr + sizeof(outstr) - out);
4582 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4586 ========================
4589 string altstr_ins(string altstr, float num, string set)
4590 ========================
4592 void VM_altstr_ins(prvm_prog_t *prog)
4598 char outstr[VM_STRINGTEMP_LENGTH];
4600 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4602 in = PRVM_G_STRING( OFS_PARM0 );
4603 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4604 set = PRVM_G_STRING( OFS_PARM2 );
4607 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4612 } else if( *in == '\'' ) {
4617 for( ; *set ; *out++ = *set++ );
4620 strlcpy(out, in, outstr + sizeof(outstr) - out);
4621 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4625 ////////////////////////////////////////
4626 // BufString functions
4627 ////////////////////////////////////////
4628 //[515]: string buffers support
4630 static size_t stringbuffers_sortlength;
4632 static void BufStr_Expand(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex)
4634 if (stringbuffer->max_strings <= strindex)
4636 char **oldstrings = stringbuffer->strings;
4637 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4638 while (stringbuffer->max_strings <= strindex)
4639 stringbuffer->max_strings *= 2;
4640 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4641 if (stringbuffer->num_strings > 0)
4642 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4644 Mem_Free(oldstrings);
4648 static void BufStr_Shrink(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
4650 // reduce num_strings if there are empty string slots at the end
4651 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4652 stringbuffer->num_strings--;
4654 // if empty, free the string pointer array
4655 if (stringbuffer->num_strings == 0)
4657 stringbuffer->max_strings = 0;
4658 if (stringbuffer->strings)
4659 Mem_Free(stringbuffer->strings);
4660 stringbuffer->strings = NULL;
4664 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4667 a = *((const char **) in1);
4668 b = *((const char **) in2);
4669 if(!a || !a[0]) return 1;
4670 if(!b || !b[0]) return -1;
4671 return strncmp(a, b, stringbuffers_sortlength);
4674 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4677 a = *((const char **) in1);
4678 b = *((const char **) in2);
4679 if(!a || !a[0]) return 1;
4680 if(!b || !b[0]) return -1;
4681 return strncmp(b, a, stringbuffers_sortlength);
4684 prvm_stringbuffer_t *BufStr_FindCreateReplace (prvm_prog_t *prog, int bufindex, int flags, const char *format)
4686 prvm_stringbuffer_t *stringbuffer;
4692 // find buffer with wanted index
4693 if (bufindex < (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray))
4695 if ( (stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, bufindex)) )
4697 if (stringbuffer->flags & STRINGBUFFER_TEMP)
4698 stringbuffer->flags = flags; // created but has not been used yet
4699 return stringbuffer;
4704 // allocate new buffer with wanted index
4707 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4708 stringbuffer->flags = STRINGBUFFER_TEMP;
4709 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4712 stringbuffer->flags = flags; // mark as used
4716 return stringbuffer;
4719 void BufStr_Set(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex, const char *str)
4723 if (!stringbuffer || strindex < 0)
4726 BufStr_Expand(prog, stringbuffer, strindex);
4727 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4728 if (stringbuffer->strings[strindex])
4729 Mem_Free(stringbuffer->strings[strindex]);
4730 stringbuffer->strings[strindex] = NULL;
4734 // not the NULL string!
4735 alloclen = strlen(str) + 1;
4736 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4737 memcpy(stringbuffer->strings[strindex], str, alloclen);
4740 BufStr_Shrink(prog, stringbuffer);
4743 void BufStr_Del(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
4750 for (i = 0;i < stringbuffer->num_strings;i++)
4751 if (stringbuffer->strings[i])
4752 Mem_Free(stringbuffer->strings[i]);
4753 if (stringbuffer->strings)
4754 Mem_Free(stringbuffer->strings);
4755 if(stringbuffer->origin)
4756 PRVM_Free((char *)stringbuffer->origin);
4757 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4760 void BufStr_Flush(prvm_prog_t *prog)
4762 prvm_stringbuffer_t *stringbuffer;
4765 numbuffers = (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray);
4766 for (i = 0; i < numbuffers; i++)
4767 if ( (stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i)) )
4768 BufStr_Del(prog, stringbuffer);
4769 Mem_ExpandableArray_NewArray(&prog->stringbuffersarray, prog->progs_mempool, sizeof(prvm_stringbuffer_t), 64);
4773 ========================
4775 creates new buffer, and returns it's index, returns -1 if failed
4776 float buf_create(prvm_prog_t *prog) = #460;
4777 float newbuf(string format, float flags) = #460;
4778 ========================
4781 void VM_buf_create (prvm_prog_t *prog)
4783 prvm_stringbuffer_t *stringbuffer;
4786 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4788 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4789 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4791 PRVM_G_FLOAT(OFS_RETURN) = -1;
4794 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4795 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4796 stringbuffer->origin = PRVM_AllocationOrigin(prog);
4797 // optional flags parm
4798 if (prog->argc >= 2)
4799 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & STRINGBUFFER_QCFLAGS;
4800 PRVM_G_FLOAT(OFS_RETURN) = i;
4806 ========================
4808 deletes buffer and all strings in it
4809 void buf_del(float bufhandle) = #461;
4810 ========================
4812 void VM_buf_del (prvm_prog_t *prog)
4814 prvm_stringbuffer_t *stringbuffer;
4815 VM_SAFEPARMCOUNT(1, VM_buf_del);
4816 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4818 BufStr_Del(prog, stringbuffer);
4821 VM_Warning(prog, "VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4827 ========================
4829 how many strings are stored in buffer
4830 float buf_getsize(float bufhandle) = #462;
4831 ========================
4833 void VM_buf_getsize (prvm_prog_t *prog)
4835 prvm_stringbuffer_t *stringbuffer;
4836 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4838 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4841 PRVM_G_FLOAT(OFS_RETURN) = -1;
4842 VM_Warning(prog, "VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4846 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4850 ========================
4852 copy all content from one buffer to another, make sure it exists
4853 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4854 ========================
4856 void VM_buf_copy (prvm_prog_t *prog)
4858 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4860 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4862 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4863 if(!srcstringbuffer)
4865 VM_Warning(prog, "VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4868 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4869 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4871 VM_Warning(prog, "VM_buf_copy: source == destination (%i) in %s\n", i, prog->name);
4874 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4875 if(!dststringbuffer)
4877 VM_Warning(prog, "VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4881 for (i = 0;i < dststringbuffer->num_strings;i++)
4882 if (dststringbuffer->strings[i])
4883 Mem_Free(dststringbuffer->strings[i]);
4884 if (dststringbuffer->strings)
4885 Mem_Free(dststringbuffer->strings);
4886 *dststringbuffer = *srcstringbuffer;
4887 if (dststringbuffer->max_strings)
4888 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4890 for (i = 0;i < dststringbuffer->num_strings;i++)
4892 if (srcstringbuffer->strings[i])
4895 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4896 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4897 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4903 ========================
4905 sort buffer by beginnings of strings (cmplength defaults it's length)
4906 "backward == true" means that sorting goes upside-down
4907 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4908 ========================
4910 void VM_buf_sort (prvm_prog_t *prog)
4912 prvm_stringbuffer_t *stringbuffer;
4913 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4915 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4918 VM_Warning(prog, "VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4921 if(stringbuffer->num_strings <= 0)
4923 VM_Warning(prog, "VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4926 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4927 if(stringbuffers_sortlength <= 0)
4928 stringbuffers_sortlength = 0x7FFFFFFF;
4930 if(!PRVM_G_FLOAT(OFS_PARM2))
4931 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4933 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4935 BufStr_Shrink(prog, stringbuffer);
4939 ========================
4941 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4942 string buf_implode(float bufhandle, string glue) = #465;
4943 ========================
4945 void VM_buf_implode (prvm_prog_t *prog)
4947 prvm_stringbuffer_t *stringbuffer;
4948 char k[VM_STRINGTEMP_LENGTH];
4952 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4954 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4955 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4958 VM_Warning(prog, "VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4961 if(!stringbuffer->num_strings)
4963 sep = PRVM_G_STRING(OFS_PARM1);
4965 for(l = i = 0;i < stringbuffer->num_strings;i++)
4967 if(stringbuffer->strings[i])
4969 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4970 if (l >= sizeof(k) - 1)
4972 strlcat(k, sep, sizeof(k));
4973 strlcat(k, stringbuffer->strings[i], sizeof(k));
4976 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, k);
4980 ========================
4982 get a string from buffer, returns tempstring, dont str_unzone it!
4983 string bufstr_get(float bufhandle, float string_index) = #465;
4984 ========================
4986 void VM_bufstr_get (prvm_prog_t *prog)
4988 prvm_stringbuffer_t *stringbuffer;
4990 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4992 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4993 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4996 VM_Warning(prog, "VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4999 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
5002 // VM_Warning(prog, "VM_bufstr_get: invalid string index %i used in %s\n", strindex, prog->name);
5005 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
5006 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, stringbuffer->strings[strindex]);
5010 ========================
5012 copies a string into selected slot of buffer
5013 void bufstr_set(float bufhandle, float string_index, string str) = #466;
5014 ========================
5016 void VM_bufstr_set (prvm_prog_t *prog)
5019 prvm_stringbuffer_t *stringbuffer;
5022 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
5024 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5027 VM_Warning(prog, "VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5030 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
5031 if(strindex < 0 || strindex >= 1000000) // huge number of strings
5033 VM_Warning(prog, "VM_bufstr_set: invalid string index %i used in %s\n", strindex, prog->name);
5037 news = PRVM_G_STRING(OFS_PARM2);
5038 BufStr_Set(prog, stringbuffer, strindex, news);
5042 ========================
5044 adds string to buffer in first free slot and returns its index
5045 "order == true" means that string will be added after last "full" slot
5046 float bufstr_add(float bufhandle, string str, float order) = #467;
5047 ========================
5049 void VM_bufstr_add (prvm_prog_t *prog)
5051 int order, strindex;
5052 prvm_stringbuffer_t *stringbuffer;
5056 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
5058 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5059 PRVM_G_FLOAT(OFS_RETURN) = -1;
5062 VM_Warning(prog, "VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5065 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
5067 VM_Warning(prog, "VM_bufstr_add: can not add an empty string to buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5070 string = PRVM_G_STRING(OFS_PARM1);
5071 order = (int)PRVM_G_FLOAT(OFS_PARM2);
5073 strindex = stringbuffer->num_strings;
5075 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
5076 if (stringbuffer->strings[strindex] == NULL)
5079 BufStr_Expand(prog, stringbuffer, strindex);
5081 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5082 alloclen = strlen(string) + 1;
5083 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5084 memcpy(stringbuffer->strings[strindex], string, alloclen);
5086 PRVM_G_FLOAT(OFS_RETURN) = strindex;
5090 ========================
5092 delete string from buffer
5093 void bufstr_free(float bufhandle, float string_index) = #468;
5094 ========================
5096 void VM_bufstr_free (prvm_prog_t *prog)
5099 prvm_stringbuffer_t *stringbuffer;
5100 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
5102 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5105 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5108 i = (int)PRVM_G_FLOAT(OFS_PARM1);
5111 VM_Warning(prog, "VM_bufstr_free: invalid string index %i used in %s\n", i, prog->name);
5115 if (i < stringbuffer->num_strings)
5117 if(stringbuffer->strings[i])
5118 Mem_Free(stringbuffer->strings[i]);
5119 stringbuffer->strings[i] = NULL;
5122 BufStr_Shrink(prog, stringbuffer);
5126 ========================
5128 load a file into string buffer, return 0 or 1
5129 float buf_loadfile(string filename, float bufhandle) = #535;
5130 ========================
5132 void VM_buf_loadfile(prvm_prog_t *prog)
5135 prvm_stringbuffer_t *stringbuffer;
5136 char string[VM_STRINGTEMP_LENGTH];
5137 int strindex, c, end;
5138 const char *filename;
5142 VM_SAFEPARMCOUNT(2, VM_buf_loadfile);
5145 filename = PRVM_G_STRING(OFS_PARM0);
5146 file = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
5148 file = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
5151 if (developer_extra.integer)
5152 VM_Warning(prog, "VM_buf_loadfile: failed to open file %s in %s\n", filename, prog->name);
5153 PRVM_G_FLOAT(OFS_RETURN) = 0;
5157 // get string buffer
5158 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5161 VM_Warning(prog, "VM_buf_loadfile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5162 PRVM_G_FLOAT(OFS_RETURN) = 0;
5166 // read file (append to the end of buffer)
5167 strindex = stringbuffer->num_strings;
5175 if (c == '\r' || c == '\n' || c < 0)
5177 if (end < VM_STRINGTEMP_LENGTH - 1)
5181 // remove \n following \r
5186 FS_UnGetc(file, (unsigned char)c);
5191 BufStr_Expand(prog, stringbuffer, strindex);
5192 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5193 alloclen = strlen(string) + 1;
5194 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5195 memcpy(stringbuffer->strings[strindex], string, alloclen);
5196 strindex = stringbuffer->num_strings;
5204 PRVM_G_FLOAT(OFS_RETURN) = 1;
5208 ========================
5210 writes stringbuffer to a file, returns 0 or 1
5211 float buf_writefile(float filehandle, float bufhandle, [, float startpos, float numstrings]) = #468;
5212 ========================
5215 void VM_buf_writefile(prvm_prog_t *prog)
5217 int filenum, strindex, strnum, strlength;
5218 prvm_stringbuffer_t *stringbuffer;
5220 VM_SAFEPARMCOUNTRANGE(2, 4, VM_buf_writefile);
5223 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
5224 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
5226 VM_Warning(prog, "VM_buf_writefile: invalid file handle %i used in %s\n", filenum, prog->name);
5229 if (prog->openfiles[filenum] == NULL)
5231 VM_Warning(prog, "VM_buf_writefile: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
5235 // get string buffer
5236 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5239 VM_Warning(prog, "VM_buf_writefile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5240 PRVM_G_FLOAT(OFS_RETURN) = 0;
5244 // get start and end parms
5247 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5248 strnum = (int)PRVM_G_FLOAT(OFS_PARM3);
5250 else if (prog->argc > 2)
5252 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5253 strnum = stringbuffer->num_strings - strindex;
5258 strnum = stringbuffer->num_strings;
5260 if (strindex < 0 || strindex >= stringbuffer->num_strings)
5262 VM_Warning(prog, "VM_buf_writefile: wrong start string index %i used in %s\n", strindex, prog->name);
5263 PRVM_G_FLOAT(OFS_RETURN) = 0;
5268 VM_Warning(prog, "VM_buf_writefile: wrong strings count %i used in %s\n", strnum, prog->name);
5269 PRVM_G_FLOAT(OFS_RETURN) = 0;
5274 while(strindex < stringbuffer->num_strings && strnum)
5276 if (stringbuffer->strings[strindex])
5278 if ((strlength = (int)strlen(stringbuffer->strings[strindex])))
5279 FS_Write(prog->openfiles[filenum], stringbuffer->strings[strindex], strlength);
5280 FS_Write(prog->openfiles[filenum], "\n", 1);
5286 PRVM_G_FLOAT(OFS_RETURN) = 1;
5289 #define MATCH_AUTO 0
5290 #define MATCH_WHOLE 1
5291 #define MATCH_LEFT 2
5292 #define MATCH_RIGHT 3
5293 #define MATCH_MIDDLE 4
5294 #define MATCH_PATTERN 5
5296 static const char *detect_match_rule(char *pattern, int *matchrule)
5301 patternlength = (int)strlen(pattern);
5302 ppos = strchr(pattern, '*');
5303 qpos = strchr(pattern, '?');
5307 *matchrule = MATCH_PATTERN;
5310 // has * - left, mid, right or pattern
5313 // starts with * - may be right/mid or pattern
5314 if ((ppos - pattern) == 0)
5316 ppos = strchr(pattern+1, '*');
5320 *matchrule = MATCH_RIGHT;
5324 if ((ppos - pattern) == patternlength)
5326 *matchrule = MATCH_MIDDLE;
5331 *matchrule = MATCH_PATTERN;
5334 // end with * - left
5335 if ((ppos - pattern) == patternlength)
5337 *matchrule = MATCH_LEFT;
5342 *matchrule = MATCH_PATTERN;
5345 // have no wildcards - whole string
5346 *matchrule = MATCH_WHOLE;
5350 // todo: support UTF8
5351 static qboolean match_rule(const char *string, int max_string, const char *pattern, int patternlength, int rule)
5356 return !strncmp(string, pattern, max_string) ? true : false;
5358 return !strncmp(string, pattern, patternlength) ? true : false;
5361 mid = strstr(string, pattern);
5362 return mid && !*(mid+patternlength);
5365 return strstr(string, pattern) ? true : false;
5367 return matchpattern_with_separator(string, pattern, false, "", false) ? true : false;
5371 ========================
5373 find an index of bufstring matching rule
5374 float bufstr_find(float bufhandle, string match, float matchrule, float startpos, float step) = #468;
5375 ========================
5378 void VM_bufstr_find(prvm_prog_t *prog)
5380 prvm_stringbuffer_t *stringbuffer;
5381 char string[VM_STRINGTEMP_LENGTH];
5382 int matchrule, matchlen, i, step;
5385 VM_SAFEPARMCOUNTRANGE(3, 5, VM_bufstr_find);
5387 PRVM_G_FLOAT(OFS_RETURN) = -1;
5389 // get string buffer
5390 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5393 VM_Warning(prog, "VM_bufstr_find: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5398 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5399 if (matchrule < 0 || matchrule > 5)
5401 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
5405 match = PRVM_G_STRING(OFS_PARM1);
5408 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5409 match = detect_match_rule(string, &matchrule);
5411 matchlen = (int)strlen(match);
5414 i = (prog->argc > 3) ? (int)PRVM_G_FLOAT(OFS_PARM3) : 0;
5415 step = (prog->argc > 4) ? (int)PRVM_G_FLOAT(OFS_PARM4) : 1;
5416 while(i < stringbuffer->num_strings)
5418 if (stringbuffer->strings[i] && match_rule(stringbuffer->strings[i], VM_STRINGTEMP_LENGTH, match, matchlen, matchrule))
5420 PRVM_G_FLOAT(OFS_RETURN) = i;
5428 ========================
5430 float matchpattern(string s, string pattern, float matchrule, float startpos) = #468;
5431 ========================
5433 void VM_matchpattern(prvm_prog_t *prog)
5435 const char *s, *match;
5436 char string[VM_STRINGTEMP_LENGTH];
5439 VM_SAFEPARMCOUNTRANGE(2, 4, VM_matchpattern);
5441 s = PRVM_G_STRING(OFS_PARM0);
5444 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5445 if (matchrule < 0 || matchrule > 5)
5447 VM_Warning(prog, "VM_matchpattern: invalid match rule %i in %s\n", matchrule, prog->name);
5451 match = PRVM_G_STRING(OFS_PARM1);
5454 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5455 match = detect_match_rule(string, &matchrule);
5459 l = (int)strlen(match);
5461 s += max(0, min((unsigned int)PRVM_G_FLOAT(OFS_PARM3), strlen(s)-1));
5464 PRVM_G_FLOAT(OFS_RETURN) = match_rule(s, VM_STRINGTEMP_LENGTH, match, l, matchrule);
5468 ========================
5470 ========================
5473 void VM_buf_cvarlist(prvm_prog_t *prog)
5476 const char *partial, *antipartial;
5477 size_t len, antilen;
5479 qboolean ispattern, antiispattern;
5481 prvm_stringbuffer_t *stringbuffer;
5482 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5484 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5487 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5491 partial = PRVM_G_STRING(OFS_PARM1);
5495 len = strlen(partial);
5498 antipartial = PRVM_G_STRING(OFS_PARM2);
5504 antilen = strlen(antipartial);
5506 for (n = 0;n < stringbuffer->num_strings;n++)
5507 if (stringbuffer->strings[n])
5508 Mem_Free(stringbuffer->strings[n]);
5509 if (stringbuffer->strings)
5510 Mem_Free(stringbuffer->strings);
5511 stringbuffer->strings = NULL;
5513 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5514 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5517 for(cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
5519 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5522 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5528 stringbuffer->max_strings = stringbuffer->num_strings = n;
5529 if (stringbuffer->max_strings)
5530 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5533 for(cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
5535 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5538 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5541 alloclen = strlen(cvar->name) + 1;
5542 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5543 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5558 This was a major timewaster in progs, so it was converted to C
5561 void VM_changeyaw (prvm_prog_t *prog)
5564 float ideal, current, move, speed;
5566 // this is called (VERY HACKISHLY) by VM_SV_MoveToGoal, so it can not use any
5567 // parameters because they are the parameters to VM_SV_MoveToGoal, not this
5568 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5570 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5571 if (ent == prog->edicts)
5573 VM_Warning(prog, "changeyaw: can not modify world entity\n");
5576 if (ent->priv.server->free)
5578 VM_Warning(prog, "changeyaw: can not modify free entity\n");
5581 current = PRVM_gameedictvector(ent, angles)[1];
5582 current = ANGLEMOD(current);
5583 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5584 speed = PRVM_gameedictfloat(ent, yaw_speed);
5586 if (current == ideal)
5588 move = ideal - current;
5589 if (ideal > current)
5611 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5619 void VM_changepitch (prvm_prog_t *prog)
5622 float ideal, current, move, speed;
5624 VM_SAFEPARMCOUNT(1, VM_changepitch);
5626 ent = PRVM_G_EDICT(OFS_PARM0);
5627 if (ent == prog->edicts)
5629 VM_Warning(prog, "changepitch: can not modify world entity\n");
5632 if (ent->priv.server->free)
5634 VM_Warning(prog, "changepitch: can not modify free entity\n");
5637 current = PRVM_gameedictvector(ent, angles)[0];
5638 current = ANGLEMOD(current);
5639 ideal = PRVM_gameedictfloat(ent, idealpitch);
5640 speed = PRVM_gameedictfloat(ent, pitch_speed);
5642 if (current == ideal)
5644 move = ideal - current;
5645 if (ideal > current)
5667 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5671 void VM_uncolorstring (prvm_prog_t *prog)
5673 char szNewString[VM_STRINGTEMP_LENGTH];
5674 const char *szString;
5677 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5678 szString = PRVM_G_STRING(OFS_PARM0);
5679 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), true);
5680 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
5684 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5685 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5686 void VM_strstrofs (prvm_prog_t *prog)
5688 const char *instr, *match;
5690 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5691 instr = PRVM_G_STRING(OFS_PARM0);
5692 match = PRVM_G_STRING(OFS_PARM1);
5693 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5694 firstofs = (int)u8_bytelen(instr, firstofs);
5696 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5698 PRVM_G_FLOAT(OFS_RETURN) = -1;
5702 match = strstr(instr+firstofs, match);
5704 PRVM_G_FLOAT(OFS_RETURN) = -1;
5706 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5709 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5710 void VM_str2chr (prvm_prog_t *prog)
5715 VM_SAFEPARMCOUNT(2, VM_str2chr);
5716 s = PRVM_G_STRING(OFS_PARM0);
5717 index = (int)u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5719 if((unsigned)index < strlen(s))
5721 if (utf8_enable.integer)
5722 ch = u8_getchar_noendptr(s + index);
5724 ch = (unsigned char)s[index];
5725 PRVM_G_FLOAT(OFS_RETURN) = ch;
5728 PRVM_G_FLOAT(OFS_RETURN) = 0;
5731 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5732 void VM_chr2str (prvm_prog_t *prog)
5737 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5738 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5739 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5741 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5746 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5747 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5748 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5750 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5753 static int chrconv_number(int i, int base, int conv)
5778 static int chrconv_punct(int i, int base, int conv)
5796 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5798 //convert case and colour seperatly...
5815 baset = 128*((charnum&1) == (convt-5));
5831 return i + basec + baset;
5833 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5834 //bulk convert a string. change case or colouring.
5835 void VM_strconv (prvm_prog_t *prog)
5837 int ccase, redalpha, rednum, len, i;
5838 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5839 unsigned char *result = resbuf;
5841 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5843 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5844 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5845 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5846 VM_VarString(prog, 3, (char *) resbuf, sizeof(resbuf));
5847 len = (int)strlen((char *) resbuf);
5849 for (i = 0; i < len; i++, result++) //should this be done backwards?
5851 if (*result >= '0' && *result <= '9') //normal numbers...
5852 *result = chrconv_number(*result, '0', rednum);
5853 else if (*result >= '0'+128 && *result <= '9'+128)
5854 *result = chrconv_number(*result, '0'+128, rednum);
5855 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5856 *result = chrconv_number(*result, '0'+128-30, rednum);
5857 else if (*result >= '0'-30 && *result <= '9'-30)
5858 *result = chrconv_number(*result, '0'-30, rednum);
5860 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5861 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5862 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5863 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5864 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5865 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5866 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5867 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5869 else if ((*result & 127) < 16 || !redalpha) //special chars..
5871 else if (*result < 128)
5872 *result = chrconv_punct(*result, 0, redalpha);
5874 *result = chrconv_punct(*result, 128, redalpha);
5878 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, (char *) resbuf);
5881 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5882 void VM_strpad (prvm_prog_t *prog)
5884 char src[VM_STRINGTEMP_LENGTH];
5885 char destbuf[VM_STRINGTEMP_LENGTH];
5887 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5888 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5889 VM_VarString(prog, 1, src, sizeof(src));
5891 // note: < 0 = left padding, > 0 = right padding,
5892 // this is reverse logic of printf!
5893 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5895 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, destbuf);
5898 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5899 //uses qw style \key\value strings
5900 void VM_infoadd (prvm_prog_t *prog)
5902 const char *info, *key;
5903 char value[VM_STRINGTEMP_LENGTH];
5904 char temp[VM_STRINGTEMP_LENGTH];
5906 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5907 info = PRVM_G_STRING(OFS_PARM0);
5908 key = PRVM_G_STRING(OFS_PARM1);
5909 VM_VarString(prog, 2, value, sizeof(value));
5911 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5913 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5915 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, temp);
5918 // #227 string(string info, string key) infoget (FTE_STRINGS)
5919 //uses qw style \key\value strings
5920 void VM_infoget (prvm_prog_t *prog)
5924 char value[VM_STRINGTEMP_LENGTH];
5926 VM_SAFEPARMCOUNT(2, VM_infoget);
5927 info = PRVM_G_STRING(OFS_PARM0);
5928 key = PRVM_G_STRING(OFS_PARM1);
5930 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5932 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, value);
5935 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5936 // also float(string s1, string s2) strcmp (FRIK_FILE)
5937 void VM_strncmp (prvm_prog_t *prog)
5939 const char *s1, *s2;
5940 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5941 s1 = PRVM_G_STRING(OFS_PARM0);
5942 s2 = PRVM_G_STRING(OFS_PARM1);
5945 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5949 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5953 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5954 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5955 void VM_strncasecmp (prvm_prog_t *prog)
5957 const char *s1, *s2;
5958 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5959 s1 = PRVM_G_STRING(OFS_PARM0);
5960 s2 = PRVM_G_STRING(OFS_PARM1);
5963 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5967 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5971 // #494 float(float caseinsensitive, string s, ...) crc16
5972 void VM_crc16(prvm_prog_t *prog)
5975 char s[VM_STRINGTEMP_LENGTH];
5976 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5977 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5978 VM_VarString(prog, 1, s, sizeof(s));
5979 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5982 // #639 float(string digest, string data, ...) digest_hex
5983 void VM_digest_hex(prvm_prog_t *prog)
5991 char s[VM_STRINGTEMP_LENGTH];
5994 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5995 digest = PRVM_G_STRING(OFS_PARM0);
5998 VM_VarString(prog, 1, s, sizeof(s));
5999 len = (int)strlen(s);
6003 if(!strcmp(digest, "MD4"))
6006 mdfour((unsigned char *) out, (unsigned char *) s, len);
6008 else if(!strcmp(digest, "SHA256") && Crypto_Available())
6011 sha256((unsigned char *) out, (unsigned char *) s, len);
6013 // no warning needed on mismatch - we return string_null to QC
6018 static const char *hexmap = "0123456789abcdef";
6019 for(i = 0; i < outlen; ++i)
6021 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
6022 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
6025 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outhex);
6028 PRVM_G_INT(OFS_RETURN) = 0;
6031 void VM_wasfreed (prvm_prog_t *prog)
6033 VM_SAFEPARMCOUNT(1, VM_wasfreed);
6034 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
6037 void VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace)
6039 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
6040 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
6041 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
6042 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
6043 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
6044 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
6045 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
6046 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
6047 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
6048 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
6049 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
6050 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
6051 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(prog, trace->hittexture->name) : 0;
6054 void VM_ClearTraceGlobals(prvm_prog_t *prog)
6056 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
6057 PRVM_gameglobalfloat(trace_allsolid) = 0;
6058 PRVM_gameglobalfloat(trace_startsolid) = 0;
6059 PRVM_gameglobalfloat(trace_fraction) = 0;
6060 PRVM_gameglobalfloat(trace_inwater) = 0;
6061 PRVM_gameglobalfloat(trace_inopen) = 0;
6062 VectorClear(PRVM_gameglobalvector(trace_endpos));
6063 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
6064 PRVM_gameglobalfloat(trace_plane_dist) = 0;
6065 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
6066 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
6067 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
6068 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
6069 PRVM_gameglobalstring(trace_dphittexturename) = 0;
6074 void VM_Cmd_Init(prvm_prog_t *prog)
6076 // only init the stuff for the current prog
6077 VM_Files_Init(prog);
6078 VM_Search_Init(prog);
6081 static void animatemodel_reset(prvm_prog_t *prog);
6083 void VM_Cmd_Reset(prvm_prog_t *prog)
6085 CL_PurgeOwner( MENUOWNER );
6086 VM_Search_Reset(prog);
6087 VM_Files_CloseAll(prog);
6088 animatemodel_reset(prog);
6091 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
6092 // does URI escaping on a string (replace evil stuff by %AB escapes)
6093 void VM_uri_escape (prvm_prog_t *prog)
6095 char src[VM_STRINGTEMP_LENGTH];
6096 char dest[VM_STRINGTEMP_LENGTH];
6098 static const char *hex = "0123456789ABCDEF";
6100 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
6101 VM_VarString(prog, 0, src, sizeof(src));
6103 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
6105 if((*p >= 'A' && *p <= 'Z')
6106 || (*p >= 'a' && *p <= 'z')
6107 || (*p >= '0' && *p <= '9')
6108 || (*p == '-') || (*p == '_') || (*p == '.')
6109 || (*p == '!') || (*p == '~')
6110 || (*p == '\'') || (*p == '(') || (*p == ')'))
6115 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
6116 *q++ = hex[ *(unsigned char *)p & 0xF];
6121 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
6124 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
6125 // does URI unescaping on a string (get back the evil stuff)
6126 void VM_uri_unescape (prvm_prog_t *prog)
6128 char src[VM_STRINGTEMP_LENGTH];
6129 char dest[VM_STRINGTEMP_LENGTH];
6133 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
6134 VM_VarString(prog, 0, src, sizeof(src));
6136 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
6140 if(p[1] >= '0' && p[1] <= '9')
6142 else if(p[1] >= 'a' && p[1] <= 'f')
6143 hi = p[1] - 'a' + 10;
6144 else if(p[1] >= 'A' && p[1] <= 'F')
6145 hi = p[1] - 'A' + 10;
6148 if(p[2] >= '0' && p[2] <= '9')
6150 else if(p[2] >= 'a' && p[2] <= 'f')
6151 lo = p[2] - 'a' + 10;
6152 else if(p[2] >= 'A' && p[2] <= 'F')
6153 lo = p[2] - 'A' + 10;
6156 if(hi != 0 || lo != 0) // don't unescape NUL bytes
6157 *q++ = (char) (hi * 0x10 + lo);
6168 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
6171 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
6172 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
6173 void VM_whichpack (prvm_prog_t *prog)
6175 const char *fn, *pack;
6177 VM_SAFEPARMCOUNT(1, VM_whichpack);
6178 fn = PRVM_G_STRING(OFS_PARM0);
6179 pack = FS_WhichPack(fn);
6181 PRVM_G_INT(OFS_RETURN) = pack ? PRVM_SetTempString(prog, pack) : 0;
6189 char buffer[MAX_INPUTLINE];
6191 unsigned char *postdata; // free when uri_to_prog_t is freed
6193 char *sigdata; // free when uri_to_prog_t is freed
6198 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
6201 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
6203 prog = handle->prog;
6206 // curl reply came too late... so just drop it
6207 if(handle->postdata)
6208 Z_Free(handle->postdata);
6210 Z_Free(handle->sigdata);
6215 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
6217 if(length_received >= sizeof(handle->buffer))
6218 length_received = sizeof(handle->buffer) - 1;
6219 handle->buffer[length_received] = 0;
6221 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
6222 PRVM_G_FLOAT(OFS_PARM1) = status;
6223 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(prog, handle->buffer);
6224 prog->ExecuteProgram(prog, PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
6227 if(handle->postdata)
6228 Z_Free(handle->postdata);
6230 Z_Free(handle->sigdata);
6234 // 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
6235 // 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
6236 void VM_uri_get (prvm_prog_t *prog)
6241 uri_to_prog_t *handle;
6242 const char *posttype = NULL;
6243 const char *postseparator = NULL;
6244 int poststringbuffer = -1;
6246 const char *query_string = NULL;
6249 if(!PRVM_allfunction(URI_Get_Callback))
6250 prog->error_cmd("uri_get called by %s without URI_Get_Callback defined", prog->name);
6252 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
6254 url = PRVM_G_STRING(OFS_PARM0);
6255 id = PRVM_G_FLOAT(OFS_PARM1);
6257 posttype = PRVM_G_STRING(OFS_PARM2);
6259 postseparator = PRVM_G_STRING(OFS_PARM3);
6261 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
6263 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
6264 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!
6266 query_string = strchr(url, '?');
6269 lq = query_string ? strlen(query_string) : 0;
6271 handle->prog = prog;
6272 handle->starttime = prog->starttime;
6274 if(postseparator && posttype && *posttype)
6276 size_t l = strlen(postseparator);
6277 if(poststringbuffer >= 0)
6282 prvm_stringbuffer_t *stringbuffer;
6283 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
6286 VM_Warning(prog, "uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
6290 for(i = 0;i < stringbuffer->num_strings;i++)
6294 if(stringbuffer->strings[i])
6295 ltotal += strlen(stringbuffer->strings[i]);
6297 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
6298 handle->postlen = ltotal;
6300 for(i = 0;i < stringbuffer->num_strings;i++)
6304 memcpy(handle->postdata + ltotal, postseparator, l);
6307 if(stringbuffer->strings[i])
6309 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
6310 ltotal += strlen(stringbuffer->strings[i]);
6313 if(ltotal != handle->postlen)
6314 prog->error_cmd("%s: string buffer content size mismatch, possible overrun", prog->name);
6318 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
6319 handle->postlen = l;
6320 memcpy(handle->postdata, postseparator, l);
6322 handle->postdata[handle->postlen] = 0;
6324 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
6327 // POST: we sign postdata \0 query string
6329 handle->sigdata = (char *)Z_Malloc(8192);
6330 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6331 l = strlen(handle->sigdata);
6332 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
6335 Z_Free(handle->sigdata);
6336 handle->sigdata = NULL;
6339 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6342 Z_Free(handle->sigdata);
6343 handle->sigdata = NULL;
6346 handle->siglen = l + ll;
6347 handle->sigdata[handle->siglen] = 0;
6350 strlcpy(handle->posttype, posttype, sizeof(handle->posttype));
6351 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, handle->posttype, handle->postdata, handle->postlen, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6355 if(postkeyid >= 0 && query_string)
6357 // GET: we sign JUST the query string
6359 handle->sigdata = (char *)Z_Malloc(8192);
6360 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6361 l = strlen(handle->sigdata);
6362 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
6365 Z_Free(handle->sigdata);
6366 handle->sigdata = NULL;
6369 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6372 Z_Free(handle->sigdata);
6373 handle->sigdata = NULL;
6376 handle->siglen = l + ll;
6377 handle->sigdata[handle->siglen] = 0;
6380 handle->postdata = NULL;
6381 handle->postlen = 0;
6382 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, NULL, NULL, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6386 PRVM_G_INT(OFS_RETURN) = 1;
6390 if(handle->postdata)
6391 Z_Free(handle->postdata);
6393 Z_Free(handle->sigdata);
6395 PRVM_G_INT(OFS_RETURN) = 0;
6399 void VM_netaddress_resolve (prvm_prog_t *prog)
6402 char normalized[128];
6404 lhnetaddress_t addr;
6406 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
6408 ip = PRVM_G_STRING(OFS_PARM0);
6411 port = (int) PRVM_G_FLOAT(OFS_PARM1);
6413 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
6414 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, normalized);
6416 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
6419 //string(prvm_prog_t *prog) getextresponse = #624; // returns the next extResponse packet that was sent to this client
6420 void VM_CL_getextresponse (prvm_prog_t *prog)
6422 VM_SAFEPARMCOUNT(0,VM_argv);
6424 if (cl_net_extresponse_count <= 0)
6425 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6429 --cl_net_extresponse_count;
6430 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6431 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, cl_net_extresponse[first]);
6435 void VM_SV_getextresponse (prvm_prog_t *prog)
6437 VM_SAFEPARMCOUNT(0,VM_argv);
6439 if (sv_net_extresponse_count <= 0)
6440 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6444 --sv_net_extresponse_count;
6445 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6446 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, sv_net_extresponse[first]);
6452 Common functions between menu.dat and clsprogs
6456 //#349 float() isdemo
6457 void VM_CL_isdemo (prvm_prog_t *prog)
6459 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
6460 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
6463 //#355 float() videoplaying
6464 void VM_CL_videoplaying (prvm_prog_t *prog)
6466 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
6467 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6474 callfunction(...,string function_name)
6478 void VM_callfunction(prvm_prog_t *prog)
6483 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6485 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6487 VM_CheckEmptyString(prog, s);
6489 func = PRVM_ED_FindFunction(prog, s);
6492 prog->error_cmd("VM_callfunction: function %s not found !", s);
6493 else if (func->first_statement < 0)
6495 // negative statements are built in functions
6496 int builtinnumber = -func->first_statement;
6497 prog->xfunction->builtinsprofile++;
6498 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6499 prog->builtins[builtinnumber](prog);
6501 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
6503 else if(func - prog->functions > 0)
6506 prog->ExecuteProgram(prog, func - prog->functions,"");
6515 float isfunction(string function_name)
6518 void VM_isfunction(prvm_prog_t *prog)
6523 VM_SAFEPARMCOUNT(1, VM_isfunction);
6525 s = PRVM_G_STRING(OFS_PARM0);
6527 VM_CheckEmptyString(prog, s);
6529 func = PRVM_ED_FindFunction(prog, s);
6532 PRVM_G_FLOAT(OFS_RETURN) = false;
6534 PRVM_G_FLOAT(OFS_RETURN) = true;
6541 string sprintf(string format, ...)
6545 void VM_sprintf(prvm_prog_t *prog)
6548 char outbuf[MAX_INPUTLINE];
6549 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6552 int width, precision, thisarg, flags;
6556 static prvm_int_t dummyivec[3] = {0, 0, 0};
6557 static prvm_vec_t dummyvec[3] = {0, 0, 0};
6560 #define PRINTF_ALTERNATE 1
6561 #define PRINTF_ZEROPAD 2
6562 #define PRINTF_LEFT 4
6563 #define PRINTF_SPACEPOSITIVE 8
6564 #define PRINTF_SIGNPOSITIVE 16
6568 s = PRVM_G_STRING(OFS_PARM0);
6570 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6571 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6572 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6573 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((prvm_int_t*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6574 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6589 // complete directive format:
6598 // is number following?
6599 if(*s >= '0' && *s <= '9')
6601 width = strtol(s, &err, 10);
6604 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6617 flags |= PRINTF_ZEROPAD;
6619 width = -1; // it was just a flag
6631 case '#': flags |= PRINTF_ALTERNATE; break;
6632 case '0': flags |= PRINTF_ZEROPAD; break;
6633 case '-': flags |= PRINTF_LEFT; break;
6634 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6635 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6645 if(*s >= '0' && *s <= '9')
6647 width = strtol(s, &err, 10);
6648 if(!err || *err != '$')
6650 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6657 width = GETARG_FLOAT(width);
6660 flags |= PRINTF_LEFT;
6664 else if(*s >= '0' && *s <= '9')
6666 width = strtol(s, &err, 10);
6669 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6675 flags |= PRINTF_LEFT;
6679 // otherwise width stays -1
6688 if(*s >= '0' && *s <= '9')
6690 precision = strtol(s, &err, 10);
6691 if(!err || *err != '$')
6693 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6699 precision = argpos++;
6700 precision = GETARG_FLOAT(precision);
6702 else if(*s >= '0' && *s <= '9')
6704 precision = strtol(s, &err, 10);
6707 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6714 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6723 case 'h': isfloat = 1; break;
6724 case 'l': isfloat = 0; break;
6725 case 'L': isfloat = 0; break;
6736 // now s points to the final directive char and is no longer changed
6751 if(*s != 's' && *s != 'c')
6752 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6753 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6754 if(flags & PRINTF_LEFT) *f++ = '-';
6755 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6756 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6763 if(*s == 'd' || *s == 'i' || *s == 'o' || *s == 'u' || *s == 'x' || *s == 'X')
6765 // make it use a good integer type
6766 for(p = INT_LOSSLESS_FORMAT_SIZE; *p; )
6772 if(width < 0) // not set
6778 if(precision < 0) // not set
6779 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
6781 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_S(GETARG_INT(thisarg))));
6783 case 'o': case 'u': case 'x': case 'X':
6784 if(precision < 0) // not set
6785 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
6787 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_FLOAT(thisarg)) : INT_LOSSLESS_FORMAT_CONVERT_U(GETARG_INT(thisarg))));
6789 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6790 if(precision < 0) // not set
6791 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6793 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6797 if(precision < 0) // not set
6798 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6799 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6800 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6801 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6804 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6805 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6806 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6807 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6811 if(flags & PRINTF_ALTERNATE)
6813 if(precision < 0) // not set
6814 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6816 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6820 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6822 const char *buf = u8_encodech(c, NULL, charbuf16);
6825 if(precision < 0) // not set
6826 precision = end - o - 1;
6827 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6831 if(flags & PRINTF_ALTERNATE)
6833 if(precision < 0) // not set
6834 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6836 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6840 if(precision < 0) // not set
6841 precision = end - o - 1;
6842 if(flags & PRINTF_SIGNPOSITIVE)
6843 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6845 o += u8_strpad_colorcodes(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6849 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6865 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outbuf);
6871 static dp_model_t *getmodel(prvm_prog_t *prog, prvm_edict_t *ed)
6873 if (prog == SVVM_prog)
6874 return SV_GetModelFromEdict(ed);
6875 else if (prog == CLVM_prog)
6876 return CL_GetModelFromEdict(ed);
6881 struct animatemodel_cache
6884 frameblend_t frameblend[MAX_FRAMEBLENDS];
6885 skeleton_t *skeleton_p;
6886 skeleton_t skeleton;
6887 float *data_vertex3f;
6888 float *data_svector3f;
6889 float *data_tvector3f;
6890 float *data_normal3f;
6892 float *buf_vertex3f;
6893 float *buf_svector3f;
6894 float *buf_tvector3f;
6895 float *buf_normal3f;
6898 static void animatemodel_reset(prvm_prog_t *prog)
6900 if (!prog->animatemodel_cache)
6902 if(prog->animatemodel_cache->buf_vertex3f) Mem_Free(prog->animatemodel_cache->buf_vertex3f);
6903 if(prog->animatemodel_cache->buf_svector3f) Mem_Free(prog->animatemodel_cache->buf_svector3f);
6904 if(prog->animatemodel_cache->buf_tvector3f) Mem_Free(prog->animatemodel_cache->buf_tvector3f);
6905 if(prog->animatemodel_cache->buf_normal3f) Mem_Free(prog->animatemodel_cache->buf_normal3f);
6906 Mem_Free(prog->animatemodel_cache);
6909 static void animatemodel(prvm_prog_t *prog, dp_model_t *model, prvm_edict_t *ed)
6911 skeleton_t *skeleton;
6912 int skeletonindex = -1;
6913 qboolean need = false;
6914 struct animatemodel_cache *animatemodel_cache;
6915 if (!prog->animatemodel_cache)
6917 prog->animatemodel_cache = (struct animatemodel_cache *)Mem_Alloc(prog->progs_mempool, sizeof(struct animatemodel_cache));
6918 memset(prog->animatemodel_cache, 0, sizeof(struct animatemodel_cache));
6920 animatemodel_cache = prog->animatemodel_cache;
6921 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6923 animatemodel_cache->data_vertex3f = model->surfmesh.data_vertex3f;
6924 animatemodel_cache->data_svector3f = model->surfmesh.data_svector3f;
6925 animatemodel_cache->data_tvector3f = model->surfmesh.data_tvector3f;
6926 animatemodel_cache->data_normal3f = model->surfmesh.data_normal3f;
6929 need |= (animatemodel_cache->model != model);
6930 VM_GenerateFrameGroupBlend(prog, ed->priv.server->framegroupblend, ed);
6931 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model, PRVM_serverglobalfloat(time));
6932 need |= (memcmp(&animatemodel_cache->frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6933 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6934 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6936 need |= (animatemodel_cache->skeleton_p != skeleton);
6938 need |= (memcmp(&animatemodel_cache->skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6941 if(model->surfmesh.num_vertices > animatemodel_cache->max_vertices)
6943 animatemodel_cache->max_vertices = model->surfmesh.num_vertices * 2;
6944 if(animatemodel_cache->buf_vertex3f) Mem_Free(animatemodel_cache->buf_vertex3f);
6945 if(animatemodel_cache->buf_svector3f) Mem_Free(animatemodel_cache->buf_svector3f);
6946 if(animatemodel_cache->buf_tvector3f) Mem_Free(animatemodel_cache->buf_tvector3f);
6947 if(animatemodel_cache->buf_normal3f) Mem_Free(animatemodel_cache->buf_normal3f);
6948 animatemodel_cache->buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6949 animatemodel_cache->buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6950 animatemodel_cache->buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6951 animatemodel_cache->buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache->max_vertices);
6953 animatemodel_cache->data_vertex3f = animatemodel_cache->buf_vertex3f;
6954 animatemodel_cache->data_svector3f = animatemodel_cache->buf_svector3f;
6955 animatemodel_cache->data_tvector3f = animatemodel_cache->buf_tvector3f;
6956 animatemodel_cache->data_normal3f = animatemodel_cache->buf_normal3f;
6957 VM_UpdateEdictSkeleton(prog, ed, model, ed->priv.server->frameblend);
6958 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);
6959 animatemodel_cache->model = model;
6960 memcpy(&animatemodel_cache->frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6961 animatemodel_cache->skeleton_p = skeleton;
6963 memcpy(&animatemodel_cache->skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6966 static void getmatrix(prvm_prog_t *prog, prvm_edict_t *ed, matrix4x4_t *out)
6968 if (prog == SVVM_prog)
6969 SV_GetEntityMatrix(prog, ed, out, false);
6970 else if (prog == CLVM_prog)
6971 CL_GetEntityMatrix(prog, ed, out, false);
6973 *out = identitymatrix;
6976 static void applytransform_forward(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6979 getmatrix(prog, ed, &m);
6980 Matrix4x4_Transform(&m, in, out);
6983 static void applytransform_forward_direction(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6986 getmatrix(prog, ed, &m);
6987 Matrix4x4_Transform3x3(&m, in, out);
6990 static void applytransform_inverted(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6993 getmatrix(prog, ed, &m);
6994 Matrix4x4_Invert_Full(&n, &m);
6995 Matrix4x4_Transform3x3(&n, in, out);
6998 static void applytransform_forward_normal(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
7002 getmatrix(prog, ed, &m);
7003 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
7007 static void clippointtosurface(prvm_prog_t *prog, prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
7010 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
7012 animatemodel(prog, model, ed);
7013 bestdist = 1000000000;
7015 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
7017 // clip original point to each triangle of the surface and find the
7018 // triangle that is closest
7019 v[0] = prog->animatemodel_cache->data_vertex3f + e[0] * 3;
7020 v[1] = prog->animatemodel_cache->data_vertex3f + e[1] * 3;
7021 v[2] = prog->animatemodel_cache->data_vertex3f + e[2] * 3;
7022 TriangleNormal(v[0], v[1], v[2], facenormal);
7023 VectorNormalize(facenormal);
7024 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
7025 VectorMA(p, offsetdist, facenormal, temp);
7026 for (j = 0, k = 2;j < 3;k = j, j++)
7028 VectorSubtract(v[k], v[j], edgenormal);
7029 CrossProduct(edgenormal, facenormal, sidenormal);
7030 VectorNormalize(sidenormal);
7031 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
7033 VectorMA(temp, offsetdist, sidenormal, temp);
7035 dist = VectorDistance2(temp, p);
7036 if (bestdist > dist)
7039 VectorCopy(temp, out);
7044 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
7046 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
7048 return model->data_surfaces + surfacenum + model->firstmodelsurface;
7052 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
7053 void VM_getsurfacenumpoints(prvm_prog_t *prog)
7056 msurface_t *surface;
7057 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
7058 // return 0 if no such surface
7059 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7061 PRVM_G_FLOAT(OFS_RETURN) = 0;
7065 // note: this (incorrectly) assumes it is a simple polygon
7066 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
7068 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
7069 void VM_getsurfacepoint(prvm_prog_t *prog)
7073 msurface_t *surface;
7076 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
7077 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7078 ed = PRVM_G_EDICT(OFS_PARM0);
7079 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7081 // note: this (incorrectly) assumes it is a simple polygon
7082 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
7083 if (pointnum < 0 || pointnum >= surface->num_vertices)
7085 animatemodel(prog, model, ed);
7086 applytransform_forward(prog, &(prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7087 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7089 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
7090 // float SPA_POSITION = 0;
7091 // float SPA_S_AXIS = 1;
7092 // float SPA_T_AXIS = 2;
7093 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
7094 // float SPA_TEXCOORDS0 = 4;
7095 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
7096 // float SPA_LIGHTMAP0_COLOR = 6;
7097 void VM_getsurfacepointattribute(prvm_prog_t *prog)
7101 msurface_t *surface;
7106 VM_SAFEPARMCOUNT(4, VM_getsurfacepointattribute);
7107 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7108 ed = PRVM_G_EDICT(OFS_PARM0);
7109 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7111 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
7112 if (pointnum < 0 || pointnum >= surface->num_vertices)
7114 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
7116 animatemodel(prog, model, ed);
7118 switch( attributetype ) {
7119 // float SPA_POSITION = 0;
7121 applytransform_forward(prog, &(prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7122 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7124 // float SPA_S_AXIS = 1;
7126 applytransform_forward_direction(prog, &(prog->animatemodel_cache->data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7127 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7129 // float SPA_T_AXIS = 2;
7131 applytransform_forward_direction(prog, &(prog->animatemodel_cache->data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7132 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7134 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
7136 applytransform_forward_direction(prog, &(prog->animatemodel_cache->data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7137 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7139 // float SPA_TEXCOORDS0 = 4;
7141 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
7142 result[0] = texcoord[0];
7143 result[1] = texcoord[1];
7145 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7148 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
7150 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
7151 result[0] = texcoord[0];
7152 result[1] = texcoord[1];
7154 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7157 // float SPA_LIGHTMAP0_COLOR = 6;
7159 // ignore alpha for now..
7160 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
7163 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
7167 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
7168 void VM_getsurfacenormal(prvm_prog_t *prog)
7171 msurface_t *surface;
7174 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
7175 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7176 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7178 // note: this only returns the first triangle, so it doesn't work very
7179 // well for curved surfaces or arbitrary meshes
7180 animatemodel(prog, model, PRVM_G_EDICT(OFS_PARM0));
7181 TriangleNormal((prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex), (prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex) + 3, (prog->animatemodel_cache->data_vertex3f + 3 * surface->num_firstvertex) + 6, normal);
7182 applytransform_forward_normal(prog, normal, PRVM_G_EDICT(OFS_PARM0), result);
7183 VectorNormalize(result);
7184 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7186 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
7187 void VM_getsurfacetexture(prvm_prog_t *prog)
7190 msurface_t *surface;
7191 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
7192 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
7193 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7195 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, surface->texture->name);
7197 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
7198 void VM_getsurfacenearpoint(prvm_prog_t *prog)
7200 int surfacenum, best;
7202 vec_t dist, bestdist;
7205 msurface_t *surface;
7207 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
7208 PRVM_G_FLOAT(OFS_RETURN) = -1;
7209 ed = PRVM_G_EDICT(OFS_PARM0);
7210 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), point);
7212 if (!ed || ed->priv.server->free)
7214 model = getmodel(prog, ed);
7215 if (!model || !model->num_surfaces)
7218 animatemodel(prog, model, ed);
7220 applytransform_inverted(prog, point, ed, p);
7222 bestdist = 1000000000;
7223 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
7225 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
7226 // first see if the nearest point on the surface's box is closer than the previous match
7227 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
7228 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
7229 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
7230 dist = VectorLength2(clipped);
7231 if (dist < bestdist)
7233 // it is, check the nearest point on the actual geometry
7234 clippointtosurface(prog, ed, model, surface, p, clipped);
7235 VectorSubtract(clipped, p, clipped);
7236 dist += VectorLength2(clipped);
7237 if (dist < bestdist)
7239 // that's closer too, store it as the best match
7245 PRVM_G_FLOAT(OFS_RETURN) = best;
7247 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
7248 void VM_getsurfaceclippedpoint(prvm_prog_t *prog)
7252 msurface_t *surface;
7254 VM_SAFEPARMCOUNT(3, VM_getsurfaceclippedpoint);
7255 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7256 ed = PRVM_G_EDICT(OFS_PARM0);
7257 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7259 animatemodel(prog, model, ed);
7260 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), inp);
7261 applytransform_inverted(prog, inp, ed, p);
7262 clippointtosurface(prog, ed, model, surface, p, out);
7263 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
7266 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
7267 void VM_getsurfacenumtriangles(prvm_prog_t *prog)
7270 msurface_t *surface;
7271 VM_SAFEPARMCOUNT(2, VM_getsurfacenumtriangles);
7272 // return 0 if no such surface
7273 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7275 PRVM_G_FLOAT(OFS_RETURN) = 0;
7279 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
7281 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
7282 void VM_getsurfacetriangle(prvm_prog_t *prog)
7284 const vec3_t d = {-1, -1, -1};
7287 msurface_t *surface;
7289 VM_SAFEPARMCOUNT(3, VM_getsurfacetriangle);
7290 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7291 ed = PRVM_G_EDICT(OFS_PARM0);
7292 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7294 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
7295 if (trinum < 0 || trinum >= surface->num_triangles)
7297 // FIXME: implement rotation/scaling
7298 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
7305 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(prog, ed, f); else World_Physics_ApplyCmd(ed, f)
7307 static edict_odefunc_t *VM_physics_newstackfunction(prvm_prog_t *prog, prvm_edict_t *ed, edict_odefunc_t *f)
7309 edict_odefunc_t *newfunc, *func;
7311 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
7312 memcpy(newfunc, f, sizeof(edict_odefunc_t));
7313 newfunc->next = NULL;
7314 if (!ed->priv.server->ode_func)
7315 ed->priv.server->ode_func = newfunc;
7318 for (func = ed->priv.server->ode_func; func->next; func = func->next);
7319 func->next = newfunc;
7324 // void(entity e, float physics_enabled) physics_enable = #;
7325 void VM_physics_enable(prvm_prog_t *prog)
7330 VM_SAFEPARMCOUNT(2, VM_physics_enable);
7331 ed = PRVM_G_EDICT(OFS_PARM0);
7334 if (developer.integer > 0)
7335 VM_Warning(prog, "VM_physics_enable: null entity!\n");
7338 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7339 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7341 VM_Warning(prog, "VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
7344 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
7345 VM_physics_ApplyCmd(ed, &f);
7348 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
7349 void VM_physics_addforce(prvm_prog_t *prog)
7354 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
7355 ed = PRVM_G_EDICT(OFS_PARM0);
7358 if (developer.integer > 0)
7359 VM_Warning(prog, "VM_physics_addforce: null entity!\n");
7362 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7363 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7365 VM_Warning(prog, "VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
7368 f.type = ODEFUNC_FORCE;
7369 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7370 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), f.v2);
7371 VM_physics_ApplyCmd(ed, &f);
7374 // void(entity e, vector torque) physics_addtorque = #;
7375 void VM_physics_addtorque(prvm_prog_t *prog)
7380 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
7381 ed = PRVM_G_EDICT(OFS_PARM0);
7384 if (developer.integer > 0)
7385 VM_Warning(prog, "VM_physics_addtorque: null entity!\n");
7388 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7389 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7391 VM_Warning(prog, "VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
7394 f.type = ODEFUNC_TORQUE;
7395 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7396 VM_physics_ApplyCmd(ed, &f);
7399 extern cvar_t prvm_coverage;
7400 void VM_coverage(prvm_prog_t *prog)
7402 VM_SAFEPARMCOUNT(0, VM_coverage);
7403 if (prog->explicit_profile[prog->xstatement]++ == 0 && (prvm_coverage.integer & 2))
7404 PRVM_ExplicitCoverageEvent(prog, prog->xfunction, prog->xstatement);