summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/zotdef.cc
diff options
context:
space:
mode:
authorNeil Moore <neil@s-z.org>2014-07-11 00:41:31 -0400
committerNeil Moore <neil@s-z.org>2014-07-11 01:02:22 -0400
commit1cf06be6ea1471d9273a24fb7f95bde6938b7d3c (patch)
tree1a20996fb8275609ecc8b9fd8a308dc7213b58e1 /crawl-ref/source/zotdef.cc
parent9555e25a93d5bb2fa10eefe79a362f5ddb270cde (diff)
downloadcrawl-ref-1cf06be6ea1471d9273a24fb7f95bde6938b7d3c.tar.gz
crawl-ref-1cf06be6ea1471d9273a24fb7f95bde6938b7d3c.zip
Refactor god and spell search using templates.
Diffstat (limited to 'crawl-ref/source/zotdef.cc')
-rw-r--r--crawl-ref/source/zotdef.cc33
1 files changed, 12 insertions, 21 deletions
diff --git a/crawl-ref/source/zotdef.cc b/crawl-ref/source/zotdef.cc
index 34dadb5238..f76458b965 100644
--- a/crawl-ref/source/zotdef.cc
+++ b/crawl-ref/source/zotdef.cc
@@ -5,6 +5,7 @@
#include "AppHdr.h"
#include "bitary.h"
+#include <functional>
#include "branch.h"
#include "coordit.h"
@@ -910,6 +911,11 @@ bool create_trap(trap_type spec_type)
return result;
}
+static bool _can_make_altar(god_type g, bool wizmode)
+{
+ return wizmode || !is_unavailable_god(g);
+}
+
/**
* Create an altar to the god of the player's choice.
* @param wizmode if true, bypass some checks.
@@ -928,28 +934,13 @@ bool zotdef_create_altar(bool wizmode)
string spec = lowercase_string(specs);
- // Find the god with the earliest match for this string.
- god_type god = GOD_NO_GOD;
- size_t bestpos = string::npos;
- for (int i = 1; i < NUM_GODS; ++i)
- {
- const god_type gi = static_cast<god_type>(i);
-
- if (!wizmode && is_unavailable_god(gi))
- continue;
-
- const string name = lowercase_string(god_name(gi));
- const size_t pos = name.find(spec);
-
- if (pos < bestpos)
- {
- // npos is never less than bestpos, so the spec was found.
- bestpos = pos;
- god = gi;
- }
- }
+ // Skip GOD_NO_GOD
+ god_type god = find_earliest_match(
+ spec, (god_type) 1, NUM_GODS,
+ bind2nd(ptr_fun(_can_make_altar), wizmode),
+ bind2nd(ptr_fun(god_name), false));
- if (god == GOD_NO_GOD)
+ if (god == NUM_GODS)
{
mpr("That god doesn't seem to be taking followers today.");
return false;