From ed8cbb3c5f399a746cb189df11f2c66184252145 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Fri, 11 Dec 2015 08:09:30 +0100 Subject: De-duplicate detect_monsters_* functions --- src/spells2.cc | 264 +++++++++++++++++++-------------------------------------- 1 file changed, 87 insertions(+), 177 deletions(-) (limited to 'src') diff --git a/src/spells2.cc b/src/spells2.cc index d62d03dc..30564dcd 100644 --- a/src/spells2.cc +++ b/src/spells2.cc @@ -2286,40 +2286,48 @@ bool_ detect_treasure(int rad) } - /* - * Detect all (string) monsters on current panel + * Detect monsters on current panel using a + * predicate function P and an update function U. + * The "update function" is called exactly once if + * the predicate succeeds. The */ -static bool_ detect_monsters_string(cptr chars, int rad) -{ - int i, y, x; - bool_ flag = FALSE; - - +template static bool detect_monsters_fn(int radius, P p, U u) { + bool flag = false; /* Scan monsters */ - for (i = 1; i < m_max; i++) + for (int i = 1; i < m_max; i++) { monster_type *m_ptr = &m_list[i]; - monster_race *r_ptr = race_inf(m_ptr); /* Skip dead monsters */ - if (!m_ptr->r_idx) continue; + if (!m_ptr->r_idx) + { + continue; + } /* Location */ - y = m_ptr->fy; - x = m_ptr->fx; + int const y = m_ptr->fy; + int const x = m_ptr->fx; /* Only detect nearby monsters */ - if (m_ptr->cdis > rad) continue; - - /* Detect evil monsters */ - if (strchr(chars, r_ptr->d_char)) + if (m_ptr->cdis > radius) + { + continue; + } + /* Detect monsters which fulfill the predicate */ + auto r_ptr = race_inf(m_ptr); + if (p(r_ptr)) { + /* Update */ + u(r_ptr); - /* Update monster recall window */ + /* We're assuming the update function does + * *something*, so we'll need to update + * the recall window if we're currently looking + * at it. + */ if (monster_race_idx == m_ptr->r_idx) { - /* Window stuff */ p_ptr->window |= (PW_MONSTER); } @@ -2339,16 +2347,32 @@ static bool_ detect_monsters_string(cptr chars, int rad) flag = TRUE; } } + /* Result */ + return flag; +} + +/* + * Detect all (string) monsters on current panel + */ +static bool_ detect_monsters_string(cptr chars, int rad) +{ + auto predicate = [chars](monster_race *r_ptr) -> bool { + return strchr(chars, r_ptr->d_char); + }; + auto update = [](monster_race *) -> void { + }; /* Describe */ - if (flag) + if (detect_monsters_fn(rad, predicate, update)) { /* Describe result */ msg_print("You sense the presence of monsters!"); + return TRUE; + } + else + { + return FALSE; } - - /* Result */ - return (flag); } @@ -2504,57 +2528,23 @@ bool_ detect_objects_normal(int rad) */ bool_ detect_monsters_normal(int rad) { - int i, y, x; - - bool_ flag = FALSE; - - - /* Scan monsters */ - for (i = 1; i < m_max; i++) - { - monster_type *m_ptr = &m_list[i]; - monster_race *r_ptr = race_inf(m_ptr); - - /* Skip dead monsters */ - if (!m_ptr->r_idx) continue; - - /* Location */ - y = m_ptr->fy; - x = m_ptr->fx; - - /* Only detect nearby monsters */ - if (m_ptr->cdis > rad) continue; - - /* Detect all non-invisible monsters */ - if ((!(r_ptr->flags2 & (RF2_INVISIBLE))) || - p_ptr->see_inv || p_ptr->tim_invis) - { - /* Repair visibility later */ - repair_monsters = TRUE; - - /* Hack -- Detect monster */ - m_ptr->mflag |= (MFLAG_MARK | MFLAG_SHOW); - - /* Hack -- See monster */ - m_ptr->ml = TRUE; - - /* Redraw */ - if (panel_contains(y, x)) lite_spot(y, x); - - /* Detect */ - flag = TRUE; - } - } + auto predicate = [](monster_race *r_ptr) -> bool { + return (!(r_ptr->flags2 & (RF2_INVISIBLE))) || + p_ptr->see_inv || p_ptr->tim_invis; + }; + auto update = [](monster_race *) -> void { + }; - /* Describe */ - if (flag) + if (detect_monsters_fn(rad, predicate, update)) { /* Describe result */ msg_print("You sense the presence of monsters!"); + return TRUE; + } + else + { + return FALSE; } - - /* Result */ - return (flag); } @@ -2563,64 +2553,23 @@ bool_ detect_monsters_normal(int rad) */ bool_ detect_monsters_invis(int rad) { - int i, y, x; - bool_ flag = FALSE; - - /* Scan monsters */ - for (i = 1; i < m_max; i++) - { - monster_type *m_ptr = &m_list[i]; - monster_race *r_ptr = race_inf(m_ptr); - - /* Skip dead monsters */ - if (!m_ptr->r_idx) continue; - - /* Location */ - y = m_ptr->fy; - x = m_ptr->fx; - - /* Only detect nearby monsters */ - if (m_ptr->cdis > rad) continue; - - /* Detect invisible monsters */ - if (r_ptr->flags2 & (RF2_INVISIBLE)) - { - /* Take note that they are invisible */ - r_ptr->r_flags2 |= (RF2_INVISIBLE); - - /* Update monster recall window */ - if (monster_race_idx == m_ptr->r_idx) - { - /* Window stuff */ - p_ptr->window |= (PW_MONSTER); - } - - /* Repair visibility later */ - repair_monsters = TRUE; - - /* Hack -- Detect monster */ - m_ptr->mflag |= (MFLAG_MARK | MFLAG_SHOW); - - /* Hack -- See monster */ - m_ptr->ml = TRUE; - - /* Redraw */ - if (panel_contains(y, x)) lite_spot(y, x); - - /* Detect */ - flag = TRUE; - } - } + auto predicate = [](monster_race *r_ptr) -> bool { + return (r_ptr->flags2 & (RF2_INVISIBLE)); + }; + auto update = [](monster_race *r_ptr) -> void { + r_ptr->r_flags2 |= (RF2_INVISIBLE); + }; - /* Describe */ - if (flag) + if (detect_monsters_fn(rad, predicate, update)) { /* Describe result */ msg_print("You sense the presence of invisible creatures!"); + return TRUE; + } + else + { + return FALSE; } - - /* Result */ - return (flag); } @@ -2630,60 +2579,16 @@ bool_ detect_monsters_invis(int rad) */ bool_ detect_monsters_xxx(u32b match_flag, int rad) { - int i, y, x; - bool_ flag = FALSE; - cptr desc_monsters = "weird monsters"; - - - /* Scan monsters */ - for (i = 1; i < m_max; i++) - { - monster_type *m_ptr = &m_list[i]; - monster_race *r_ptr = race_inf(m_ptr); - - /* Skip dead monsters */ - if (!m_ptr->r_idx) continue; - - /* Location */ - y = m_ptr->fy; - x = m_ptr->fx; - - /* Only detect nearby monsters */ - if (m_ptr->cdis > rad) continue; - - /* Detect evil monsters */ - if (r_ptr->flags3 & (match_flag)) - { - /* Take note that they are something */ - r_ptr->r_flags3 |= (match_flag); - - /* Update monster recall window */ - if (monster_race_idx == m_ptr->r_idx) - { - /* Window stuff */ - p_ptr->window |= (PW_MONSTER); - } - - /* Repair visibility later */ - repair_monsters = TRUE; - - /* Hack -- Detect monster */ - m_ptr->mflag |= (MFLAG_MARK | MFLAG_SHOW); - - /* Hack -- See monster */ - m_ptr->ml = TRUE; - - /* Redraw */ - if (panel_contains(y, x)) lite_spot(y, x); - - /* Detect */ - flag = TRUE; - } - } + auto predicate = [match_flag](monster_race *r_ptr) -> bool { + return (r_ptr->flags3 & match_flag); + }; + auto update = [match_flag](monster_race *r_ptr) -> void { + r_ptr->r_flags3 |= (match_flag); + }; - /* Describe */ - if (flag) + if (detect_monsters_fn(rad, predicate, update)) { + cptr desc_monsters = "weird monsters"; switch (match_flag) { case RF3_DEMON: @@ -2695,15 +2600,20 @@ bool_ detect_monsters_xxx(u32b match_flag, int rad) case RF3_GOOD: desc_monsters = "good"; break; + case RF3_ORC: + desc_monsters = "orcs"; + break; } /* Describe result */ msg_format("You sense the presence of %s!", desc_monsters); msg_print(NULL); + return TRUE; + } + else + { + return FALSE; } - - /* Result */ - return (flag); } -- cgit v1.2.3