Core/Grid: Replaced ambiguous operator overloads with functions (Makes it clear to read)

This commit is contained in:
Spp
2011-10-18 11:42:40 +02:00
parent e5fb7f093f
commit 805e14a463
5 changed files with 59 additions and 45 deletions

View File

@@ -51,10 +51,10 @@ struct CellArea
void ResizeBorders(CellPair& begin_cell, CellPair& end_cell) const
{
begin_cell << left_offset;
begin_cell -= lower_offset;
end_cell >> right_offset;
end_cell += upper_offset;
begin_cell.dec_x(left_offset);
begin_cell.dec_y(lower_offset);
end_cell.inc_x(right_offset);
end_cell.inc_y(upper_offset);
}
int right_offset;

View File

@@ -50,7 +50,6 @@ Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor<T, CONTAINER> &v
}
// set up the cell range based on the district
// the overloaded operators handle range checking
CellPair begin_cell = standing_cell;
CellPair end_cell = standing_cell;
@@ -58,54 +57,54 @@ Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor<T, CONTAINER> &v
{
case ALL_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
end_cell >> 1; end_cell += 1; // lower right
begin_cell.dec_x(1); begin_cell.dec_y(1); // upper left
end_cell.inc_x(1); end_cell.inc_y(1); // lower right
break;
}
case UPPER_LEFT_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
begin_cell.dec_x(1); begin_cell.dec_y(1); // upper left
break;
}
case UPPER_RIGHT_DISTRICT:
{
begin_cell -= 1; // up
end_cell >> 1; // right
begin_cell.dec_y(1); // up
end_cell.inc_x(1); // right
break;
}
case LOWER_LEFT_DISTRICT:
{
begin_cell << 1; // left
end_cell += 1; // down
begin_cell.dec_x(1); // left
end_cell.inc_y(1); // down
break;
}
case LOWER_RIGHT_DISTRICT:
{
end_cell >> 1; end_cell += 1; // lower right
end_cell.inc_x(1); end_cell.inc_y(1); // lower right
break;
}
case LEFT_DISTRICT:
{
begin_cell -= 1; // up
end_cell >> 1; end_cell += 1; // lower right
begin_cell.dec_y(1); // up
end_cell.inc_x(1); end_cell.inc_y(1); // lower right
break;
}
case RIGHT_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
end_cell += 1; // down
begin_cell.dec_x(1); begin_cell.dec_y(1); // upper left
end_cell.inc_y(1); // down
break;
}
case UPPER_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
end_cell >> 1; // right
begin_cell.dec_x(1); begin_cell.dec_y(1); // upper left
end_cell.inc_x(1); // right
break;
}
case LOWER_DISTRICT:
{
begin_cell << 1; // left
end_cell >> 1; end_cell += 1; // lower right
begin_cell.dec_x(1); // left
end_cell.inc_x(1); end_cell.inc_y(1); // lower right
break;
}
default:

View File

