Skip to content

Commit

Permalink
World: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Oct 6, 2013
1 parent 7af0914 commit 0a14f07
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 58 deletions.
21 changes: 13 additions & 8 deletions doomsday/client/include/world/bsp/partitioncost.h
Expand Up @@ -26,6 +26,8 @@
#ifndef DENG_WORLD_BSP_PARTITIONCOST_H
#define DENG_WORLD_BSP_PARTITIONCOST_H

#include <de/String>

#include "world/bsp/linesegment.h"

namespace de {
Expand All @@ -36,12 +38,6 @@ namespace bsp {
*/
struct PartitionCost
{
enum Side
{
Right,
Left
};

int total;
int splits;
int iffy;
Expand All @@ -58,14 +54,14 @@ struct PartitionCost

inline PartitionCost &addSegmentRight(LineSegmentSide const &seg)
{
if(seg.hasMapSide()) mapRight += 1;
if(seg.hasMapSide()) mapRight += 1;
else partRight += 1;
return *this;
}

inline PartitionCost &addSegmentLeft(LineSegmentSide const &seg)
{
if(seg.hasMapSide()) mapLeft += 1;
if(seg.hasMapSide()) mapLeft += 1;
else partLeft += 1;
return *this;
}
Expand Down Expand Up @@ -100,6 +96,15 @@ struct PartitionCost
{
return total < rhs.total;
}

String asText() const
{
return String("PartitionCost(Total= %1.%2; splits:%3, iffy:%4, near:%5, left:%6+%7, right:%8+%9)")
.arg(total / 100).arg(total % 100, 2, QChar('0'))
.arg(splits).arg(iffy).arg(nearMiss)
.arg(mapLeft).arg(partLeft)
.arg(mapRight).arg(partRight);
}
};

} // namespace bsp
Expand Down
4 changes: 3 additions & 1 deletion doomsday/client/include/world/p_objlink.h
Expand Up @@ -22,6 +22,8 @@

#ifdef __CLIENT__

#include "world/map.h"

class BspLeaf;
class Lumobj;

Expand All @@ -45,7 +47,7 @@ void R_InitObjlinkBlockmapForMap(de::Map &map);
* Initialize the object => BspLeaf contact lists, ready for linking to
* objects. To be called at the beginning of a new world frame.
*/
void R_InitForNewFrame();
void R_InitForNewFrame(de::Map &map);

/**
* To be called at the begining of a render frame to clear the objlink
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/world/bsp/partitioner.cpp
Expand Up @@ -157,7 +157,7 @@ DENG2_PIMPL(Partitioner)
{
foreach(Line *line, lines)
{
//if(!line.hasZeroLength()) Screened at a higher level.
//if(line.hasZeroLength()) Screened at a higher level.
// continue;

Sector *frontSec = line->frontSectorPtr();
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/world/map.cpp
Expand Up @@ -2883,7 +2883,7 @@ void Map::worldFrameBegins(World &world, bool resetNextViewer)
{
DENG2_ASSERT(&world.map() == this); // Sanity check.

/// @todo optimize: Use a de::BitField instead.
/// @todo optimize: Use a QBitArray instead.
foreach(BspLeaf *bspLeaf, d->bspLeafs)
{
bspLeaf->markVisible(false);
Expand All @@ -2905,7 +2905,7 @@ void Map::worldFrameBegins(World &world, bool resetNextViewer)
R_ClearObjlinksForFrame(); // Zeroes the links.

// Clear the objlinks.
R_InitForNewFrame();
R_InitForNewFrame(*this);

// Generate surface decorations for the frame.
if(useLightDecorations)
Expand Down
91 changes: 45 additions & 46 deletions doomsday/client/src/world/p_objlink.cpp
Expand Up @@ -19,24 +19,21 @@

#include <cmath>

#include "de_base.h"
#include "de_console.h"
#include "de_render.h"
#include "de_graphics.h"
//#include "de_misc.h"
#include "de_play.h"
#include "de_defs.h"
#include <de/memoryzone.h>
#include <de/vector1.h>

#include "Face"
#include "gridmap.h"

#include "world/map.h"
#include "world/p_object.h"
#include "BspLeaf"
#include "Surface"

#include "MaterialSnapshot"
#include "render/r_main.h" // validCount
#include "render/rend_main.h" // Rend_MapSurfaceMaterialSpec
#include "WallEdge"

#include "gridmap.h"

#include "world/p_objlink.h"

using namespace de;
Expand Down Expand Up @@ -226,13 +223,21 @@ class objlinkblockmap_t
return cellBlock;
}

/// @pre Coordinates held by @a blockXY are within valid range.
void link(GridmapCell cell, objlink_t *link)
/**
* @param objlink Object to be linked. Note that if linked object's origin
* lies outside the blockmap it will not be linked!
*/
void link(objlink_t *objlink)
{
if(!link) return;
if(!objlink) return;

bool outside;
GridmapCell cell = toCell(objlink->objectOrigin(), &outside);
if(outside) return;

CellData *block = data(cell, true/*can allocate a block*/);
link->nextInBlock = block->head;
block->head = link;
objlink->nextInBlock = block->head;
block->head = objlink;
}

// Clear all the contact list heads and spread flags.
Expand Down Expand Up @@ -305,7 +310,7 @@ static objcontact_t *contFirst, *contCursor;
// List of contacts for each BSP leaf.
static objcontactlist_t *bspLeafContacts;

static void spreadInBspLeaf(BspLeaf *bspLeaf, contactfinderparams_t *parms);
static void spreadInBspLeaf(BspLeaf &bspLeaf, contactfinderparams_t &parms);

static inline void linkContact(objcontact_t *con, objcontact_t **list, uint index)
{
Expand Down Expand Up @@ -465,9 +470,9 @@ static coord_t pointOnHEdgeSide(HEdge const &hedge, Vector2d const &point)
return V2d_PointOnLineSide(pointV1, fromOriginV1, directionV1);
}

static void maybeSpreadOverEdge(HEdge *hedge, contactfinderparams_t *parms)
static void maybeSpreadOverEdge(HEdge *hedge, contactfinderparams_t &parms)
{
DENG_ASSERT(hedge != 0 && parms != 0);
if(!hedge) return;

BspLeaf &leaf = hedge->face().mapElementAs<BspLeaf>();
SectorCluster &cluster = leaf.cluster();
Expand All @@ -490,16 +495,16 @@ static void maybeSpreadOverEdge(HEdge *hedge, contactfinderparams_t *parms)
AABoxd const &backLeafAABox = backLeaf.poly().aaBox();

// Is the leaf on the back side outside the origin's AABB?
if(backLeafAABox.maxX <= parms->objAABox.minX ||
backLeafAABox.minX >= parms->objAABox.maxX ||
backLeafAABox.maxY <= parms->objAABox.minY ||
backLeafAABox.minY >= parms->objAABox.maxY)
if(backLeafAABox.maxX <= parms.objAABox.minX ||
backLeafAABox.minX >= parms.objAABox.maxX ||
backLeafAABox.maxY <= parms.objAABox.minY ||
backLeafAABox.minY >= parms.objAABox.maxY)
return;

// Too far from the edge?
coord_t const length = (hedge->twin().origin() - hedge->origin()).length();
coord_t const distance = pointOnHEdgeSide(*hedge, parms->objlink->objectOrigin()) / length;
if(de::abs(distance) >= parms->objlink->objectRadius())
coord_t const distance = pointOnHEdgeSide(*hedge, parms.objlink->objectOrigin()) / length;
if(de::abs(distance) >= parms.objlink->objectRadius())
return;

// Do not spread if the sector on the back side is closed with no height.
Expand Down Expand Up @@ -572,9 +577,9 @@ static void maybeSpreadOverEdge(HEdge *hedge, contactfinderparams_t *parms)
backLeaf.setValidCount(validCount);

// Link up a new contact with the back BSP leaf.
linkObjToBspLeaf(backLeaf, parms->objlink);
linkObjToBspLeaf(backLeaf, parms.objlink);

spreadInBspLeaf(&backLeaf, parms);
spreadInBspLeaf(backLeaf, parms);
}

/**
Expand All @@ -586,17 +591,18 @@ static void maybeSpreadOverEdge(HEdge *hedge, contactfinderparams_t *parms)
*
* @return Always @c true. (This function is also used as an iterator.)
*/
static void spreadInBspLeaf(BspLeaf *bspLeaf, contactfinderparams_t *parms)
static void spreadInBspLeaf(BspLeaf &bspLeaf, contactfinderparams_t &parms)
{
if(!bspLeaf || !bspLeaf->hasCluster())
return;

HEdge *base = bspLeaf->poly().hedge();
HEdge *hedge = base;
do
if(bspLeaf.hasCluster())
{
maybeSpreadOverEdge(hedge, parms);
} while((hedge = &hedge->next()) != base);
HEdge *base = bspLeaf.poly().hedge();
HEdge *hedge = base;
do
{
maybeSpreadOverEdge(hedge, parms);

} while((hedge = &hedge->next()) != base);
}
}

