summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/mon-util.cc
diff options
context:
space:
mode:
authorNeil Moore <neil@s-z.org>2014-07-12 11:47:23 -0400
committerNeil Moore <neil@s-z.org>2014-07-12 12:41:05 -0400
commit2727fff9fba9eba01b8ed5b2b0cbc0fc0a268161 (patch)
tree04aa232e165f80c9c292a2dc3fcc0437a3817a18 /crawl-ref/source/mon-util.cc
parent241103d554b1ee1b1b426e17fc0dce85f9a3b706 (diff)
downloadcrawl-ref-2727fff9fba9eba01b8ed5b2b0cbc0fc0a268161.tar.gz
crawl-ref-2727fff9fba9eba01b8ed5b2b0cbc0fc0a268161.zip
Use find_earliest_match for wizmode monster lookup (&M etc)
Diffstat (limited to 'crawl-ref/source/mon-util.cc')
-rw-r--r--crawl-ref/source/mon-util.cc32
1 files changed, 9 insertions, 23 deletions
diff --git a/crawl-ref/source/mon-util.cc b/crawl-ref/source/mon-util.cc
index c47f3f024f..5cb4f9dc17 100644
--- a/crawl-ref/source/mon-util.cc
+++ b/crawl-ref/source/mon-util.cc
@@ -213,6 +213,11 @@ void init_mon_name_cache()
}
}
+static const char *_mon_entry_name(size_t idx)
+{
+ return mondata[idx].name;
+}
+
monster_type get_monster_by_name(string name, bool substring)
{
if (name.empty())
@@ -230,29 +235,10 @@ monster_type get_monster_by_name(string name, bool substring)
return MONS_PROGRAM_BUG;
}
- int best = 0x7fffffff;
-
- monster_type mon = MONS_PROGRAM_BUG;
- for (unsigned i = 0; i < ARRAYSZ(mondata); ++i)
- {
- string candidate = mondata[i].name;
- lowercase(candidate);
-
- const int mtype = mondata[i].mc;
-
- const string::size_type match = candidate.find(name);
- if (match == string::npos)
- continue;
-
- int qual = 0x7ffffffe;
- // We prefer prefixes over partial matches.
- if (match == 0)
- qual = candidate.length();;
-
- if (qual < best)
- best = qual, mon = monster_type(mtype);
- }
- return mon;
+ size_t idx = find_earliest_match(name, (size_t) 0, ARRAYSZ(mondata),
+ _always_true<size_t>, _mon_entry_name);
+ return idx == ARRAYSZ(mondata) ? MONS_PROGRAM_BUG
+ : (monster_type) mondata[idx].mc;
}
void init_monsters()