]> git.pond.sub.org Git - empserver/commitdiff
(line_of_sight): Normalize coordinates before indexing. Closes
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 9 May 2004 15:54:00 +0000 (15:54 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 9 May 2004 15:54:00 +0000 (15:54 +0000)
#950514.

(line_of_sight): There's just one straight line between two points on
a plane, but on a torus there are four.  Code assumed plane, and thus
could screw up near the origin.  Pick a shortest line of the four.
Partial fix for #950510.

src/lib/commands/sona.c

index 40484bc7eccabe89edc52406cdae4edcd9617332..e6ccdbe3cd0d9a538d394560fd6964e9db4471a1 100644 (file)
@@ -332,10 +332,12 @@ plane_sona(struct emp_qelem *plane_list, int x, int y,
 #define DIST(ax,ay,bx,by) LEN(bx - ax, by -ay)
 
 int
-line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
+line_of_sight(char **rad, int ax, int ay, int bx, int by)
 {
-    int dx = bx - ax;
-    int dy = by - ay;
+    int dxn = XNORM(bx - ax);
+    int dyn = YNORM(by - ay);
+    int dx = dxn > WORLD_X / 2 ? dxn - WORLD_X : dxn;
+    int dy = dyn > WORLD_Y / 2 ? dyn - WORLD_Y : dyn;
     int dlen = LEN(dx, dy);
     int n;
     int cx = 0;
@@ -366,12 +368,13 @@ line_of_sight(s_char **rad, int ax, int ay, int bx, int by)
                closest = n;
            }
        }
-       if (closest < 0)        /* not possible */
+       if (CANT_HAPPEN(closest < 0))
            return 0;
        cx = cx + diroff[closest][0];
        cy = cy + diroff[closest][1];
        if (rad) {
-           blocked = (rad[ay + cy][ax + cx] != dchr[SCT_WATER].d_mnem);
+           blocked = (rad[YNORM(ay + cy)][XNORM(ax + cx)]
+                      != dchr[SCT_WATER].d_mnem);
        } else {
            sectp = getsectp((ax + cx), (ay + cy));
            if (sectp) {