]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - clvm_cmds.c
fixed RF_VIEWMODEL really good this time
[xonotic/darkplaces.git] / clvm_cmds.c
index 3e2e325f1561d483ef5f3bf3b91ec7b41624bf69..98c342695cdfb869fae3b2712de715e3edf1782d 100644 (file)
@@ -265,7 +265,7 @@ static void VM_CL_traceline (void)
        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));
 
-       trace = CL_TraceLine(v1, v2, move, ent, CL_GenericHitSuperContentsMask(ent), CL_HitNetworkBrushModels(move), CL_HitNetworkPlayers(move), &svent, true);
+       trace = CL_TraceLine(v1, v2, move, ent, CL_GenericHitSuperContentsMask(ent), CL_HitNetworkBrushModels(move), CL_HitNetworkPlayers(move), &svent, true, false);
 
        CL_VM_SetTraceGlobals(&trace, svent);
 //     R_TimeReport("traceline");
@@ -608,7 +608,7 @@ realcheck:
        start[0] = stop[0] = (mins[0] + maxs[0])*0.5;
        start[1] = stop[1] = (mins[1] + maxs[1])*0.5;
        stop[2] = start[2] - 2*sv_stepheight.value;
-       trace = CL_TraceLine(start, stop, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), true, true, NULL, true);
+       trace = CL_TraceLine(start, stop, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), true, true, NULL, true, false);
 
        if (trace.fraction == 1.0)
                return;
@@ -622,7 +622,7 @@ realcheck:
                        start[0] = stop[0] = x ? maxs[0] : mins[0];
                        start[1] = stop[1] = y ? maxs[1] : mins[1];
 
-                       trace = CL_TraceLine(start, stop, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), true, true, NULL, true);
+                       trace = CL_TraceLine(start, stop, MOVE_NORMAL, ent, CL_GenericHitSuperContentsMask(ent), true, true, NULL, true, false);
 
                        if (trace.fraction != 1.0 && trace.endpos[2] > bottom)
                                bottom = trace.endpos[2];
@@ -667,24 +667,25 @@ static void VM_CL_ambientsound (void)
        S_StaticSound (s, f, PRVM_G_FLOAT(OFS_PARM2), PRVM_G_FLOAT(OFS_PARM3)*64);
 }
 
-// #92 vector(vector org) getlight (DP_QC_GETLIGHT)
+// #92 vector(vector org[, float lpflag]) getlight (DP_QC_GETLIGHT)
 static void VM_CL_getlight (void)
 {
        vec3_t ambientcolor, diffusecolor, diffusenormal;
        vec_t *p;
 
-       VM_SAFEPARMCOUNT(1, VM_CL_getlight);
+       VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getlight);
 
        p = PRVM_G_VECTOR(OFS_PARM0);
        VectorClear(ambientcolor);
        VectorClear(diffusecolor);
        VectorClear(diffusenormal);
-       if (cl.worldmodel && cl.worldmodel->brush.LightPoint)
+       if (prog->argc >= 2)
+               R_CompleteLightPoint(ambientcolor, diffusecolor, diffusenormal, p, PRVM_G_FLOAT(OFS_PARM1));
+       else if (cl.worldmodel && cl.worldmodel->brush.LightPoint)
                cl.worldmodel->brush.LightPoint(cl.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
        VectorMA(ambientcolor, 0.5, diffusecolor, PRVM_G_VECTOR(OFS_RETURN));
 }
 
-
 //============================================================================
 //[515]: SCENE MANAGER builtins
 extern qboolean CSQC_AddRenderEdict (prvm_edict_t *ed, int edictnum);//csprogs.c
@@ -692,8 +693,8 @@ extern qboolean CSQC_AddRenderEdict (prvm_edict_t *ed, int edictnum);//csprogs.c
 static void CSQC_R_RecalcView (void)
 {
        extern matrix4x4_t viewmodelmatrix;
-       Matrix4x4_CreateFromQuakeEntity(&r_refdef.view.matrix, cl.csqc_origin[0], cl.csqc_origin[1], cl.csqc_origin[2], cl.csqc_angles[0], cl.csqc_angles[1], cl.csqc_angles[2], 1);
-       Matrix4x4_CreateFromQuakeEntity(&viewmodelmatrix, cl.csqc_origin[0], cl.csqc_origin[1], cl.csqc_origin[2], cl.csqc_angles[0], cl.csqc_angles[1], cl.csqc_angles[2], cl_viewmodel_scale.value);
+       Matrix4x4_CreateFromQuakeEntity(&r_refdef.view.matrix, cl.csqc_vieworigin[0], cl.csqc_vieworigin[1], cl.csqc_vieworigin[2], cl.csqc_viewangles[0], cl.csqc_viewangles[1], cl.csqc_viewangles[2], 1);
+       Matrix4x4_CreateFromQuakeEntity(&viewmodelmatrix, cl.csqc_vieworigin[0], cl.csqc_vieworigin[1], cl.csqc_vieworigin[2], cl.csqc_viewangles[0], cl.csqc_viewangles[1], cl.csqc_viewangles[2], cl_viewmodel_scale.value);
 }
 
 void CL_RelinkLightFlashes(void);