/**
Expand All @@ -621,7 +627,7 @@ static void findContacts(objlink_t *link)
// Always contact the obj's own BspLeaf.
linkObjToBspLeaf(bspLeaf, link);

spreadInBspLeaf(&bspLeaf, &parms);
spreadInBspLeaf(bspLeaf, parms);
}

/**
Expand Down Expand Up @@ -683,23 +689,17 @@ void R_LinkObjs()
// Link object-links into the relevant blockmap.
for(objlink_t *link = objlinks; link; link = link->next)
{
objlinkblockmap_t &bmap = blockmap(link->type());
bool clamped;
GridmapCell cell = bmap.toCell(link->objectOrigin(), &clamped);
if(!clamped)
{
bmap.link(cell, link);
}
blockmap(link->type()).link(link);
}
}

void R_InitForNewFrame()
void R_InitForNewFrame(Map &map)
{
// Start reusing nodes from the first one in the list.
contCursor = contFirst;
if(bspLeafContacts)
{
std::memset(bspLeafContacts, 0, App_World().map().bspLeafCount() * sizeof *bspLeafContacts);
std::memset(bspLeafContacts, 0, map.bspLeafCount() * sizeof *bspLeafContacts);
}
}

Expand Down Expand Up @@ -729,7 +729,6 @@ int R_IterateBspLeafLumobjContacts(BspLeaf &bspLeaf,

void R_ObjlinkCreate(mobj_t &mobj)
{
if(!Mobj_IsLinked(mobj)) return;
createObjlink(Mobj_BspLeafAtOrigin(mobj), &mobj, OT_MOBJ);
}

Expand Down

0 comments on commit 0a14f07

Please sign in to comment.