aboutsummaryrefslogtreecommitdiff
path: root/scumm/boxes.cpp
diff options
context:
space:
mode:
authorMax Horn2004-09-28 23:30:50 +0000
committerMax Horn2004-09-28 23:30:50 +0000
commit16549bef3a6fbccb1865d9f6cc1001aecfca63af (patch)
treeb75e984d25813a5ed8f2d3607af9318b70e08642 /scumm/boxes.cpp
parentad3062ab933d404c9ac32579f4c8dfc2f717964a (diff)
downloadscummvm-rg350-16549bef3a6fbccb1865d9f6cc1001aecfca63af.tar.gz
scummvm-rg350-16549bef3a6fbccb1865d9f6cc1001aecfca63af.tar.bz2
scummvm-rg350-16549bef3a6fbccb1865d9f6cc1001aecfca63af.zip
Cleanup, add some comments to ScummEngine::areBoxesNeighbours
svn-id: r15336
Diffstat (limited to 'scumm/boxes.cpp')
-rw-r--r--scumm/boxes.cpp87
1 files changed, 42 insertions, 45 deletions
diff --git a/scumm/boxes.cpp b/scumm/boxes.cpp
index a5db93ac86..4a33354fc4 100644
--- a/scumm/boxes.cpp
+++ b/scumm/boxes.cpp
@@ -1028,9 +1028,7 @@ void ScummEngine::createBoxMatrix() {
/** Check if two boxes are neighbours. */
bool ScummEngine::areBoxesNeighbours(int box1nr, int box2nr) {
- int j, k, m, n;
int tmp_x, tmp_y;
- bool result;
BoxCoords box;
BoxCoords box2;
@@ -1040,51 +1038,52 @@ bool ScummEngine::areBoxesNeighbours(int box1nr, int box2nr) {
getBoxCoordinates(box1nr, &box2);
getBoxCoordinates(box2nr, &box);
- result = false;
- j = 4;
-
- do {
- k = 4;
- do {
- if (box2.ur.x == box2.ul.x && box.ul.x == box2.ul.x && box.ur.x == box2.ur.x) {
- n = m = 0;
+ // Roughly, the idea of this algorithm is to check if we can find
+ // two sides of the two given boxes which touch.
+ // In order to keep te code simple, we only match the upper sides;
+ // then, we "rotate" the box coordinates four times each, for a total
+ // of 16 comparisions (four sides compared with four sides each).
+ for (int j = 0; j < 4; j++) {
+ for (int k = 0; k < 4; k++) {
+ // Are the "upper" sides of the boxes on a single vertical line
+ // (i.e. all share one x value) ?
+ if (box2.ur.x == box2.ul.x && box.ul.x == box2.ul.x && box.ur.x == box2.ul.x) {
+ bool swappedBox2 = false, swappedBox1 = false;
if (box2.ur.y < box2.ul.y) {
- n = 1;
+ swappedBox2 = 1;
SWAP(box2.ur.y, box2.ul.y);
}
if (box.ur.y < box.ul.y) {
- m = 1;
+ swappedBox1 = 1;
SWAP(box.ur.y, box.ul.y);
}
if (box.ur.y < box2.ul.y ||
box.ul.y > box2.ur.y ||
(box.ul.y == box2.ur.y ||
box.ur.y == box2.ul.y) && box2.ur.y != box2.ul.y && box.ul.y != box.ur.y) {
- if (n) {
- SWAP(box2.ur.y, box2.ul.y);
- }
- if (m) {
- SWAP(box.ur.y, box.ul.y);
- }
} else {
- if (n) {
- SWAP(box2.ur.y, box2.ul.y);
- }
- if (m) {
- SWAP(box.ur.y, box.ul.y);
- }
- result = true;
+ return true;
+ }
+
+ // Swap back if necessary
+ if (swappedBox2) {
+ SWAP(box2.ur.y, box2.ul.y);
+ }
+ if (swappedBox1) {
+ SWAP(box.ur.y, box.ul.y);
}
}
- if (box2.ur.y == box2.ul.y && box.ul.y == box2.ul.y && box.ur.y == box2.ur.y) {
- n = m = 0;
+ // Are the "upper" sides of the boxes on a single horizontal line
+ // (i.e. all share one y value) ?
+ if (box2.ur.y == box2.ul.y && box.ul.y == box2.ul.y && box.ur.y == box2.ul.y) {
+ bool swappedBox2 = false, swappedBox1 = false;
if (box2.ur.x < box2.ul.x) {
- n = 1;
+ swappedBox2 = 1;
SWAP(box2.ur.x, box2.ul.x);
}
if (box.ur.x < box.ul.x) {
- m = 1;
+ swappedBox1 = 1;
SWAP(box.ur.x, box.ul.x);
}
if (box.ur.x < box2.ul.x ||
@@ -1092,23 +1091,20 @@ bool ScummEngine::areBoxesNeighbours(int box1nr, int box2nr) {
(box.ul.x == box2.ur.x ||
box.ur.x == box2.ul.x) && box2.ur.x != box2.ul.x && box.ul.x != box.ur.x) {
- if (n) {
- SWAP(box2.ur.x, box2.ul.x);
- }
- if (m) {
- SWAP(box.ur.x, box.ul.x);
- }
} else {
- if (n) {
- SWAP(box2.ur.x, box2.ul.x);
- }
- if (m) {
- SWAP(box.ur.x, box.ul.x);
- }
- result = true;
+ return true;
+ }
+
+ // Swap back if necessary
+ if (swappedBox2) {
+ SWAP(box2.ur.x, box2.ul.x);
+ }
+ if (swappedBox1) {
+ SWAP(box.ur.x, box.ul.x);
}
}
+ // "Rotate" the box coordinates
tmp_x = box2.ul.x;
tmp_y = box2.ul.y;
box2.ul.x = box2.ur.x;
@@ -1119,8 +1115,9 @@ bool ScummEngine::areBoxesNeighbours(int box1nr, int box2nr) {
box2.lr.y = box2.ll.y;
box2.ll.x = tmp_x;
box2.ll.y = tmp_y;
- } while (--k);
+ }
+ // "Rotate" the box coordinates
tmp_x = box.ul.x;
tmp_y = box.ul.y;
box.ul.x = box.ur.x;
@@ -1131,9 +1128,9 @@ bool ScummEngine::areBoxesNeighbours(int box1nr, int box2nr) {
box.lr.y = box.ll.y;
box.ll.x = tmp_x;
box.ll.y = tmp_y;
- } while (--j);
+ }
- return result;
+ return false;
}
void Actor::findPathTowardsOld(byte box1, byte box2, byte finalBox, Common::Point &p2, Common::Point &p3) {