From f88c896138589fd2328002a7a4f097c582f79f9b Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Fri, 11 Dec 2015 08:09:30 +0100 Subject: De-duplicate detect_objects_* functions --- src/spells2.cc | 130 +++++++++++++++++++++----------------------------------- src/spells2.hpp | 4 +- 2 files changed, 51 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/spells2.cc b/src/spells2.cc index 30564dcd..9498ef94 100644 --- a/src/spells2.cc +++ b/src/spells2.cc @@ -2376,25 +2376,24 @@ static bool_ detect_monsters_string(cptr chars, int rad) } - -/* - * Detect all "gold" objects on the current panel +/** + * Detect objects on the current panel. */ -bool_ detect_objects_gold(int rad) +template bool detect_objects_fn(int radius, const char *object_message, const char *monsters, P p) { - int i, y, x; - - bool_ detect = FALSE; - + bool detect = false; /* Scan objects */ - for (i = 1; i < o_max; i++) + for (int i = 1; i < o_max; i++) { object_type *o_ptr = &o_list[i]; /* Skip dead objects */ if (!o_ptr->k_idx) continue; + /* Location */ + int y, x; + /* Skip held objects */ if (o_ptr->held_m_idx) { @@ -2402,7 +2401,10 @@ bool_ detect_objects_gold(int rad) monster_type *m_ptr = &m_list[o_ptr->held_m_idx]; monster_race *r_ptr = race_inf(m_ptr); - if (!(r_ptr->flags9 & RF9_MIMIC)) continue; + if (!(r_ptr->flags9 & RF9_MIMIC)) + { + continue; /* Skip mimics completely */ + } else { /* Location */ @@ -2418,10 +2420,13 @@ bool_ detect_objects_gold(int rad) } /* Only detect nearby objects */ - if (distance(p_ptr->py, p_ptr->px, y, x) > rad) continue; + if (distance(p_ptr->py, p_ptr->px, y, x) > radius) + { + continue; + } - /* Detect "gold" objects */ - if (o_ptr->tval == TV_GOLD) + /* Detect objects that satisfy predicate */ + if (p(o_ptr)) { /* Hack -- memorize it */ o_ptr->marked = TRUE; @@ -2437,89 +2442,52 @@ bool_ detect_objects_gold(int rad) /* Describe */ if (detect) { - msg_print("You sense the presence of treasure!"); + msg_print(object_message); } - if (detect_monsters_string("$", rad)) + if (detect_monsters_string(monsters, radius)) { - detect = TRUE; + detect = true; } /* Result */ - return (detect); + return detect; } /* - * Detect all "normal" objects on the current panel + * Detect all "gold" objects on the current panel */ -bool_ detect_objects_normal(int rad) +bool detect_objects_gold(int rad) { - int i, y, x; - - bool_ detect = FALSE; - - - /* Scan objects */ - for (i = 1; i < o_max; i++) - { - object_type *o_ptr = &o_list[i]; - - /* Skip dead objects */ - if (!o_ptr->k_idx) continue; - - /* Skip held objects */ - if (o_ptr->held_m_idx) - { - /* Access the monster */ - monster_type *m_ptr = &m_list[o_ptr->held_m_idx]; - monster_race *r_ptr = race_inf(m_ptr); - - if (!(r_ptr->flags9 & RF9_MIMIC)) continue; - else - { - /* Location */ - y = m_ptr->fy; - x = m_ptr->fx; - } - } - else - { - /* Location */ - y = o_ptr->iy; - x = o_ptr->ix; - } - - /* Only detect nearby objects */ - if (distance(p_ptr->py, p_ptr->px, y, x) > rad) continue; - - /* Detect "real" objects */ - if (o_ptr->tval != TV_GOLD) - { - /* Hack -- memorize it */ - o_ptr->marked = TRUE; - - /* Redraw */ - if (panel_contains(y, x)) lite_spot(y, x); - - /* Detect */ - detect = TRUE; - } - } + auto predicate = [](object_type const *o_ptr) -> bool { + return o_ptr->tval == TV_GOLD; + }; - /* Describe */ - if (detect) - { - msg_print("You sense the presence of objects!"); - } + return detect_objects_fn( + rad, + "You sense the presence of treasure!", + "$", + predicate); +} - if (detect_monsters_string("!=?|", rad)) - { - detect = TRUE; - } - /* Result */ - return (detect); +/* + * Detect all "normal" objects on the current panel + */ +bool detect_objects_normal(int rad) +{ + auto predicate = [](object_type const *o_ptr) -> bool { + return o_ptr->tval != TV_GOLD; + }; + const char *object_message = "You sense the presence of objects!"; + const char *monsters = "!=?|"; + + return detect_objects_fn( + rad, + object_message, + monsters, + predicate); } diff --git a/src/spells2.hpp b/src/spells2.hpp index 1806e6b0..bffc4a2c 100644 --- a/src/spells2.hpp +++ b/src/spells2.hpp @@ -28,8 +28,8 @@ extern bool_ detect_traps(int rad); extern bool_ detect_doors(int rad); extern bool_ detect_stairs(int rad); extern bool_ detect_treasure(int rad); -extern bool_ detect_objects_gold(int rad); -extern bool_ detect_objects_normal(int rad); +extern bool detect_objects_gold(int rad); +extern bool detect_objects_normal(int rad); extern bool_ detect_monsters_normal(int rad); extern bool_ detect_monsters_invis(int rad); extern bool_ detect_monsters_xxx(u32b match_flag, int rad); -- cgit v1.2.3