]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - world.c
physics: fix and refactor unsticking
[xonotic/darkplaces.git] / world.c
diff --git a/world.c b/world.c
index 9a20fd72817d18feb8e056eccdf98d312d385673..a95330fae99a4ba0ed9dc3d138d7038a6dcb9b78 100644 (file)
--- a/world.c
+++ b/world.c
@@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "clvm_cmds.h"
 #include "cl_collision.h"
+#include "com_list.h"
 
 /*
 
@@ -31,29 +32,45 @@ line of sight checks trace->inopen and trace->inwater, but bullets don't
 
 */
 
+#ifdef USEODE
 static void World_Physics_Init(void);
+#endif
 void World_Init(void)
 {
        Collision_Init();
+#ifdef USEODE
        World_Physics_Init();
+#endif
 }
 
+#ifdef USEODE
 static void World_Physics_Shutdown(void);
+#endif
 void World_Shutdown(void)
 {
+#ifdef USEODE
        World_Physics_Shutdown();
+#endif
 }
 
+#ifdef USEODE
 static void World_Physics_Start(world_t *world);
+#endif
 void World_Start(world_t *world)
 {
+#ifdef USEODE
        World_Physics_Start(world);
+#endif
 }
 
+#ifdef USEODE
 static void World_Physics_End(world_t *world);
+#endif
 void World_End(world_t *world)
 {
+#ifdef USEODE
        World_Physics_End(world);
+#endif
 }
 
 //============================================================================
@@ -102,7 +119,7 @@ void World_SetSize(world_t *world, const char *filename, const vec3_t mins, cons
 {
        int i;
 
-       strlcpy(world->filename, filename, sizeof(world->filename));
+       dp_strlcpy(world->filename, filename, sizeof(world->filename));
        VectorCopy(mins, world->mins);
        VectorCopy(maxs, world->maxs);
        world->prog = prog;
@@ -147,10 +164,10 @@ void World_UnlinkAll(world_t *world)
        // unlink all entities one by one
        grid = &world->areagrid_outside;
        while (grid->list.next != &grid->list)
-               World_UnlinkEdict(PRVM_EDICT_NUM(List_Entry(*grid->list.next, link_t, list)->entitynumber));
+               World_UnlinkEdict(PRVM_EDICT_NUM(List_Entry(grid->list.next, link_t, list)->entitynumber));
        for (i = 0, grid = world->areagrid;i < AREA_GRIDNODES;i++, grid++)
                while (grid->list.next != &grid->list)
-                       World_UnlinkEdict(PRVM_EDICT_NUM(List_Entry(*grid->list.next, link_t, list)->entitynumber));
+                       World_UnlinkEdict(PRVM_EDICT_NUM(List_Entry(grid->list.next, link_t, list)->entitynumber));
 }
 
 /*
@@ -172,7 +189,6 @@ int World_EntitiesInBox(world_t *world, const vec3_t requestmins, const vec3_t r
 {
        prvm_prog_t *prog = world->prog;
        int numlist;
-       llist_t *pos;
        link_t *grid;
        link_t *l;
        prvm_edict_t *ent;
@@ -215,14 +231,13 @@ int World_EntitiesInBox(world_t *world, const vec3_t requestmins, const vec3_t r
        if (world->areagrid_outside.list.next)
        {
                grid = &world->areagrid_outside;
-               List_For_Each(pos, &grid->list)
+               List_For_Each_Entry(l, &grid->list, link_t, list)
                {
-                       l = List_Entry(*pos, link_t, list);
                        ent = PRVM_EDICT_NUM(l->entitynumber);
                        if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
                        {
                                ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
-                               if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
+                               if (!ent->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
                                {
                                        if (numlist < maxlist)
                                                list[numlist] = ent;
@@ -240,14 +255,13 @@ int World_EntitiesInBox(world_t *world, const vec3_t requestmins, const vec3_t r
                {
                        if (grid->list.next)
                        {
-                               List_For_Each(pos, &grid->list)
+                               List_For_Each_Entry(l, &grid->list, link_t, list)
                                {
-                                       l = List_Entry(*pos, link_t, list);
                                        ent = PRVM_EDICT_NUM(l->entitynumber);
                                        if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
                                        {
                                                ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
-                                               if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
+                                               if (!ent->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
                                                {
                                                        if (numlist < maxlist)
                                                                list[numlist] = ent;
@@ -303,19 +317,23 @@ World_LinkEdict
 
 ===============
 */
-void World_LinkEdict(world_t *world, prvm_edict_t *ent, const vec3_t mins, const vec3_t maxs)
+void World_LinkEdict(world_t *world, prvm_edict_t *ent, const vec3_t mins, const vec3_t maxs, qbool link_solid_not)
 {
        prvm_prog_t *prog = world->prog;
        // unlink from old position first
        if (ent->priv.server->areagrid[0].list.prev)
                World_UnlinkEdict(ent);
 
+       // some games don't want SOLID_NOT entities linked
+       if (!link_solid_not && PRVM_serveredictfloat(ent, solid) == SOLID_NOT)
+               return;
+
        // don't add the world
        if (ent == prog->edicts)
                return;
 
        // don't add free entities
-       if (ent->priv.server->free)
+       if (ent->free)
                return;
 
        VectorCopy(mins, ent->priv.server->areamins);
@@ -1461,15 +1479,12 @@ static dllfunction_t odefuncs[] =
 //     {"dGeomTriMeshDataUpdate",                      (void **) &dGeomTriMeshDataUpdate},
        {NULL, NULL}
 };
-
 // Handle for ODE DLL
 dllhandle_t ode_dll = NULL;
 #endif
-#endif
 
 static void World_Physics_Init(void)
 {
-#ifdef USEODE
 #ifndef LINK_TO_LIBODE
        const char* dllnames [] =
        {
@@ -1522,7 +1537,7 @@ static void World_Physics_Init(void)
 
 #ifndef LINK_TO_LIBODE
        // Load the DLL
-       if (Sys_LoadLibrary (dllnames, &ode_dll, odefuncs))
+       if (Sys_LoadDependency (dllnames, &ode_dll, odefuncs))
 #endif
        {
                dInitODE();
@@ -1539,7 +1554,7 @@ static void World_Physics_Init(void)
 # else
                        Con_Printf("ODE library not compiled for double precision - incompatible!  Not using ODE physics.\n");
 # endif
-                       Sys_UnloadLibrary(&ode_dll);
+                       Sys_FreeLibrary(&ode_dll);
                        ode_dll = NULL;
                }
                else
@@ -1553,26 +1568,21 @@ static void World_Physics_Init(void)
                }
 #endif
        }
-#endif
 }
