]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - clvm_cmds.c
major overhaul for thread-safety - many global variables and static
[xonotic/darkplaces.git] / clvm_cmds.c
index a9c30b4297d49526ab10f6461e475270912a6d4e..62b84db36843aee60a0fcab2e8c69059bb8caa75 100644 (file)
@@ -25,22 +25,15 @@ extern cvar_t r_equalize_entities_fullbright;
 
 r_refdef_view_t csqc_original_r_refdef_view;
 
-sfx_t *S_FindName(const char *name);
-int Sbar_GetSortedPlayerIndex (int index);
-void Sbar_SortFrags (void);
-void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius);
-void CSQC_RelinkAllEntities (int drawmask);
-void CSQC_RelinkCSQCEntities (void);
-
 // #1 void(vector ang) makevectors
-static void VM_CL_makevectors (void)
+static void VM_CL_makevectors (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_CL_makevectors);
        AngleVectors (PRVM_G_VECTOR(OFS_PARM0), PRVM_clientglobalvector(v_forward), PRVM_clientglobalvector(v_right), PRVM_clientglobalvector(v_up));
 }
 
 // #2 void(entity e, vector o) setorigin
-void VM_CL_setorigin (void)
+static void VM_CL_setorigin (prvm_prog_t *prog)
 {
        prvm_edict_t    *e;
        float   *org;
@@ -49,12 +42,12 @@ void VM_CL_setorigin (void)
        e = PRVM_G_EDICT(OFS_PARM0);
        if (e == prog->edicts)
        {
-               VM_Warning("setorigin: can not modify world entity\n");
+               VM_Warning(prog, "setorigin: can not modify world entity\n");
                return;
        }
        if (e->priv.required->free)
        {
-               VM_Warning("setorigin: can not modify free entity\n");
+               VM_Warning(prog, "setorigin: can not modify free entity\n");
                return;
        }
        org = PRVM_G_VECTOR(OFS_PARM1);
@@ -62,13 +55,13 @@ void VM_CL_setorigin (void)
        CL_LinkEdict(e);
 }
 
-static void SetMinMaxSize (prvm_edict_t *e, float *min, float *max)
+static void SetMinMaxSize (prvm_prog_t *prog, prvm_edict_t *e, float *min, float *max)
 {
        int             i;
 
        for (i=0 ; i<3 ; i++)
                if (min[i] > max[i])
-                       PRVM_ERROR("SetMinMaxSize: backwards mins/maxs");
+                       prog->error_cmd("SetMinMaxSize: backwards mins/maxs");
 
        // set derived values
        VectorCopy (min, PRVM_clientedictvector(e, mins));
@@ -79,7 +72,7 @@ static void SetMinMaxSize (prvm_edict_t *e, float *min, float *max)
 }
 
 // #3 void(entity e, string m) setmodel
-void VM_CL_setmodel (void)
+static void VM_CL_setmodel (prvm_prog_t *prog)
 {
        prvm_edict_t    *e;
        const char              *m;
@@ -99,7 +92,7 @@ void VM_CL_setmodel (void)
                if (!strcmp(cl.csqc_model_precache[i]->name, m))
                {
                        mod = cl.csqc_model_precache[i];
-                       PRVM_clientedictstring(e, model) = PRVM_SetEngineString(mod->name);
+                       PRVM_clientedictstring(e, model) = PRVM_SetEngineString(prog, mod->name);
                        PRVM_clientedictfloat(e, modelindex) = -(i+1);
                        break;
                }
@@ -111,7 +104,7 @@ void VM_CL_setmodel (void)
                        mod = cl.model_precache[i];
                        if (mod && !strcmp(mod->name, m))
                        {
-                               PRVM_clientedictstring(e, model) = PRVM_SetEngineString(mod->name);
+                               PRVM_clientedictstring(e, model) = PRVM_SetEngineString(prog, mod->name);
                                PRVM_clientedictfloat(e, modelindex) = i;
                                break;
                        }
@@ -124,13 +117,13 @@ void VM_CL_setmodel (void)
        }
        else
        {
-               SetMinMaxSize (e, vec3_origin, vec3_origin);
-               VM_Warning ("setmodel: model '%s' not precached\n", m);
+               SetMinMaxSize (prog, e, vec3_origin, vec3_origin);
+               VM_Warning(prog, "setmodel: model '%s' not precached\n", m);
        }
 }
 
 // #4 void(entity e, vector min, vector max) setsize
-static void VM_CL_setsize (void)
+static void VM_CL_setsize (prvm_prog_t *prog)
 {
        prvm_edict_t    *e;
        float                   *min, *max;
@@ -139,24 +132,24 @@ static void VM_CL_setsize (void)
        e = PRVM_G_EDICT(OFS_PARM0);
        if (e == prog->edicts)
        {
-               VM_Warning("setsize: can not modify world entity\n");
+               VM_Warning(prog, "setsize: can not modify world entity\n");
                return;
        }
        if (e->priv.server->free)
        {
-               VM_Warning("setsize: can not modify free entity\n");
+               VM_Warning(prog, "setsize: can not modify free entity\n");
                return;
        }
        min = PRVM_G_VECTOR(OFS_PARM1);
        max = PRVM_G_VECTOR(OFS_PARM2);
 
-       SetMinMaxSize( e, min, max );
+       SetMinMaxSize( prog, e, min, max );
 
        CL_LinkEdict(e);
 }
 
 // #8 void(entity e, float chan, string samp, float volume, float atten) sound
-static void VM_CL_sound (void)
+static void VM_CL_sound (prvm_prog_t *prog)
 {
        const char                      *sample;
        int                                     channel;
@@ -177,13 +170,13 @@ static void VM_CL_sound (void)
 
        if (volume < 0 || volume > 1)
        {
-               VM_Warning("VM_CL_sound: volume must be in range 0-1\n");
+               VM_Warning(prog, "VM_CL_sound: volume must be in range 0-1\n");
                return;
        }
 
        if (attenuation < 0 || attenuation > 4)
        {
-               VM_Warning("VM_CL_sound: attenuation must be in range 0-4\n");
+               VM_Warning(prog, "VM_CL_sound: attenuation must be in range 0-4\n");
                return;
        }
 
@@ -202,7 +195,7 @@ static void VM_CL_sound (void)
 
        if (!IS_CHAN(channel))
        {
-               VM_Warning("VM_CL_sound: channel must be in range 0-127\n");
+               VM_Warning(prog, "VM_CL_sound: channel must be in range 0-127\n");
                return;
        }
 
@@ -211,7 +204,7 @@ static void VM_CL_sound (void)
 }
 
 // #483 void(vector origin, string sample, float volume, float attenuation) pointsound
-static void VM_CL_pointsound(void)
+static void VM_CL_pointsound(prvm_prog_t *prog)
 {
        const char                      *sample;
        float                           volume;
@@ -227,13 +220,13 @@ static void VM_CL_pointsound(void)
 
        if (volume < 0 || volume > 1)
        {
-               VM_Warning("VM_CL_pointsound: volume must be in range 0-1\n");
+               VM_Warning(prog, "VM_CL_pointsound: volume must be in range 0-1\n");
                return;
        }
 
        if (attenuation < 0 || attenuation > 4)
        {
-               VM_Warning("VM_CL_pointsound: attenuation must be in range 0-4\n");
+               VM_Warning(prog, "VM_CL_pointsound: attenuation must be in range 0-4\n");
                return;
        }
 
@@ -242,16 +235,16 @@ static void VM_CL_pointsound(void)
 }
 
 // #14 entity() spawn
-static void VM_CL_spawn (void)
+static void VM_CL_spawn (prvm_prog_t *prog)
 {
        prvm_edict_t *ed;
-       ed = PRVM_ED_Alloc();
+       ed = PRVM_ED_Alloc(prog);
        VM_RETURN_EDICT(ed);
 }
 
-void CL_VM_SetTraceGlobals(const trace_t *trace, int svent)
+static void CL_VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace, int svent)
 {
-       VM_SetTraceGlobals(trace);
+       VM_SetTraceGlobals(prog, trace);
        PRVM_clientglobalfloat(trace_networkentity) = svent;
 }
 
@@ -259,7 +252,7 @@ void CL_VM_SetTraceGlobals(const trace_t *trace, int svent)
 #define CL_HitNetworkPlayers(move)     !((move) == MOVE_WORLDONLY || (move) == MOVE_NOMONSTERS)
 
 // #16 void(vector v1, vector v2, float movetype, entity ignore) traceline
-static void VM_CL_traceline (void)
+static void VM_CL_traceline (prvm_prog_t *prog)
 {
        float   *v1, *v2;
        trace_t trace;
@@ -278,11 +271,11 @@ static void VM_CL_traceline (void)
        ent = PRVM_G_EDICT(OFS_PARM3);
 
        if (IS_NAN(v1[0]) || IS_NAN(v1[1]) || IS_NAN(v1[2]) || IS_NAN(v2[0]) || IS_NAN(v2[1]) || IS_NAN(v2[2]))
-               PRVM_ERROR("%s: NAN errors detected in traceline('%f %f %f', '%f %f %f', %i, entity %i)\n", PRVM_NAME, v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], move, PRVM_EDICT_TO_PROG(ent));
+               prog->error_cmd("%s: NAN errors detected in traceline('%f %f %f', '%f %f %f', %i, entity %i)\n", prog->name, v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], move, PRVM_EDICT_TO_PROG(ent));
 
        trace = CL_TraceLine(v1, v2, move, ent, CL_GenericHitSuperContentsMask(ent), CL_HitNetworkBrushModels(move), CL_HitNetworkPlayers(move), &svent, true, false);
 
-       CL_VM_SetTraceGlobals(&trace, svent);
+       CL_VM_SetTraceGlobals(prog, &trace, svent);
 //     R_TimeReport("traceline");
 }
 
@@ -298,7 +291,7 @@ tracebox (vector1, vector mins, vector maxs, vector2, tryents)
 =================
 */
 // LordHavoc: added this for my own use, VERY useful, similar to traceline
-static void VM_CL_tracebox (void)
+static void VM_CL_tracebox (prvm_prog_t *prog)
 {
        float   *v1, *v2, *m1, *m2;
        trace_t trace;
@@ -318,15 +311,15 @@ static void VM_CL_tracebox (void)
        ent = PRVM_G_EDICT(OFS_PARM5);
 
        if (IS_NAN(v1[0]) || IS_NAN(v1[1]) || IS_NAN(v1[2]) || IS_NAN(v2[0]) || IS_NAN(v2[1]) || IS_NAN(v2[2]))
-               PRVM_ERROR("%s: NAN errors detected in tracebox('%f %f %f', '%f %f %f', '%f %f %f', '%f %f %f', %i, entity %i)\n", PRVM_NAME, v1[0], v1[1], v1[2], m1[0], m1[1], m1[2], m2[0], m2[1], m2[2], v2[0], v2[1], v2[2], move, PRVM_EDICT_TO_PROG(ent));
+               prog->error_cmd("%s: NAN errors detected in tracebox('%f %f %f', '%f %f %f', '%f %f %f', '%f %f %f', %i, entity %i)\n", prog->name, v1[0], v1[1], v1[2], m1[0], m1[1], m1[2], m2[0], m2[1], m2[2], v2[0], v2[1], v2[2], move, PRVM_EDICT_TO_PROG(ent));
 
        trace = CL_TraceBox(v1, m1, m2, v2, move, ent, CL_GenericHitSuperContentsMask(ent), CL_HitNetworkBrushModels(move), CL_HitNetworkPlayers(move), &svent, true);
 
-       CL_VM_SetTraceGlobals(&trace, svent);
+       CL_VM_SetTraceGlobals(prog, &trace, svent);
 //     R_TimeReport("tracebox");
 }
 