@@ -74,18 +74,24 @@ typedef TypeMapContainer<AllWorldObjectTypes> WorldTypeMapContainer;
template<const unsigned int LIMIT>
struct CoordPair
{
CoordPair(uint32 x=0, uint32 y=0) : x_coord(x), y_coord(y) {}
CoordPair(const CoordPair<LIMIT> &obj) : x_coord(obj.x_coord), y_coord(obj.y_coord) {}
bool operator == (const CoordPair<LIMIT> &obj) const { return (obj.x_coord == x_coord && obj.y_coord == y_coord); }
bool operator != (const CoordPair<LIMIT> &obj) const { return !operator == (obj); }
CoordPair<LIMIT>& operator=(const CoordPair<LIMIT> &obj)
CoordPair(uint32 x=0, uint32 y=0)
: x_coord(x)
, y_coord(y)
{}
CoordPair(const CoordPair<LIMIT> &obj)
: x_coord(obj.x_coord)
, y_coord(obj.y_coord)
{}
CoordPair<LIMIT> & operator=(const CoordPair<LIMIT> &obj)
{
x_coord = obj.x_coord;
y_coord = obj.y_coord;
return *this;
}
void operator<<(const uint32 val)
void dec_x(uint32 val)
{
if (x_coord > val)
x_coord -= val;
@@ -93,15 +99,15 @@ struct CoordPair
x_coord = 0;
}
void operator>>(const uint32 val)
void inc_x(uint32 val)
{
if (x_coord+val < LIMIT)
if (x_coord + val < LIMIT)
x_coord += val;
else
x_coord = LIMIT - 1;
}
void operator-=(const uint32 val)
void dec_y(uint32 val)
{
if (y_coord > val)
y_coord -= val;
@@ -109,9 +115,9 @@ struct CoordPair
y_coord = 0;
}
void operator+=(const uint32 val)
void inc_y(uint32 val)
{
if (y_coord+val < LIMIT)
if (y_coord + val < LIMIT)
y_coord += val;
else
y_coord = LIMIT - 1;
@@ -121,6 +127,18 @@ struct CoordPair
uint32 y_coord;
};
template<const unsigned int LIMIT>
bool operator==(const CoordPair<LIMIT> &p1, const CoordPair<LIMIT> &p2)
{
return (p1.x_coord == p2.x_coord && p1.y_coord == p2.y_coord);
}
template<const unsigned int LIMIT>
bool operator!=(const CoordPair<LIMIT> &p1, const CoordPair<LIMIT> &p2)
{
return !(p1 == p2);
}
typedef CoordPair<MAX_NUMBER_OF_GRIDS> GridPair;
typedef CoordPair<TOTAL_NUMBER_OF_CELLS_PER_MAP> CellPair;

View File

@@ -87,15 +87,13 @@ class NGrid
const GridType& operator()(unsigned short x, unsigned short y) const
{
ASSERT(x < N);
ASSERT(y < N);
ASSERT(x < N && y < N);
return i_cells[x][y];
}
GridType& operator()(unsigned short x, unsigned short y)
{
ASSERT(x < N);
ASSERT(y < N);
ASSERT(x < N && y < N);
return i_cells[x][y];
}
@@ -168,8 +166,7 @@ class NGrid
GridType& getGridType(const uint32 x, const uint32 y)
{
ASSERT(x < N);
ASSERT(y < N);
ASSERT(x < N && y < N);
return i_cells[x][y];
}

View File

@@ -815,7 +815,7 @@ void Map::AddCreatureToMoveList(Creature* c, float x, float y, float z, float an
if (_creatureToMoveLock) //can this happen?
return;
if(c->_moveState == CREATURE_CELL_MOVE_NONE)
if (c->_moveState == CREATURE_CELL_MOVE_NONE)
_creaturesToMove.push_back(c);
c->SetNewCellPosition(x, y, z, ang);
}
@@ -825,7 +825,7 @@ void Map::RemoveCreatureFromMoveList(Creature* c)
if (_creatureToMoveLock) //can this happen?
return;
if(c->_moveState == CREATURE_CELL_MOVE_ACTIVE)
if (c->_moveState == CREATURE_CELL_MOVE_ACTIVE)
c->_moveState = CREATURE_CELL_MOVE_INACTIVE;
}
@@ -952,7 +952,7 @@ bool Map::CreatureRespawnRelocation(Creature* c, bool diffGridOnly)
Cell resp_cell(resp_val);
//creature will be unloaded with grid
if(diffGridOnly && !c->GetCurrentCell().DiffGrid(resp_cell))
if (diffGridOnly && !c->GetCurrentCell().DiffGrid(resp_cell))
return true;
c->CombatStop();
@@ -2142,10 +2142,10 @@ bool Map::ActiveObjectsNearGrid(uint32 x, uint32 y) const
float viewDist = GetVisibilityRange();
int cell_range = (int)ceilf(viewDist / SIZE_OF_GRID_CELL) + 1;
cell_min << cell_range;
cell_min -= cell_range;
cell_max >> cell_range;
cell_max += cell_range;
cell_min.dec_x(cell_range);
cell_min.dec_y(cell_range);
cell_max.inc_x(cell_range);
cell_max.inc_y(cell_range);
for (MapRefManager::const_iterator iter = m_mapRefManager.begin(); iter != m_mapRefManager.end(); ++iter)
{