Skip to content

Commit

Permalink
Refactor|GameMap: Moved more GameMap_* functions to methods of GameMap
Browse files Browse the repository at this point in the history
Line of sight trace state accessors.
  • Loading branch information
danij-deng committed Apr 3, 2013
1 parent cb7e87d commit 0083200
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 84 deletions.
69 changes: 35 additions & 34 deletions doomsday/client/include/map/gamemap.h
Expand Up @@ -147,8 +147,8 @@ class GameMap

/// Current LOS trace state.
/// @todo Refactor to support concurrent traces.
TraceOpening traceOpening;
divline_t traceLOS;
TraceOpening _traceOpening;
divline_t _traceLine;

public:
GameMap();
Expand Down Expand Up @@ -327,16 +327,42 @@ class GameMap
/**
* Traces a line of sight.
*
* @param from World position, trace origin coordinates.
* @param to World position, trace target coordinates.
* @param flags Line Sight Flags (LS_*) @ref lineSightFlags
* @param from World position, trace origin coordinates.
* @param to World position, trace target coordinates.
* @param flags Line Sight Flags (LS_*) @ref lineSightFlags.
*
* @return @c true if the traverser function returns @c true
* for all visited lines.
* @return @c true if the traverser function returns @c true for all
* visited lines.
*/
bool lineOfSight(const_pvec3d_t from, const_pvec3d_t to, coord_t bottomSlope,
coord_t topSlope, int flags);

/**
* Retrieve an immutable copy of the LOS trace line state.
*
* @todo GameMap should not own this data.
*/
divline_t const &traceLine() const;

/**
* Retrieve an immutable copy of the LOS TraceOpening state.
*
* @todo GameMap should not own this data.
*/
TraceOpening const &traceOpening() const;

/**
* Update the TraceOpening state for according to the opening defined by the
* inner-minimal planes heights which intercept @a line
*
* If @a line is not owned by the map this is a no-op.
*
* @todo GameMap should not own this data.
*
* @param line Map line to configure the opening for.
*/
void setTraceOpening(LineDef &line);

/**
* Trace a line between @a from and @a to, making a callback for each
* interceptable object linked within Blockmap cells which cover the path
Expand All @@ -350,8 +376,8 @@ class GameMap
*
* @param fromX X axis map space coordinate for the path origin.
* @param fromY Y axis map space coordinate for the path origin.
* @param toX X axis map space coordinate for the path origin.
* @param toY Y axis map space coordinate for the path origin.
* @param toX X axis map space coordinate for the path destination.
* @param toY Y axis map space coordinate for the path destination.
*/
inline int pathTraverse(coord_t fromX, coord_t fromY, coord_t toX, coord_t toY,
int flags, traverser_t callback, void *parameters = 0)
Expand Down Expand Up @@ -648,31 +674,6 @@ class GameMap
void updateSurfacesOnMaterialChange(Material &material);
};

/**
* Retrieve an immutable copy of the LOS trace line.
*
* @param map GameMap instance.
*/
divline_t const *GameMap_TraceLOS(GameMap *map);

/**
* Retrieve an immutable copy of the LOS TraceOpening state.
*
* @param map GameMap instance.
*/
TraceOpening const *GameMap_TraceOpening(GameMap *map);

/**
* Update the TraceOpening state for according to the opening defined by the
* inner-minimal planes heights which intercept @a line
*
* If @a line is not owned by the map this is a no-op.
*
* @param map GameMap instance.
* @param line Map line to configure the opening for.
*/
void GameMap_SetTraceOpening(GameMap *map, LineDef *line);

