From fb97415ee7762b02f114f3c4663c44e867531d0e Mon Sep 17 00:00:00 2001
From: Xinyuan Chen <xinyuan.c@yahoo.com>
Date: Thu, 30 May 2019 22:57:19 -0700
Subject: [PATCH] simplified getRandomTileWithAdjacent

---
 .../net/simon987/server/game/world/World.java | 29 ++++---------------
 1 file changed, 6 insertions(+), 23 deletions(-)

diff --git a/Server/src/main/java/net/simon987/server/game/world/World.java b/Server/src/main/java/net/simon987/server/game/world/World.java
index c3890da..3e73cb2 100644
--- a/Server/src/main/java/net/simon987/server/game/world/World.java
+++ b/Server/src/main/java/net/simon987/server/game/world/World.java
@@ -412,6 +412,8 @@ public class World implements MongoSerializable {
      */
     public Point getRandomTileWithAdjacent(int n, int tile) {
         int counter = 0;
+        int[] xPositions = {1, 0, -1, 0, 1, -1, 1, -1};
+        int[] yPositions = {0, 1, 0, -1, 1, 1, -1, -1};
         while (true) {
             counter++;
 
@@ -425,29 +427,10 @@ public class World implements MongoSerializable {
             if (rTile != null) {
                 int adjacentTiles = 0;
 
-                if (tileMap.isInBounds(rTile.x, rTile.y - 1) && !isTileBlocked(rTile.x, rTile.y - 1)) {
-                    adjacentTiles++;
-                }
-                if (tileMap.isInBounds(rTile.x + 1, rTile.y) && !isTileBlocked(rTile.x + 1, rTile.y)) {
-                    adjacentTiles++;
-                }
-                if (tileMap.isInBounds(rTile.x, rTile.y + 1) && !isTileBlocked(rTile.x, rTile.y + 1)) {
-                    adjacentTiles++;
-                }
-                if (tileMap.isInBounds(rTile.x - 1, rTile.y) && !isTileBlocked(rTile.x - 1, rTile.y)) {
-                    adjacentTiles++;
-                }
-                if (tileMap.isInBounds(rTile.x + 1, rTile.y + 1) && !isTileBlocked(rTile.x + 1, rTile.y + 1)) {
-                    adjacentTiles++;
-                }
-                if (tileMap.isInBounds(rTile.x - 1, rTile.y + 1) && !isTileBlocked(rTile.x - 1, rTile.y + 1)) {
-                    adjacentTiles++;
-                }
-                if (tileMap.isInBounds(rTile.x + 1, rTile.y - 1) && !isTileBlocked(rTile.x + 1, rTile.y - 1)) {
-                    adjacentTiles++;
-                }
-                if (tileMap.isInBounds(rTile.x - 1, rTile.y - 1) && !isTileBlocked(rTile.x - 1, rTile.y - 1)) {
-                    adjacentTiles++;
+                for (int idx = 0; idx < xPositions.length; idx++) {
+                    if (tileMap.isInBounds(rTile.x + xPositions[idx], rTile.y + yPositions[idx]) && !isTileBlocked(rTile.x + xPositions[idx], rTile.y + yPositions[idx])) {
+                        adjacentTiles++;
+                    }
                 }
 
                 if (adjacentTiles >= n) {