[Scummvm-cvs-logs] CVS: scummvm/scumm boxes.cpp,1.79,1.80

Max Horn fingolfin at users.sourceforge.net
Tue Sep 28 16:38:22 CEST 2004


Update of /cvsroot/scummvm/scummvm/scumm
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13566

Modified Files:
	boxes.cpp 
Log Message:
Cleanup, add some comments to ScummEngine::areBoxesNeighbours

Index: boxes.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/scumm/boxes.cpp,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -d -r1.79 -r1.80
--- boxes.cpp	28 Sep 2004 23:05:45 -0000	1.79
+++ boxes.cpp	28 Sep 2004 23:30:50 -0000	1.80
@@ -1028,9 +1028,7 @@
 
 /** 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 @@
 	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 @@
 						(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 @@
 			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 @@
 		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) {





More information about the Scummvm-git-logs mailing list