]> git.pond.sub.org Git - empserver/blobdiff - src/util/fairland.c
fairland: Track adjacent land sectors
[empserver] / src / util / fairland.c
index 712d808976bb761daba668d43b86c30b7512d753..563c2330a91fc6e24c0ffc33dbcf0f6a28d24e88 100644 (file)
@@ -188,6 +188,13 @@ static int diry[] = { 0, -1, -1, 0, 1, 1 };
 
 static int **own;              /* owner of the sector.  -1 means water */
 
+/*
+ * Adjacent land sectors
+ * adj_land[XYOFFSET(x, y)] bit d is set exactly when the sector next
+ * to x, y in direction d is land.
+ */
+static unsigned char *adj_land;
+
 /*
  * Exclusive zones
  * Each island is surrounded by an exclusive zone where only it may
@@ -494,6 +501,7 @@ allocate_memory(void)
     capx = calloc(nc, sizeof(int));
     capy = calloc(nc, sizeof(int));
     own = calloc(WORLD_X, sizeof(int *));
+    adj_land = malloc(WORLD_SZ() * sizeof(*adj_land));
     xzone = malloc(WORLD_SZ() * sizeof(*xzone));
     elev = calloc(WORLD_X, sizeof(int *));
     for (i = 0; i < WORLD_X; ++i) {
@@ -530,6 +538,7 @@ init(void)
            own[i][j] = -1;
        }
     }
+    memset(adj_land, 0, WORLD_SZ() * sizeof(*adj_land));
 }
 
 /****************************************************************************
@@ -766,6 +775,21 @@ can_grow_at(int c, int x, int y)
     return own[x][y] == -1 && xzone_ok(c, x, y);
 }
 
+static void
+adj_land_update(int x, int y)
+{
+    int dir, nx, ny, noff;
+
+    assert(own[x][y] != -1);
+
+    for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
+       nx = new_x(x + diroff[dir][0]);
+       ny = new_y(y + diroff[dir][1]);
+       noff = XYOFFSET(nx, ny);
+       adj_land[noff] |= 1u << DIR_BACK(dir);
+    }
+}
+
 static void
 add_sector(int c, int x, int y)
 {
@@ -775,28 +799,15 @@ add_sector(int c, int x, int y)
     secty[c][isecs[c]] = y;
     isecs[c]++;
     own[x][y] = c;
+    adj_land_update(x, y);
 }
 
 static int
-try_to_grow(int c, int newx, int newy, int extra_dist)
+try_to_grow(int c, int newx, int newy)
 {
-    int d = c < nc ? di : id;
-    int i, px, py;
-    struct hexagon_iter hexit;
-
     if (!can_grow_at(c, newx, newy))
        return 0;
 
-    for (i = 1; i <= extra_dist; i++) {
-       hexagon_first(&hexit, newx, newy, d + i, &px, &py);
-       do {
-           if (own[px][py] != -1 &&
-               own[px][py] != c &&
-               (DISTINCT_ISLANDS || own[px][py] < nc))
-               return 0;
-       } while (hexagon_next(&hexit, &px, &py));
-    }
-
     add_sector(c, newx, newy);
     return 1;
 }
@@ -876,14 +887,14 @@ grow_one_sector(int c)
                if (n > 5 ||
                    (own[new_x(x+dirx[(i+5)%6])][new_y(y+diry[(i+5)%6])] == -1 &&
                     own[new_x(x+dirx[(i+1)%6])][new_y(y+diry[(i+1)%6])] == -1))
-                   if (try_to_grow(c, newx, newy, 0))
+                   if (try_to_grow(c, newx, newy))
                        done = 1;
            }
        } else
            for (i = roll0(6), n = 0; n < 6 && !done; i = (i + 1) % 6, ++n) {
                newx = new_x(x + dirx[i]);
                newy = new_y(y + diry[i]);
-               if (try_to_grow(c, newx, newy, 0))
+               if (try_to_grow(c, newx, newy))
                    done = 1;
            }
        next_coast(c, x, y, &x, &y);
@@ -908,8 +919,8 @@ grow_continents(void)
 
     for (c = 0; c < nc; ++c) {
        isecs[c] = 0;
-       if (!try_to_grow(c, capx[c], capy[c], 0)
-           || !try_to_grow(c, new_x(capx[c] + 2), capy[c], 0)) {
+       if (!try_to_grow(c, capx[c], capy[c])
+           || !try_to_grow(c, new_x(capx[c] + 2), capy[c])) {
            done = 0;
            continue;
        }