@@ -721,8 +722,8 @@ void VM_CL_R_ClearScene (void)
        r_refdef.view.ortho_y = scr_fov.value * (3.0 / 4.0);
        r_refdef.view.clear = true;
        r_refdef.view.isoverlay = false;
-       // FIXME: restore cl.csqc_origin
-       // FIXME: restore cl.csqc_angles
+       VectorCopy(cl.csqc_vieworiginfromengine, cl.csqc_vieworigin);
+       VectorCopy(cl.csqc_viewanglesfromengine, cl.csqc_viewangles);
        cl.csqc_vidvars.drawworld = r_drawworld.integer != 0;
        cl.csqc_vidvars.drawenginesbar = false;
        cl.csqc_vidvars.drawcrosshair = false;
@@ -744,6 +745,8 @@ void VM_CL_R_AddEntities (void)
        prog->globals.client->time = cl.time;
        for(i=1;i<prog->num_edicts;i++)
        {
+               // so we can easily check if CSQC entity #edictnum is currently drawn
+               cl.csqcrenderentities[i].entitynumber = 0;
                ed = &prog->edicts[i];
                if(ed->priv.required->free)
                        continue;
@@ -822,28 +825,28 @@ void VM_CL_R_SetView (void)
                        PRVM_G_FLOAT(OFS_RETURN) = r_refdef.view.ortho_y;
                        break;
                case VF_ORIGIN:
-                       VectorCopy(cl.csqc_origin, PRVM_G_VECTOR(OFS_RETURN));
+                       VectorCopy(cl.csqc_vieworigin, PRVM_G_VECTOR(OFS_RETURN));
                        break;
                case VF_ORIGIN_X:
-                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_origin[0];
+                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vieworigin[0];
                        break;
                case VF_ORIGIN_Y:
-                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_origin[1];
+                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vieworigin[1];
                        break;
                case VF_ORIGIN_Z:
-                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_origin[2];
+                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vieworigin[2];
                        break;
                case VF_ANGLES:
-                       VectorCopy(cl.csqc_angles, PRVM_G_VECTOR(OFS_RETURN));
+                       VectorCopy(cl.csqc_viewangles, PRVM_G_VECTOR(OFS_RETURN));
                        break;
                case VF_ANGLES_X:
-                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_angles[0];
+                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_viewangles[0];
                        break;
                case VF_ANGLES_Y:
-                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_angles[1];
+                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_viewangles[1];
                        break;
                case VF_ANGLES_Z:
-                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_angles[2];
+                       PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_viewangles[2];
                        break;
                case VF_DRAWWORLD:
                        PRVM_G_FLOAT(OFS_RETURN) = cl.csqc_vidvars.drawworld;
@@ -929,35 +932,35 @@ void VM_CL_R_SetView (void)
                r_refdef.view.frustum_y = tan(k * M_PI / 360.0);r_refdef.view.ortho_y = k;
                break;
        case VF_ORIGIN:
-               VectorCopy(f, cl.csqc_origin);
+               VectorCopy(f, cl.csqc_vieworigin);
                CSQC_R_RecalcView();
                break;
        case VF_ORIGIN_X:
-               cl.csqc_origin[0] = k;
+               cl.csqc_vieworigin[0] = k;
                CSQC_R_RecalcView();
                break;
        case VF_ORIGIN_Y:
-               cl.csqc_origin[1] = k;
+               cl.csqc_vieworigin[1] = k;
                CSQC_R_RecalcView();
                break;
        case VF_ORIGIN_Z:
-               cl.csqc_origin[2] = k;
+               cl.csqc_vieworigin[2] = k;
                CSQC_R_RecalcView();
                break;
        case VF_ANGLES:
-               VectorCopy(f, cl.csqc_angles);
+               VectorCopy(f, cl.csqc_viewangles);
                CSQC_R_RecalcView();
                break;
        case VF_ANGLES_X:
-               cl.csqc_angles[0] = k;
+               cl.csqc_viewangles[0] = k;
                CSQC_R_RecalcView();
                break;
        case VF_ANGLES_Y:
-               cl.csqc_angles[1] = k;
+               cl.csqc_viewangles[1] = k;
                CSQC_R_RecalcView();
                break;
        case VF_ANGLES_Z:
-               cl.csqc_angles[2] = k;
+               cl.csqc_viewangles[2] = k;
                CSQC_R_RecalcView();
                break;
        case VF_DRAWWORLD:
@@ -1258,7 +1261,7 @@ static void VM_CL_pointparticles (void)
 static void VM_CL_boxparticles (void)
 {
        int effectnum;
-       prvm_edict_t *own;
+       // prvm_edict_t *own;
        float *origin_from, *origin_to, *dir_from, *dir_to;
        float count;
        int flags;
@@ -1267,7 +1270,7 @@ static void VM_CL_boxparticles (void)
        VM_SAFEPARMCOUNTRANGE(7, 8, VM_CL_boxparticles);
 
        effectnum = (int)PRVM_G_FLOAT(OFS_PARM0);
-       own = PRVM_G_EDICT(OFS_PARM1); // TODO find use for this
+       // own = PRVM_G_EDICT(OFS_PARM1); // TODO find use for this
        origin_from = PRVM_G_VECTOR(OFS_PARM2);
        origin_to = PRVM_G_VECTOR(OFS_PARM3);
        dir_from = PRVM_G_VECTOR(OFS_PARM4);
@@ -1542,7 +1545,7 @@ 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)
-                               MSG_ReadByte();
+                               (void) MSG_ReadByte();
                }
                else
                {
@@ -2197,9 +2200,20 @@ void CL_GetEntityMatrix (prvm_edict_t *ent, matrix4x4_t *out, qboolean viewmatri
        if (val && val->_float != 0)
                scale = val->_float;
 
-       // TODO do we need the same weird angle inverting logic here as in the server side case?
        if(viewmatrix)
-               Matrix4x4_CreateFromQuakeEntity(out, cl.csqc_origin[0], cl.csqc_origin[1], cl.csqc_origin[2], cl.csqc_angles[0], cl.csqc_angles[1], cl.csqc_angles[2], scale * cl_viewmodel_scale.value);
+               *out = r_refdef.view.matrix;
+       else if ((val = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.renderflags)) && ((int)val->_float & RF_USEAXIS))
+       {
+               vec3_t forward;
+               vec3_t left;
+               vec3_t up;
+               vec3_t origin;
+               VectorScale(prog->globals.client->v_forward, scale, forward);
+               VectorScale(prog->globals.client->v_right, -scale, left);
+               VectorScale(prog->globals.client->v_up, scale, up);
+               VectorCopy(ent->fields.client->origin, origin);
+               Matrix4x4_FromVectors(out, forward, left, up, origin);
+       }
        else
        {
                pitchsign = CL_GetPitchSign(ent);
@@ -2986,6 +3000,7 @@ typedef struct vmpolygons_s
        unsigned short  *data_sortedelement3s;
 
        qboolean                begin_active;
+       int     begin_draw2d;
        rtexture_t              *begin_texture;
        int                             begin_drawflag;
        int                             begin_vertices;
@@ -3072,9 +3087,11 @@ static void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t
        vmpolygons_t* polys = vmpolygons + PRVM_GetProgNr();
        if(polys->progstarttime != prog->starttime) // from other progs? won't draw these (this can cause crashes!)
                return;
-       R_Mesh_ResetTextureState();
+//     R_Mesh_ResetTextureState();
        R_EntityMatrix(&identitymatrix);
        GL_CullFace(GL_NONE);
+       GL_DepthTest(true); // polys in 3D space shall always have depth test
+       GL_DepthRange(0, 1);
        R_Mesh_PrepareVertices_Generic_Arrays(polys->num_vertices, polys->data_vertex3f, polys->data_color4f, polys->data_texcoord2f);
 
        for (surfacelistindex = 0;surfacelistindex < numsurfaces;)
@@ -3107,7 +3124,7 @@ void VMPolygons_Store(vmpolygons_t *polys)
                if(polys->begin_color[i][3] < 1)
                        hasalpha = true;
 
-       if (r_refdef.draw2dstage)
+       if (polys->begin_draw2d)
        {
                // draw the polygon as 2D immediately
                drawqueuemesh_t mesh;
@@ -3127,7 +3144,8 @@ void VMPolygons_Store(vmpolygons_t *polys)
                int i;
                if (polys->max_triangles < polys->num_triangles + polys->begin_vertices-2)
                {
-                       polys->max_triangles *= 2;
+                       while (polys->max_triangles < polys->num_triangles + polys->begin_vertices-2)
+                               polys->max_triangles *= 2;
                        VM_ResizePolygons(polys);
                }
                if (polys->num_vertices + polys->begin_vertices <= polys->max_vertices)
@@ -3180,7 +3198,7 @@ void VM_CL_AddPolygonsToMeshQueue (void)
          polys->num_vertices = 0;  // otherwise it's not rendered at all and prints an error message --blub */
 }
 
-//void(string texturename, float flag) R_BeginPolygon
+//void(string texturename, float flag[, float is2d]) R_BeginPolygon
 void VM_CL_R_PolygonBegin (void)
 {
        const char              *picname;
@@ -3192,7 +3210,7 @@ void VM_CL_R_PolygonBegin (void)
        // better management of flags, and is more suited for 3D rendering), what
        // about supporting Q3 shaders?
 
-       VM_SAFEPARMCOUNT(2, VM_CL_R_PolygonBegin);
+       VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_R_PolygonBegin);
 
        if (!polys->initialized)
                VM_InitPolygons(polys);
@@ -3234,6 +3252,7 @@ void VM_CL_R_PolygonBegin (void)
        polys->begin_drawflag = (int)PRVM_G_FLOAT(OFS_PARM1) & DRAWFLAG_MASK;
        polys->begin_vertices = 0;
        polys->begin_active = true;
+       polys->begin_draw2d = (prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : r_refdef.draw2dstage);
 }
 
 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
@@ -3386,7 +3405,7 @@ realcheck:
        start[0] = stop[0] = (mins[0] + maxs[0])*0.5;
        start[1] = stop[1] = (mins[1] + maxs[1])*0.5;
        stop[2] = start[2] - 2*sv_stepheight.value;
-       trace = CL_TraceLine(start, stop, MOVE_NOMONSTERS, ent, CL_GenericHitSuperContentsMask(ent), true, false, NULL, true);
+       trace = CL_TraceLine(start, stop, MOVE_NOMONSTERS, ent, CL_GenericHitSuperContentsMask(ent), true, false, NULL, true, false);
 
        if (trace.fraction == 1.0)
                return false;
@@ -3399,7 +3418,7 @@ realcheck:
                        start[0] = stop[0] = x ? maxs[0] : mins[0];
                        start[1] = stop[1] = y ? maxs[1] : mins[1];
 
-                       trace = CL_TraceLine(start, stop, MOVE_NOMONSTERS, ent, CL_GenericHitSuperContentsMask(ent), true, false, NULL, true);
+                       trace = CL_TraceLine(start, stop, MOVE_NOMONSTERS, ent, CL_GenericHitSuperContentsMask(ent), true, false, NULL, true, false);
 
                        if (trace.fraction != 1.0 && trace.endpos[2] > bottom)
                                bottom = trace.endpos[2];
@@ -3982,6 +4001,44 @@ static void VM_CL_frameduration(void)
                PRVM_G_FLOAT(OFS_RETURN) = model->animscenes[framenum].framecount / model->animscenes[framenum].framerate;
 }
 
