From 1386492c45cc9c5887066dfa3157941e778d7194 Mon Sep 17 00:00:00 2001 From: iks Date: Sat, 16 Feb 2013 05:08:09 +0000 Subject: [PATCH] Travel algorithm is improved for avoiding traps, lavas, waters. --- src/cmd1.c | 5 +++- src/cmd2.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++-------------- src/xtra2.c | 2 +- 3 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/cmd1.c b/src/cmd1.c index e33f81ceb..a249bcf2a 100644 --- a/src/cmd1.c +++ b/src/cmd1.c @@ -954,7 +954,7 @@ void carry(bool pickup) next_o_idx = o_ptr->next_o_idx; /* Hack -- disturb */ - disturb(0, 1); + disturb(0, 0); /* Pick up gold */ if (o_ptr->tval == TV_GOLD) @@ -5154,5 +5154,8 @@ void travel_step(void) travel.run = 0; else if (travel.run > 0) travel.run--; + + /* Travel Delay */ + Term_xtra(TERM_XTRA_DELAY, delay_factor); } #endif diff --git a/src/cmd2.c b/src/cmd2.c index 19763f55f..fa018e1e8 100644 --- a/src/cmd2.c +++ b/src/cmd2.c @@ -4786,27 +4786,61 @@ void forget_travel_flow(void) for (x = 0; x < cur_wid; x++) { /* Forget the old data */ - travel.cost[y][x] = TRAVEL_UNABLE; + travel.cost[y][x] = MAX_SHORT; } } } -static bool travel_flow_aux(int y, int x, int n, bool wall) +static int travel_flow_cost(int y, int x) +{ + feature_type *f_ptr = &f_info[cave[y][x].feat]; + int cost = 1; + + /* Avoid obstacles (ex. trees) */ + if (have_flag(f_ptr->flags, FF_AVOID_RUN)) cost += 1; + + /* Water */ + if (have_flag(f_ptr->flags, FF_WATER)) + { + if (have_flag(f_ptr->flags, FF_DEEP) && !p_ptr->levitation) cost += 5; + } + + /* Lava */ + if (have_flag(f_ptr->flags, FF_LAVA)) + { + int lava = 2; + if (!p_ptr->resist_fire) lava *= 2; + if (!p_ptr->levitation) lava *= 2; + if (have_flag(f_ptr->flags, FF_DEEP)) lava *= 2; + + cost += lava; + } + + /* Detected traps and doors */ + if (cave[y][x].info & (CAVE_MARK)) + { + if (have_flag(f_ptr->flags, FF_DOOR)) cost += 1; + if (have_flag(f_ptr->flags, FF_TRAP)) cost += 10; + } + + return (cost); +} + +static void travel_flow_aux(int y, int x, int n, bool wall) { cave_type *c_ptr = &cave[y][x]; feature_type *f_ptr = &f_info[c_ptr->feat]; int old_head = flow_head; - - n = n % TRAVEL_UNABLE; + int add_cost = 1; + int base_cost = (n % TRAVEL_UNABLE); + int from_wall = (n / TRAVEL_UNABLE); + int cost; /* Ignore out of bounds */ - if (!in_bounds(y, x)) return wall; - - /* Ignore "pre-stamped" entries */ - if (travel.cost[y][x] != TRAVEL_UNABLE) return wall; + if (!in_bounds(y, x)) return; /* Ignore unknown grid */ - if (!(c_ptr->info & CAVE_KNOWN)) return wall; + if (!(c_ptr->info & CAVE_KNOWN)) return; /* Ignore "walls" and "rubble" (include "secret doors") */ if (have_flag(f_ptr->flags, FF_WALL) || @@ -4814,16 +4848,21 @@ static bool travel_flow_aux(int y, int x, int n, bool wall) (have_flag(f_ptr->flags, FF_DOOR) && cave[y][x].mimic) || (!have_flag(f_ptr->flags, FF_MOVE) && have_flag(f_ptr->flags, FF_CAN_FLY) && !p_ptr->levitation)) { - if (!wall) return wall; + if (!wall || !from_wall) return; + add_cost += TRAVEL_UNABLE; } else { - wall = FALSE; + add_cost = travel_flow_cost(y, x); } + cost = base_cost + add_cost; + + /* Ignore lower cost entries */ + if (travel.cost[y][x] <= cost) return; + /* Save the flow cost */ - travel.cost[y][x] = n; - if (wall) travel.cost[y][x] += TRAVEL_UNABLE; + travel.cost[y][x] = cost; /* Enqueue that entry */ temp2_y[flow_head] = y; @@ -4835,7 +4874,7 @@ static bool travel_flow_aux(int y, int x, int n, bool wall) /* Hack -- notice overflow by forgetting new entry */ if (flow_head == flow_tail) flow_head = old_head; - return wall; + return; } @@ -4843,15 +4882,16 @@ static void travel_flow(int ty, int tx) { int x, y, d; bool wall = FALSE; - feature_type *f_ptr = &f_info[cave[ty][tx].feat]; + feature_type *f_ptr = &f_info[cave[py][px].feat]; /* Reset the "queue" */ flow_head = flow_tail = 0; + /* is player in the wall? */ if (!have_flag(f_ptr->flags, FF_MOVE)) wall = TRUE; - /* Add the player's grid to the queue */ - wall = travel_flow_aux(ty, tx, 0, wall); + /* Start at the target grid */ + travel_flow_aux(ty, tx, 0, wall); /* Now process the queue */ while (flow_head != flow_tail) @@ -4863,11 +4903,14 @@ static void travel_flow(int ty, int tx) /* Forget that entry */ if (++flow_tail == MAX_SHORT) flow_tail = 0; + /* Ignore too far entries */ + //if (distance(ty, tx, y, x) > 100) continue; + /* Add the "children" */ for (d = 0; d < 8; d++) { /* Add that child if "legal" */ - wall = travel_flow_aux(y + ddy_ddd[d], x + ddx_ddd[d], travel.cost[y][x] + 1, wall); + travel_flow_aux(y + ddy_ddd[d], x + ddx_ddd[d], travel.cost[y][x], wall); } } diff --git a/src/xtra2.c b/src/xtra2.c index 76d95ebec..38aefd1ea 100644 --- a/src/xtra2.c +++ b/src/xtra2.c @@ -3767,7 +3767,7 @@ static int target_set_aux(int y, int x, int mode, cptr info) if (c_ptr->mimic) sprintf(f_idx_str, "%d/%d", c_ptr->feat, c_ptr->mimic); else sprintf(f_idx_str, "%d", c_ptr->feat); #ifdef JP - sprintf(out_val, "%s%s%s%s[%s] %x %s %d %d %d (%d,%d)", s1, name, s2, s3, info, c_ptr->info, f_idx_str, c_ptr->dist, c_ptr->cost, c_ptr->when, y, x); + sprintf(out_val, "%s%s%s%s[%s] %x %s %d %d %d (%d,%d) %d", s1, name, s2, s3, info, c_ptr->info, f_idx_str, c_ptr->dist, c_ptr->cost, c_ptr->when, y, x, travel.cost[y][x]); #else sprintf(out_val, "%s%s%s%s [%s] %x %s %d %d %d (%d,%d)", s1, s2, s3, name, info, c_ptr->info, f_idx_str, c_ptr->dist, c_ptr->cost, c_ptr->when, y, x); #endif -- 2.11.0