OSDN Git Service

[Refactor] #40577 Separated calc_vertical_projection() from projection_path()
authorHourier <hourier@users.sourceforge.jp>
Thu, 20 Aug 2020 08:21:49 +0000 (17:21 +0900)
committerHourier <hourier@users.sourceforge.jp>
Thu, 20 Aug 2020 08:21:49 +0000 (17:21 +0900)
src/target/projection-path-calculator.c

index f6a32bc..55e544d 100644 (file)
@@ -6,6 +6,9 @@
 #include "system/floor-type-definition.h"
 
 typedef struct projection_path_type {
+    u16b *gp;
+    POSITION range;
+    BIT_FLAGS flag;
     POSITION y1;
     POSITION x1;
     POSITION y2;
@@ -32,8 +35,12 @@ typedef struct projection_path_type {
  */
 static u16b location_to_grid(POSITION y, POSITION x) { return 256 * y + x; }
 
-static projection_path_type *initialize_projection_path_type(projection_path_type *pp_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
+static projection_path_type *initialize_projection_path_type(
+    projection_path_type *pp_ptr, u16b *gp, POSITION range, BIT_FLAGS flag, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
 {
+    pp_ptr->gp = gp;
+    pp_ptr->range = range;
+    pp_ptr->flag = flag;
     pp_ptr->y1 = y1;
     pp_ptr->x1 = x1;
     pp_ptr->y2 = y2;
@@ -60,6 +67,66 @@ static void set_asxy(projection_path_type *pp_ptr)
     }
 }
 
+static bool calc_vertical_projection(player_type *player_ptr, projection_path_type *pp_ptr)
+{
+    if (pp_ptr->ay > pp_ptr->ax)
+        return FALSE;
+
+    pp_ptr->m = pp_ptr->ax * pp_ptr->ax * 2;
+    pp_ptr->y = pp_ptr->y1 + pp_ptr->sy;
+    pp_ptr->x = pp_ptr->x1;
+    pp_ptr->frac = pp_ptr->m;
+    if (pp_ptr->frac > pp_ptr->half) {
+        pp_ptr->x += pp_ptr->sx;
+        pp_ptr->frac -= pp_ptr->full;
+        pp_ptr->k++;
+    }
+
+    floor_type *floor_ptr = player_ptr->current_floor_ptr;
+    while (TRUE) {
+        pp_ptr->gp[pp_ptr->n++] = location_to_grid(pp_ptr->y, pp_ptr->x);
+        if ((pp_ptr->n + (pp_ptr->k >> 1)) >= pp_ptr->range)
+            break;
+
+        if (!(pp_ptr->flag & PROJECT_THRU)) {
+            if ((pp_ptr->x == pp_ptr->x2) && (pp_ptr->y == pp_ptr->y2))
+                break;
+        }
+
+        if (pp_ptr->flag & PROJECT_DISI) {
+            if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x))
+                break;
+        } else if (pp_ptr->flag & PROJECT_LOS) {
+            if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x))
+                break;
+        } else if (!(pp_ptr->flag & PROJECT_PATH)) {
+            if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT))
+                break;
+        }
+
+        if (pp_ptr->flag & PROJECT_STOP) {
+            if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0))
+                break;
+        }
+
+        if (!in_bounds(floor_ptr, pp_ptr->y, pp_ptr->x))
+            break;
+
+        if (pp_ptr->m) {
+            pp_ptr->frac += pp_ptr->m;
+            if (pp_ptr->frac > pp_ptr->half) {
+                pp_ptr->x += pp_ptr->sx;
+                pp_ptr->frac -= pp_ptr->full;
+                pp_ptr->k++;
+            }
+        }
+
+        pp_ptr->y += pp_ptr->sy;
+    }
+
+    return TRUE;
+}
+
 /*!
  * @brief \8en\93_\82©\82ç\8fI\93_\82Ö\82Ì\92¼\90ü\8co\98H\82ð\95Ô\82· /
  * Determine the path taken by a projection.
@@ -70,78 +137,24 @@ static void set_asxy(projection_path_type *pp_ptr)
  * @param x1 \8en\93_X\8dÀ\95W
  * @param y2 \8fI\93_Y\8dÀ\95W
  * @param x2 \8fI\93_X\8dÀ\95W
- * @param flg \83t\83\89\83OID
+ * @param fla\83t\83\89\83OID
  * @return \83\8a\83X\83g\82Ì\92·\82³
  */