+void VM_CL_RotateMoves(void)
+{
+       /*
+        * Obscure builtin used by GAME_XONOTIC.
+        *
+        * Edits the input history of cl_movement by rotating all move commands
+        * currently in the queue using the given transform.
+        *
+        * The vector passed is an "angles transform" as used by warpzonelib, i.e.
+        * v_angle-like (non-inverted) euler angles that perform the rotation
+        * of the space that is to be done.
+        *
+        * This is meant to be used as a fixangle replacement after passing
+        * through a warpzone/portal: the client is told about the warp transform,
+        * and calls this function in the same frame as the one on which the
+        * client's origin got changed by the serverside teleport. Then this code
+        * transforms the pre-warp input (which matches the empty space behind
+        * the warp plane) into post-warp input (which matches the target area
+        * of the warp). Also, at the same time, the client has to use
+        * R_SetView to adjust VF_CL_VIEWANGLES according to the same transform.
+        *
+        * This together allows warpzone motion to be perfectly predicted by
+        * the client!
+        *
+        * Furthermore, for perfect warpzone behaviour, the server side also
+        * has to detect input the client sent before it received the origin
+        * update, but after the warp occurred on the server, and has to adjust
+        * input appropriately.
+        */
+       matrix4x4_t m;
+       vec3_t v = {0, 0, 0};
+       vec3_t x, y, z;
+       VM_SAFEPARMCOUNT(1, VM_CL_RotateMoves);
+       AngleVectorsFLU(PRVM_G_VECTOR(OFS_PARM0), x, y, z);
+       Matrix4x4_FromVectors(&m, x, y, z, v);
+       CL_RotateMoves(&m);
+}
+
 //============================================================================
 
 // To create a almost working builtin file from this replace:
