]> git.pond.sub.org Git - empserver/commitdiff
Replace broken GCFx(), GCFy() by sctoff()
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 23 Aug 2008 18:04:10 +0000 (14:04 -0400)
committerMarkus Armbruster <armbru@pond.sub.org>
Wed, 27 Aug 2008 01:42:32 +0000 (21:42 -0400)
GCFx() and GCFy() pointlessly duplicate sctoff(), and don't work when
the argument is less than -WORLD_X or -WORLD_Y, respectively.  With
current code, this happens only for impractically tiny worlds:
emp_setbitmap() writes to bitmap[] out of bounds, and smashes the
heap.

src/lib/subs/getbit.c

index dcded930a8185db7285afab5cfc0d6cd1ea7532b..8379ffcdaf469c02739f6ef2c002f7b2df935353 100644 (file)
@@ -114,25 +114,22 @@ static int *bitmaps[5] = {
     bitmap4,
 };
 
-#define GCFx(x) ((x + WORLD_X) % WORLD_X)
-#define GCFy(y) ((y + WORLD_Y) % WORLD_Y)
-
 int
 emp_getbit(int x, int y, unsigned char *bitmap)
 {
-    int id;
-
-    id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
-    return bitmap[id / 8] & bit(id & 07);
+    int id = sctoff(x, y);
+    if (id < 0)
+       return 0;
+    return bitmap[id >> 3] & bit(id & 07);
 }
 
 void
 emp_setbit(int x, int y, unsigned char *bitmap)
 {
-    int id;
-
-    id = (GCFy(y)) * WORLD_X / 2 + GCFx(x) / 2;
-    bitmap[id / 8] |= bit(id & 07);
+    int id = sctoff(x, y);
+    if (id < 0)
+       return;
+    bitmap[id >> 3] |= bit(id & 07);
 }
 
 static void
@@ -145,8 +142,8 @@ emp_setbitmap(int x, int y, unsigned char *bitmap, int *bitmaps)
     for (mp = bitmaps; *mp != 9999;) {
        dx = x + *mp++;
        dy = y + *mp++;
-       id = (GCFy(dy)) * WORLD_X / 2 + GCFx(dx) / 2;
-       bitmap[id / 8] |= bit(id & 07);
+       id = sctoff(dx, dy);
+       bitmap[id >> 3] |= bit(id & 07);
     }
 }