-
 static void World_Physics_Shutdown(void)
 {
-#ifdef USEODE
 #ifndef LINK_TO_LIBODE
        if (ode_dll)
 #endif
        {
                dCloseODE();
 #ifndef LINK_TO_LIBODE
-               Sys_UnloadLibrary(&ode_dll);
+               Sys_FreeLibrary(&ode_dll);
                ode_dll = NULL;
 #endif
        }
-#endif
 }
 
-#ifdef USEODE
 static void World_Physics_UpdateODE(world_t *world)
 {
        dWorldID odeworld;
@@ -1629,20 +1639,16 @@ static void World_Physics_EnableODE(world_t *world)
 
        World_Physics_UpdateODE(world);
 }
-#endif
 
 static void World_Physics_Start(world_t *world)
 {
-#ifdef USEODE
        if (world->physics.ode)
                return;
        World_Physics_EnableODE(world);
-#endif
 }
 
 static void World_Physics_End(world_t *world)
 {
-#ifdef USEODE
        if (world->physics.ode)
        {
                dWorldDestroy((dWorldID)world->physics.ode_world);
@@ -1650,17 +1656,14 @@ static void World_Physics_End(world_t *world)
                dJointGroupDestroy((dJointGroupID)world->physics.ode_contactgroup);
                world->physics.ode = false;
        }
-#endif
 }
 
 void World_Physics_RemoveJointFromEntity(world_t *world, prvm_edict_t *ed)
 {
        ed->priv.server->ode_joint_type = 0;
-#ifdef USEODE
        if(ed->priv.server->ode_joint)
                dJointDestroy((dJointID)ed->priv.server->ode_joint);
        ed->priv.server->ode_joint = NULL;
-#endif
 }
 
 void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
