summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2015-12-11 08:09:30 +0100
committerBardur Arantsson <bardur@scientician.net>2015-12-11 08:09:30 +0100
commitf88c896138589fd2328002a7a4f097c582f79f9b (patch)
treeb527777e87f21f989f364253797f9193a983bcdf
parented8cbb3c5f399a746cb189df11f2c66184252145 (diff)
De-duplicate detect_objects_* functions
-rw-r--r--src/spells2.cc130
-rw-r--r--src/spells2.hpp4
2 files changed, 51 insertions, 83 deletions
diff --git a/src/spells2.cc b/src/spells2.cc
index 30564dc..9498ef9 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 <typename P> 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 1806e6b..bffc4a2 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);