Skip to content

Commit

Permalink
Infrastructure: make TRoom coordinates private (Mudlet#7539)
Browse files Browse the repository at this point in the history
#### Summary of PR Changes/Additions
Makes the coordinate members of the `TRoom` class private so that access
to them can be tracked via methods to set and get them.

#### Motivation for Adding to Mudlet
This is so that the setters can then subsequently include any extra code
that needs to be aware when the room is moved. I intend to improve the
detection of rooms being placed in the same position but realised this
would be a good preliminary step.

#### Additional Information (related issues, discussions, etc.)
Removes some dead code setting but not using `(int) quads` and `(int)
verts` in `(void) GLWidget::paintGL()`

Also using the mouse to drag and thus move selected rooms when those
rooms were on different levels would squash them all down to be on the
same z-coordinate as the "highlighted centre of the selection" room.
This is not as helpful it might seem and instead increased the
likelihood of causing room collisions - so now each room will retain
it's z coordinate if it is not on the same level as the centre of the
multiple room selection.

Also move code that likely needs to be run whenever rooms are
added/removed/moved within an area to a common block of code (`(void)
TArea::clean()`) to help keep things DRY. I intend to put code to update
a per area record of rooms that are in the same place within that block
in the future - so that the record can be reused without having to be
repeatedly recalculated, especially in the paint event for the 2D
mapper.

---------

Signed-off-by: Stephen Lyons <[email protected]>
  • Loading branch information
SlySven authored Dec 9, 2024
1 parent f28c984 commit dc97bd0
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 237 deletions.
216 changes: 121 additions & 95 deletions src/T2DMap.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/T2DMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Host;
class TArea;
class TMap;
class TRoom;
class MapInfoProperties;
struct MapInfoProperties;

class QCheckBox;
class QComboBox;
Expand Down
109 changes: 59 additions & 50 deletions src/TArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ QMap<int, QMap<int, QMultiMap<int, int>>> TArea::koordinatenSystem()
QList<TRoom*> const roomList = mpRoomDB->getRoomPtrList();
for (auto room : roomList) {
const int id = room->getId();
const int x = room->x;
const int y = room->y;
const int z = room->z;
const int x = room->x();
const int y = room->y();
const int z = room->z();
QMap<int, QMultiMap<int, int>> const _y;
QMultiMap<int, int> const _z;
if (!kS.contains(x)) {
Expand All @@ -104,7 +104,7 @@ QList<int> TArea::getRoomsByPosition(int x, int y, int z)
const int roomId = itAreaRoom.next();
TRoom* pR = mpRoomDB->getRoom(roomId);
if (pR) {
if (pR->x == x && pR->y == y && pR->z == z) {
if (pR->x() == x && pR->y() == y && pR->z() == z) {
dL.push_back(roomId);
}
}
Expand Down Expand Up @@ -332,9 +332,9 @@ void TArea::fast_calcSpan(int id)
return;
}

const int x = pR->x;
const int y = pR->y;
const int z = pR->z;
const int x = pR->x();
const int y = pR->y();
const int z = pR->z();
if (x > max_x) {
max_x = x;
}
Expand Down Expand Up @@ -389,72 +389,72 @@ void TArea::calcSpan()

if (!isFirstDone) {
// Only do this initialization for the first valid room
min_x = pR->x;
min_x = pR->x();
max_x = min_x;
min_y = pR->y * -1;
min_y = pR->y() * -1;
max_y = min_y;
min_z = pR->z;
min_z = pR->z();
max_z = min_z;
zLevels.push_back(pR->z);
xminForZ.insert(pR->z, pR->x);
xmaxForZ.insert(pR->z, pR->x);
yminForZ.insert(pR->z, pR->y);
ymaxForZ.insert(pR->z, pR->y);
zLevels.push_back(pR->z());
xminForZ.insert(pR->z(), pR->x());
xmaxForZ.insert(pR->z(), pR->x());
yminForZ.insert(pR->z(), pR->y());
ymaxForZ.insert(pR->z(), pR->y());
isFirstDone = true;
continue;
} else {
// Already had one valid room so now must check more things

if (!zLevels.contains(pR->z)) {
zLevels.push_back(pR->z);
if (!zLevels.contains(pR->z())) {
zLevels.push_back(pR->z());
}

if (!xminForZ.contains(pR->z)) {
xminForZ.insert(pR->z, pR->x);
} else if (pR->x < xminForZ.value(pR->z)) {
xminForZ.insert(pR->z, pR->x);
if (!xminForZ.contains(pR->z())) {
xminForZ.insert(pR->z(), pR->x());
} else if (pR->x() < xminForZ.value(pR->z())) {
xminForZ.insert(pR->z(), pR->x());
}

if (pR->x < min_x) {
min_x = pR->x;
if (pR->x() < min_x) {
min_x = pR->x();
}

if (!xmaxForZ.contains(pR->z)) {
xmaxForZ.insert(pR->z, pR->x);
} else if (pR->x > xmaxForZ.value(pR->z)) {
xmaxForZ.insert(pR->z, pR->x);
if (!xmaxForZ.contains(pR->z())) {
xmaxForZ.insert(pR->z(), pR->x());
} else if (pR->x() > xmaxForZ.value(pR->z())) {
xmaxForZ.insert(pR->z(), pR->x());
}

if (pR->x > max_x) {
max_x = pR->x;
if (pR->x() > max_x) {
max_x = pR->x();
}

if (!yminForZ.contains(pR->z)) {
yminForZ.insert(pR->z, (-1 * pR->y));
} else if ((-1 * pR->y) < yminForZ.value(pR->z)) {
yminForZ.insert(pR->z, (-1 * pR->y));
if (!yminForZ.contains(pR->z())) {
yminForZ.insert(pR->z(), (-1 * pR->y()));
} else if ((-1 * pR->y()) < yminForZ.value(pR->z())) {
yminForZ.insert(pR->z(), (-1 * pR->y()));
}

if ((-1 * pR->y) < min_y) {
min_y = (-1 * pR->y);
if ((-1 * pR->y()) < min_y) {
min_y = (-1 * pR->y());
}

if ((-1 * pR->y) > max_y) {
max_y = (-1 * pR->y);
if ((-1 * pR->y()) > max_y) {
max_y = (-1 * pR->y());
}

if (!ymaxForZ.contains(pR->z)) {
ymaxForZ.insert(pR->z, (-1 * pR->y));
} else if ((-1 * pR->y) > ymaxForZ.value(pR->z)) {
ymaxForZ.insert(pR->z, (-1 * pR->y));
if (!ymaxForZ.contains(pR->z())) {
ymaxForZ.insert(pR->z(), (-1 * pR->y()));
} else if ((-1 * pR->y()) > ymaxForZ.value(pR->z())) {
ymaxForZ.insert(pR->z(), (-1 * pR->y()));
}

if (pR->z < min_z) {
min_z = pR->z;
if (pR->z() < min_z) {
min_z = pR->z();
}

if (pR->z > max_z) {
max_z = pR->z;
if (pR->z() > max_z) {
max_z = pR->z();
}
}
}
Expand Down Expand Up @@ -487,15 +487,15 @@ void TArea::removeRoom(int room, bool deferAreaRecalculations)
if (pR) {
// Now see if the room is on an extreme - if it the only room on a
// particular z-coordinate it will be on all four
if (xminForZ.contains(pR->z) && xminForZ.value(pR->z) >= pR->x) {
if (xminForZ.contains(pR->z()) && xminForZ.value(pR->z()) >= pR->x()) {
isOnExtreme = true;
} else if (xmaxForZ.contains(pR->z) && xmaxForZ.value(pR->z) <= pR->x) {
} else if (xmaxForZ.contains(pR->z()) && xmaxForZ.value(pR->z()) <= pR->x()) {
isOnExtreme = true;
} else if (yminForZ.contains(pR->z) && yminForZ.value(pR->z) >= (-1 * pR->y)) {
} else if (yminForZ.contains(pR->z()) && yminForZ.value(pR->z()) >= (-1 * pR->y())) {
isOnExtreme = true;
} else if (ymaxForZ.contains(pR->z) && ymaxForZ.value(pR->z) <= (-1 * pR->y)) {
} else if (ymaxForZ.contains(pR->z()) && ymaxForZ.value(pR->z()) <= (-1 * pR->y())) {
isOnExtreme = true;
} else if (min_x >= pR->x || min_y >= (-1 * pR->y) || max_x <= pR->x || max_y <= (-1 * pR->y)) {
} else if (min_x >= pR->x() || min_y >= (-1 * pR->y()) || max_x <= pR->x() || max_y <= (-1 * pR->y())) {
isOnExtreme = true;
}
}
Expand Down Expand Up @@ -981,3 +981,12 @@ void TArea::set2DMapZoom(const qreal zoom)
mLast2DMapZoom = zoom;
}
}

void TArea::clean()
{
if (mIsDirty) {
determineAreaExits();
calcSpan();
mIsDirty = false;
}
}
1 change: 1 addition & 0 deletions src/TArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TArea
bool hasPermanentLabels() const;
qreal get2DMapZoom() const { return mLast2DMapZoom; }
void set2DMapZoom(const qreal zoom);
void clean();


QSet<int> rooms; // rooms of this area
Expand Down
6 changes: 3 additions & 3 deletions src/TAstar.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class distance_heuristic : public boost::astar_heuristic<Graph, CostType>
if (m_location[m_goal].pR->getArea() != m_location[u].pR->getArea()) {
return 1;
}
CostType dx = m_location[m_goal].pR->x - m_location[u].pR->x;
CostType dy = m_location[m_goal].pR->y - m_location[u].pR->y;
CostType dz = m_location[m_goal].pR->z - m_location[u].pR->z;
CostType dx = m_location[m_goal].pR->x() - m_location[u].pR->x();
CostType dy = m_location[m_goal].pR->y() - m_location[u].pR->y();
CostType dz = m_location[m_goal].pR->z() - m_location[u].pR->z();

return std::sqrt(dx * dx + dy * dy + dz * dz);
}
Expand Down
14 changes: 7 additions & 7 deletions src/TLuaInterpreterMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ int TLuaInterpreter::getCustomLines1(lua_State* L)
lua_pushnumber(L, pointL.at(k).y());
lua_settable(L, -3);
lua_pushinteger(L, 3);
lua_pushnumber(L, pR->z);
lua_pushnumber(L, pR->z());
lua_settable(L, -3);
lua_settable(L, -3); //customLines[direction]["points"][3 x coordinates]
}
Expand Down Expand Up @@ -339,9 +339,9 @@ int TLuaInterpreter::addCustomLine(lua_State* L)
(host.mpMap->mpRoomDB->getAreaNamesMap()).value(area), QString::number(area)));
}

x.append(static_cast<qreal>(pR_to->x));
y.append(static_cast<qreal>(pR_to->y));
z.append(pR->z);
x.append(static_cast<qreal>(pR_to->x()));
y.append(static_cast<qreal>(pR_to->y()));
z.append(pR->z());
} else if (lua_istable(L, 2)) {
lua_pushnil(L);
int i = 0; // Indexes groups of coordinates in the table
Expand Down Expand Up @@ -1988,9 +1988,9 @@ int TLuaInterpreter::getRoomCoordinates(lua_State* L)
lua_pushnil(L);
return 3;
} else {
lua_pushnumber(L, pR->x);
lua_pushnumber(L, pR->y);
lua_pushnumber(L, pR->z);
lua_pushnumber(L, pR->x());
lua_pushnumber(L, pR->y());
lua_pushnumber(L, pR->z());
return 3;
}
}
Expand Down
41 changes: 19 additions & 22 deletions src/TMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ bool TMap::setRoomCoordinates(int id, int x, int y, int z)
return false;
}

pR->x = x;
pR->y = y;
pR->z = z;
pR->setCoordinates(x, y, z);

setUnsaved(__func__);
return true;
Expand Down Expand Up @@ -225,9 +223,9 @@ QString TMap::connectExitStubByDirection(const int fromRoomId, const int dirType
const int ux = qRound(unitVector.x());
const int uy = qRound(unitVector.y());
const int uz = qRound(unitVector.z());
const int rx = pFromR->x;
const int ry = pFromR->y;
const int rz = pFromR->z;
const int rx = pFromR->x();
const int ry = pFromR->y();
const int rz = pFromR->z();
int dx = 0;
int dy = 0;
int dz = 0;
Expand All @@ -251,42 +249,42 @@ QString TMap::connectExitStubByDirection(const int fromRoomId, const int dirType
}

if (uz) {
dz = pToR->z - rz;
dz = pToR->z() - rz;
if (!compSign(dz, uz) || !dz) {
continue;
}

} else {
//to avoid lower/upper floors from stealing stubs
if (pToR->z != rz) {
if (pToR->z() != rz) {
continue;
}
}

if (ux) {
dx = pToR->x - rx;
dx = pToR->x() - rx;
if (!compSign(dx, ux) || !dx) {
//we do !dx pRto make sure we have a component in the desired direction
continue;
}

} else {
//to avoid rooms on same plane from stealing stubs
if (pToR->x != rx) {
if (pToR->x() != rx) {
continue;
}
}

if (uy) {
dy = pToR->y - ry;
dy = pToR->y() - ry;
//if the sign is the SAME here we keep it b/c we flip our y coordinate.
if (compSign(dy, uy) || !dy) {
continue;
}

} else {
//to avoid rooms on same plane from stealing stubs
if (pToR->y != ry) {
if (pToR->y() != ry) {
continue;
}
}
Expand Down Expand Up @@ -588,9 +586,7 @@ void TMap::audit()
QMapIterator<int, TArea*> itArea(mpRoomDB->getAreaMap());
while (itArea.hasNext()) {
itArea.next();
itArea.value()->determineAreaExits();
itArea.value()->calcSpan();
itArea.value()->mIsDirty = false;
itArea.value()->clean();
}

{ // Blocked - just to limit the scope of infoMsg...!
Expand All @@ -606,6 +602,7 @@ void TMap::audit()
}
}

// This may be duplicating TArea class functionality:
QList<int> TMap::detectRoomCollisions(int id)
{
QList<int> collList;
Expand All @@ -614,9 +611,9 @@ QList<int> TMap::detectRoomCollisions(int id)
return collList;
}
const int area = pR->getArea();
const int x = pR->x;
const int y = pR->y;
const int z = pR->z;
const int x = pR->x();
const int y = pR->y();
const int z = pR->z();
TArea* pA = mpRoomDB->getArea(area);
if (!pA) {
return collList;
Expand All @@ -629,7 +626,7 @@ QList<int> TMap::detectRoomCollisions(int id)
if (!pR) {
continue;
}
if (pR->x == x && pR->y == y && pR->z == z) {
if (pR->x() == x && pR->y() == y && pR->z() == z) {
collList.push_back(checkRoomId);
}
}
Expand Down Expand Up @@ -1304,9 +1301,9 @@ bool TMap::serialize(QDataStream& ofs, int saveVersion)
}
}
ofs << pR->getArea();
ofs << pR->x;
ofs << pR->y;
ofs << pR->z;
ofs << pR->x();
ofs << pR->y();
ofs << pR->z();
ofs << pR->getNorth();
ofs << pR->getNortheast();
ofs << pR->getEast();
Expand Down
Loading

0 comments on commit dc97bd0

Please sign in to comment.