-int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flg)
+int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flag)
 {
     if ((x1 == x2) && (y1 == y2))
         return 0;
 
     projection_path_type tmp_projection_path;
-    projection_path_type *pp_ptr = initialize_projection_path_type(&tmp_projection_path, y1, x1, y2, x2);
+    projection_path_type *pp_ptr = initialize_projection_path_type(&tmp_projection_path, gp, range, flag, y1, x1, y2, x2);
     set_asxy(pp_ptr);
     pp_ptr->half = pp_ptr->ay * pp_ptr->ax;
     pp_ptr->full = pp_ptr->half << 1;
     pp_ptr->n = 0;
     pp_ptr->k = 0;
 
-    /* Vertical */
-    if (pp_ptr->ay > pp_ptr->ax) {
-        pp_ptr->m = pp_ptr->ax * pp_ptr->ax * 2;
-        pp_ptr->y = y1 + pp_ptr->sy;
-        pp_ptr->x = x1;
-        pp_ptr->frac = pp_ptr->m;
-        if (pp_ptr->frac > pp_ptr->half) {
-            pp_ptr->x += pp_ptr->sx;
-            pp_ptr->frac -= pp_ptr->full;
-            pp_ptr->k++;
-        }
-
-        floor_type *floor_ptr = player_ptr->current_floor_ptr;
-        while (TRUE) {
-            gp[pp_ptr->n++] = location_to_grid(pp_ptr->y, pp_ptr->x);
-            if ((pp_ptr->n + (pp_ptr->k >> 1)) >= range)
-                break;
-
-            if (!(flg & PROJECT_THRU)) {
-                if ((pp_ptr->x == x2) && (pp_ptr->y == y2))
-                    break;
-            }
-
-            if (flg & PROJECT_DISI) {
-                if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x))
-                    break;
-            } else if (flg & PROJECT_LOS) {
-                if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x))
-                    break;
-            } else if (!(flg & PROJECT_PATH)) {
-                if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT))
-                    break;
-            }
-
-            if (flg & PROJECT_STOP) {
-                if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0))
-                    break;
-            }
-
-            if (!in_bounds(floor_ptr, pp_ptr->y, pp_ptr->x))
-                break;
-
-            if (pp_ptr->m) {
-                pp_ptr->frac += pp_ptr->m;
-                if (pp_ptr->frac > pp_ptr->half) {
-                    pp_ptr->x += pp_ptr->sx;
-                    pp_ptr->frac -= pp_ptr->full;
-                    pp_ptr->k++;
-                }
-            }
-
-            pp_ptr->y += pp_ptr->sy;
-        }
-
+    if (calc_vertical_projection(player_ptr, pp_ptr))
         return pp_ptr->n;
-    }
 
     /* Horizontal */
     if (pp_ptr->ax > pp_ptr->ay) {
@@ -161,23 +174,23 @@ int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION
             if ((pp_ptr->n + (pp_ptr->k >> 1)) >= range)
                 break;
 
-            if (!(flg & (PROJECT_THRU))) {
+            if (!(flag & (PROJECT_THRU))) {
                 if ((pp_ptr->x == x2) && (pp_ptr->y == y2))
                     break;
             }
 
-            if (flg & (PROJECT_DISI)) {
+            if (flag & (PROJECT_DISI)) {
                 if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x))
                     break;
-            } else if (flg & (PROJECT_LOS)) {
+            } else if (flag & (PROJECT_LOS)) {
                 if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x))
                     break;
-            } else if (!(flg & (PROJECT_PATH))) {
+            } else if (!(flag & (PROJECT_PATH))) {
                 if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT))
                     break;
             }
 
-            if (flg & (PROJECT_STOP)) {
+            if (flag & (PROJECT_STOP)) {
                 if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0))
                     break;
             }
@@ -209,23 +222,23 @@ int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION
         if ((pp_ptr->n + (pp_ptr->n >> 1)) >= range)
             break;
 
-        if (!(flg & PROJECT_THRU)) {
+        if (!(flag & PROJECT_THRU)) {
             if ((pp_ptr->x == x2) && (pp_ptr->y == y2))
                 break;
         }
 
-        if (flg & PROJECT_DISI) {
+        if (flag & PROJECT_DISI) {
             if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x))
                 break;
-        } else if (flg & PROJECT_LOS) {
+        } else if (flag & PROJECT_LOS) {
             if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x))
                 break;
-        } else if (!(flg & PROJECT_PATH)) {
+        } else if (!(flag & PROJECT_PATH)) {
             if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT))
                 break;
         }
 
-        if (flg & PROJECT_STOP) {
+        if (flag & PROJECT_STOP) {
             if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0))
                 break;
         }