Skip to content

Commit

Permalink
Refactor: Moved line of sight test algorithm into GameMap
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 4, 2012
1 parent 1977698 commit fa9c500
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 271 deletions.
25 changes: 19 additions & 6 deletions doomsday/engine/api/dd_share.h
Expand Up @@ -257,7 +257,7 @@ enum {

// Non-integer/special values for Set/Get
DD_TRANSLATIONTABLES_ADDRESS,
DD_TRACE_ADDRESS, ///< divline 'trace' used by PathTraverse.
DD_TRACE_ADDRESS, ///< obsolete divline 'trace' used by PathTraverse.
DD_SPRITE_REPLACEMENT, ///< Sprite <-> model replacement.
DD_ACTION_LINK, ///< State action routine addresses.
DD_MAP_NAME,
Expand All @@ -283,10 +283,10 @@ enum {
DD_XGFUNC_LINK, ///< XG line classes
DD_SHARED_FIXED_TRIGGER_OBSOLETE, ///< obsolete
DD_GAMETIC,
DD_OPENRANGE,
DD_OPENTOP,
DD_OPENBOTTOM,
DD_LOWFLOOR,
DD_OPENRANGE, ///< obsolete
DD_OPENTOP, ///< obsolete
DD_OPENBOTTOM, ///< obsolete
DD_LOWFLOOR, ///< obsolete
DD_CPLAYER_THRUST_MUL_OBSOLETE, ///< obsolete
DD_GRAVITY,
DD_PSPRITE_OFFSET_X, ///< 10x
Expand Down Expand Up @@ -873,7 +873,20 @@ typedef struct intercept_s {

typedef int (*traverser_t) (const intercept_t* intercept, void* paramaters);

#define NO_INDEX 0xffffffff
/**
* A simple POD data structure for representing line trace openings.
*/
typedef struct {
/// Top and bottom z of the opening.
float top, bottom;

/// Distance from top to bottom.
float range;

/// Z height of the lowest Plane at the opening on the X|Y axis.
/// @todo Does not belong here?
float lowFloor;
} TraceOpening;

//------------------------------------------------------------------------
//
Expand Down
6 changes: 4 additions & 2 deletions doomsday/engine/api/doomsday.def
@@ -1,6 +1,6 @@
; Doomsday Engine API (Routines exported from Doomsday.exe).
;
; Highest ordinal is currently: --> 827 <--
; Highest ordinal is currently: --> 829 <--
; Other free ordinals: none

NAME "DOOMSDAY"
Expand Down Expand Up @@ -446,7 +446,9 @@ EXPORTS
P_MakeDivline @61 NONAME
P_PointOnDivlineSide @62 NONAME
P_InterceptVector @63 NONAME
P_LineOpening @64 NONAME
P_TraceLOS @829 NONAME
P_TraceOpening @828 NONAME
P_SetTraceOpening @64 NONAME
P_SubsectorAtPointXY @102 NONAME
P_MobjsBoxIterator @346 NONAME
P_LinesBoxIterator @435 NONAME
Expand Down
5 changes: 4 additions & 1 deletion doomsday/engine/api/doomsday.h
Expand Up @@ -422,7 +422,10 @@ boolean ClMobj_LocalActionsEnabled(struct mobj_s* mo);
void P_MakeDivline(struct linedef_s* li, divline_t* dl);
int P_PointOnDivlineSide(float x, float y, const divline_t* line);
float P_InterceptVector(divline_t* v2, divline_t* v1);
void P_LineOpening(struct linedef_s* linedef);

const divline_t* P_TraceLOS(void);
TraceOpening* P_TraceOpening(void);
void P_SetTraceOpening(struct linedef_s* linedef);

struct subsector_s* P_SubsectorAtPointXY(float x, float y);

Expand Down
30 changes: 30 additions & 0 deletions doomsday/engine/portable/include/gamemap.h
Expand Up @@ -73,6 +73,11 @@ typedef struct gamemap_s {

float globalGravity; // Gravity for the current map.
int ambientLightLevel; // Ambient lightlevel for the current map.

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

/**
Expand All @@ -86,6 +91,31 @@ const char* GameMap_OldUniqueId(GameMap* map);

void GameMap_Bounds(GameMap* map, float* min, float* max);

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

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

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

/**
* Retrieve the map-global ambient light level.
*
Expand Down
28 changes: 24 additions & 4 deletions doomsday/engine/portable/include/p_maputil.h
Expand Up @@ -37,9 +37,6 @@
#define IS_SECTOR_LINKED(mo) ((mo)->sPrev != NULL)
#define IS_BLOCK_LINKED(mo) ((mo)->bNext != NULL)

extern float opentop, openbottom, openrange, lowfloor;
extern divline_t traceLOS;

float P_AccurateDistanceFixed(fixed_t dx, fixed_t dy);
float P_AccurateDistance(float dx, float dy);
float P_ApproxDistance(float dx, float dy);
Expand Down Expand Up @@ -72,7 +69,26 @@ float P_InterceptVector(const divline_t* v2, const divline_t* v1);
int P_PointOnDivLineSidef(fvertex_t *pnt, fdivline_t *dline);
float P_FloatInterceptVertex(fvertex_t *start, fvertex_t *end,
fdivline_t *fdiv, fvertex_t *inter);
void P_LineOpening(linedef_t* linedef);

/**
* Retrieve an immutable copy of the LOS trace line for the CURRENT map.
*
* @note Always returns a valid divline_t even if there is no current map.
*/
const divline_t* P_TraceLOS(void);

/**
* Retrieve an immutable copy of the TraceOpening state for the CURRENT map.
*
* @note Always returns a valid TraceOpening even if there is no current map.
*/
const TraceOpening* P_TraceOpening(void);

/**
* Update the TraceOpening state for the CURRENT map according to the opening
* defined by the inner-minimal planes heights which intercept @a linedef
*/
void P_SetTraceOpening(linedef_t* linedef);

/**
* Determine the BSP leaf (subsector) on the back side of the BS partition that
Expand Down Expand Up @@ -154,4 +170,8 @@ int P_PathTraverse2(float const from[2], float const to[2], int flags, traverser
*/
int P_PathXYTraverse(float fromX, float fromY, float toX, float toY, int flags, traverser_t callback);
int P_PathXYTraverse2(float fromX, float fromY, float toX, float toY, int flags, traverser_t callback, void* paramaters);

boolean P_CheckLineSight(const float from[3], const float to[3], float bottomSlope,
float topSlope, int flags);

#endif
60 changes: 33 additions & 27 deletions doomsday/engine/portable/include/p_sight.h
@@ -1,35 +1,41 @@
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
/**
* @file p_sight.h
* Gamemap Line of Sight Testing. @ingroup map
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

/**
* de_play.h: Line of Sight Testing
*/
#ifndef LIBDENG_MAP_GAMEMAP_SIGHT_H
#define LIBDENG_MAP_GAMEMAP_SIGHT_H

#ifndef __DOOMSDAY_PLAY_SIGHT_H__
#define __DOOMSDAY_PLAY_SIGHT_H__
#include "gamemap.h"

boolean P_CheckLineSight(const float from[3], const float to[3],
float bottomSlope, float topSlope, int flags);
/**
* 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_*) @see lineSightFlags
*
* @return @c true if the traverser function returns @c true
* for all visited lines.
*/
boolean GameMap_CheckLineSight(GameMap* map, const float from[3], const float to[3],
float bottomSlope, float topSlope, int flags);

#endif
#endif /// LIBDENG_MAP_GAMEMAP_SIGHT_H
6 changes: 4 additions & 2 deletions doomsday/engine/portable/src/dam_main.c
Expand Up @@ -523,12 +523,14 @@ boolean DAM_AttemptMapLoad(const Uri* uri)
Z_FreeTags(PU_MAP, PU_PURGELEVEL - 1);

if(mapCache && dam->cachedMapFound)
{ // Attempt to load the cached map data.
{
// Attempt to load the cached map data.
if(loadMap(&map, dam))
loadedOK = true;
}
else
{ // Try a JIT conversion with the help of a plugin.
{
// Try a JIT conversion with the help of a plugin.
if(convertMap(&map, dam))
loadedOK = true;
}
Expand Down
36 changes: 23 additions & 13 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -2089,6 +2089,7 @@ void DD_SetInteger(int ddvalue, int parm)
void* DD_GetVariable(int ddvalue)
{
static uint valueU;
static float valueF;

switch(ddvalue)
{
Expand Down Expand Up @@ -2128,7 +2129,8 @@ void* DD_GetVariable(int ddvalue)
return &valueU;

case DD_TRACE_ADDRESS:
return &traceLOS;
/// @fixme Do not cast away const.
return (void*)P_TraceLOS();

case DD_TRANSLATIONTABLES_ADDRESS:
return translationTables;
Expand Down Expand Up @@ -2223,25 +2225,33 @@ void* DD_GetVariable(int ddvalue)
case DD_GAMETIC: {
static timespan_t fracTic;
fracTic = gameTime * TICSPERSEC;
return &fracTic;
}
case DD_OPENRANGE:
return &openrange;
return &fracTic; }

case DD_OPENRANGE: {
const TraceOpening* open = P_TraceOpening();
valueF = open->range;
return &valueF; }

case DD_OPENTOP:
return &opentop;
case DD_OPENTOP: {
const TraceOpening* open = P_TraceOpening();
valueF = open->top;
return &valueF; }

case DD_OPENBOTTOM:
return &openbottom;
case DD_OPENBOTTOM: {
const TraceOpening* open = P_TraceOpening();
valueF = open->bottom;
return &valueF; }

case DD_LOWFLOOR:
return &lowfloor;
case DD_LOWFLOOR: {
const TraceOpening* open = P_TraceOpening();
valueF = open->lowFloor;
return &valueF; }

case DD_NUMLUMPS: {
static int count;
count = F_LumpCount();
return &count;
}
return &count; }

default: break;
}

Expand Down

0 comments on commit fa9c500

Please sign in to comment.