Navigation Menu

Skip to content

Commit

Permalink
Refactor|Map: Encapsulated map element blockmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jun 14, 2013
1 parent e9d660c commit 6336a25
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 69 deletions.
62 changes: 22 additions & 40 deletions doomsday/client/include/world/map.h
Expand Up @@ -231,6 +231,26 @@ class Map

inline int bspLeafCount() const { return bspLeafs().count(); }

/**
* Provides access to the mobj blockmap.
*/
Blockmap const *mobjBlockmap() const;

/**
* Provides access to the polyobj blockmap.
*/
Blockmap const *polyobjBlockmap() const;

/**
* Provides access to the line blockmap.
*/
Blockmap const *lineBlockmap() const;

/**
* Provides access to the BSP leaf blockmap.
*/
Blockmap const *bspLeafBlockmap() const;

/**
* Given an @a emitter origin, attempt to identify the map element
* to which it belongs.
Expand Down Expand Up @@ -281,26 +301,6 @@ class Map
*/
BspLeaf *bspLeafAtPoint_FixedPrecision(Vector2d const &point) const;

/**
* Provides access to the mobj blockmap.
*/
Blockmap const *mobjBlockmap() const;

/**
* Provides access to the polyobj blockmap.
*/
Blockmap const *polyobjBlockmap() const;

/**
* Provides access to the line blockmap.
*/
Blockmap const *lineBlockmap() const;

/**
* Provides access to the BSP leaf blockmap.
*/
Blockmap const *bspLeafBlockmap() const;

int mobjsBoxIterator(AABoxd const &box,
int (*callback) (struct mobj_s *, void *), void *parameters = 0) const;

Expand Down Expand Up @@ -430,32 +430,14 @@ class Map
*/
int unlink(struct mobj_s &mobj);

/**
* Link the specified @a mobj in any internal data structures for
* bookkeeping purposes. Should be called AFTER mobj translation to
* (re-)insert the mobj.
*
* @param mobj Mobj to be linked (must be currently unlinked).
*/
void linkMobjInBlockmap(struct mobj_s &mobj);

/**
* Unlink the specified @a mobj from any internal data structures for
* bookkeeping purposes. Should be called BEFORE mobj translation to
* extract the mobj.
*
* @param mobj Mobj to be unlinked.
*/
bool unlinkMobjInBlockmap(struct mobj_s &mobj);

/**
* Link the specified @a polyobj in any internal data structures for
* bookkeeping purposes. Should be called AFTER Polyobj rotation and/or
* translation to (re-)insert the polyobj.
*
* @param polyobj Polyobj to be linked.
*/
void linkPolyobjInBlockmap(Polyobj &polyobj);
void link(Polyobj &polyobj);

/**
* Unlink the specified @a polyobj from any internal data structures for
Expand All @@ -464,7 +446,7 @@ class Map
*
* @param polyobj Polyobj to be unlinked.
*/
void unlinkPolyobjInBlockmap(Polyobj &polyobj);
void unlink(Polyobj &polyobj);

EntityDatabase &entityDatabase() const;

Expand Down
78 changes: 51 additions & 27 deletions doomsday/client/src/world/map.cpp
Expand Up @@ -539,6 +539,32 @@ DENG2_PIMPL(Map)
#undef BLOCKMAP_MARGIN
}

/**
* Unlink the specified @a mobj from any internal data structures for
* bookkeeping purposes. Should be called BEFORE mobj translation to
* extract the mobj.
*
* @param mobj Mobj to be unlinked.
*/
bool unlinkMobjInBlockmap(mobj_t &mo)
{
Blockmap::Cell cell = mobjBlockmap->toCell(mo.origin);
return mobjBlockmap->unlink(cell, &mo);
}

/**
* Link the specified @a mobj in any internal data structures for
* bookkeeping purposes. Should be called AFTER mobj translation to
* (re-)insert the mobj.
*
* @param mobj Mobj to be linked (must be currently unlinked).
*/
void linkMobjInBlockmap(mobj_t &mo)
{
Blockmap::Cell cell = mobjBlockmap->toCell(mo.origin);
mobjBlockmap->link(cell, &mo);
}

