Attached Files |
0001-Make-str_to_weapon-use-existing-item-data.patch [^] (5,743 bytes) 2014-07-14 22:29 [Show Content] [Hide Content]From 39115d5739ffa83b3dc7a05f65f09fe59cf7dfc6 Mon Sep 17 00:00:00 2001
From: Tanner Swett <tannerswett@gmail.com>
Date: Mon, 14 Jul 2014 15:58:49 -0400
Subject: [PATCH] Make str_to_weapon use existing item data
---
crawl-ref/source/initfile.cc | 92 +++++++++++++++++++++++---------------------
crawl-ref/source/itemprop.cc | 20 ++++++++++
crawl-ref/source/itemprop.h | 7 ++++
3 files changed, 75 insertions(+), 44 deletions(-)
diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc
index 5e77811..6ce4892 100644
--- a/crawl-ref/source/initfile.cc
+++ b/crawl-ref/source/initfile.cc
@@ -197,53 +197,57 @@ string channel_to_str(int channel)
return message_channel_names[channel];
}
+/**
+ * Populate the map used to interpret a crawlrc entry as a starting weapon
+ * type. For most entries, we can just look up which weapon has the entry as
+ * its name; this map contains the exceptions.
+ */
+static map<string, weapon_type> _create_special_weapon_map()
+{
+ map<string, weapon_type> weapon_map;
+
+ // "staff" normally refers to a magical staff, but here we want to
+ // interpret it as a quarterstaff.
+ weapon_map["staff"] = WPN_QUARTERSTAFF;
+
+ // This weapon's base name is "hunting sling", not "sling".
+ weapon_map["sling"] = WPN_HUNTING_SLING;
+ // Likewise, "crossbow" is short for "hand crossbow".
+ weapon_map["crossbow"] = WPN_HAND_CROSSBOW;
+
+ // Pseudo-weapons.
+ weapon_map["unarmed"] = WPN_UNARMED;
+ weapon_map["claws"] = WPN_UNARMED;
+
+ weapon_map["thrown"] = WPN_THROWN;
+ weapon_map["rocks"] = WPN_THROWN;
+ weapon_map["javelins"] = WPN_THROWN;
+ weapon_map["tomahawks"] = WPN_THROWN;
+
+ weapon_map["random"] = WPN_RANDOM;
+
+ weapon_map["viable"] = WPN_VIABLE;
+
+ return weapon_map;
+}
+
+// This should be const, but operator[] on maps isn't const.
+static map<string, weapon_type> _special_weapon_map = _create_special_weapon_map();
+
+/// Interpret a crawlrc entry as a starting weapon type.
weapon_type str_to_weapon(const string &str)
{
- if (str == "shortsword" || str == "short sword")
- return WPN_SHORT_SWORD;
- else if (str == "cutlass")
- return WPN_CUTLASS;
- else if (str == "falchion")
- return WPN_FALCHION;
- else if (str == "longsword" || str == "long sword")
- return WPN_LONG_SWORD;
- else if (str == "quarterstaff" || str == "staff")
- return WPN_QUARTERSTAFF;
- else if (str == "mace")
- return WPN_MACE;
- else if (str == "flail")
- return WPN_FLAIL;
- else if (str == "spear")
- return WPN_SPEAR;
- else if (str == "trident")
- return WPN_TRIDENT;
- else if (str == "hand axe" || str == "handaxe")
- return WPN_HAND_AXE;
- else if (str == "war axe" || str == "waraxe")
- return WPN_WAR_AXE;
- else if (str == "unarmed" || str == "claws")
- return WPN_UNARMED;
- else if (str == "sling" || str == "hunting sling")
- return WPN_HUNTING_SLING;
- else if (str == "greatsling")
- return WPN_GREATSLING;
- else if (str == "shortbow" || str == "short bow")
- return WPN_SHORTBOW;
- else if (str == "crossbow" || str == "hand crossbow")
- return WPN_HAND_CROSSBOW;
- else if (str == "thrown"
- || str == "rocks"
- || str == "javelins"
- || str == "tomahawks")
- {
- return WPN_THROWN;
- }
- else if (str == "random")
- return WPN_RANDOM;
- else if (str == "viable")
- return WPN_VIABLE;
+ string str_nospace = str;
+ remove_whitespace(str_nospace);
+
+ // Synonyms and pseudo-weapons.
+ if (_special_weapon_map.count(str_nospace))
+ {
+ return _special_weapon_map[str_nospace];
+ }
- return WPN_UNKNOWN;
+ // Real weapons referred to by their standard names.
+ return name_nospace_to_weapon(str_nospace);
}
static string _weapon_to_str(weapon_type wpn_type)
diff --git a/crawl-ref/source/itemprop.cc b/crawl-ref/source/itemprop.cc
index c9373b0..d634434 100644
--- a/crawl-ref/source/itemprop.cc
+++ b/crawl-ref/source/itemprop.cc
@@ -2729,6 +2729,26 @@ const char* weapon_base_name(weapon_type subtype)
return Weapon_prop[Weapon_index[subtype]].name;
}
+/**
+ * Try to find a weapon, given the weapon's name without whitespace.
+ */
+weapon_type name_nospace_to_weapon(string name_nospace)
+{
+ for (size_t ii = 0; ii < ARRAYSZ(Weapon_prop); ii++)
+ {
+ string weap_nospace = Weapon_prop[ii].name;
+ remove_whitespace(weap_nospace);
+
+ if (name_nospace == weap_nospace)
+ {
+ return (weapon_type) Weapon_prop[ii].id;
+ }
+ }
+
+ // No match found
+ return WPN_UNKNOWN;
+}
+
void seen_item(const item_def &item)
{
if (!is_artefact(item) && _is_affordable(item))
diff --git a/crawl-ref/source/itemprop.h b/crawl-ref/source/itemprop.h
index a371128..1ba76e3 100644
--- a/crawl-ref/source/itemprop.h
+++ b/crawl-ref/source/itemprop.h
@@ -191,6 +191,7 @@ string item_base_name(const item_def &item);
string item_base_name(object_class_type type, int sub_type);
string food_type_name(int sub_type);
const char *weapon_base_name(weapon_type subtype) IMMUTABLE;
+weapon_type name_nospace_to_weapon(string name_nospace);
void seen_item(const item_def &item);
@@ -200,4 +201,10 @@ static inline bool is_weapon(const item_def &item)
|| item.base_type == OBJ_STAVES
|| item.base_type == OBJ_RODS;
}
+
+inline void remove_whitespace(string &str)
+{
+ str.erase(remove_if(str.begin(), str.end(),
+ static_cast<int(*)(int)>(isspace)), str.end());
+}
#endif
--
1.8.1.3
0001-Make-str_to_weapon-use-existing-item-data-2.patch [^] (6,042 bytes) 2014-07-14 22:57 [Show Content] [Hide Content]From d83f8a5a2aedd15c2fa22d7508d954a0b31728f1 Mon Sep 17 00:00:00 2001
From: Tanner Swett <tannerswett@gmail.com>
Date: Mon, 14 Jul 2014 15:58:49 -0400
Subject: [PATCH] Make str_to_weapon use existing item data
---
crawl-ref/source/initfile.cc | 97 ++++++++++++++++++++++++--------------------
crawl-ref/source/itemprop.cc | 21 ++++++++++
crawl-ref/source/itemprop.h | 7 ++++
3 files changed, 81 insertions(+), 44 deletions(-)
diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc
index 5e77811..94782bb 100644
--- a/crawl-ref/source/initfile.cc
+++ b/crawl-ref/source/initfile.cc
@@ -197,53 +197,62 @@ string channel_to_str(int channel)
return message_channel_names[channel];
}
+/**
+ * Populate the map used to interpret a crawlrc entry as a starting weapon
+ * type. For most entries, we can just look up which weapon has the entry as
+ * its name; this map contains the exceptions.
+ *
+ * @return The special starting weapon type map.
+ */
+static map<string, weapon_type> _create_special_weapon_map()
+{
+ map<string, weapon_type> weapon_map;
+
+ // "staff" normally refers to a magical staff, but here we want to
+ // interpret it as a quarterstaff.
+ weapon_map["staff"] = WPN_QUARTERSTAFF;
+
+ // These weapons' base names have changed; we want to interpret the old
+ // names correctly.
+ weapon_map["sling"] = WPN_HUNTING_SLING;
+ weapon_map["crossbow"] = WPN_HAND_CROSSBOW;
+
+ // Pseudo-weapons.
+ weapon_map["unarmed"] = WPN_UNARMED;
+ weapon_map["claws"] = WPN_UNARMED;
+
+ weapon_map["thrown"] = WPN_THROWN;
+ weapon_map["rocks"] = WPN_THROWN;
+ weapon_map["javelins"] = WPN_THROWN;
+ weapon_map["tomahawks"] = WPN_THROWN;
+
+ weapon_map["random"] = WPN_RANDOM;
+
+ weapon_map["viable"] = WPN_VIABLE;
+
+ return weapon_map;
+}
+
+// This should be const, but operator[] on maps isn't const.
+static map<string, weapon_type> _special_weapon_map = _create_special_weapon_map();
+
+/**
+ * Interpret a crawlrc entry as a starting weapon type.
+ *
+ * @param str The value of the crawlrc entry.
+ * @return The weapon the string refers to, or WPN_UNKNOWN if invalid
+ */
weapon_type str_to_weapon(const string &str)
{
- if (str == "shortsword" || str == "short sword")
- return WPN_SHORT_SWORD;
- else if (str == "cutlass")
- return WPN_CUTLASS;
- else if (str == "falchion")
- return WPN_FALCHION;
- else if (str == "longsword" || str == "long sword")
- return WPN_LONG_SWORD;
- else if (str == "quarterstaff" || str == "staff")
- return WPN_QUARTERSTAFF;
- else if (str == "mace")
- return WPN_MACE;
- else if (str == "flail")
- return WPN_FLAIL;
- else if (str == "spear")
- return WPN_SPEAR;
- else if (str == "trident")
- return WPN_TRIDENT;
- else if (str == "hand axe" || str == "handaxe")
- return WPN_HAND_AXE;
- else if (str == "war axe" || str == "waraxe")
- return WPN_WAR_AXE;
- else if (str == "unarmed" || str == "claws")
- return WPN_UNARMED;
- else if (str == "sling" || str == "hunting sling")
- return WPN_HUNTING_SLING;
- else if (str == "greatsling")
- return WPN_GREATSLING;
- else if (str == "shortbow" || str == "short bow")
- return WPN_SHORTBOW;
- else if (str == "crossbow" || str == "hand crossbow")
- return WPN_HAND_CROSSBOW;
- else if (str == "thrown"
- || str == "rocks"
- || str == "javelins"
- || str == "tomahawks")
- {
- return WPN_THROWN;
- }
- else if (str == "random")
- return WPN_RANDOM;
- else if (str == "viable")
- return WPN_VIABLE;
+ string str_nospace = str;
+ remove_whitespace(str_nospace);
+
+ // Synonyms and pseudo-weapons.
+ if (_special_weapon_map.count(str_nospace))
+ return _special_weapon_map[str_nospace];
- return WPN_UNKNOWN;
+ // Real weapons referred to by their standard names.
+ return name_nospace_to_weapon(str_nospace);
}
static string _weapon_to_str(weapon_type wpn_type)
diff --git a/crawl-ref/source/itemprop.cc b/crawl-ref/source/itemprop.cc
index c9373b0..5e80b66 100644
--- a/crawl-ref/source/itemprop.cc
+++ b/crawl-ref/source/itemprop.cc
@@ -2729,6 +2729,27 @@ const char* weapon_base_name(weapon_type subtype)
return Weapon_prop[Weapon_index[subtype]].name;
}
+/**
+ * Try to find a weapon, given the weapon's name without whitespace.
+ *
+ * @param name_nospace The weapon's base name, with all whitespace removed.
+ * @return The id of the weapon, or WPN_UNKNOWN if nothing matches.
+ */
+weapon_type name_nospace_to_weapon(string name_nospace)
+{
+ for (size_t ii = 0; ii < ARRAYSZ(Weapon_prop); ii++)
+ {
+ string weap_nospace = Weapon_prop[ii].name;
+ remove_whitespace(weap_nospace);
+
+ if (name_nospace == weap_nospace)
+ return (weapon_type) Weapon_prop[ii].id;
+ }
+
+ // No match found
+ return WPN_UNKNOWN;
+}
+
void seen_item(const item_def &item)
{
if (!is_artefact(item) && _is_affordable(item))
diff --git a/crawl-ref/source/itemprop.h b/crawl-ref/source/itemprop.h
index a371128..1ba76e3 100644
--- a/crawl-ref/source/itemprop.h
+++ b/crawl-ref/source/itemprop.h
@@ -191,6 +191,7 @@ string item_base_name(const item_def &item);
string item_base_name(object_class_type type, int sub_type);
string food_type_name(int sub_type);
const char *weapon_base_name(weapon_type subtype) IMMUTABLE;
+weapon_type name_nospace_to_weapon(string name_nospace);
void seen_item(const item_def &item);
@@ -200,4 +201,10 @@ static inline bool is_weapon(const item_def &item)
|| item.base_type == OBJ_STAVES
|| item.base_type == OBJ_RODS;
}
+
+inline void remove_whitespace(string &str)
+{
+ str.erase(remove_if(str.begin(), str.end(),
+ static_cast<int(*)(int)>(isspace)), str.end());
+}
#endif
--
1.8.1.3
|