From 47173f1092b4f4f3bdc0da19d6dc4553f62cdee7 Mon Sep 17 00:00:00 2001 From: zelgadis Date: Thu, 6 Dec 2007 09:54:50 +0000 Subject: "?/" can now be used to look up items. A side effect of this is that item descriptions have been moved to dat/descript/items.txt and dat/descript/unident.txt. STL maps are now used to look up exact matches for the names of monsters, features and spells, so that looking them up with "?/" won't get too slow if there's a large descriptions DB and each key has to be tested if its a monster/spell/etc. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@3009 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/spl-util.cc | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'crawl-ref/source/spl-util.cc') diff --git a/crawl-ref/source/spl-util.cc b/crawl-ref/source/spl-util.cc index 9cbaace2df..1a35365798 100644 --- a/crawl-ref/source/spl-util.cc +++ b/crawl-ref/source/spl-util.cc @@ -76,13 +76,39 @@ void init_spell_descs(void) spell_list[spelldata[i].id] = i; } // end init_spell_descs() +typedef std::map spell_name_map; +static spell_name_map spell_name_cache; + +void init_spell_name_cache() +{ + for (int i = 0; i < NUM_SPELLS; i++) + { + spell_type type = static_cast(i); + const char *sptitle = spell_title(type); + if (!sptitle) + continue; + + const std::string spell_name = lowercase_string(sptitle); + spell_name_cache[spell_name] = type; + } +} + spell_type spell_by_name(std::string name, bool partial_match) { if (name.empty()) return (SPELL_NO_SPELL); - lowercase(name); + if (!partial_match) + { + spell_name_map::iterator i = spell_name_cache.find(name); + + if (i != spell_name_cache.end()) + return (i->second); + + return (SPELL_NO_SPELL); + } + int spellmatch = -1; for (int i = 0; i < NUM_SPELLS; i++) { @@ -92,10 +118,8 @@ spell_type spell_by_name(std::string name, bool partial_match) continue; const std::string spell_name = lowercase_string(sptitle); - if (name == spell_name) - return (type); - if (partial_match && spell_name.find(name) != std::string::npos) + if (spell_name.find(name) != std::string::npos) spellmatch = i; } -- cgit v1.2.3-54-g00ecf