/**
* Unlinks the mobj from all the lines it's been linked to. Can be called
* without checking that the list does indeed contain lines.
Expand Down Expand Up @@ -669,6 +695,24 @@ DENG2_PIMPL(Map)
#undef BLOCKMAP_MARGIN
}

void unlinkPolyobjInBlockmap(Polyobj &polyobj)
{
Blockmap::CellBlock cellBlock = polyobjBlockmap->toCellBlock(polyobj.aaBox);
polyobjBlockmap->unlink(cellBlock, &polyobj);
}

void linkPolyobjInBlockmap(Polyobj &polyobj)
{
Blockmap::CellBlock cellBlock = polyobjBlockmap->toCellBlock(polyobj.aaBox);

Blockmap::Cell cell;
for(cell.y = cellBlock.min.y; cell.y <= cellBlock.max.y; ++cell.y)
for(cell.x = cellBlock.min.x; cell.x <= cellBlock.max.x; ++cell.x)
{
polyobjBlockmap->link(cell, &polyobj);
}
}

/**
* Construct an initial (empty) BSP leaf blockmap for "this" map.
*
Expand Down Expand Up @@ -1226,18 +1270,6 @@ Blockmap const *Map::bspLeafBlockmap() const
return d->bspLeafBlockmap.data();
}

void Map::linkMobjInBlockmap(mobj_t &mo)
{
Blockmap::Cell cell = d->mobjBlockmap->toCell(mo.origin);
d->mobjBlockmap->link(cell, &mo);
}

bool Map::unlinkMobjInBlockmap(mobj_t &mo)
{
Blockmap::Cell cell = d->mobjBlockmap->toCell(mo.origin);
return d->mobjBlockmap->unlink(cell, &mo);
}

struct bmapmoiterparams_t
{
int localValidCount;
Expand Down Expand Up @@ -1570,7 +1602,7 @@ int Map::unlink(mobj_t &mo)

if(Mobj_UnlinkFromSector(&mo))
links |= DDLINK_SECTOR;
if(unlinkMobjInBlockmap(mo))
if(d->unlinkMobjInBlockmap(mo))
links |= DDLINK_BLOCKMAP;
if(!d->unlinkMobjFromLines(mo))
links |= DDLINK_NOLINE;
Expand Down Expand Up @@ -1605,8 +1637,8 @@ void Map::link(mobj_t &mo, byte flags)
if(flags & DDLINK_BLOCKMAP)
{
// Unlink from the old block, if any.
unlinkMobjInBlockmap(mo);
linkMobjInBlockmap(mo);
d->unlinkMobjInBlockmap(mo);
d->linkMobjInBlockmap(mo);
}

// Link into lines.
Expand Down Expand Up @@ -1643,22 +1675,14 @@ void Map::link(mobj_t &mo, byte flags)
}
}

void Map::unlinkPolyobjInBlockmap(Polyobj &polyobj)
void Map::unlink(Polyobj &polyobj)
{
Blockmap::CellBlock cellBlock = d->polyobjBlockmap->toCellBlock(polyobj.aaBox);
d->polyobjBlockmap->unlink(cellBlock, &polyobj);
d->unlinkPolyobjInBlockmap(polyobj);
}

void Map::linkPolyobjInBlockmap(Polyobj &polyobj)
void Map::link(Polyobj &polyobj)
{
Blockmap::CellBlock cellBlock = d->polyobjBlockmap->toCellBlock(polyobj.aaBox);

Blockmap::Cell cell;
for(cell.y = cellBlock.min.y; cell.y <= cellBlock.max.y; ++cell.y)
for(cell.x = cellBlock.min.x; cell.x <= cellBlock.max.x; ++cell.x)
{
d->polyobjBlockmap->link(cell, &polyobj);
}
d->linkPolyobjInBlockmap(polyobj);
}

struct bmappoiterparams_t
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/world/polyobj.cpp
Expand Up @@ -135,15 +135,15 @@ void Polyobj::unlink()
_bspLeaf = 0;

/// @todo Do not assume polyobj is from the CURRENT map.
App_World().map().unlinkPolyobjInBlockmap(*this);
App_World().map().unlink(*this);
}
}

void Polyobj::link()
{
if(!_bspLeaf)
{
App_World().map().linkPolyobjInBlockmap(*this);
App_World().map().link(*this);

// Find the center point of the polyobj.
Vector2d avg;
Expand Down

0 comments on commit 6336a25

Please sign in to comment.