/**
* Have the thinker lists been initialized yet?
* @param map GameMap instance.
Expand Down
29 changes: 13 additions & 16 deletions doomsday/client/src/map/gamemap.cpp
Expand Up @@ -58,8 +58,8 @@ GameMap::GameMap()
_effectiveGravity = 0;
_ambientLightLevel = 0;
std::memset(_skyFix, 0, sizeof(_skyFix));
std::memset(&traceOpening, 0, sizeof(traceOpening));
std::memset(&traceLOS, 0, sizeof(traceLOS));
std::memset(&_traceOpening, 0, sizeof(_traceOpening));
std::memset(&_traceLine, 0, sizeof(_traceLine));
}

GameMap::~GameMap()
Expand Down Expand Up @@ -147,25 +147,22 @@ void GameMap::setGravity(coord_t newGravity)
_effectiveGravity = newGravity;
}

divline_t const *GameMap_TraceLOS(GameMap *map)
divline_t const &GameMap::traceLine() const
{
DENG2_ASSERT(map);
return &map->traceLOS;
return _traceLine;
}

TraceOpening const *GameMap_TraceOpening(GameMap *map)
TraceOpening const &GameMap::traceOpening() const
{
DENG2_ASSERT(map);
return &map->traceOpening;
return _traceOpening;
}

void GameMap_SetTraceOpening(GameMap *map, LineDef *line)
void GameMap::setTraceOpening(LineDef &line)
{
DENG2_ASSERT(map);
// Is the linedef part of this map?
if(!line || map->lineIndex(line) < 0) return; // Odd...
if(lineIndex(&line) < 0) return; // Odd...

line->configureTraceOpening(map->traceOpening);
line.configureTraceOpening(_traceOpening);
}

int GameMap::ambientLightLevel() const
Expand Down Expand Up @@ -1022,10 +1019,10 @@ static int traverseCellPath(GameMap* map, Blockmap* bmap, coord_t const from_[2]
if(INRANGE_OF(dX, 0, epsilon)) from[VX] += unitOffset;
if(INRANGE_OF(dY, 0, epsilon)) from[VY] += unitOffset;

map->traceLOS.origin[VX] = FLT2FIX(from[VX]);
map->traceLOS.origin[VY] = FLT2FIX(from[VY]);
map->traceLOS.direction[VX] = FLT2FIX(to[VX] - from[VX]);
map->traceLOS.direction[VY] = FLT2FIX(to[VY] - from[VY]);
map->_traceLine.origin[VX] = FLT2FIX(from[VX]);
map->_traceLine.origin[VY] = FLT2FIX(from[VY]);
map->_traceLine.direction[VX] = FLT2FIX(to[VX] - from[VX]);
map->_traceLine.direction[VY] = FLT2FIX(to[VY] - from[VY]);

/**
* It is possible that one or both points are outside the blockmap.
Expand Down
61 changes: 27 additions & 34 deletions doomsday/client/src/map/p_maputil.cpp
Expand Up @@ -51,7 +51,7 @@ DENG_EXTERN_C divline_t const *P_TraceLOS()
static divline_t emptyLOS;
if(theMap)
{
return GameMap_TraceLOS(theMap);
return &theMap->traceLine();
}
return &emptyLOS;
}
Expand All @@ -62,21 +62,17 @@ DENG_EXTERN_C TraceOpening const *P_TraceOpening()
static TraceOpening zeroOpening;
if(theMap)
{
return GameMap_TraceOpening(theMap);
return &theMap->traceOpening();
}
return &zeroOpening;
}

#undef P_SetTraceOpening
DENG_EXTERN_C void P_SetTraceOpening(LineDef *line)
{
if(!theMap)
{
DEBUG_Message(("Warning: P_SetTraceOpening() attempted with no current map, ignoring."));
return;
}
if(!theMap || !line) return;
/// @todo Do not assume line is from the CURRENT map.
GameMap_SetTraceOpening(theMap, line);
theMap->setTraceOpening(*line);
}

#undef P_BspLeafAtPoint
Expand Down Expand Up @@ -530,29 +526,29 @@ int GameMap_SectorTouchingMobjsIterator(GameMap *map, Sector *sector,
int PIT_AddLineDefIntercepts(LineDef *line, void * /*parameters*/)
{
/// @todo Do not assume line is from the current map.
divline_t const *traceLOS = GameMap_TraceLOS(theMap);
divline_t const &traceLos = theMap->traceLine();
int s1, s2;

// Is this line crossed?
// Avoid precision problems with two routines.
if(traceLOS->direction[VX] > FRACUNIT * 16 || traceLOS->direction[VY] > FRACUNIT * 16 ||
traceLOS->direction[VX] < -FRACUNIT * 16 || traceLOS->direction[VY] < -FRACUNIT * 16)
if(traceLos.direction[VX] > FRACUNIT * 16 || traceLos.direction[VY] > FRACUNIT * 16 ||
traceLos.direction[VX] < -FRACUNIT * 16 || traceLos.direction[VY] < -FRACUNIT * 16)
{
s1 = Divline_PointOnSide(traceLOS, line->v1Origin());
s2 = Divline_PointOnSide(traceLOS, line->v2Origin());
s1 = Divline_PointOnSide(&traceLos, line->v1Origin());
s2 = Divline_PointOnSide(&traceLos, line->v2Origin());
}
else
{
s1 = line->pointOnSide(FIX2FLT(traceLOS->origin[VX]), FIX2FLT(traceLOS->origin[VY])) < 0;
s2 = line->pointOnSide(FIX2FLT(traceLOS->origin[VX] + traceLOS->direction[VX]),
FIX2FLT(traceLOS->origin[VY] + traceLOS->direction[VY])) < 0;
s1 = line->pointOnSide(FIX2FLT(traceLos.origin[VX]), FIX2FLT(traceLos.origin[VY])) < 0;
s2 = line->pointOnSide(FIX2FLT(traceLos.origin[VX] + traceLos.direction[VX]),
FIX2FLT(traceLos.origin[VY] + traceLos.direction[VY])) < 0;
}
if(s1 == s2) return false;

// Calculate interception point.
divline_t dl;
line->configureDivline(dl);
float distance = FIX2FLT(Divline_Intersection(&dl, traceLOS));
float distance = FIX2FLT(Divline_Intersection(&dl, &traceLos));

// On the correct side of the trace origin?
if(!(distance < 0))
Expand All @@ -564,21 +560,16 @@ int PIT_AddLineDefIntercepts(LineDef *line, void * /*parameters*/)
return false;
}

