Skip to content

Commit

Permalink
libcommon|Cleanup|All Games: Calculating mobj movement friction
Browse files Browse the repository at this point in the history
The code for calculating friction was half-commonized already, with
the function in libcommon having the logic for Doom and Heretic. This
commit makes the friction calculation fully common so that the
Mobj_Friction() in libcommon suits all games.

Removed redundant code from the game plugins.
  • Loading branch information
skyjake committed Dec 14, 2014
1 parent 2a6ab90 commit bd32948
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 120 deletions.
2 changes: 1 addition & 1 deletion doomsday/api/api_gameexport.h
Expand Up @@ -122,7 +122,7 @@ typedef struct {

// Miscellaneous.
void (*MobjThinker) (void *mobj);
coord_t (*MobjFriction) (void* mobj); // Returns a friction factor.
coord_t (*MobjFriction) (struct mobj_s const *mobj); // Returns a friction factor.
dd_bool (*MobjCheckPositionXYZ) (struct mobj_s* mobj, coord_t x, coord_t y, coord_t z);
dd_bool (*MobjTryMoveXYZ) (struct mobj_s* mobj, coord_t x, coord_t y, coord_t z);
void (*SectorHeightChangeNotification)(int sectorIdx); // Applies necessary checks on objects.
Expand Down
10 changes: 10 additions & 0 deletions doomsday/plugins/common/include/mobj.h
Expand Up @@ -28,6 +28,16 @@
extern "C" {
#endif

/**
* Determines the current friction affecting @a mo, given the sector it is in and
* whether it is on the floor.
*
* @param mo Object.
*
* @return Friction multiplier to apply to the momentum.
*/
coord_t Mobj_Friction(mobj_t const *mo);

/**
* Handles the stopping of mobj movement. Also stops player walking animation.
*
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/include/p_actor.h
Expand Up @@ -95,7 +95,7 @@ dd_bool Mobj_IsDroppedItem(mobj_t *mobj);
*
* @param mobj Mobj instance.
*/
terraintype_t const *P_MobjFloorTerrain(mobj_t *mobj);
terraintype_t const *P_MobjFloorTerrain(mobj_t const *mobj);

/**
* The first three bits of the selector special byte contain a relative
Expand Down
19 changes: 16 additions & 3 deletions doomsday/plugins/common/src/mobj.cpp
Expand Up @@ -33,6 +33,7 @@
#include "player.h"
#include "p_map.h"
#include "p_saveg.h"
#include "p_actor.h"

#include <de/mathutil.h>
#include <cmath>
Expand All @@ -46,7 +47,7 @@
/// Threshold for stopping walk animation.
#define STANDSPEED (1.0 / 2) // FIX2FLT(0x8000)

static coord_t getFriction(mobj_t *mo)
coord_t Mobj_Friction(mobj_t const *mo)
{
if((mo->flags2 & MF2_FLY) && !(mo->origin[VZ] <= mo->floorZ) && !mo->onMobj)
{
Expand All @@ -62,7 +63,19 @@ static coord_t getFriction(mobj_t *mo)
}
#endif

return P_MobjGetFriction(mo);
#ifdef __JHEXEN__
terraintype_t const *tt = P_MobjFloorTerrain(mo);
if(tt->flags & TTF_FRICTION_LOW)
{
return FRICTION_LOW;
}
return FRICTION_NORMAL; // Hexen doesn't have XG sectors.
#endif

#ifndef __JHEXEN__
// Use the current sector's friction.
return XS_Friction(Mobj_Sector(mo));
#endif
}

dd_bool Mobj_IsVoodooDoll(mobj_t const *mo)
Expand Down Expand Up @@ -153,7 +166,7 @@ void Mobj_XYMoveStopping(mobj_t *mo)
}
else
{
coord_t friction = getFriction(mo);
coord_t friction = Mobj_Friction(mo);
mo->mom[MX] *= friction;
mo->mom[MY] *= friction;
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/common/src/p_actor.cpp
Expand Up @@ -224,7 +224,7 @@ dd_bool Mobj_IsDroppedItem(mobj_t *mobj)
#endif
}

terraintype_t const *P_MobjFloorTerrain(mobj_t *mobj)
terraintype_t const *P_MobjFloorTerrain(mobj_t const *mobj)
{
return P_PlaneMaterialTerrainType(Mobj_Sector(mobj), PLN_FLOOR);
}
Expand Down
2 changes: 0 additions & 2 deletions doomsday/plugins/doom/include/p_mobj.h
Expand Up @@ -248,8 +248,6 @@ void P_SpawnBlood(coord_t x, coord_t y, coord_t z, int damage, angle_t angle);

mobj_t *P_SpawnTeleFog(coord_t x, coord_t y, angle_t angle);

coord_t P_MobjGetFriction(mobj_t* mo);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/doom/src/d_api.c
Expand Up @@ -248,7 +248,7 @@ game_export_t* GetGameAPI(void)
gx.Responder = G_Responder;
gx.EndFrame = D_EndFrame;
gx.MobjThinker = P_MobjThinker;
gx.MobjFriction = (coord_t (*)(void *)) P_MobjGetFriction;
gx.MobjFriction = Mobj_Friction;
gx.MobjCheckPositionXYZ = P_CheckPositionXYZ;
gx.MobjTryMoveXYZ = P_TryMoveXYZ;
gx.SectorHeightChangeNotification = P_HandleSectorHeightChange;
Expand Down
33 changes: 0 additions & 33 deletions doomsday/plugins/doom/src/p_mobj.c
Expand Up @@ -74,44 +74,11 @@ void P_FloorBounceMissile(mobj_t *mo)
P_MobjChangeState(mo, P_GetState(mo->type, SN_DEATH));
}

/**
* @return The ground friction factor for the mobj.
*/
coord_t P_MobjGetFriction(mobj_t *mo)
{
if((mo->flags2 & MF2_FLY) && !(mo->origin[VZ] <= mo->floorZ) && !mo->onMobj)
{
return FRICTION_FLY;
}

return XS_Friction(Mobj_Sector(mo));
}

static __inline dd_bool isInWalkState(player_t *pl)
{
return pl->plr->mo->state - STATES - PCLASS_INFO(pl->class_)->runState < 4;
}

static coord_t getFriction(mobj_t *mo)
{
if((mo->flags2 & MF2_FLY) && !(mo->origin[VZ] <= mo->floorZ) &&
!mo->onMobj)
{
// Airborne friction.
return FRICTION_FLY;
}

#if __JHERETIC__
if(P_ToXSector(Mobj_Sector(mo))->special == 15)
{
// Friction_Low
return FRICTION_LOW;
}
#endif

return P_MobjGetFriction(mo);
}

void P_MobjMoveXY(mobj_t *mo)
{
coord_t pos[3], mom[3];
Expand Down
2 changes: 0 additions & 2 deletions doomsday/plugins/doom64/include/p_mobj.h
Expand Up @@ -263,8 +263,6 @@ extern "C" {

void P_ExplodeMissile(mobj_t *mo);

coord_t P_MobjGetFriction(mobj_t *mo);

mobj_t *P_SPMAngle(mobjtype_t type, mobj_t *source, angle_t angle);

/**
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/doom64/src/d_api.c
Expand Up @@ -154,7 +154,7 @@ game_export_t* GetGameAPI(void)
gx.Responder = G_Responder;
gx.EndFrame = D_EndFrame;
gx.MobjThinker = P_MobjThinker;
gx.MobjFriction = (coord_t (*)(void *)) P_MobjGetFriction;
gx.MobjFriction = Mobj_Friction;
gx.MobjCheckPositionXYZ = P_CheckPositionXYZ;
gx.MobjTryMoveXYZ = P_TryMoveXYZ;
gx.SectorHeightChangeNotification = P_HandleSectorHeightChange;
Expand Down
33 changes: 0 additions & 33 deletions doomsday/plugins/doom64/src/p_mobj.c
Expand Up @@ -79,39 +79,6 @@ void P_FloorBounceMissile(mobj_t *mo)
P_MobjChangeState(mo, P_GetState(mo->type, SN_DEATH));
}

/**
* @return The ground friction factor for the mobj.
*/
coord_t P_MobjGetFriction(mobj_t *mo)
{
if((mo->flags2 & MF2_FLY) && !(mo->origin[VZ] <= mo->floorZ) && !mo->onMobj)
{
return FRICTION_FLY;
}

return XS_Friction(Mobj_Sector(mo));
}

static coord_t getFriction(mobj_t *mo)
{
if((mo->flags2 & MF2_FLY) && !(mo->origin[VZ] <= mo->floorZ) &&
!mo->onMobj)
{
// Airborne friction.
return FRICTION_FLY;
}

#if __JHERETIC__
if(P_ToXSector(Mobj_Sector(mo))->special == 15)
{
// Friction_Low
return FRICTION_LOW;
}
#endif

return P_MobjGetFriction(mo);
}

void P_MobjMoveXY(mobj_t *mo)
{
coord_t pos[3], mom[3];
Expand Down
2 changes: 0 additions & 2 deletions doomsday/plugins/heretic/include/p_mobj.h
Expand Up @@ -283,8 +283,6 @@ mobj_t *Vanilla_P_SpawnMissileAngle(mobj_t *source, mobjtype_t type, angle_t ang

mobj_t *P_SpawnTeleFog(coord_t x, coord_t y, angle_t angle);

coord_t P_MobjGetFriction(mobj_t *mobj);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/heretic/src/h_api.c
Expand Up @@ -183,7 +183,7 @@ game_export_t* GetGameAPI(void)
gx.Responder = G_Responder;
gx.EndFrame = H_EndFrame;
gx.MobjThinker = P_MobjThinker;
gx.MobjFriction = (coord_t (*)(void *)) P_MobjGetFriction;
gx.MobjFriction = Mobj_Friction;
gx.MobjCheckPositionXYZ = P_CheckPositionXYZ;
gx.MobjTryMoveXYZ = P_TryMoveXYZ;
gx.SectorHeightChangeNotification = P_HandleSectorHeightChange;
Expand Down
19 changes: 0 additions & 19 deletions doomsday/plugins/heretic/src/p_mobj.c
Expand Up @@ -230,25 +230,6 @@ void P_WindThrust(mobj_t *mo)
}
}

coord_t P_MobjGetFriction(mobj_t *mo)
{
if((mo->flags2 & MF2_FLY) && !(mo->origin[VZ] <= mo->floorZ) && !mo->onMobj)
{
return FRICTION_FLY;
}
else
{
Sector *sec = Mobj_Sector(mo);

if(P_ToXSector(sec)->special == 15)
{
return FRICTION_LOW;
}

return XS_Friction(sec);
}
}

void P_MobjMoveXY(mobj_t *mo)
{
coord_t pos[2], mom[2];
Expand Down
1 change: 0 additions & 1 deletion doomsday/plugins/hexen/include/p_local.h
Expand Up @@ -114,7 +114,6 @@ dd_bool P_HealRadius(player_t* plr);
void P_BlastRadius(player_t* plr);

dd_bool P_CheckMissileSpawn(mobj_t* mo);
coord_t P_MobjGetFriction(mobj_t* mo);
void P_RipperBlood(mobj_t* mo);
dd_bool P_HitFloor(mobj_t* mo);

Expand Down
19 changes: 1 addition & 18 deletions doomsday/plugins/hexen/src/p_mobj.c
Expand Up @@ -258,23 +258,6 @@ static __inline dd_bool isInWalkState(player_t* pl)
return pl->plr->mo->state - STATES - PCLASS_INFO(pl->class_)->runState < 4;
}

coord_t P_MobjGetFriction(mobj_t* mo)
{
if((mo->flags2 & MF2_FLY) && !(mo->origin[VZ] <= mo->floorZ) && !mo->onMobj)
{
return FRICTION_FLY;
}
else
{
const terraintype_t* tt = P_MobjFloorTerrain(mo);

if(tt->flags & TTF_FRICTION_LOW)
return FRICTION_LOW;
}

return FRICTION_NORMAL;
}

void P_MobjMoveXY(mobj_t* mo)
{
static const coord_t windTab[3] = {
Expand Down Expand Up @@ -586,7 +569,7 @@ void P_MobjMoveXY(mobj_t* mo)
}
else
{
coord_t friction = P_MobjGetFriction(mo);
coord_t friction = Mobj_Friction(mo);
mo->mom[MX] *= friction;
mo->mom[MY] *= friction;
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/plugins/hexen/src/x_api.c
Expand Up @@ -211,7 +211,7 @@ game_export_t* GetGameAPI(void)
gx.Responder = G_Responder;
gx.EndFrame = X_EndFrame;
gx.MobjThinker = P_MobjThinker;
gx.MobjFriction = (coord_t (*)(void *)) P_MobjGetFriction;
gx.MobjFriction = Mobj_Friction;
gx.MobjCheckPositionXYZ = P_CheckPositionXYZ;
gx.MobjTryMoveXYZ = P_TryMoveXYZ;
gx.SectorHeightChangeNotification = P_HandleSectorHeightChange;
Expand Down

0 comments on commit bd32948

Please sign in to comment.