@@ -4301,7 +4358,7 @@ VM_CL_R_AddEntity,                                // #302 void(entity ent) addentity (EXT_CSQC)
 VM_CL_R_SetView,                               // #303 float(float property, ...) setproperty (EXT_CSQC)
 VM_CL_R_RenderScene,                   // #304 void() renderscene (EXT_CSQC)
 VM_CL_R_AddDynamicLight,               // #305 void(vector org, float radius, vector lightcolours) adddynamiclight (EXT_CSQC)
-VM_CL_R_PolygonBegin,                  // #306 void(string texturename, float flag[, float is2d, float lines]) R_BeginPolygon
+VM_CL_R_PolygonBegin,                  // #306 void(string texturename, float flag, float is2d[NYI: , float lines]) R_BeginPolygon
 VM_CL_R_PolygonVertex,                 // #307 void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
 VM_CL_R_PolygonEnd,                            // #308 void() R_EndPolygon
 NULL /* R_LoadWorldModel in menu VM, should stay unassigned in client*/, // #309
@@ -4509,7 +4566,7 @@ NULL,                                                     // #509
 VM_uri_escape,                                 // #510 string(string in) uri_escape = #510;
 VM_uri_unescape,                               // #511 string(string in) uri_unescape = #511;
 VM_etof,                                       // #512 float(entity ent) num_for_edict = #512 (DP_QC_NUM_FOR_EDICT)