-trace_t CL_Trace_Toss (prvm_edict_t *tossent, prvm_edict_t *ignore, int *svent)
+static trace_t CL_Trace_Toss (prvm_prog_t *prog, prvm_edict_t *tossent, prvm_edict_t *ignore, int *svent)
 {
        int i;
        float gravity;
@@ -368,7 +361,7 @@ trace_t CL_Trace_Toss (prvm_edict_t *tossent, prvm_edict_t *ignore, int *svent)
        return trace;
 }
 
-static void VM_CL_tracetoss (void)
+static void VM_CL_tracetoss (prvm_prog_t *prog)
 {
        trace_t trace;
        prvm_edict_t    *ent;
@@ -382,19 +375,19 @@ static void VM_CL_tracetoss (void)
        ent = PRVM_G_EDICT(OFS_PARM0);
        if (ent == prog->edicts)
        {
-               VM_Warning("tracetoss: can not use world entity\n");
+               VM_Warning(prog, "tracetoss: can not use world entity\n");
                return;
        }
        ignore = PRVM_G_EDICT(OFS_PARM1);
 
-       trace = CL_Trace_Toss (ent, ignore, &svent);
+       trace = CL_Trace_Toss (prog, ent, ignore, &svent);
 
-       CL_VM_SetTraceGlobals(&trace, svent);
+       CL_VM_SetTraceGlobals(prog, &trace, svent);
 }
 
 
 // #20 void(string s) precache_model
-void VM_CL_precache_model (void)
+static void VM_CL_precache_model (prvm_prog_t *prog)
 {
        const char      *name;
        int                     i;
@@ -424,13 +417,13 @@ void VM_CL_precache_model (void)
                                return;
                        }
                }
-               VM_Warning("VM_CL_precache_model: no free models\n");
+               VM_Warning(prog, "VM_CL_precache_model: no free models\n");
                return;
        }
-       VM_Warning("VM_CL_precache_model: model \"%s\" not found\n", name);
+       VM_Warning(prog, "VM_CL_precache_model: model \"%s\" not found\n", name);
 }
 
-int CSQC_EntitiesInBox (vec3_t mins, vec3_t maxs, int maxlist, prvm_edict_t **list)
+static int CSQC_EntitiesInBox (prvm_prog_t *prog, vec3_t mins, vec3_t maxs, int maxlist, prvm_edict_t **list)
 {
        prvm_edict_t    *ent;
        int                             i, k;
@@ -447,7 +440,7 @@ int CSQC_EntitiesInBox (vec3_t mins, vec3_t maxs, int maxlist, prvm_edict_t **li
 }
 
 // #22 entity(vector org, float rad) findradius
-static void VM_CL_findradius (void)
+static void VM_CL_findradius (prvm_prog_t *prog)
 {
        prvm_edict_t    *ent, *chain;
        vec_t                   radius, radius2;
@@ -463,7 +456,7 @@ static void VM_CL_findradius (void)
        else
                chainfield = prog->fieldoffsets.chain;
        if(chainfield < 0)
-               PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
+               prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
 
        chain = (prvm_edict_t *)prog->edicts;
 
@@ -477,7 +470,7 @@ static void VM_CL_findradius (void)
        maxs[0] = org[0] + (radius + 1);
        maxs[1] = org[1] + (radius + 1);
        maxs[2] = org[2] + (radius + 1);
-       numtouchedicts = CSQC_EntitiesInBox(mins, maxs, MAX_EDICTS, touchedicts);
+       numtouchedicts = CSQC_EntitiesInBox(prog, mins, maxs, MAX_EDICTS, touchedicts);
        if (numtouchedicts > MAX_EDICTS)
        {
                // this never happens   //[515]: for what then ?
@@ -514,7 +507,7 @@ static void VM_CL_findradius (void)
 }
 
 // #34 float() droptofloor
-static void VM_CL_droptofloor (void)
+static void VM_CL_droptofloor (prvm_prog_t *prog)
 {
        prvm_edict_t            *ent;
        vec3_t                          end;
@@ -528,12 +521,12 @@ static void VM_CL_droptofloor (void)
        ent = PRVM_PROG_TO_EDICT(PRVM_clientglobaledict(self));
        if (ent == prog->edicts)
        {
-               VM_Warning("droptofloor: can not modify world entity\n");
+               VM_Warning(prog, "droptofloor: can not modify world entity\n");
                return;
        }
        if (ent->priv.server->free)
        {
-               VM_Warning("droptofloor: can not modify free entity\n");
+               VM_Warning(prog, "droptofloor: can not modify free entity\n");
                return;
        }
 
@@ -554,7 +547,7 @@ static void VM_CL_droptofloor (void)
 }
 
 // #35 void(float style, string value) lightstyle
-static void VM_CL_lightstyle (void)
+static void VM_CL_lightstyle (prvm_prog_t *prog)
 {
        int                     i;
        const char      *c;
@@ -565,7 +558,7 @@ static void VM_CL_lightstyle (void)
        c = PRVM_G_STRING(OFS_PARM1);
        if (i >= cl.max_lightstyle)
        {
-               VM_Warning("VM_CL_lightstyle >= MAX_LIGHTSTYLES\n");
+               VM_Warning(prog, "VM_CL_lightstyle >= MAX_LIGHTSTYLES\n");
                return;
        }
        strlcpy (cl.lightstyle[i].map, c, sizeof (cl.lightstyle[i].map));
@@ -574,7 +567,7 @@ static void VM_CL_lightstyle (void)
 }
 
 // #40 float(entity e) checkbottom
-static void VM_CL_checkbottom (void)
+static void VM_CL_checkbottom (prvm_prog_t *prog)
 {
        static int              cs_yes, cs_no;
        prvm_edict_t    *ent;
@@ -645,14 +638,14 @@ realcheck:
 }
 
 // #41 float(vector v) pointcontents
-static void VM_CL_pointcontents (void)
+static void VM_CL_pointcontents (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_CL_pointcontents);
        PRVM_G_FLOAT(OFS_RETURN) = Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_PointSuperContents(PRVM_G_VECTOR(OFS_PARM0)));
 }
 
 // #48 void(vector o, vector d, float color, float count) particle
-static void VM_CL_particle (void)
+static void VM_CL_particle (prvm_prog_t *prog)
 {
        float   *org, *dir;
        int             count;
@@ -667,7 +660,7 @@ static void VM_CL_particle (void)
 }
 
 // #74 void(vector pos, string samp, float vol, float atten) ambientsound
-static void VM_CL_ambientsound (void)
+static void VM_CL_ambientsound (prvm_prog_t *prog)
 {
        float   *f;
        sfx_t   *s;
@@ -678,7 +671,7 @@ static void VM_CL_ambientsound (void)
 }
 
 // #92 vector(vector org[, float lpflag]) getlight (DP_QC_GETLIGHT)
-static void VM_CL_getlight (void)
+static void VM_CL_getlight (prvm_prog_t *prog)
 {
        vec3_t ambientcolor, diffusecolor, diffusenormal;
        vec_t *p;
@@ -704,7 +697,6 @@ static void VM_CL_getlight (void)
 
 //============================================================================
 //[515]: SCENE MANAGER builtins
-extern qboolean CSQC_AddRenderEdict (prvm_edict_t *ed, int edictnum);//csprogs.c
 
 void CSQC_R_RecalcView (void)
 {
@@ -716,9 +708,8 @@ void CSQC_R_RecalcView (void)
        Matrix4x4_Concat(&viewmodelmatrix_withbob, &r_refdef.view.matrix, &cl.csqc_viewmodelmatrixfromengine);
 }
 
-void CL_RelinkLightFlashes(void);
 //#300 void() clearscene (EXT_CSQC)
-void VM_CL_R_ClearScene (void)
+static void VM_CL_R_ClearScene (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_R_ClearScene);
        // clear renderable entity and light lists
@@ -735,11 +726,9 @@ void VM_CL_R_ClearScene (void)
 }
 
 //#301 void(float mask) addentities (EXT_CSQC)
-extern void CSQC_Predraw (prvm_edict_t *ed);//csprogs.c
-extern void CSQC_Think (prvm_edict_t *ed);//csprogs.c
-void VM_CL_R_AddEntities (void)
+static void VM_CL_R_AddEntities (prvm_prog_t *prog)
 {
-       double t = Sys_DoubleTime();
+       double t = Sys_DirtyTime();
        int                     i, drawmask;
        prvm_edict_t *ed;
        VM_SAFEPARMCOUNT(1, VM_CL_R_AddEntities);
@@ -768,16 +757,18 @@ void VM_CL_R_AddEntities (void)
        }
 
        // callprofile fixing hack: do not include this time in what is counted for CSQC_UpdateView
-       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= Sys_DoubleTime() - t;
+       t = Sys_DirtyTime() - t;if (t < 0 || t >= 1800) t = 0;
+       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= t;
 }
 
 //#302 void(entity ent) addentity (EXT_CSQC)
-void VM_CL_R_AddEntity (void)
+static void VM_CL_R_AddEntity (prvm_prog_t *prog)
 {
-       double t = Sys_DoubleTime();
+       double t = Sys_DirtyTime();
        VM_SAFEPARMCOUNT(1, VM_CL_R_AddEntity);
        CSQC_AddRenderEdict(PRVM_G_EDICT(OFS_PARM0), 0);
-       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= Sys_DoubleTime() - t;
+       t = Sys_DirtyTime() - t;if (t < 0 || t >= 1800) t = 0;
+       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= t;
 }
 
 //#303 float(float property, ...) setproperty (EXT_CSQC)
@@ -786,7 +777,7 @@ void VM_CL_R_AddEntity (void)
 //#309 float(float property) getproperty
 //#309 vector(float property) getpropertyvec
 // VorteX: make this function be able to return previously set property if new value is not given
-void VM_CL_R_SetView (void)
+static void VM_CL_R_SetView (prvm_prog_t *prog)
 {
        int             c;
        float   *f;
@@ -820,7 +811,7 @@ void VM_CL_R_SetView (void)
                        PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.height;
                        break;
                case VF_VIEWPORT:
-                       VM_Warning("VM_CL_R_GetView : VF_VIEWPORT can't be retrieved, use VF_MIN/VF_SIZE instead\n");
+                       VM_Warning(prog, "VM_CL_R_GetView : VF_VIEWPORT can't be retrieved, use VF_MIN/VF_SIZE instead\n");
                        break;
                case VF_FOV:
                        VectorSet(PRVM_G_VECTOR(OFS_RETURN), r_refdef.view.ortho_x, r_refdef.view.ortho_y, 0);
@@ -916,7 +907,7 @@ void VM_CL_R_SetView (void)
                        break;
                default:
                        PRVM_G_FLOAT(OFS_RETURN) = 0;
-                       VM_Warning("VM_CL_R_GetView : unknown parm %i\n", c);
+                       VM_Warning(prog, "VM_CL_R_GetView : unknown parm %i\n", c);
                        return;
                }
                return;
@@ -1063,16 +1054,16 @@ void VM_CL_R_SetView (void)
                break;
        default:
                PRVM_G_FLOAT(OFS_RETURN) = 0;
-               VM_Warning("VM_CL_R_SetView : unknown parm %i\n", c);
+               VM_Warning(prog, "VM_CL_R_SetView : unknown parm %i\n", c);
                return;
        }
        PRVM_G_FLOAT(OFS_RETURN) = 1;
 }
 
 //#305 void(vector org, float radius, vector lightcolours[, float style, string cubemapname, float pflags]) adddynamiclight (EXT_CSQC)