int PIT_AddMobjIntercepts(mobj_t* mo, void* /*paramaters*/)
int PIT_AddMobjIntercepts(mobj_t *mo, void * /*parameters*/)
{
const divline_t* traceLOS;
vec2d_t from, to;
coord_t distance;
divline_t dl;
int s1, s2;

if(mo->dPlayer && (mo->dPlayer->flags & DDPF_CAMERA))
return false; // $democam: ssshh, keep going, we're not here...

// Check a corner to corner crossection for hit.
/// @todo Do not assume mobj is from the current map.
traceLOS = GameMap_TraceLOS(theMap);
if((traceLOS->direction[VX] ^ traceLOS->direction[VY]) > 0)
divline_t const &traceLos = theMap->traceLine();
vec2d_t from, to;
if((traceLos.direction[VX] ^ traceLos.direction[VY]) > 0)
{
// \ Slope
V2d_Set(from, mo->origin[VX] - mo->radius,
Expand All @@ -596,21 +587,23 @@ int PIT_AddMobjIntercepts(mobj_t* mo, void* /*paramaters*/)
}

// Is this line crossed?
s1 = Divline_PointOnSide(traceLOS, from);
s2 = Divline_PointOnSide(traceLOS, to);
if(s1 == s2) return false;
if(Divline_PointOnSide(&traceLos, from) != Divline_PointOnSide(&traceLos, to))
return false;

// Calculate interception point.
dl.origin[VX] = FLT2FIX((float)from[VX]);
dl.origin[VY] = FLT2FIX((float)from[VY]);
dl.direction[VX] = FLT2FIX((float)(to[VX] - from[VX]));
dl.direction[VY] = FLT2FIX((float)(to[VY] - from[VY]));
distance = FIX2FLT(Divline_Intersection(&dl, traceLOS));
divline_t dl;
dl.origin[VX] = FLT2FIX(float( from[VX] ));
dl.origin[VY] = FLT2FIX(float( from[VY] ));
dl.direction[VX] = FLT2FIX(float( to[VX] - from[VX] ));
dl.direction[VY] = FLT2FIX(float( to[VY] - from[VY] ));
coord_t distance = FIX2FLT(Divline_Intersection(&dl, &traceLos));

// On the correct side of the trace origin?
if(!(distance < 0))
{
P_AddIntercept(ICPT_MOBJ, distance, mo);
}

// Continue iteration.
return false;
}
Expand Down

0 comments on commit 0083200

Please sign in to comment.