-VM_uri_get,                                            // #513 float(string uril, float id) uri_get = #512; (DP_QC_URI_GET)
+VM_uri_get,                                            // #513 float(string uri, float id, [string post_contenttype, string post_delim, [float buf]]) uri_get = #513; (DP_QC_URI_GET, DP_QC_URI_POST)
 VM_tokenize_console,                                   // #514 float(string str) tokenize_console = #514; (DP_QC_TOKENIZE_CONSOLE)
 VM_argv_start_index,                                   // #515 float(float idx) argv_start_index = #515; (DP_QC_TOKENIZE_CONSOLE)
 VM_argv_end_index,                                             // #516 float(float idx) argv_end_index = #516; (DP_QC_TOKENIZE_CONSOLE)
@@ -4536,9 +4593,9 @@ NULL,                                                     // #536
 NULL,                                                  // #537
 NULL,                                                  // #538
 NULL,                                                  // #539
-NULL,                                                  // #540
-NULL,                                                  // #541
-NULL,                                                  // #542
+VM_physics_enable,                             // #540 void(entity e, float physics_enabled) physics_enable = #540; (DP_PHYSICS_ODE)
+VM_physics_addforce,                   // #541 void(entity e, vector force, vector relative_ofs) physics_addforce = #541; (DP_PHYSICS_ODE)
+VM_physics_addtorque,                  // #542 void(entity e, vector torque) physics_addtorque = #542; (DP_PHYSICS_ODE)
 NULL,                                                  // #543
 NULL,                                                  // #544
 NULL,                                                  // #545
@@ -4630,6 +4687,12 @@ VM_setkeybind,                                           // #630 float(float key, string bind[, float bindmap]) setke
 VM_getbindmaps,                                                // #631 vector(void) getbindmap
 VM_setbindmaps,                                                // #632 float(vector bm) setbindmap
 NULL,                                                  // #633
+NULL,                                                  // #634
+NULL,                                                  // #635
+NULL,                                                  // #636
+NULL,                                                  // #637
+VM_CL_RotateMoves,                                     // #638
+NULL,                                                  // #639
 };
 
 const int vm_cl_numbuiltins = sizeof(vm_cl_builtins) / sizeof(prvm_builtin_t);