-void VM_CL_R_AddDynamicLight (void)
+static void VM_CL_R_AddDynamicLight (prvm_prog_t *prog)
 {
-       double t = Sys_DoubleTime();
+       double t = Sys_DirtyTime();
        vec_t *org;
        float radius = 300;
        vec_t *col;
@@ -1119,13 +1110,14 @@ void VM_CL_R_AddDynamicLight (void)
 
        R_RTLight_Update(&r_refdef.scene.templights[r_refdef.scene.numlights], false, &matrix, col, style, cubemapname, castshadow, coronaintensity, coronasizescale, ambientscale, diffusescale, specularscale, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
        r_refdef.scene.lights[r_refdef.scene.numlights] = &r_refdef.scene.templights[r_refdef.scene.numlights];r_refdef.scene.numlights++;
-       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= Sys_DoubleTime() - t;
+       t = Sys_DirtyTime() - t;if (t < 0 || t >= 1800) t = 0;
+       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= t;
 }
 
 //============================================================================
 
 //#310 vector (vector v) cs_unproject (EXT_CSQC)
-static void VM_CL_unproject (void)
+static void VM_CL_unproject (prvm_prog_t *prog)
 {
        float   *f;
        vec3_t  temp;
@@ -1142,7 +1134,7 @@ static void VM_CL_unproject (void)
 }
 
 //#311 vector (vector v) cs_project (EXT_CSQC)
-static void VM_CL_project (void)
+static void VM_CL_project (prvm_prog_t *prog)
 {
        float   *f;
        vec3_t  v;
@@ -1164,7 +1156,7 @@ static void VM_CL_project (void)
 }
 
 //#330 float(float stnum) getstatf (EXT_CSQC)
-static void VM_CL_getstatf (void)
+static void VM_CL_getstatf (prvm_prog_t *prog)
 {
        int i;
        union
@@ -1176,7 +1168,7 @@ static void VM_CL_getstatf (void)
        i = (int)PRVM_G_FLOAT(OFS_PARM0);
        if(i < 0 || i >= MAX_CL_STATS)
        {
-               VM_Warning("VM_CL_getstatf: index>=MAX_CL_STATS or index<0\n");
+               VM_Warning(prog, "VM_CL_getstatf: index>=MAX_CL_STATS or index<0\n");
                return;
        }
        dat.l = cl.stats[i];
@@ -1184,7 +1176,7 @@ static void VM_CL_getstatf (void)
 }
 
 //#331 float(float stnum) getstati (EXT_CSQC)
-static void VM_CL_getstati (void)
+static void VM_CL_getstati (prvm_prog_t *prog)
 {
        int i, index;
        int firstbit, bitcount;
@@ -1208,7 +1200,7 @@ static void VM_CL_getstati (void)
 
        if(index < 0 || index >= MAX_CL_STATS)
        {
-               VM_Warning("VM_CL_getstati: index>=MAX_CL_STATS or index<0\n");
+               VM_Warning(prog, "VM_CL_getstati: index>=MAX_CL_STATS or index<0\n");
                return;
        }
        i = cl.stats[index];
@@ -1218,7 +1210,7 @@ static void VM_CL_getstati (void)
 }
 
 //#332 string(float firststnum) getstats (EXT_CSQC)
-static void VM_CL_getstats (void)
+static void VM_CL_getstats (prvm_prog_t *prog)
 {
        int i;
        char t[17];
@@ -1227,15 +1219,15 @@ static void VM_CL_getstats (void)
        if(i < 0 || i > MAX_CL_STATS-4)
        {
                PRVM_G_INT(OFS_RETURN) = OFS_NULL;
-               VM_Warning("VM_CL_getstats: index>MAX_CL_STATS-4 or index<0\n");
+               VM_Warning(prog, "VM_CL_getstats: index>MAX_CL_STATS-4 or index<0\n");
                return;
        }
        strlcpy(t, (char*)&cl.stats[i], sizeof(t));
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
+       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
 }
 
 //#333 void(entity e, float mdlindex) setmodelindex (EXT_CSQC)
-static void VM_CL_setmodelindex (void)
+static void VM_CL_setmodelindex (prvm_prog_t *prog)
 {
        int                             i;
        prvm_edict_t    *t;
@@ -1256,23 +1248,23 @@ static void VM_CL_setmodelindex (void)
        model = CL_GetModelByIndex(i);
        if (!model)
        {
-               VM_Warning("VM_CL_setmodelindex: null model\n");
+               VM_Warning(prog, "VM_CL_setmodelindex: null model\n");
                return;
        }
-       PRVM_clientedictstring(t, model) = PRVM_SetEngineString(model->name);
+       PRVM_clientedictstring(t, model) = PRVM_SetEngineString(prog, model->name);
        PRVM_clientedictfloat(t, modelindex) = i;
 
        // TODO: check if this breaks needed consistency and maybe add a cvar for it too?? [1/10/2008 Black]
        if (model)
        {
-               SetMinMaxSize (t, model->normalmins, model->normalmaxs);
+               SetMinMaxSize (prog, t, model->normalmins, model->normalmaxs);
        }
        else
-               SetMinMaxSize (t, vec3_origin, vec3_origin);
+               SetMinMaxSize (prog, t, vec3_origin, vec3_origin);
 }
 
 //#334 string(float mdlindex) modelnameforindex (EXT_CSQC)
-static void VM_CL_modelnameforindex (void)
+static void VM_CL_modelnameforindex (prvm_prog_t *prog)
 {
        dp_model_t *model;
 
@@ -1280,11 +1272,11 @@ static void VM_CL_modelnameforindex (void)
 
        PRVM_G_INT(OFS_RETURN) = OFS_NULL;
        model = CL_GetModelByIndex((int)PRVM_G_FLOAT(OFS_PARM0));
-       PRVM_G_INT(OFS_RETURN) = model ? PRVM_SetEngineString(model->name) : 0;
+       PRVM_G_INT(OFS_RETURN) = model ? PRVM_SetEngineString(prog, model->name) : 0;
 }
 
 //#335 float(string effectname) particleeffectnum (EXT_CSQC)
-static void VM_CL_particleeffectnum (void)
+static void VM_CL_particleeffectnum (prvm_prog_t *prog)
 {
        int                     i;
        VM_SAFEPARMCOUNT(1, VM_CL_particleeffectnum);
@@ -1295,7 +1287,7 @@ static void VM_CL_particleeffectnum (void)
 }
 
 // #336 void(entity ent, float effectnum, vector start, vector end[, float color]) trailparticles (EXT_CSQC)
-static void VM_CL_trailparticles (void)
+static void VM_CL_trailparticles (prvm_prog_t *prog)
 {
        int                             i;
        float                   *start, *end;
@@ -1313,7 +1305,7 @@ static void VM_CL_trailparticles (void)
 }
 
 //#337 void(float effectnum, vector origin, vector dir, float count[, float color]) pointparticles (EXT_CSQC)
-static void VM_CL_pointparticles (void)
+static void VM_CL_pointparticles (prvm_prog_t *prog)
 {
        int                     i;
        float n;
@@ -1329,7 +1321,7 @@ static void VM_CL_pointparticles (void)
 }
 
 //#502 void(float effectnum, entity own, vector origin_from, vector origin_to, vector dir_from, vector dir_to, float count, float extflags) boxparticles (DP_CSQC_BOXPARTICLES)
-static void VM_CL_boxparticles (void)
+static void VM_CL_boxparticles (prvm_prog_t *prog)
 {
        int effectnum;
        // prvm_edict_t *own;
@@ -1368,7 +1360,7 @@ static void VM_CL_boxparticles (void)
 }
 
 //#531 void(float pause) setpause
-static void VM_CL_setpause(void) 
+static void VM_CL_setpause(prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_CL_setpause);
        if ((int)PRVM_G_FLOAT(OFS_PARM0) != 0)
@@ -1378,7 +1370,7 @@ static void VM_CL_setpause(void)
 }
 
 //#343 void(float usecursor) setcursormode (DP_CSQC)
-static void VM_CL_setcursormode (void)
+static void VM_CL_setcursormode (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_CL_setcursormode);
        cl.csqc_wantsmousemove = PRVM_G_FLOAT(OFS_PARM0) != 0;
@@ -1386,7 +1378,7 @@ static void VM_CL_setcursormode (void)
 }
 
 //#344 vector() getmousepos (DP_CSQC)
-static void VM_CL_getmousepos(void)
+static void VM_CL_getmousepos(prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0,VM_CL_getmousepos);
 
@@ -1399,7 +1391,7 @@ static void VM_CL_getmousepos(void)
 }
 
 //#345 float(float framenum) getinputstate (EXT_CSQC)
-static void VM_CL_getinputstate (void)
+static void VM_CL_getinputstate (prvm_prog_t *prog)
 {
        int i, frame;
        VM_SAFEPARMCOUNT(1, VM_CL_getinputstate);
@@ -1431,19 +1423,19 @@ static void VM_CL_getinputstate (void)
 }
 
 //#346 void(float sens) setsensitivityscaler (EXT_CSQC)
-static void VM_CL_setsensitivityscale (void)
+static void VM_CL_setsensitivityscale (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_CL_setsensitivityscale);
        cl.sensitivityscale = PRVM_G_FLOAT(OFS_PARM0);
 }
 
 //#347 void() runstandardplayerphysics (EXT_CSQC)
-static void VM_CL_runplayerphysics (void)
+static void VM_CL_runplayerphysics (prvm_prog_t *prog)
 {
 }
 
 //#348 string(float playernum, string keyname) getplayerkeyvalue (EXT_CSQC)
-static void VM_CL_getplayerkey (void)
+static void VM_CL_getplayerkey (prvm_prog_t *prog)
 {
        int                     i;
        char            t[128];
@@ -1508,11 +1500,11 @@ static void VM_CL_getplayerkey (void)
                }
        if(!t[0])
                return;
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
+       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
 }
 
 //#351 void(vector origin, vector forward, vector right, vector up) SetListener (EXT_CSQC)
-static void VM_CL_setlistener (void)
+static void VM_CL_setlistener (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(4, VM_CL_setlistener);
        Matrix4x4_FromVectors(&cl.csqc_listenermatrix, PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), PRVM_G_VECTOR(OFS_PARM3), PRVM_G_VECTOR(OFS_PARM0));
@@ -1520,7 +1512,7 @@ static void VM_CL_setlistener (void)
 }
 
 //#352 void(string cmdname) registercommand (EXT_CSQC)
-static void VM_CL_registercmd (void)
+static void VM_CL_registercmd (prvm_prog_t *prog)
 {
        char *t;
        VM_SAFEPARMCOUNT(1, VM_CL_registercmd);
@@ -1539,64 +1531,64 @@ static void VM_CL_registercmd (void)
 }
 
 //#360 float() readbyte (EXT_CSQC)
-static void VM_CL_ReadByte (void)
+static void VM_CL_ReadByte (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadByte);
-       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadByte();
+       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadByte(&cl_message);
 }
 
 //#361 float() readchar (EXT_CSQC)
-static void VM_CL_ReadChar (void)
+static void VM_CL_ReadChar (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadChar);
-       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadChar();
+       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadChar(&cl_message);
 }
 
 //#362 float() readshort (EXT_CSQC)
-static void VM_CL_ReadShort (void)
+static void VM_CL_ReadShort (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadShort);
-       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadShort();
+       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadShort(&cl_message);
 }
 
 //#363 float() readlong (EXT_CSQC)
-static void VM_CL_ReadLong (void)
+static void VM_CL_ReadLong (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadLong);
-       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadLong();
+       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadLong(&cl_message);
 }
 
 //#364 float() readcoord (EXT_CSQC)
-static void VM_CL_ReadCoord (void)
+static void VM_CL_ReadCoord (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadCoord);
-       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadCoord(cls.protocol);
+       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadCoord(&cl_message, cls.protocol);
 }
 
 //#365 float() readangle (EXT_CSQC)
-static void VM_CL_ReadAngle (void)
+static void VM_CL_ReadAngle (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadAngle);
-       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadAngle(cls.protocol);
+       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadAngle(&cl_message, cls.protocol);
 }
 
 //#366 string() readstring (EXT_CSQC)