@@ -1669,7 +1672,6 @@ void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
 
        // entity is not physics controlled, free any physics data
        ed->priv.server->ode_physics = false;
-#ifdef USEODE
        if (ed->priv.server->ode_geom)
                dGeomDestroy((dGeomID)ed->priv.server->ode_geom);
        ed->priv.server->ode_geom = NULL;
@@ -1699,7 +1701,6 @@ void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
                dBodyDestroy((dBodyID)ed->priv.server->ode_body);
        }
        ed->priv.server->ode_body = NULL;
-#endif
        if (ed->priv.server->ode_vertex3f)
                Mem_Free(ed->priv.server->ode_vertex3f);
        ed->priv.server->ode_vertex3f = NULL;
@@ -1722,7 +1723,6 @@ void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
 
 void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f)
 {
-#ifdef USEODE
        dBodyID body = (dBodyID)ed->priv.server->ode_body;
 
        switch(f->type)
@@ -1744,10 +1744,8 @@ void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f)
        default:
                break;
        }
-#endif
 }
 
-#ifdef USEODE
 static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
 {
        prvm_prog_t *prog = world->prog;
@@ -1864,7 +1862,7 @@ static void World_Physics_Frame_ForceFromEntity(world_t *world, prvm_edict_t *ed
        if (!forcetype)
                return;
        enemy = PRVM_gameedictedict(ed, enemy);
-       if (enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
+       if (enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].free || prog->edicts[enemy].priv.server->ode_body == 0)
                return;
        VectorCopy(PRVM_gameedictvector(ed, movedir), movedir);
        VectorCopy(PRVM_gameedictvector(ed, origin), origin);
@@ -1916,9 +1914,9 @@ static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed
        VectorCopy(PRVM_gameedictvector(ed, movedir), movedir);
        if(movetype == MOVETYPE_PHYSICS)
                jointtype = JOINTTYPE_NONE; // can't have both
-       if(enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
+       if(enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].free || prog->edicts[enemy].priv.server->ode_body == 0)
                enemy = 0;
-       if(aiment <= 0 || aiment >= prog->num_edicts || prog->edicts[aiment].priv.required->free || prog->edicts[aiment].priv.server->ode_body == 0)
+       if(aiment <= 0 || aiment >= prog->num_edicts || prog->edicts[aiment].free || prog->edicts[aiment].priv.server->ode_body == 0)
                aiment = 0;
        // see http://www.ode.org/old_list_archives/2006-January/017614.html
        // we want to set ERP? make it fps independent and work like a spring constant
@@ -2290,9 +2288,9 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
 
                        // check if trimesh can be defined with convex
                        convex_compatible = false;
-                       for (i = 0;i < model->nummodelsurfaces;i++)
+                       for (i = model->submodelsurfaces_start;i < model->submodelsurfaces_end;i++)
                        {
-                               if (!strcmp(((msurface_t *)(model->data_surfaces + model->firstmodelsurface + i))->texture->name, "collisionconvex"))
+                               if (!strcmp(model->data_surfaces[i].texture->name, "collisionconvex"))
                                {
                                        convex_compatible = true;
                                        break;
@@ -2735,17 +2733,17 @@ treatasbox:
        if (physics_ode_trick_fixnan.integer)
        {
                test = VectorLength2(origin) + VectorLength2(forward) + VectorLength2(left) + VectorLength2(up) + VectorLength2(velocity) + VectorLength2(spinvelocity);
-               if (VEC_IS_NAN(test))
+               if (isnan(test))
                {
                        modified = true;
                        //Con_Printf("Fixing NAN values on entity %i : .classname = \"%s\" .origin = '%f %f %f' .velocity = '%f %f %f' .axis_forward = '%f %f %f' .axis_left = '%f %f %f' .axis_up = %f %f %f' .spinvelocity = '%f %f %f'\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_gameedictstring(ed, classname)), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], forward[0], forward[1], forward[2], left[0], left[1], left[2], up[0], up[1], up[2], spinvelocity[0], spinvelocity[1], spinvelocity[2]);
                        if (physics_ode_trick_fixnan.integer >= 2)
                                Con_Printf("Fixing NAN values on entity %i : .classname = \"%s\" .origin = '%f %f %f' .velocity = '%f %f %f' .angles = '%f %f %f' .avelocity = '%f %f %f'\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], angles[0], angles[1], angles[2], avelocity[0], avelocity[1], avelocity[2]);
                        test = VectorLength2(origin);
-                       if (VEC_IS_NAN(test))
+                       if (isnan(test))
                                VectorClear(origin);
                        test = VectorLength2(forward) * VectorLength2(left) * VectorLength2(up);
-                       if (VEC_IS_NAN(test))
+                       if (isnan(test))
                        {
                                VectorSet(angles, 0, 0, 0);
                                VectorSet(forward, 1, 0, 0);
@@ -2753,10 +2751,10 @@ treatasbox:
                                VectorSet(up, 0, 0, 1);
                        }
                        test = VectorLength2(velocity);
-                       if (VEC_IS_NAN(test))
+                       if (isnan(test))
                                VectorClear(velocity);
                        test = VectorLength2(spinvelocity);
-                       if (VEC_IS_NAN(test))
+                       if (isnan(test))
                        {
                                VectorClear(avelocity);
                                VectorClear(spinvelocity);
@@ -2927,7 +2925,7 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
                return;
 
        ed1 = (prvm_edict_t *) dGeomGetData(o1);
-       if(ed1 && ed1->priv.server->free)
+       if(ed1 && ed1->free)
                ed1 = NULL;
        if(ed1)
        {
@@ -2938,7 +2936,7 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
        }
 
        ed2 = (prvm_edict_t *) dGeomGetData(o2);
-       if(ed2 && ed2->priv.server->free)
+       if(ed2 && ed2->free)
                ed2 = NULL;
        if(ed2)
        {
@@ -3008,11 +3006,9 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
                dJointAttach(c, b1, b2);
        }
 }
-#endif
 
 void World_Physics_Frame(world_t *world, double frametime, double gravity)
 {
-#ifdef USEODE
        prvm_prog_t *prog = world->prog;
        double tdelta, tdelta2, tdelta3, simulationtime, collisiontime;
 
@@ -3052,11 +3048,11 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity)
                if (prog)
                {
                        for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
-                               if (!prog->edicts[i].priv.required->free)
+                               if (!prog->edicts[i].free)
                                        World_Physics_Frame_BodyFromEntity(world, ed);
                        // oh, and it must be called after all bodies were created
                        for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
-                               if (!prog->edicts[i].priv.required->free)
+                               if (!prog->edicts[i].free)
                                        World_Physics_Frame_JointFromEntity(world, ed);
                }
 
@@ -3077,7 +3073,7 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity)
                        {
                                int j;
                                for (j = 0, ed = prog->edicts + j;j < prog->num_edicts;j++, ed++)
-                                       if (!prog->edicts[j].priv.required->free)
+                                       if (!prog->edicts[j].free)
                                                World_Physics_Frame_ForceFromEntity(world, ed);
                        }
                        // run physics (move objects, calculate new velocities)
@@ -3094,7 +3090,7 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity)
                if (prog)
                {
                        for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
-                               if (!prog->edicts[i].priv.required->free)
+                               if (!prog->edicts[i].free)
                                        World_Physics_Frame_BodyToEntity(world, ed);
 
                        // print stats
@@ -3106,7 +3102,7 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity)
                                world->physics.ode_activeovjects = 0;
                                for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
                                {
-                                       if (prog->edicts[i].priv.required->free)
+                                       if (prog->edicts[i].free)
                                                continue;
                                        body = (dBodyID)prog->edicts[i].priv.server->ode_body;
                                        if (!body)
@@ -3119,5 +3115,5 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity)
                        }
                }
        }
-#endif
 }
+#endif