]> granicus.if.org Git - nethack/commitdiff
Split coord truncate-to-map into separate function
authorPasi Kallinen <paxed@alt.org>
Thu, 1 Jul 2021 11:29:04 +0000 (14:29 +0300)
committerPasi Kallinen <paxed@alt.org>
Thu, 1 Jul 2021 11:29:04 +0000 (14:29 +0300)
src/do_name.c

index 3f8b739122855a456290d2cd197821919015ad10..4ab872d6fdb1a2a07f53253a960824d52f29d716 100644 (file)
@@ -17,6 +17,7 @@ static void gloc_filter_done(void);
 static boolean gather_locs_interesting(int, int, int);
 static void gather_locs(coord **, int *, int);
 static void auto_describe(int, int);
+static void truncate_to_map(int *, int *, schar, schar);
 static void do_mgivenname(void);
 static boolean alreadynamed(struct monst *, char *, char *);
 static void do_oname(struct obj *);
@@ -623,6 +624,29 @@ getpos_menu(coord *ccp, int gloc)
     return (pick_cnt > 0);
 }
 
+/* add dx,dy to cx,cy, truncating at map edges */
+static void
+truncate_to_map(int *cx, int *cy, schar dx, schar dy)
+{
+    /* diagonal moves complicate this... */
+    if (*cx + dx < 1) {
+        dy -= sgn(dy) * (1 - (*cx + dx));
+        dx = 1 - *cx; /* so that (cx+dx == 1) */
+    } else if (*cx + dx > COLNO - 1) {
+        dy += sgn(dy) * ((COLNO - 1) - (*cx + dx));
+        dx = (COLNO - 1) - *cx;
+    }
+    if (*cy + dy < 0) {
+        dx -= sgn(dx) * (0 - (*cy + dy));
+        dy = 0 - *cy; /* so that (cy+dy == 0) */
+    } else if (*cy + dy > ROWNO - 1) {
+        dx += sgn(dx) * ((ROWNO - 1) - (*cy + dy));
+        dy = (ROWNO - 1) - *cy;
+    }
+    *cx += dx;
+    *cy += dy;
+}
+
 int
 getpos(coord *ccp, boolean force, const char *goal)
 {
@@ -758,23 +782,7 @@ getpos(coord *ccp, boolean force, const char *goal)
             } else
                 continue;
 
-            /* truncate at map edge; diagonal moves complicate this... */
-            if (cx + dx < 1) {
-                dy -= sgn(dy) * (1 - (cx + dx));
-                dx = 1 - cx; /* so that (cx+dx == 1) */
-            } else if (cx + dx > COLNO - 1) {
-                dy += sgn(dy) * ((COLNO - 1) - (cx + dx));
-                dx = (COLNO - 1) - cx;
-            }
-            if (cy + dy < 0) {
-                dx -= sgn(dx) * (0 - (cy + dy));
-                dy = 0 - cy; /* so that (cy+dy == 0) */
-            } else if (cy + dy > ROWNO - 1) {
-                dx += sgn(dx) * ((ROWNO - 1) - (cy + dy));
-                dy = (ROWNO - 1) - cy;
-            }
-            cx += dx;
-            cy += dy;
+            truncate_to_map(&cx, &cy, dx, dy);
             goto nxtc;
         }