-static void VM_CL_ReadString (void)
+static void VM_CL_ReadString (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadString);
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(MSG_ReadString());
+       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, MSG_ReadString(&cl_message, cl_readstring, sizeof(cl_readstring)));
 }
 
 //#367 float() readfloat (EXT_CSQC)
-static void VM_CL_ReadFloat (void)
+static void VM_CL_ReadFloat (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ReadFloat);
-       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadFloat();
+       PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadFloat(&cl_message);
 }
 
 //#501 string() readpicture (DP_CSQC_READWRITEPICTURE)
 extern cvar_t cl_readpicture_force;
-static void VM_CL_ReadPicture (void)
+static void VM_CL_ReadPicture (prvm_prog_t *prog)
 {
        const char *name;
        unsigned char *data;
@@ -1607,8 +1599,8 @@ static void VM_CL_ReadPicture (void)
 
        VM_SAFEPARMCOUNT(0, VM_CL_ReadPicture);
 
-       name = MSG_ReadString();
-       size = MSG_ReadShort();
+       name = MSG_ReadString(&cl_message, cl_readstring, sizeof(cl_readstring));
+       size = MSG_ReadShort(&cl_message);
 
        // check if a texture of that name exists
        // if yes, it is used and the data is discarded
@@ -1625,14 +1617,14 @@ static void VM_CL_ReadPicture (void)
                        // texture found and loaded
                        // skip over the jpeg as we don't need it
                        for(i = 0; i < size; ++i)
-                               (void) MSG_ReadByte();
+                               (void) MSG_ReadByte(&cl_message);
                }
                else
                {
                        // texture not found
                        // use the attached jpeg as texture
                        buf = (unsigned char *) Mem_Alloc(tempmempool, size);
-                       MSG_ReadBytes(size, buf);
+                       MSG_ReadBytes(&cl_message, size, buf);
                        data = JPEG_LoadImage_BGRA(buf, size, NULL);
                        Mem_Free(buf);
                        Draw_NewPic(name, image_width, image_height, false, data);
@@ -1640,12 +1632,12 @@ static void VM_CL_ReadPicture (void)
                }
        }
 
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(name);
+       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, name);
 }
 
 //////////////////////////////////////////////////////////
 
-static void VM_CL_makestatic (void)
+static void VM_CL_makestatic (prvm_prog_t *prog)
 {
        prvm_edict_t *ent;
 
@@ -1654,12 +1646,12 @@ static void VM_CL_makestatic (void)
        ent = PRVM_G_EDICT(OFS_PARM0);
        if (ent == prog->edicts)
        {
-               VM_Warning("makestatic: can not modify world entity\n");
+               VM_Warning(prog, "makestatic: can not modify world entity\n");
                return;
        }
        if (ent->priv.server->free)
        {
-               VM_Warning("makestatic: can not modify free entity\n");
+               VM_Warning(prog, "makestatic: can not modify free entity\n");
                return;
        }
 
@@ -1728,7 +1720,7 @@ static void VM_CL_makestatic (void)
                Con_Printf("Too many static entities");
 
 // throw the entity away now
-       PRVM_ED_Free (ent);
+       PRVM_ED_Free(prog, ent);
 }
 
 //=================================================================//
@@ -1742,30 +1734,30 @@ copies data from one entity to another
 copyentity(src, dst)
 =================
 */
-static void VM_CL_copyentity (void)
+static void VM_CL_copyentity (prvm_prog_t *prog)
 {
        prvm_edict_t *in, *out;
        VM_SAFEPARMCOUNT(2, VM_CL_copyentity);
        in = PRVM_G_EDICT(OFS_PARM0);
        if (in == prog->edicts)
        {
-               VM_Warning("copyentity: can not read world entity\n");
+               VM_Warning(prog, "copyentity: can not read world entity\n");
                return;
        }
        if (in->priv.server->free)
        {
-               VM_Warning("copyentity: can not read free entity\n");
+               VM_Warning(prog, "copyentity: can not read free entity\n");
                return;
        }
        out = PRVM_G_EDICT(OFS_PARM1);
        if (out == prog->edicts)
        {
-               VM_Warning("copyentity: can not modify world entity\n");
+               VM_Warning(prog, "copyentity: can not modify world entity\n");
                return;
        }
        if (out->priv.server->free)
        {
-               VM_Warning("copyentity: can not modify free entity\n");
+               VM_Warning(prog, "copyentity: can not modify free entity\n");
                return;
        }
        memcpy(out->fields.vp, in->fields.vp, prog->entityfields * 4);
@@ -1775,14 +1767,14 @@ static void VM_CL_copyentity (void)
 //=================================================================//
 
 // #404 void(vector org, string modelname, float startframe, float endframe, float framerate) effect (DP_SV_EFFECT)
-static void VM_CL_effect (void)
+static void VM_CL_effect (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(5, VM_CL_effect);
        CL_Effect(PRVM_G_VECTOR(OFS_PARM0), (int)PRVM_G_FLOAT(OFS_PARM1), (int)PRVM_G_FLOAT(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), PRVM_G_FLOAT(OFS_PARM4));
 }
 
 // #405 void(vector org, vector velocity, float howmany) te_blood (DP_TE_BLOOD)
-static void VM_CL_te_blood (void)
+static void VM_CL_te_blood (prvm_prog_t *prog)
 {
        float   *pos;
        vec3_t  pos2;
@@ -1795,7 +1787,7 @@ static void VM_CL_te_blood (void)
 }
 
 // #406 void(vector mincorner, vector maxcorner, float explosionspeed, float howmany) te_bloodshower (DP_TE_BLOODSHOWER)
-static void VM_CL_te_bloodshower (void)
+static void VM_CL_te_bloodshower (prvm_prog_t *prog)
 {
        vec_t speed;
        vec3_t vel1, vel2;
@@ -1813,7 +1805,7 @@ static void VM_CL_te_bloodshower (void)
 }
 
 // #407 void(vector org, vector color) te_explosionrgb (DP_TE_EXPLOSIONRGB)
-static void VM_CL_te_explosionrgb (void)
+static void VM_CL_te_explosionrgb (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1827,28 +1819,28 @@ static void VM_CL_te_explosionrgb (void)
 }
 
 // #408 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color, float gravityflag, float randomveljitter) te_particlecube (DP_TE_PARTICLECUBE)
-static void VM_CL_te_particlecube (void)
+static void VM_CL_te_particlecube (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(7, VM_CL_te_particlecube);
        CL_ParticleCube(PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), (int)PRVM_G_FLOAT(OFS_PARM4), PRVM_G_FLOAT(OFS_PARM5), PRVM_G_FLOAT(OFS_PARM6));
 }
 
 // #409 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color) te_particlerain (DP_TE_PARTICLERAIN)
-static void VM_CL_te_particlerain (void)
+static void VM_CL_te_particlerain (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(5, VM_CL_te_particlerain);
        CL_ParticleRain(PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), (int)PRVM_G_FLOAT(OFS_PARM4), 0);
 }
 
 // #410 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color) te_particlesnow (DP_TE_PARTICLESNOW)
-static void VM_CL_te_particlesnow (void)
+static void VM_CL_te_particlesnow (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(5, VM_CL_te_particlesnow);
        CL_ParticleRain(PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), (int)PRVM_G_FLOAT(OFS_PARM4), 1);
 }
 
 // #411 void(vector org, vector vel, float howmany) te_spark
-static void VM_CL_te_spark (void)
+static void VM_CL_te_spark (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1861,7 +1853,7 @@ static void VM_CL_te_spark (void)
 
 extern cvar_t cl_sound_ric_gunshot;
 // #412 void(vector org) te_gunshotquad (DP_QUADEFFECTS1)
-static void VM_CL_te_gunshotquad (void)
+static void VM_CL_te_gunshotquad (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1885,7 +1877,7 @@ static void VM_CL_te_gunshotquad (void)
 }
 
 // #413 void(vector org) te_spikequad (DP_QUADEFFECTS1)
-static void VM_CL_te_spikequad (void)
+static void VM_CL_te_spikequad (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1906,7 +1898,7 @@ static void VM_CL_te_spikequad (void)
 }
 
 // #414 void(vector org) te_superspikequad (DP_QUADEFFECTS1)
-static void VM_CL_te_superspikequad (void)
+static void VM_CL_te_superspikequad (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1927,7 +1919,7 @@ static void VM_CL_te_superspikequad (void)
 }
 
 // #415 void(vector org) te_explosionquad (DP_QUADEFFECTS1)
-static void VM_CL_te_explosionquad (void)
+static void VM_CL_te_explosionquad (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1940,7 +1932,7 @@ static void VM_CL_te_explosionquad (void)
 }
 
 // #416 void(vector org) te_smallflash (DP_TE_SMALLFLASH)
-static void VM_CL_te_smallflash (void)
+static void VM_CL_te_smallflash (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1952,7 +1944,7 @@ static void VM_CL_te_smallflash (void)
 }
 
 // #417 void(vector org, float radius, float lifetime, vector color) te_customflash (DP_TE_CUSTOMFLASH)
-static void VM_CL_te_customflash (void)
+static void VM_CL_te_customflash (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1966,7 +1958,7 @@ static void VM_CL_te_customflash (void)
 }
 
 // #418 void(vector org) te_gunshot (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_gunshot (void)
+static void VM_CL_te_gunshot (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -1990,7 +1982,7 @@ static void VM_CL_te_gunshot (void)
 }
 
 // #419 void(vector org) te_spike (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_spike (void)
+static void VM_CL_te_spike (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -2011,7 +2003,7 @@ static void VM_CL_te_spike (void)
 }
 
 // #420 void(vector org) te_superspike (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_superspike (void)
+static void VM_CL_te_superspike (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -2032,7 +2024,7 @@ static void VM_CL_te_superspike (void)
 }
 
 // #421 void(vector org) te_explosion (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_explosion (void)
+static void VM_CL_te_explosion (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -2045,7 +2037,7 @@ static void VM_CL_te_explosion (void)
 }
 
 // #422 void(vector org) te_tarexplosion (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_tarexplosion (void)
+static void VM_CL_te_tarexplosion (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -2058,7 +2050,7 @@ static void VM_CL_te_tarexplosion (void)
 }
 
 // #423 void(vector org) te_wizspike (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_wizspike (void)
+static void VM_CL_te_wizspike (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -2071,7 +2063,7 @@ static void VM_CL_te_wizspike (void)
 }
 
 // #424 void(vector org) te_knightspike (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_knightspike (void)
+static void VM_CL_te_knightspike (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -2084,21 +2076,21 @@ static void VM_CL_te_knightspike (void)
 }
 
 // #425 void(vector org) te_lavasplash (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_lavasplash (void)
+static void VM_CL_te_lavasplash (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_CL_te_lavasplash);
        CL_ParticleEffect(EFFECT_TE_LAVASPLASH, 1, PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM0), vec3_origin, vec3_origin, NULL, 0);
 }
 
 // #426 void(vector org) te_teleport (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_teleport (void)
+static void VM_CL_te_teleport (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(1, VM_CL_te_teleport);
        CL_ParticleEffect(EFFECT_TE_TELEPORT, 1, PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM0), vec3_origin, vec3_origin, NULL, 0);
 }
 
 // #427 void(vector org, float colorstart, float colorlength) te_explosion2 (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_explosion2 (void)
+static void VM_CL_te_explosion2 (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2, color;
@@ -2123,35 +2115,35 @@ static void VM_CL_te_explosion2 (void)
 
 
 // #428 void(entity own, vector start, vector end) te_lightning1 (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_lightning1 (void)
+static void VM_CL_te_lightning1 (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(3, VM_CL_te_lightning1);
        CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_bolt, true);
 }
 
 // #429 void(entity own, vector start, vector end) te_lightning2 (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_lightning2 (void)
+static void VM_CL_te_lightning2 (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(3, VM_CL_te_lightning2);
        CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_bolt2, true);
 }
 
 // #430 void(entity own, vector start, vector end) te_lightning3 (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_lightning3 (void)
+static void VM_CL_te_lightning3 (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(3, VM_CL_te_lightning3);
        CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_bolt3, false);
 }
 
 // #431 void(entity own, vector start, vector end) te_beam (DP_TE_STANDARDEFFECTBUILTINS)
-static void VM_CL_te_beam (void)
+static void VM_CL_te_beam (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(3, VM_CL_te_beam);
        CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_beam, false);
 }
 
 // #433 void(vector org) te_plasmaburn (DP_TE_PLASMABURN)
-static void VM_CL_te_plasmaburn (void)
+static void VM_CL_te_plasmaburn (prvm_prog_t *prog)
 {
        float           *pos;
        vec3_t          pos2;
@@ -2163,7 +2155,7 @@ static void VM_CL_te_plasmaburn (void)
 }
 
 // #457 void(vector org, vector velocity, float howmany) te_flamejet (DP_TE_FLAMEJET)
-static void VM_CL_te_flamejet (void)
+static void VM_CL_te_flamejet (prvm_prog_t *prog)
 {
        float *pos;
        vec3_t pos2;
@@ -2177,7 +2169,7 @@ static void VM_CL_te_flamejet (void)
 
 
 // #443 void(entity e, entity tagentity, string tagname) setattachment
-void VM_CL_setattachment (void)
+static void VM_CL_setattachment (prvm_prog_t *prog)
 {
        prvm_edict_t *e;
        prvm_edict_t *tagentity;
@@ -2193,12 +2185,12 @@ void VM_CL_setattachment (void)
 
        if (e == prog->edicts)
        {
-               VM_Warning("setattachment: can not modify world entity\n");
+               VM_Warning(prog, "setattachment: can not modify world entity\n");
                return;
        }
        if (e->priv.server->free)
        {
-               VM_Warning("setattachment: can not modify free entity\n");
+               VM_Warning(prog, "setattachment: can not modify free entity\n");
                return;
        }
 
@@ -2227,7 +2219,7 @@ void VM_CL_setattachment (void)
 /////////////////////////////////////////
 // DP_MD3_TAGINFO extension coded by VorteX
 
-int CL_GetTagIndex (prvm_edict_t *e, const char *tagname)
+static int CL_GetTagIndex (prvm_prog_t *prog, prvm_edict_t *e, const char *tagname)
 {
        dp_model_t *model = CL_GetModelFromEdict(e);
        if (model)
@@ -2236,7 +2228,7 @@ int CL_GetTagIndex (prvm_edict_t *e, const char *tagname)
                return -1;
 }
 
-int CL_GetExtendedTagInfo (prvm_edict_t *e, int tagindex, int *parentindex, const char **tagname, matrix4x4_t *tag_localmatrix)
+static int CL_GetExtendedTagInfo (prvm_prog_t *prog, prvm_edict_t *e, int tagindex, int *parentindex, const char **tagname, matrix4x4_t *tag_localmatrix)
 {
        int r;
        dp_model_t *model;
@@ -2260,7 +2252,7 @@ int CL_GetExtendedTagInfo (prvm_edict_t *e, int tagindex, int *parentindex, cons
        return 1;
 }
 
-int CL_GetPitchSign(prvm_edict_t *ent)
+int CL_GetPitchSign(prvm_prog_t *prog, prvm_edict_t *ent)
 {
        dp_model_t *model;
        if ((model = CL_GetModelFromEdict(ent)) && model->type == mod_alias)
@@ -2268,7 +2260,7 @@ int CL_GetPitchSign(prvm_edict_t *ent)
        return 1;
 }
 
-void CL_GetEntityMatrix (prvm_edict_t *ent, matrix4x4_t *out, qboolean viewmatrix)
+void CL_GetEntityMatrix (prvm_prog_t *prog, prvm_edict_t *ent, matrix4x4_t *out, qboolean viewmatrix)
 {
        float scale;
        float pitchsign = 1;
@@ -2293,21 +2285,21 @@ void CL_GetEntityMatrix (prvm_edict_t *ent, matrix4x4_t *out, qboolean viewmatri
        }
        else
        {
-               pitchsign = CL_GetPitchSign(ent);
+               pitchsign = CL_GetPitchSign(prog, ent);
                Matrix4x4_CreateFromQuakeEntity(out, PRVM_clientedictvector(ent, origin)[0], PRVM_clientedictvector(ent, origin)[1], PRVM_clientedictvector(ent, origin)[2], pitchsign * PRVM_clientedictvector(ent, angles)[0], PRVM_clientedictvector(ent, angles)[1], PRVM_clientedictvector(ent, angles)[2], scale);
        }
 }
 
-int CL_GetEntityLocalTagMatrix(prvm_edict_t *ent, int tagindex, matrix4x4_t *out)
+static int CL_GetEntityLocalTagMatrix(prvm_prog_t *prog, prvm_edict_t *ent, int tagindex, matrix4x4_t *out)
 {
        dp_model_t *model;
        if (tagindex >= 0
         && (model = CL_GetModelFromEdict(ent))
         && model->animscenes)
        {
-               VM_GenerateFrameGroupBlend(ent->priv.server->framegroupblend, ent);
+               VM_GenerateFrameGroupBlend(prog, ent->priv.server->framegroupblend, ent);
                VM_FrameBlendFromFrameGroupBlend(ent->priv.server->frameblend, ent->priv.server->framegroupblend, model);
-               VM_UpdateEdictSkeleton(ent, model, ent->priv.server->frameblend);
+               VM_UpdateEdictSkeleton(prog, ent, model, ent->priv.server->frameblend);
                return Mod_Alias_GetTagMatrix(model, ent->priv.server->frameblend, &ent->priv.server->skeleton, tagindex, out);
        }
        *out = identitymatrix;
@@ -2324,7 +2316,7 @@ int CL_GetEntityLocalTagMatrix(prvm_edict_t *ent, int tagindex, matrix4x4_t *out
 extern cvar_t cl_bob;
 extern cvar_t cl_bobcycle;
 extern cvar_t cl_bobup;
-int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
+int CL_GetTagMatrix (prvm_prog_t *prog, matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
 {
        int ret;
        int attachloop;
@@ -2350,10 +2342,10 @@ int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
                        return 5;
                // apply transformation by child's tagindex on parent entity and then
                // by parent entity itself
-               ret = CL_GetEntityLocalTagMatrix(ent, tagindex - 1, &attachmatrix);
+               ret = CL_GetEntityLocalTagMatrix(prog, ent, tagindex - 1, &attachmatrix);
                if(ret && attachloop == 0)
                        return ret;
-               CL_GetEntityMatrix(ent, &entitymatrix, false);
+               CL_GetEntityMatrix(prog, ent, &entitymatrix, false);
                Matrix4x4_Concat(&tagmatrix, &attachmatrix, out);
                Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
                // next iteration we process the parent entity
@@ -2372,7 +2364,7 @@ int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
        {
                Matrix4x4_Copy(&tagmatrix, out);
 
-               CL_GetEntityMatrix(prog->edicts, &entitymatrix, true);
+               CL_GetEntityMatrix(prog, prog->edicts, &entitymatrix, true);
                Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
 
                /*
@@ -2402,7 +2394,7 @@ int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
 }
 
 // #451 float(entity ent, string tagname) gettagindex (DP_QC_GETTAGINFO)
-void VM_CL_gettagindex (void)
+static void VM_CL_gettagindex (prvm_prog_t *prog)
 {
        prvm_edict_t *ent;
        const char *tag_name;
@@ -2414,12 +2406,12 @@ void VM_CL_gettagindex (void)
        tag_name = PRVM_G_STRING(OFS_PARM1);
        if (ent == prog->edicts)
        {
-               VM_Warning("VM_CL_gettagindex(entity #%i): can't affect world entity\n", PRVM_NUM_FOR_EDICT(ent));
+               VM_Warning(prog, "VM_CL_gettagindex(entity #%i): can't affect world entity\n", PRVM_NUM_FOR_EDICT(ent));
                return;
        }
        if (ent->priv.server->free)
        {
-               VM_Warning("VM_CL_gettagindex(entity #%i): can't affect free entity\n", PRVM_NUM_FOR_EDICT(ent));
+               VM_Warning(prog, "VM_CL_gettagindex(entity #%i): can't affect free entity\n", PRVM_NUM_FOR_EDICT(ent));
                return;
        }
 
@@ -2428,7 +2420,7 @@ void VM_CL_gettagindex (void)
                Con_DPrintf("VM_CL_gettagindex(entity #%i): null or non-precached model\n", PRVM_NUM_FOR_EDICT(ent));
        else
        {
-               tag_index = CL_GetTagIndex(ent, tag_name);
+               tag_index = CL_GetTagIndex(prog, ent, tag_name);
                if (tag_index == 0)
                        Con_DPrintf("VM_CL_gettagindex(entity #%i): tag \"%s\" not found\n", PRVM_NUM_FOR_EDICT(ent), tag_name);
        }
@@ -2436,7 +2428,7 @@ void VM_CL_gettagindex (void)
 }
 
 // #452 vector(entity ent, float tagindex) gettaginfo (DP_QC_GETTAGINFO)
-void VM_CL_gettaginfo (void)
+static void VM_CL_gettaginfo (prvm_prog_t *prog)
 {
        prvm_edict_t *e;
        int tagindex;
@@ -2452,18 +2444,18 @@ void VM_CL_gettaginfo (void)
 
        e = PRVM_G_EDICT(OFS_PARM0);
        tagindex = (int)PRVM_G_FLOAT(OFS_PARM1);
-       returncode = CL_GetTagMatrix(&tag_matrix, e, tagindex);
+       returncode = CL_GetTagMatrix(prog, &tag_matrix, e, tagindex);
        Matrix4x4_ToVectors(&tag_matrix, PRVM_clientglobalvector(v_forward), le, PRVM_clientglobalvector(v_up), PRVM_G_VECTOR(OFS_RETURN));
        VectorScale(le, -1, PRVM_clientglobalvector(v_right));
        model = CL_GetModelFromEdict(e);
-       VM_GenerateFrameGroupBlend(e->priv.server->framegroupblend, e);
+       VM_GenerateFrameGroupBlend(prog, e->priv.server->framegroupblend, e);
        VM_FrameBlendFromFrameGroupBlend(e->priv.server->frameblend, e->priv.server->framegroupblend, model);
-       VM_UpdateEdictSkeleton(e, model, e->priv.server->frameblend);
-       CL_GetExtendedTagInfo(e, tagindex, &parentindex, &tagname, &tag_localmatrix);
+       VM_UpdateEdictSkeleton(prog, e, model, e->priv.server->frameblend);
+       CL_GetExtendedTagInfo(prog, e, tagindex, &parentindex, &tagname, &tag_localmatrix);
        Matrix4x4_ToVectors(&tag_localmatrix, fo, le, up, trans);
 
        PRVM_clientglobalfloat(gettaginfo_parent) = parentindex;
-       PRVM_clientglobalstring(gettaginfo_name) = tagname ? PRVM_SetTempString(tagname) : 0;
+       PRVM_clientglobalstring(gettaginfo_name) = tagname ? PRVM_SetTempString(prog, tagname) : 0;
        VectorCopy(trans, PRVM_clientglobalvector(gettaginfo_offset));
        VectorCopy(fo, PRVM_clientglobalvector(gettaginfo_forward));
        VectorScale(le, -1, PRVM_clientglobalvector(gettaginfo_right));
@@ -2472,10 +2464,10 @@ void VM_CL_gettaginfo (void)
        switch(returncode)
        {
                case 1:
-                       VM_Warning("gettagindex: can't affect world entity\n");
+                       VM_Warning(prog, "gettagindex: can't affect world entity\n");
                        break;
                case 2:
-                       VM_Warning("gettagindex: can't affect free entity\n");
+                       VM_Warning(prog, "gettagindex: can't affect free entity\n");
                        break;
                case 3:
                        Con_DPrintf("CL_GetTagMatrix(entity #%i): null or non-precached model\n", PRVM_NUM_FOR_EDICT(e));
@@ -2572,7 +2564,7 @@ typedef struct vmparticlespawner_s
 vmparticlespawner_t vmpartspawner;
 
 // TODO: automatic max_themes grow
-static void VM_InitParticleSpawner (int maxthemes)
+static void VM_InitParticleSpawner (prvm_prog_t *prog, int maxthemes)
 {
        // bound max themes to not be an insane value
        if (maxthemes < 4)
@@ -2657,7 +2649,7 @@ static void VM_ResetParticleTheme (vmparticletheme_t *theme)
 }
 
 // particle theme -> QC globals
-void VM_CL_ParticleThemeToGlobals(vmparticletheme_t *theme)
+static void VM_CL_ParticleThemeToGlobals(vmparticletheme_t *theme)
 {
        *vmpartspawner.particle_type = theme->typeindex;
        *vmpartspawner.particle_blendmode = theme->blendmode;
@@ -2698,7 +2690,7 @@ void VM_CL_ParticleThemeToGlobals(vmparticletheme_t *theme)
 }
 
 // QC globals ->  particle theme
-void VM_CL_ParticleThemeFromGlobals(vmparticletheme_t *theme)
+static void VM_CL_ParticleThemeFromGlobals(vmparticletheme_t *theme)
 {
        theme->typeindex = (unsigned short)*vmpartspawner.particle_type;
        theme->blendmode = (pblend_t)(int)*vmpartspawner.particle_blendmode;
@@ -2732,48 +2724,48 @@ void VM_CL_ParticleThemeFromGlobals(vmparticletheme_t *theme)
 
 // init particle spawner interface
 // # float(float max_themes) initparticlespawner
-void VM_CL_InitParticleSpawner (void)
+static void VM_CL_InitParticleSpawner (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNTRANGE(0, 1, VM_CL_InitParticleSpawner);
-       VM_InitParticleSpawner((int)PRVM_G_FLOAT(OFS_PARM0));
+       VM_InitParticleSpawner(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
        vmpartspawner.themes[0].initialized = true;
        VM_ResetParticleTheme(&vmpartspawner.themes[0]);
        PRVM_G_FLOAT(OFS_RETURN) = (vmpartspawner.verified == true) ? 1 : 0;
 }
 
 // void() resetparticle
-void VM_CL_ResetParticle (void)
+static void VM_CL_ResetParticle (prvm_prog_t *prog)
 {
        VM_SAFEPARMCOUNT(0, VM_CL_ResetParticle);
        if (vmpartspawner.verified == false)
        {
-               VM_Warning("VM_CL_ResetParticle: particle spawner not initialized\n");
+               VM_Warning(prog, "VM_CL_ResetParticle: particle spawner not initialized\n");
                return;
        }
        VM_CL_ParticleThemeToGlobals(&vmpartspawner.themes[0]);
 }
 
 // void(float themenum) particletheme
-void VM_CL_ParticleTheme (void)
+static void VM_CL_ParticleTheme (prvm_prog_t *prog)
 {
        int themenum;
 
        VM_SAFEPARMCOUNT(1, VM_CL_ParticleTheme);
        if (vmpartspawner.verified == false)
        {
-               VM_Warning("VM_CL_ParticleTheme: particle spawner not initialized\n");
+               VM_Warning(prog, "VM_CL_ParticleTheme: particle spawner not initialized\n");
                return;
        }
        themenum = (int)PRVM_G_FLOAT(OFS_PARM0);
        if (themenum < 0 || themenum >= vmpartspawner.max_themes)
        {
-               VM_Warning("VM_CL_ParticleTheme: bad theme number %i\n", themenum);
+               VM_Warning(prog, "VM_CL_ParticleTheme: bad theme number %i\n", themenum);
                VM_CL_ParticleThemeToGlobals(&vmpartspawner.themes[0]);
                return;
        }
        if (vmpartspawner.themes[themenum].initialized == false)
        {
-               VM_Warning("VM_CL_ParticleTheme: theme #%i not exists\n", themenum);
+               VM_Warning(prog, "VM_CL_ParticleTheme: theme #%i not exists\n", themenum);
                VM_CL_ParticleThemeToGlobals(&vmpartspawner.themes[0]);
                return;
        }
@@ -2783,14 +2775,14 @@ void VM_CL_ParticleTheme (void)
 
 // float() saveparticletheme
 // void(float themenum) updateparticletheme
-void VM_CL_ParticleThemeSave (void)
+static void VM_CL_ParticleThemeSave (prvm_prog_t *prog)
 {
        int themenum;
 
        VM_SAFEPARMCOUNTRANGE(0, 1, VM_CL_ParticleThemeSave);
        if (vmpartspawner.verified == false)
        {
-               VM_Warning("VM_CL_ParticleThemeSave: particle spawner not initialized\n");
+               VM_Warning(prog, "VM_CL_ParticleThemeSave: particle spawner not initialized\n");
                return;
        }
        // allocate new theme, save it and return
@@ -2802,9 +2794,9 @@ void VM_CL_ParticleThemeSave (void)
                if (themenum >= vmpartspawner.max_themes)
                {
                        if (vmpartspawner.max_themes == 2048)
-                               VM_Warning("VM_CL_ParticleThemeSave: no free theme slots\n");
+                               VM_Warning(prog, "VM_CL_ParticleThemeSave: no free theme slots\n");
                        else
-                               VM_Warning("VM_CL_ParticleThemeSave: no free theme slots, try initparticlespawner() with highter max_themes\n");
+                               VM_Warning(prog, "VM_CL_ParticleThemeSave: no free theme slots, try initparticlespawner() with highter max_themes\n");
                        PRVM_G_FLOAT(OFS_RETURN) = -1;
                        return;
                }
@@ -2817,7 +2809,7 @@ void VM_CL_ParticleThemeSave (void)
        themenum = (int)PRVM_G_FLOAT(OFS_PARM0);
        if (themenum < 0 || themenum >= vmpartspawner.max_themes)
        {
-               VM_Warning("VM_CL_ParticleThemeSave: bad theme number %i\n", themenum);
+               VM_Warning(prog, "VM_CL_ParticleThemeSave: bad theme number %i\n", themenum);
                return;
        }
        vmpartspawner.themes[themenum].initialized = true;
@@ -2825,26 +2817,26 @@ void VM_CL_ParticleThemeSave (void)
 }
 
 // void(float themenum) freeparticletheme
-void VM_CL_ParticleThemeFree (void)
+static void VM_CL_ParticleThemeFree (prvm_prog_t *prog)
 {
        int themenum;
 
        VM_SAFEPARMCOUNT(1, VM_CL_ParticleThemeFree);
        if (vmpartspawner.verified == false)
        {
-               VM_Warning("VM_CL_ParticleThemeFree: particle spawner not initialized\n");
+               VM_Warning(prog, "VM_CL_ParticleThemeFree: particle spawner not initialized\n");
                return;
        }
        themenum = (int)PRVM_G_FLOAT(OFS_PARM0);
        // check parms
        if (themenum <= 0 || themenum >= vmpartspawner.max_themes)
        {
-               VM_Warning("VM_CL_ParticleThemeFree: bad theme number %i\n", themenum);
+               VM_Warning(prog, "VM_CL_ParticleThemeFree: bad theme number %i\n", themenum);
                return;
        }
        if (vmpartspawner.themes[themenum].initialized == false)
        {
-               VM_Warning("VM_CL_ParticleThemeFree: theme #%i already freed\n", themenum);
+               VM_Warning(prog, "VM_CL_ParticleThemeFree: theme #%i already freed\n", themenum);
                VM_CL_ParticleThemeToGlobals(&vmpartspawner.themes[0]);
                return;
        }
@@ -2855,7 +2847,7 @@ void VM_CL_ParticleThemeFree (void)
 
 // float(vector org, vector dir, [float theme]) particle
 // returns 0 if failed, 1 if succesful
-void VM_CL_SpawnParticle (void)
+static void VM_CL_SpawnParticle (prvm_prog_t *prog)
 {
        float *org, *dir;
        vmparticletheme_t *theme;
@@ -2865,7 +2857,7 @@ void VM_CL_SpawnParticle (void)
        VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_SpawnParticle2);
        if (vmpartspawner.verified == false)
        {
-               VM_Warning("VM_CL_SpawnParticle: particle spawner not initialized\n");
+               VM_Warning(prog, "VM_CL_SpawnParticle: particle spawner not initialized\n");
                PRVM_G_FLOAT(OFS_RETURN) = 0; 
                return;
        }
@@ -2890,7 +2882,7 @@ void VM_CL_SpawnParticle (void)
                themenum = (int)PRVM_G_FLOAT(OFS_PARM2);
                if (themenum <= 0 || themenum >= vmpartspawner.max_themes)
                {
-                       VM_Warning("VM_CL_SpawnParticle: bad theme number %i\n", themenum);
+                       VM_Warning(prog, "VM_CL_SpawnParticle: bad theme number %i\n", themenum);
                        PRVM_G_FLOAT(OFS_RETURN) = 0; 
                        return;
                }
@@ -2911,7 +2903,7 @@ void VM_CL_SpawnParticle (void)
 
 // float(vector org, vector dir, float spawndelay, float collisiondelay, [float theme]) delayedparticle
 // returns 0 if failed, 1 if success
-void VM_CL_SpawnParticleDelayed (void)
+static void VM_CL_SpawnParticleDelayed (prvm_prog_t *prog)
 {
        float *org, *dir;
        vmparticletheme_t *theme;
@@ -2921,7 +2913,7 @@ void VM_CL_SpawnParticleDelayed (void)
        VM_SAFEPARMCOUNTRANGE(4, 5, VM_CL_SpawnParticle2);
        if (vmpartspawner.verified == false)
        {
-               VM_Warning("VM_CL_SpawnParticle: particle spawner not initialized\n");
+               VM_Warning(prog, "VM_CL_SpawnParticle: particle spawner not initialized\n");
                PRVM_G_FLOAT(OFS_RETURN) = 0; 
                return;
        }
@@ -2934,7 +2926,7 @@ void VM_CL_SpawnParticleDelayed (void)
                themenum = (int)PRVM_G_FLOAT(OFS_PARM4);
                if (themenum <= 0 || themenum >= vmpartspawner.max_themes)
                {
-                       VM_Warning("VM_CL_SpawnParticle: bad theme number %i\n", themenum);
+                       VM_Warning(prog, "VM_CL_SpawnParticle: bad theme number %i\n", themenum);
                        PRVM_G_FLOAT(OFS_RETURN) = 0;  
                        return;
                }
@@ -2959,7 +2951,7 @@ void VM_CL_SpawnParticleDelayed (void)
 // vector(float entitynum, float whatfld) getentityvec;
 // querying engine-drawn entity
 // VorteX: currently it's only tested with whatfld = 1..7
-void VM_CL_GetEntity (void)
+static void VM_CL_GetEntity (prvm_prog_t *prog)
 {
        int entnum, fieldnum;
        float org[3], v1[3], v2[3];
@@ -3037,55 +3029,14 @@ void VM_CL_GetEntity (void)
 //QC POLYGON functions
 //====================
 
-#define VMPOLYGONS_MAXPOINTS 64
-
-typedef struct vmpolygons_triangle_s
-{
-       rtexture_t              *texture;
-       int                             drawflag;
-       qboolean hasalpha;
-       unsigned short  elements[3];
-}vmpolygons_triangle_t;
-
-typedef struct vmpolygons_s
-{
-       mempool_t               *pool;
-       qboolean                initialized;
-       double          progstarttime;
-
-       int                             max_vertices;
-       int                             num_vertices;
-       float                   *data_vertex3f;
-       float                   *data_color4f;
-       float                   *data_texcoord2f;
-
-       int                             max_triangles;
-       int                             num_triangles;
-       vmpolygons_triangle_t *data_triangles;
-       unsigned short  *data_sortedelement3s;
-
-       qboolean                begin_active;
-       int     begin_draw2d;
-       rtexture_t              *begin_texture;
-       int                             begin_drawflag;
-       int                             begin_vertices;
-       float                   begin_vertex[VMPOLYGONS_MAXPOINTS][3];
-       float                   begin_color[VMPOLYGONS_MAXPOINTS][4];
-       float                   begin_texcoord[VMPOLYGONS_MAXPOINTS][2];
-       qboolean                begin_texture_hasalpha;
-} vmpolygons_t;
-
-// FIXME: make VM_CL_R_Polygon functions use Debug_Polygon functions?
-vmpolygons_t vmpolygons[PRVM_MAXPROGS];
-
 //#304 void() renderscene (EXT_CSQC)
 // moved that here to reset the polygons,
 // resetting them earlier causes R_Mesh_Draw to be called with numvertices = 0
 // --blub
-void VM_CL_R_RenderScene (void)
+static void VM_CL_R_RenderScene (prvm_prog_t *prog)
 {
-       double t = Sys_DoubleTime();
-       vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
+       double t = Sys_DirtyTime();
+       vmpolygons_t *polys = &prog->vmpolygons;
        VM_SAFEPARMCOUNT(0, VM_CL_R_RenderScene);
 
        // we need to update any RENDER_VIEWMODEL entities at this point because
@@ -3096,10 +3047,10 @@ void VM_CL_R_RenderScene (void)
        R_RenderView();
 
        polys->num_vertices = polys->num_triangles = 0;
-       polys->progstarttime = prog->starttime;
 
        // callprofile fixing hack: do not include this time in what is counted for CSQC_UpdateView
-       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= Sys_DoubleTime() - t;
+       t = Sys_DirtyTime() - t;if (t < 0 || t >= 1800) t = 0;
+       prog->functions[PRVM_clientfunction(CSQC_UpdateView)].totaltime -= t;
 }
 
 static void VM_ResizePolygons(vmpolygons_t *polys)
@@ -3150,9 +3101,7 @@ static void VM_InitPolygons (vmpolygons_t* polys)
 static void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
 {
        int surfacelistindex;
-       vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
-       if(polys->progstarttime != prog->starttime) // from other progs? won't draw these (this can cause crashes!)
-               return;
+       vmpolygons_t *polys = (vmpolygons_t *)ent;
 //     R_Mesh_ResetTextureState();
        R_EntityMatrix(&identitymatrix);
        GL_CullFace(GL_NONE);
@@ -3179,7 +3128,7 @@ static void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t
        }
 }
 
-void VMPolygons_Store(vmpolygons_t *polys)
+static void VMPolygons_Store(vmpolygons_t *polys)
 {
        qboolean hasalpha;
        int i;
@@ -3241,10 +3190,10 @@ void VMPolygons_Store(vmpolygons_t *polys)
 
 // TODO: move this into the client code and clean-up everything else, too! [1/6/2008 Black]
 // LordHavoc: agreed, this is a mess
-void VM_CL_AddPolygonsToMeshQueue (void)
+void VM_CL_AddPolygonsToMeshQueue (prvm_prog_t *prog)
 {
        int i;
-       vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
+       vmpolygons_t *polys = &prog->vmpolygons;
        vec3_t center;
 
        // only add polygons of the currently active prog to the queue - if there is none, we're done
@@ -3257,7 +3206,7 @@ void VM_CL_AddPolygonsToMeshQueue (void)
        for (i = 0;i < polys->num_triangles;i++)
        {
                VectorMAMAM(1.0f / 3.0f, polys->data_vertex3f + 3*polys->data_triangles[i].elements[0], 1.0f / 3.0f, polys->data_vertex3f + 3*polys->data_triangles[i].elements[1], 1.0f / 3.0f, polys->data_vertex3f + 3*polys->data_triangles[i].elements[2], center);
-               R_MeshQueue_AddTransparent(center, VM_DrawPolygonCallback, NULL, i, NULL);
+               R_MeshQueue_AddTransparent(center, VM_DrawPolygonCallback, (entity_render_t *)polys, i, NULL);
        }
 
        /*polys->num_triangles = 0; // now done after rendering the scene,
@@ -3265,11 +3214,11 @@ void VM_CL_AddPolygonsToMeshQueue (void)
 }
 
 //void(string texturename, float flag[, float is2d]) R_BeginPolygon
-void VM_CL_R_PolygonBegin (void)
+static void VM_CL_R_PolygonBegin (prvm_prog_t *prog)
 {
        const char              *picname;
        skinframe_t     *sf;
-       vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
+       vmpolygons_t *polys = &prog->vmpolygons;
        int tf;
 
        // TODO instead of using skinframes here (which provides the benefit of
@@ -3280,15 +3229,9 @@ void VM_CL_R_PolygonBegin (void)
 
        if (!polys->initialized)
                VM_InitPolygons(polys);
-       if(polys->progstarttime != prog->starttime)
-       {
-               // from another progs? then reset the polys first (fixes crashes on map change, because that can make skinframe textures invalid)
-               polys->num_vertices = polys->num_triangles = 0;
-               polys->progstarttime = prog->starttime;
-       }
        if (polys->begin_active)
        {
-               VM_Warning("VM_CL_R_PolygonBegin: called twice without VM_CL_R_PolygonBegin after first\n");
+               VM_Warning(prog, "VM_CL_R_PolygonBegin: called twice without VM_CL_R_PolygonBegin after first\n");
                return;
        }
        picname = PRVM_G_STRING(OFS_PARM0);
@@ -3322,21 +3265,21 @@ void VM_CL_R_PolygonBegin (void)
 }
 
 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
-void VM_CL_R_PolygonVertex (void)
+static void VM_CL_R_PolygonVertex (prvm_prog_t *prog)
 {
-       vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
+       vmpolygons_t *polys = &prog->vmpolygons;
 
        VM_SAFEPARMCOUNT(4, VM_CL_R_PolygonVertex);
 
        if (!polys->begin_active)
        {
-               VM_Warning("VM_CL_R_PolygonVertex: VM_CL_R_PolygonBegin wasn't called\n");
+               VM_Warning(prog, "VM_CL_R_PolygonVertex: VM_CL_R_PolygonBegin wasn't called\n");
                return;
        }
 
        if (polys->begin_vertices >= VMPOLYGONS_MAXPOINTS)
        {
-               VM_Warning("VM_CL_R_PolygonVertex: may have %i vertices max\n", VMPOLYGONS_MAXPOINTS);
+               VM_Warning(prog, "VM_CL_R_PolygonVertex: may have %i vertices max\n", VMPOLYGONS_MAXPOINTS);
                return;
        }
 
@@ -3353,21 +3296,21 @@ void VM_CL_R_PolygonVertex (void)
 }
 
 //void() R_EndPolygon
-void VM_CL_R_PolygonEnd (void)
+static void VM_CL_R_PolygonEnd (prvm_prog_t *prog)
 {
-       vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
+       vmpolygons_t *polys = &prog->vmpolygons;
 
        VM_SAFEPARMCOUNT(0, VM_CL_R_PolygonEnd);
        if (!polys->begin_active)
        {
-               VM_Warning("VM_CL_R_PolygonEnd: VM_CL_R_PolygonBegin wasn't called\n");
+               VM_Warning(prog, "VM_CL_R_PolygonEnd: VM_CL_R_PolygonBegin wasn't called\n");
                return;
        }
        polys->begin_active = false;
        if (polys->begin_vertices >= 3)
                VMPolygons_Store(polys);
        else
-               VM_Warning("VM_CL_R_PolygonEnd: %i vertices isn't a good choice\n", polys->begin_vertices);
+               VM_Warning(prog, "VM_CL_R_PolygonEnd: %i vertices isn't a good choice\n", polys->begin_vertices);
 }
 
 static vmpolygons_t debugPolys;
@@ -3436,8 +3379,9 @@ is not a staircase.
 
 =============
 */
-qboolean CL_CheckBottom (prvm_edict_t *ent)
+static qboolean CL_CheckBottom (prvm_edict_t *ent)
 {
+       prvm_prog_t *prog = CLVM_prog;
        vec3_t  mins, maxs, start, stop;
        trace_t trace;
        int             x, y;
@@ -3504,8 +3448,9 @@ The move will be adjusted for slopes and stairs, but if the move isn't
 possible, no move is done and false is returned
 =============
 */
-qboolean CL_movestep (prvm_edict_t *ent, vec3_t move, qboolean relink, qboolean noenemy, qboolean settrace)
+static qboolean CL_movestep (prvm_edict_t *ent, vec3_t move, qboolean relink, qboolean noenemy, qboolean settrace)
 {
+       prvm_prog_t *prog = CLVM_prog;
        float           dz;
        vec3_t          oldorg, neworg, end, traceendpos;
        trace_t         trace;
@@ -3534,7 +3479,7 @@ qboolean CL_movestep (prvm_edict_t *ent, vec3_t move, qboolean relink, qboolean
                        }
                        trace = CL_TraceBox(PRVM_clientedictvector(ent, origin), PRVM_clientedictvector(ent, mins), PRVM_clientedictvector(ent, maxs), neworg, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), true, true, &svent, true);
                        if (settrace)
-                               CL_VM_SetTraceGlobals(&trace, svent);
+                               CL_VM_SetTraceGlobals(prog, &trace, svent);
 
                        if (trace.fraction == 1)
                        {
@@ -3562,14 +3507,14 @@ qboolean CL_movestep (prvm_edict_t *ent, vec3_t move, qboolean relink, qboolean
 
        trace = CL_TraceBox(neworg, PRVM_clientedictvector(ent, mins), PRVM_clientedictvector(ent, maxs), end, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), true, true, &svent, true);
        if (settrace)
-               CL_VM_SetTraceGlobals(&trace, svent);
+               CL_VM_SetTraceGlobals(prog, &trace, svent);
 
        if (trace.startsolid)
        {
                neworg[2] -= sv_stepheight.value;
                trace = CL_TraceBox(neworg, PRVM_clientedictvector(ent, mins), PRVM_clientedictvector(ent, maxs), end, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), true, true, &svent, true);
                if (settrace)
-                       CL_VM_SetTraceGlobals(&trace, svent);
+                       CL_VM_SetTraceGlobals(prog, &trace, svent);
                if (trace.startsolid)
                        return false;
        }
@@ -3622,7 +3567,7 @@ VM_CL_walkmove
 float(float yaw, float dist[, settrace]) walkmove
 ===============
 */
-static void VM_CL_walkmove (void)
+static void VM_CL_walkmove (prvm_prog_t *prog)
 {
        prvm_edict_t    *ent;
        float   yaw, dist;
@@ -3639,12 +3584,12 @@ static void VM_CL_walkmove (void)
        ent = PRVM_PROG_TO_EDICT(PRVM_clientglobaledict(self));
        if (ent == prog->edicts)
        {
-               VM_Warning("walkmove: can not modify world entity\n");
+               VM_Warning(prog, "walkmove: can not modify world entity\n");
                return;
        }
        if (ent->priv.server->free)
        {
-               VM_Warning("walkmove: can not modify free entity\n");
+               VM_Warning(prog, "walkmove: can not modify free entity\n");
                return;
        }
        yaw = PRVM_G_FLOAT(OFS_PARM0);
@@ -3679,12 +3624,12 @@ VM_CL_serverkey
 string(string key) serverkey
 ===============
 */
-void VM_CL_serverkey(void)
+static void VM_CL_serverkey(prvm_prog_t *prog)
 {
        char string[VM_STRINGTEMP_LENGTH];
        VM_SAFEPARMCOUNT(1, VM_CL_serverkey);
        InfoString_GetValue(cl.qw_serverinfo, PRVM_G_STRING(OFS_PARM0), string, sizeof(string));
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
+       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
 }
 
 /*
@@ -3697,7 +3642,7 @@ Should be fast but can be inexact.
 float checkpvs(vector viewpos, entity viewee) = #240;
 =================
 */
-static void VM_CL_checkpvs (void)
+static void VM_CL_checkpvs (prvm_prog_t *prog)
 {
        vec3_t viewpos;
        prvm_edict_t *viewee;
@@ -3715,7 +3660,7 @@ static void VM_CL_checkpvs (void)
 
        if(viewee->priv.required->free)
        {
-               VM_Warning("checkpvs: can not check free entity\n");
+               VM_Warning(prog, "checkpvs: can not check free entity\n");
                PRVM_G_FLOAT(OFS_RETURN) = 4;
                return;
        }
@@ -3758,7 +3703,7 @@ static void VM_CL_checkpvs (void)
 }
 
 // #263 float(float modlindex) skel_create = #263; // (FTE_CSQC_SKELETONOBJECTS) create a skeleton (be sure to assign this value into .skeletonindex for use), returns skeleton index (1 or higher) on success, returns 0 on failure  (for example if the modelindex is not skeletal), it is recommended that you create a new skeleton if you change modelindex.
-static void VM_CL_skel_create(void)
+static void VM_CL_skel_create(prvm_prog_t *prog)
 {
        int modelindex = (int)PRVM_G_FLOAT(OFS_PARM0);
        dp_model_t *model = CL_GetModelByIndex(modelindex);
@@ -3782,7 +3727,7 @@ static void VM_CL_skel_create(void)
 }
 
 // #264 float(float skel, entity ent, float modlindex, float retainfrac, float firstbone, float lastbone) skel_build = #264; // (FTE_CSQC_SKELETONOBJECTS) blend in a percentage of standard animation, 0 replaces entirely, 1 does nothing, 0.5 blends half, etc, and this only alters the bones in the specified range for which out of bounds values like 0,100000 are safe (uses .frame, .frame2, .frame3, .frame4, .lerpfrac, .lerpfrac3, .lerpfrac4, .frame1time, .frame2time, .frame3time, .frame4time), returns skel on success, 0 on failure
-static void VM_CL_skel_build(void)
+static void VM_CL_skel_build(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        skeleton_t *skeleton;
@@ -3806,7 +3751,7 @@ static void VM_CL_skel_build(void)
        firstbone = max(0, firstbone);
        lastbone = min(lastbone, model->num_bones - 1);
        lastbone = min(lastbone, skeleton->model->num_bones - 1);
-       VM_GenerateFrameGroupBlend(framegroupblend, ed);
+       VM_GenerateFrameGroupBlend(prog, framegroupblend, ed);
        VM_FrameBlendFromFrameGroupBlend(frameblend, framegroupblend, model);
        blendfrac = 1.0f - retainfrac;
        for (numblends = 0;numblends < MAX_FRAMEBLENDS && frameblend[numblends].lerp;numblends++)
@@ -3826,7 +3771,7 @@ static void VM_CL_skel_build(void)
 }
 
 // #265 float(float skel) skel_get_numbones = #265; // (FTE_CSQC_SKELETONOBJECTS) returns how many bones exist in the created skeleton
-static void VM_CL_skel_get_numbones(void)
+static void VM_CL_skel_get_numbones(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        skeleton_t *skeleton;
@@ -3837,7 +3782,7 @@ static void VM_CL_skel_get_numbones(void)
 }
 
 // #266 string(float skel, float bonenum) skel_get_bonename = #266; // (FTE_CSQC_SKELETONOBJECTS) returns name of bone (as a tempstring)
-static void VM_CL_skel_get_bonename(void)
+static void VM_CL_skel_get_bonename(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int bonenum = (int)PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -3847,11 +3792,11 @@ static void VM_CL_skel_get_bonename(void)
                return;
        if (bonenum < 0 || bonenum >= skeleton->model->num_bones)
                return;
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(skeleton->model->data_bones[bonenum].name);
+       PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, skeleton->model->data_bones[bonenum].name);
 }
 
 // #267 float(float skel, float bonenum) skel_get_boneparent = #267; // (FTE_CSQC_SKELETONOBJECTS) returns parent num for supplied bonenum, 0 if bonenum has no parent or bone does not exist (returned value is always less than bonenum, you can loop on this)
-static void VM_CL_skel_get_boneparent(void)
+static void VM_CL_skel_get_boneparent(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int bonenum = (int)PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -3865,7 +3810,7 @@ static void VM_CL_skel_get_boneparent(void)
 }
 
 // #268 float(float skel, string tagname) skel_find_bone = #268; // (FTE_CSQC_SKELETONOBJECTS) get number of bone with specified name, 0 on failure, tagindex (bonenum+1) on success, same as using gettagindex on the modelindex
-static void VM_CL_skel_find_bone(void)
+static void VM_CL_skel_find_bone(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        const char *tagname = PRVM_G_STRING(OFS_PARM1);
@@ -3877,7 +3822,7 @@ static void VM_CL_skel_find_bone(void)
 }
 
 // #269 vector(float skel, float bonenum) skel_get_bonerel = #269; // (FTE_CSQC_SKELETONOBJECTS) get matrix of bone in skeleton relative to its parent - sets v_forward, v_right, v_up, returns origin (relative to parent bone)
-static void VM_CL_skel_get_bonerel(void)
+static void VM_CL_skel_get_bonerel(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int bonenum = (int)PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -3901,7 +3846,7 @@ static void VM_CL_skel_get_bonerel(void)
 }
 
 // #270 vector(float skel, float bonenum) skel_get_boneabs = #270; // (FTE_CSQC_SKELETONOBJECTS) get matrix of bone in skeleton in model space - sets v_forward, v_right, v_up, returns origin (relative to entity)
-static void VM_CL_skel_get_boneabs(void)
+static void VM_CL_skel_get_boneabs(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int bonenum = (int)PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -3932,7 +3877,7 @@ static void VM_CL_skel_get_boneabs(void)
 }
 
 // #271 void(float skel, float bonenum, vector org) skel_set_bone = #271; // (FTE_CSQC_SKELETONOBJECTS) set matrix of bone relative to its parent, reads v_forward, v_right, v_up, takes origin as parameter (relative to parent bone)
-static void VM_CL_skel_set_bone(void)
+static void VM_CL_skel_set_bone(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int bonenum = (int)PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -3952,7 +3897,7 @@ static void VM_CL_skel_set_bone(void)
 }
 
 // #272 void(float skel, float bonenum, vector org) skel_mul_bone = #272; // (FTE_CSQC_SKELETONOBJECTS) transform bone matrix (relative to its parent) by the supplied matrix in v_forward, v_right, v_up, takes origin as parameter (relative to parent bone)
-static void VM_CL_skel_mul_bone(void)
+static void VM_CL_skel_mul_bone(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int bonenum = (int)PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -3974,7 +3919,7 @@ static void VM_CL_skel_mul_bone(void)
 }
 
 // #273 void(float skel, float startbone, float endbone, vector org) skel_mul_bones = #273; // (FTE_CSQC_SKELETONOBJECTS) transform bone matrices (relative to their parents) by the supplied matrix in v_forward, v_right, v_up, takes origin as parameter (relative to parent bones)
-static void VM_CL_skel_mul_bones(void)
+static void VM_CL_skel_mul_bones(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int firstbone = PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -4001,7 +3946,7 @@ static void VM_CL_skel_mul_bones(void)
 }
 
 // #274 void(float skeldst, float skelsrc, float startbone, float endbone) skel_copybones = #274; // (FTE_CSQC_SKELETONOBJECTS) copy bone matrices (relative to their parents) from one skeleton to another, useful for copying a skeleton to a corpse
-static void VM_CL_skel_copybones(void)
+static void VM_CL_skel_copybones(prvm_prog_t *prog)
 {
        int skeletonindexdst = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        int skeletonindexsrc = (int)PRVM_G_FLOAT(OFS_PARM1) - 1;
@@ -4022,7 +3967,7 @@ static void VM_CL_skel_copybones(void)
 }
 
 // #275 void(float skel) skel_delete = #275; // (FTE_CSQC_SKELETONOBJECTS) deletes skeleton at the beginning of the next frame (you can add the entity, delete the skeleton, renderscene, and it will still work)
-static void VM_CL_skel_delete(void)
+static void VM_CL_skel_delete(prvm_prog_t *prog)
 {
        int skeletonindex = (int)PRVM_G_FLOAT(OFS_PARM0) - 1;
        skeleton_t *skeleton;
@@ -4033,7 +3978,7 @@ static void VM_CL_skel_delete(void)
 }
 
 // #276 float(float modlindex, string framename) frameforname = #276; // (FTE_CSQC_SKELETONOBJECTS) finds number of a specified frame in the animation, returns -1 if no match found
-static void VM_CL_frameforname(void)
+static void VM_CL_frameforname(prvm_prog_t *prog)
 {
        int modelindex = (int)PRVM_G_FLOAT(OFS_PARM0);
        dp_model_t *model = CL_GetModelByIndex(modelindex);
@@ -4053,7 +3998,7 @@ static void VM_CL_frameforname(void)
 }
 
 // #277 float(float modlindex, float framenum) frameduration = #277; // (FTE_CSQC_SKELETONOBJECTS) returns the intended play time (in seconds) of the specified framegroup, if it does not exist the result is 0, if it is a single frame it may be a small value around 0.1 or 0.
-static void VM_CL_frameduration(void)
+static void VM_CL_frameduration(prvm_prog_t *prog)
 {
        int modelindex = (int)PRVM_G_FLOAT(OFS_PARM0);
        dp_model_t *model = CL_GetModelByIndex(modelindex);
@@ -4065,7 +4010,7 @@ static void VM_CL_frameduration(void)
                PRVM_G_FLOAT(OFS_RETURN) = model->animscenes[framenum].framecount / model->animscenes[framenum].framerate;
 }
 
-void VM_CL_RotateMoves(void)
+static void VM_CL_RotateMoves(prvm_prog_t *prog)
 {
        /*
         * Obscure builtin used by GAME_XONOTIC.
@@ -4104,7 +4049,7 @@ void VM_CL_RotateMoves(void)
 }
 
 // #358 void(string cubemapname) loadcubemap
-static void VM_CL_loadcubemap(void)
+static void VM_CL_loadcubemap(prvm_prog_t *prog)
 {
        const char *name;
 
@@ -4772,9 +4717,9 @@ NULL,                                                     // #640
 
 const int vm_cl_numbuiltins = sizeof(vm_cl_builtins) / sizeof(prvm_builtin_t);
 
-void VM_Polygons_Reset(void)
+void VM_Polygons_Reset(prvm_prog_t *prog)
 {
-       vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
+       vmpolygons_t *polys = &prog->vmpolygons;
 
        // TODO: replace vm_polygons stuff with a more general debugging polygon system, and make vm_polygons functions use that system
        if(polys->initialized)
@@ -4784,17 +4729,15 @@ void VM_Polygons_Reset(void)
        }
 }
 
-void VM_CL_Cmd_Init(void)
+void CLVM_init_cmd(prvm_prog_t *prog)
 {
-       VM_Cmd_Init();
-       VM_Polygons_Reset();
+       VM_Cmd_Init(prog);
+       VM_Polygons_Reset(prog);
 }
 
-void VM_CL_Cmd_Reset(void)
+void CLVM_reset_cmd(prvm_prog_t *prog)
 {
        World_End(&cl.world);
-       VM_Cmd_Reset();
-       VM_Polygons_Reset();
+       VM_Cmd_Reset(prog);
+       VM_Polygons_Reset(prog);
 }
-
-