summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2018-03-25 20:07:30 +0200
committerBardur Arantsson <bardur@scientician.net>2018-03-25 20:07:30 +0200
commita9e8ef4434fd505a4db627d9156919952586e209 (patch)
tree9a5ddc477d54dcc241cef96f0ffa1950fcd02046
parent7040407bdea291f1c6c0bd0dcca69a475240f93a (diff)
Change k_info to an unordered_map<>
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/birth.cc8
-rw-r--r--src/cave.cc35
-rw-r--r--src/cmd2.cc6
-rw-r--r--src/cmd3.cc2
-rw-r--r--src/cmd4.cc103
-rw-r--r--src/cmd6.cc20
-rw-r--r--src/cmd7.cc4
-rw-r--r--src/dungeon.cc6
-rw-r--r--src/files.cc16
-rw-r--r--src/game_edit_data.cc18
-rw-r--r--src/game_edit_data.hpp8
-rw-r--r--src/generate.cc8
-rw-r--r--src/init1.cc11
-rw-r--r--src/init2.cc12
-rw-r--r--src/loadsave.cc69
-rw-r--r--src/monster2.cc10
-rw-r--r--src/object1.cc55
-rw-r--r--src/object2.cc87
-rw-r--r--src/powers.cc2
-rw-r--r--src/q_rand.cc4
-rw-r--r--src/spells1.cc4
-rw-r--r--src/spells2.cc2
-rw-r--r--src/spells3.cc2
-rw-r--r--src/squelch/condition.cc2
-rw-r--r--src/squeltch.cc2
-rw-r--r--src/store.cc14
-rw-r--r--src/util.cc11
-rw-r--r--src/wizard2.cc21
-rw-r--r--src/xtra1.cc2
-rw-r--r--src/xtra2.cc11
31 files changed, 311 insertions, 245 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 30ceb76c..c07da201 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -26,6 +26,7 @@ SET(SRCS_COMMON
dungeon.cc
files.cc
game.cc
+ game_edit_data.cc
gen_evol.cc
gen_maze.cc
generate.cc
diff --git a/src/birth.cc b/src/birth.cc
index f677e6e1..ab3503d5 100644
--- a/src/birth.cc
+++ b/src/birth.cc
@@ -630,15 +630,11 @@ static void player_wipe()
}
/* Reset the "objects" */
- for (auto &k_ref: k_info)
+ for (auto &k_entry: k_info)
{
- /* Reset "tried" */
+ auto &k_ref = k_entry.second;
k_ref.tried = FALSE;
-
- /* Reset "aware" */
k_ref.aware = FALSE;
-
- /* Reset "artifact" */
k_ref.artifact = 0;
}
diff --git a/src/cave.cc b/src/cave.cc
index cad1506e..2ab97283 100644
--- a/src/cave.cc
+++ b/src/cave.cc
@@ -438,31 +438,10 @@ static void image_object(byte *ap, char *cp)
{
auto const &k_info = game->edit_data.k_info;
- // Cached state which keeps a list of the "live" object_kind entries.
- static std::vector<size_t> *instance = nullptr;
-
- // First-time initialization
- if (!instance)
- {
- // Create the list of "live" indexes
- instance = new std::vector<size_t>();
- // Filter all the "live" entries
- for (size_t i = 0; i < k_info.size(); i++)
- {
- if (k_info[i].name)
- {
- instance->push_back(i);
- }
- }
- }
-
- // Sanity check
- assert(instance != nullptr);
-
// Select an object kind at random
- int n = rand_int(instance->size());
- *cp = k_info[(*instance)[n]].x_char;
- *ap = k_info[(*instance)[n]].x_attr;
+ auto const &k_ref = uniform_element(k_info)->second;
+ *cp = k_ref.x_char;
+ *ap = k_ref.x_attr;
}
@@ -1059,7 +1038,7 @@ static void map_info(int y, int x, byte *ap, char *cp)
*ap = object_attr(o_ptr);
/* Multi-hued attr */
- if (!options->avoid_other && (k_info[o_ptr->k_idx].flags & TR_ATTR_MULTI))
+ if (!options->avoid_other && (k_info.at(o_ptr->k_idx).flags & TR_ATTR_MULTI))
{
*ap = get_shimmer_color();
}
@@ -1095,7 +1074,7 @@ static void map_info(int y, int x, byte *ap, char *cp)
*ap = object_attr(o_ptr);
/* Multi-hued attr */
- if (!options->avoid_other && (k_info[o_ptr->k_idx].flags & TR_ATTR_MULTI))
+ if (!options->avoid_other && (k_info.at(o_ptr->k_idx).flags & TR_ATTR_MULTI))
{
*ap = get_shimmer_color();
}
@@ -1449,7 +1428,7 @@ void map_info_default(int y, int x, byte *ap, char *cp)
*ap = object_attr_default(o_ptr);
/* Multi-hued attr */
- if (!avoid_other && (k_info[o_ptr->k_idx].flags & TR_ATTR_MULTI))
+ if (!avoid_other && (k_info.at(o_ptr->k_idx).flags & TR_ATTR_MULTI))
{
*ap = get_shimmer_color();
}
@@ -1485,7 +1464,7 @@ void map_info_default(int y, int x, byte *ap, char *cp)
*ap = object_attr_default(o_ptr);
/* Multi-hued attr */
- if (!avoid_other && (k_info[o_ptr->k_idx].flags & TR_ATTR_MULTI))
+ if (!avoid_other && (k_info.at(o_ptr->k_idx).flags & TR_ATTR_MULTI))
{
*ap = get_shimmer_color();
}
diff --git a/src/cmd2.cc b/src/cmd2.cc
index a348c221..2623ca54 100644
--- a/src/cmd2.cc
+++ b/src/cmd2.cc
@@ -3377,7 +3377,7 @@ void do_cmd_throw()
if (special) attack_special(m_ptr, special, tdam);
/* Anger friends */
- if (!(k_info[q_ptr->k_idx].tval == TV_POTION))
+ if (!(k_info.at(q_ptr->k_idx).tval == TV_POTION))
{
char m_name[80];
monster_desc(m_name, m_ptr, 0);
@@ -3417,7 +3417,7 @@ void do_cmd_throw()
j = (hit_body ? breakage_chance(q_ptr) : 0);
/* Potions smash open */
- if (k_info[q_ptr->k_idx].tval == TV_POTION)
+ if (k_info.at(q_ptr->k_idx).tval == TV_POTION)
{
if ((hit_body) || (hit_wall) || (randint(100) < j))
{
@@ -3703,7 +3703,7 @@ void do_cmd_boomerang()
if (special) attack_special(m_ptr, special, tdam);
/* Anger friends */
- if (!(k_info[q_ptr->k_idx].tval == TV_POTION))
+ if (!(k_info.at(q_ptr->k_idx).tval == TV_POTION))
{
char m_name[80];
monster_desc(m_name, m_ptr, 0);
diff --git a/src/cmd3.cc b/src/cmd3.cc
index cbf58820..59a21ff6 100644
--- a/src/cmd3.cc
+++ b/src/cmd3.cc
@@ -677,7 +677,7 @@ void do_cmd_destroy()
/* Eru wont be happy */
if (flags & TR_BLESSED)
{
- inc_piety(GOD_ERU, -10 * k_info[o_ptr->k_idx].level);
+ inc_piety(GOD_ERU, -10 * k_info.at(o_ptr->k_idx).level);
}
/* Eliminate the item */
diff --git a/src/cmd4.cc b/src/cmd4.cc
index a820da41..7e2ad240 100644
--- a/src/cmd4.cc
+++ b/src/cmd4.cc
@@ -47,6 +47,7 @@
#include <memory>
#include <numeric>
#include <string>
+#include <unordered_set>
#include <vector>
/*
@@ -2026,15 +2027,9 @@ void do_cmd_visuals()
auto &f_info = game->edit_data.f_info;
auto &k_info = game->edit_data.k_info;
- int i;
-
- FILE *fff;
-
char tmp[160];
-
char buf[1024];
-
/* Enter "icky" mode */
character_icky = TRUE;
@@ -2067,7 +2062,7 @@ void do_cmd_visuals()
prt("Command: ", 15, 0);
/* Prompt */
- i = inkey();
+ int i = inkey();
/* Done */
if (i == ESCAPE) break;
@@ -2110,7 +2105,7 @@ void do_cmd_visuals()
path_build(buf, 1024, ANGBAND_DIR_USER, tmp);
/* Append to the file */
- fff = my_fopen(buf, "a");
+ FILE *fff = my_fopen(buf, "a");
/* Failure */
if (!fff) continue;
@@ -2165,7 +2160,7 @@ void do_cmd_visuals()
path_build(buf, 1024, ANGBAND_DIR_USER, tmp);
/* Append to the file */
- fff = my_fopen(buf, "a");
+ FILE *fff = my_fopen(buf, "a");
/* Failure */
if (!fff) continue;
@@ -2175,9 +2170,9 @@ void do_cmd_visuals()
fprintf(fff, "# Object attr/char definitions\n\n");
/* Dump objects */
- for (std::size_t k = 0; k < k_info.size(); k++)
+ for (auto const &k_entry: k_info)
{
- object_kind *k_ptr = &k_info[k];
+ auto const k_ptr = &k_entry.second;
/* Skip non-entries */
if (!k_ptr->name) continue;
@@ -2186,7 +2181,7 @@ void do_cmd_visuals()
fprintf(fff, "# %s\n", k_ptr->name);
/* Dump the object attr/char info */
- fprintf(fff, "K:%zu:0x%02X:0x%02X\n\n", k,
+ fprintf(fff, "K:%d:0x%02X:0x%02X\n\n", k_entry.first,
(byte)(k_ptr->x_attr), (byte)(k_ptr->x_char));
}
@@ -2219,7 +2214,7 @@ void do_cmd_visuals()
path_build(buf, 1024, ANGBAND_DIR_USER, tmp);
/* Append to the file */
- fff = my_fopen(buf, "a");
+ FILE *fff = my_fopen(buf, "a");
/* Failure */
if (!fff) continue;
@@ -2312,7 +2307,10 @@ void do_cmd_visuals()
/* Modify object attr/chars */
else if (i == '7')
{
- static int k = 0;
+ static auto const k_info_keys =
+ game->edit_data.k_info_keys();
+
+ static int k_idx = 0;
/* Prompt */
prt("Command: Change object attr/chars", 15, 0);
@@ -2320,7 +2318,7 @@ void do_cmd_visuals()
/* Hack -- query until done */
while (1)
{
- object_kind *k_ptr = &k_info[k];
+ object_kind *k_ptr = &k_info.at(k_info_keys[k_idx]);
byte da = k_ptr->d_attr;
char dc = k_ptr->d_char;
@@ -2330,7 +2328,7 @@ void do_cmd_visuals()
/* Label the object */
Term_putstr(5, 17, -1, TERM_WHITE,
format("Object = %d, Name = %-40.40s",
- k, k_ptr->name));
+ k_info_keys[k_idx], k_ptr->name));
/* Label the Default values */
Term_putstr(10, 19, -1, TERM_WHITE,
@@ -2355,12 +2353,12 @@ void do_cmd_visuals()
if (i == ESCAPE) break;
/* Analyze */
- if (i == 'n') k = (k + k_info.size() + 1) % k_info.size();
- if (i == 'N') k = (k + k_info.size() - 1) % k_info.size();
- if (i == 'a') k_info[k].x_attr = (ca + 1);
- if (i == 'A') k_info[k].x_attr = (ca - 1);
- if (i == 'c') k_info[k].x_char = (cc + 1);
- if (i == 'C') k_info[k].x_char = (cc - 1);
+ if (i == 'n') k_idx = (k_idx + k_info_keys.size() + 1) % k_info_keys.size();
+ if (i == 'N') k_idx = (k_idx + k_info_keys.size() - 1) % k_info_keys.size();
+ if (i == 'a') k_ptr->x_attr = (ca + 1);
+ if (i == 'A') k_ptr->x_attr = (ca - 1);
+ if (i == 'c') k_ptr->x_char = (cc + 1);
+ if (i == 'C') k_ptr->x_char = (cc - 1);
}
}
@@ -3035,12 +3033,15 @@ void do_cmd_knowledge_artifacts()
auto const &k_info = game->edit_data.k_info;
auto const &a_info = game->edit_data.a_info;
+ auto const k_info_keys = game->edit_data.k_info_keys();
+
int i, z, x, y;
char base_name[80];
/* Scan the artifacts */
std::vector<bool_> okay(a_info.size(), FALSE);
+
for (std::size_t k = 0; k < a_info.size(); k++)
{
auto a_ptr = &a_info[k];
@@ -3055,10 +3056,10 @@ void do_cmd_knowledge_artifacts()
okay[k] = TRUE;
}
- std::vector<bool_> okayk(k_info.size(), FALSE);
- for (std::size_t k = 0; k < k_info.size(); k++)
+ std::unordered_set<int> okayk;
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_info[k];
+ auto k_ptr = &k_entry.second;
/* Skip "empty" artifacts */
if (!(k_ptr->flags & TR_NORM_ART)) continue;
@@ -3067,7 +3068,7 @@ void do_cmd_knowledge_artifacts()
if (!k_ptr->artifact) continue;
/* Assume okay */
- okayk[k] = TRUE;
+ okayk.insert(k_entry.first);
}
/* Check the dungeon */
@@ -3093,9 +3094,9 @@ void do_cmd_knowledge_artifacts()
if (object_known_p(o_ptr)) continue;
/* Note the artifact */
- if (k_info[o_ptr->k_idx].flags & TR_NORM_ART)
+ if (k_info.at(o_ptr->k_idx).flags & TR_NORM_ART)
{
- okayk[o_ptr->k_idx] = FALSE;
+ okayk.erase(o_ptr->k_idx);
}
else
{
@@ -3126,9 +3127,9 @@ void do_cmd_knowledge_artifacts()
if (object_known_p(o_ptr)) continue;
/* Note the artifact */
- if (k_info[o_ptr->k_idx].flags & TR_NORM_ART)
+ if (k_info.at(o_ptr->k_idx).flags & TR_NORM_ART)
{
- okayk[o_ptr->k_idx] = FALSE;
+ okayk.erase(o_ptr->k_idx);
}
else
{
@@ -3155,9 +3156,9 @@ void do_cmd_knowledge_artifacts()
if (object_known_p(o_ptr)) continue;
/* Note the artifact */
- if (k_info[o_ptr->k_idx].flags & TR_NORM_ART)
+ if (k_info.at(o_ptr->k_idx).flags & TR_NORM_ART)
{
- okayk[o_ptr->k_idx] = FALSE;
+ okayk.erase(o_ptr->k_idx);
}
else
{
@@ -3212,29 +3213,28 @@ void do_cmd_knowledge_artifacts()
w.write(" The {}\n", base_name);
}
- for (std::size_t k = 0; k < k_info.size(); k++)
+ for (auto const &k: k_info_keys)
{
/* List "dead" ones */
- if (!okayk[k]) continue;
+ if (!okayk.count(k))
+ {
+ continue;
+ }
/* Paranoia */
strcpy(base_name, "Unknown Artifact");
- /* Real object */
- if (k)
- {
- object_type forge;
- object_type *q_ptr;
+ object_type forge;
+ object_type *q_ptr;
- /* Get local object */
- q_ptr = &forge;
+ /* Get local object */
+ q_ptr = &forge;
- /* Create fake object */
- object_prep(q_ptr, k);
+ /* Create fake object */
+ object_prep(q_ptr, k);
- /* Describe the artifact */
- object_desc_store(base_name, q_ptr, FALSE, 0);
- }
+ /* Describe the artifact */
+ object_desc_store(base_name, q_ptr, FALSE, 0);
/* Hack -- Build the artifact name */
w.write(" The {}\n", base_name);
@@ -3578,16 +3578,19 @@ static void do_cmd_knowledge_kill_count()
static void do_cmd_knowledge_objects()
{
auto const &k_info = game->edit_data.k_info;
+ auto const k_info_keys = game->edit_data.k_info_keys();
fmt::MemoryWriter w;
- /* Scan the object kinds */
- for (std::size_t k = 1; k < k_info.size(); k++)
+ for (auto const &k: k_info_keys)
{
- auto k_ptr = &k_info[k];
+ auto k_ptr = &k_info.at(k);
/* Hack -- skip artifacts */
- if (k_ptr->flags & (TR_INSTA_ART)) continue;
+ if (k_ptr->flags & TR_INSTA_ART)
+ {
+ continue;
+ }
/* List known flavored objects */
if (k_ptr->flavor && k_ptr->aware)
diff --git a/src/cmd6.cc b/src/cmd6.cc
index e36f0cb6..508aaabc 100644
--- a/src/cmd6.cc
+++ b/src/cmd6.cc
@@ -1007,7 +1007,7 @@ void do_cmd_eat_food()
ident = FALSE;
/* Object level */
- lev = k_info[o_ptr->k_idx].level;
+ lev = k_info.at(o_ptr->k_idx).level;
/* Scripted foods */
hook_eat_in in = { o_ptr };
@@ -2507,7 +2507,7 @@ void do_cmd_quaff_potion()
ident = FALSE;
/* Object level */
- lev = k_info[o_ptr->k_idx].level;
+ lev = k_info.at(o_ptr->k_idx).level;
/* Demon Breath corruption can spoil potions. */
if (player_has_corruption(CORRUPT_DEMON_BREATH) && magik(9))
@@ -2682,9 +2682,9 @@ void do_cmd_drink_fountain()
sval = c_ptr->special - SV_POTION_LAST;
}
- for (auto const &k_ref: k_info)
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_ref;
+ auto k_ptr = &k_entry.second;
if (k_ptr->tval != tval) continue;
if (k_ptr->sval != sval) continue;
@@ -2902,7 +2902,7 @@ void do_cmd_read_scroll()
int ident = FALSE;
/* Object level */
- int lev = k_info[o_ptr->k_idx].level;
+ int lev = k_info.at(o_ptr->k_idx).level;
/* Assume the scroll will get used up */
int used_up = TRUE;
@@ -4044,7 +4044,7 @@ void do_cmd_zap_rod()
{
auto const &k_info = game->edit_data.k_info;
- int item, ident, chance, dir, lev;
+ int item, ident, chance, dir;
int cost;
@@ -4140,8 +4140,8 @@ void do_cmd_zap_rod()
ident = FALSE;
/* Extract the item level */
- auto tip_ptr = &k_info[lookup_kind(TV_ROD, o_ptr->pval)];
- lev = k_info[lookup_kind(TV_ROD, o_ptr->pval)].level;
+ auto const tip_ptr = &k_info.at(lookup_kind(TV_ROD, o_ptr->pval));
+ auto const lev = tip_ptr->level;
/* Base chance of success */
chance = p_ptr->skill_dev;
@@ -4832,7 +4832,7 @@ void do_cmd_activate()
energy_use = 100;
/* Extract the item level */
- lev = k_info[o_ptr->k_idx].level;
+ lev = k_info.at(o_ptr->k_idx).level;
/* Hack -- Use artifact level instead */
if (artifact_p(o_ptr))
@@ -4991,7 +4991,7 @@ const char *activation_aux(object_type * o_ptr, bool_ doit, int item)
/* Intrinsic to item type (rings of Ice, etc) */
if (!spell)
- spell = k_info[o_ptr->k_idx].activate;
+ spell = k_info.at(o_ptr->k_idx).activate;
/* Complain about mis-configured .txt files? */
if (!spell)
diff --git a/src/cmd7.cc b/src/cmd7.cc
index 6c77f82f..0d58647b 100644
--- a/src/cmd7.cc
+++ b/src/cmd7.cc
@@ -124,7 +124,7 @@ void mimic_info(char *p, int power)
switch (power)
{
case 0:
- strnfmt(p, 80, " dur %d", k_info[o_ptr->k_idx].pval2 + get_skill_scale(SKILL_MIMICRY, 1000));
+ strnfmt(p, 80, " dur %d", k_info.at(o_ptr->k_idx).pval2 + get_skill_scale(SKILL_MIMICRY, 1000));
break;
case 1:
strnfmt(p, 80, " dur %d+d20", 10 + plev);
@@ -832,7 +832,7 @@ void do_cmd_mimic_lore()
/* Success */
else
{
- set_mimic(k_info[o_ptr->k_idx].pval2 + get_skill_scale(SKILL_MIMICRY, 1000), o_ptr->pval2, get_skill(SKILL_MIMICRY));
+ set_mimic(k_info.at(o_ptr->k_idx).pval2 + get_skill_scale(SKILL_MIMICRY, 1000), o_ptr->pval2, get_skill(SKILL_MIMICRY));
}
}
diff --git a/src/dungeon.cc b/src/dungeon.cc
index f8671387..47f47e92 100644
--- a/src/dungeon.cc
+++ b/src/dungeon.cc
@@ -136,7 +136,7 @@ static byte value_check_aux1_magic(object_type const *o_ptr)
{
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
switch (o_ptr->tval)
@@ -227,7 +227,7 @@ static byte value_check_aux2_magic(object_type const *o_ptr)
{
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
switch (o_ptr->tval)
@@ -4457,7 +4457,7 @@ static void process_player()
{
/* Acquire object -- for speed only base items are allowed to shimmer */
object_type *o_ptr = &o_list[i];
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Skip dead or carried objects */
if ((!o_ptr->k_idx) || (!o_ptr->ix)) continue;
diff --git a/src/files.cc b/src/files.cc
index 6b3de0f0..dc9262a0 100644
--- a/src/files.cc
+++ b/src/files.cc
@@ -341,12 +341,12 @@ errr process_pref_file_aux(char *buf)
n1 = strtol(zz[1], NULL, 0);
n2 = strtol(zz[2], NULL, 0);
- if (i >= k_info.size())
+ if (!k_info.count(i))
{
return (1);
}
- auto k_ptr = &k_info[i];
+ auto k_ptr = &k_info.at(i);
if (n1)
{
@@ -427,10 +427,10 @@ errr process_pref_file_aux(char *buf)
n1 = strtol(zz[1], NULL, 0);
n2 = strtol(zz[2], NULL, 0);
- for (auto &k_ref: k_info)
+ for (auto &k_entry: k_info)
{
- auto k_ptr = &k_ref;
- if (k_ptr->tval == j)
+ auto &k_ref = k_entry.second;
+ if (k_ref.tval == j)
{
if (n1)
{
@@ -4044,9 +4044,9 @@ static long total_points()
temp /= comp_death;
/* The known objects increase the score */
- for (std::size_t k = 1; k < k_info.size(); k++)
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_info[k];
+ auto k_ptr = &k_entry.second;
/* Hack -- skip artifacts */
if (k_ptr->flags & TR_INSTA_ART) continue;
@@ -4061,7 +4061,7 @@ static long total_points()
i_ptr = &object_type_body;
/* Create fake object */
- object_prep(i_ptr, k);
+ object_prep(i_ptr, k_entry.first);
temp += object_value_real(i_ptr);
}
diff --git a/src/game_edit_data.cc b/src/game_edit_data.cc
new file mode 100644
index 00000000..1195dd14
--- /dev/null
+++ b/src/game_edit_data.cc
@@ -0,0 +1,18 @@
+#include "game_edit_data.hpp"
+
+#include <algorithm>
+
+std::vector<int> const GameEditData::k_info_keys() const
+{
+ std::vector<int> keys;
+
+ std::transform(std::begin(k_info),
+ std::end(k_info),
+ std::back_inserter(keys),
+ [] (auto e) { return e.first; });
+
+ std::sort(std::begin(keys),
+ std::end(keys));
+
+ return keys;
+};
diff --git a/src/game_edit_data.hpp b/src/game_edit_data.hpp
index a4727d90..e83a5d51 100644
--- a/src/game_edit_data.hpp
+++ b/src/game_edit_data.hpp
@@ -21,6 +21,7 @@
#include "vault_type.hpp"
#include "wilderness_type_info.hpp"
+#include <unordered_map>
#include <vector>
/**
@@ -68,7 +69,12 @@ struct GameEditData {
/**
* Object kinds
*/
- std::vector<object_kind> k_info;
+ std::unordered_map<int, object_kind> k_info;
+
+ /**
+ * Get a sorted vector of all the keys of k_info.
+ */
+ std::vector<int> const k_info_keys() const;
/**
* Building actions.
diff --git a/src/generate.cc b/src/generate.cc
index 1d5a17b6..1da96bb9 100644
--- a/src/generate.cc
+++ b/src/generate.cc
@@ -830,9 +830,9 @@ static void place_fountain(int y, int x)
int maxsval = 0;
/* List of usable svals */
- for (auto const &k_ref: k_info)
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_ref;
+ auto k_ptr = &k_entry.second;
if (((k_ptr->tval == TV_POTION) || (k_ptr->tval == TV_POTION2)) &&
(k_ptr->level <= dun_level) && (k_ptr->flags & TR_FOUNTAIN))
@@ -7971,7 +7971,7 @@ static bool_ cave_gen()
}
if (m_idx && d_ptr->final_object &&
- (k_info[d_ptr->final_object].artifact == FALSE))
+ (k_info.at(d_ptr->final_object).artifact == FALSE))
{
object_type *q_ptr, forge, *o_ptr;
int o_idx;
@@ -8003,7 +8003,7 @@ static bool_ cave_gen()
k_info[d_ptr->final_object].allow_special = FALSE;
- k_info[d_ptr->final_object].artifact = TRUE;
+ k_info.at(d_ptr->final_object).artifact = TRUE;
/* Get the item */
o_ptr = &o_list[o_idx];
diff --git a/src/init1.cc b/src/init1.cc
index 2224bc25..65372dbe 100644
--- a/src/init1.cc
+++ b/src/init1.cc
@@ -2285,8 +2285,8 @@ errr init_k_info_txt(FILE *fp)
/* Save the index */
error_idx = i;
- /* Point at the "info" */
- k_ptr = &expand_to_fit_index(k_info, i);
+ /* Point at the "info"; automatically creates an entry */
+ k_ptr = &k_info[i];
/* Advance and Save the name index */
assert(!k_ptr->name);
@@ -5354,7 +5354,12 @@ errr init_st_info_txt(FILE *fp)
/* Add to items array */
auto chance = atoi(buf + 2);
int k_idx = test_item_name(s);
- assert(k_idx >= 0);
+
+ if (k_idx < 0)
+ {
+ msg_format("Unknown k_info entry: [%s].", s);
+ return 1;
+ }
st_ptr->items.emplace_back(
store_item::k_idx(k_idx, chance));
diff --git a/src/init2.cc b/src/init2.cc
index f1d68dec..c42c5970 100644
--- a/src/init2.cc
+++ b/src/init2.cc
@@ -714,9 +714,9 @@ static errr init_alloc()
/* Scan the objects */
std::size_t kind_size = 0;
- for (auto const &k_ref: k_info)
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_ref;
+ auto k_ptr = &k_entry.second;
/* Scan allocation pairs */
for (std::size_t j = 0; j < ALLOCATION_MAX; j++)
@@ -751,9 +751,9 @@ static errr init_alloc()
alloc.kind_table.resize(kind_size);
/* Scan the objects */
- for (std::size_t i = 1; i < k_info.size(); i++)
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_info[i];
+ auto const k_ptr = &k_entry.second;
/* Scan allocation pairs */
for (std::size_t j = 0; j < ALLOCATION_MAX; j++)
@@ -777,7 +777,7 @@ static errr init_alloc()
/* Load the entry */
auto &entry = alloc.kind_table[z];
- entry.index = i;
+ entry.index = k_entry.first;
entry.level = x;
entry.prob1 = p;
entry.prob2 = p;
@@ -928,7 +928,7 @@ static void init_guardians()
/* Mark the final object */
if (d_ptr->final_object)
{
- auto k_ptr = &k_info[d_ptr->final_object];
+ auto k_ptr = &k_info.at(d_ptr->final_object);
k_ptr->flags |= TR_SPECIAL_GENE;
}
diff --git a/src/loadsave.cc b/src/loadsave.cc
index 3c843a36..94445736 100644
--- a/src/loadsave.cc
+++ b/src/loadsave.cc
@@ -216,6 +216,23 @@ static void do_s32b(s32b *ip, ls_flag_t flag)
do_u32b((u32b *)ip, flag);
}
+static void do_int(int *sz, ls_flag_t flag)
+{
+ u32b x;
+
+ if (flag == ls_flag_t::SAVE)
+ {
+ x = *sz;
+ }
+
+ do_u32b(&x, flag);
+
+ if (flag == ls_flag_t::LOAD)
+ {
+ *sz = x;
+ }
+}
+
static void save_std_string(std::string const *s)
{
// Length prefix.
@@ -322,6 +339,52 @@ template<typename A, typename F> void do_array(std::string const &what, ls_flag_
}
}
+template<typename M, typename FK, typename FV> void do_fixed_map(ls_flag_t flag, M &map, FK fk, FV fv)
+{
+ // Since our file format is currently quite inflexible, we'll
+ // have to prefix with the size of the map and store everything
+ // as key-value pairs.
+ u32b n = map.size();
+ do_u32b(&n, flag);
+
+ if (flag == ls_flag_t::LOAD)
+ {
+ // Read each of the n entries. We ignore data for keys
+ // which no longer exist. This is pretty common if e.g.
+ // game data gets removed.
+ for (std::size_t i = 0; i < n; i++)
+ {
+ // Read key
+ typename M::key_type key;
+ fk(&key, flag);
+ // If the key is present, we'll update the value
+ // by reading. Otherwise just read into a dummy
+ // value.
+ if (map.count(key))
+ {
+ fv(&map.at(key), flag);
+ }
+ else
+ {
+ typename M::mapped_type v;
+ fv(&v, flag);
+ }
+ }
+ }
+
+ if (flag == ls_flag_t::SAVE)
+ {
+ // Write each of the n entries.
+ for (auto &entry: map)
+ {
+ auto key = entry.first;
+ auto value = entry.second;
+ fk(&key, flag);
+ fv(&value, flag);
+ }
+ }
+}
+
static void do_bytes(ls_flag_t flag, std::uint8_t *buf, std::size_t n)
{
for (std::size_t i = 0; i < n; i++)
@@ -1029,7 +1092,7 @@ static void do_item(object_type *o_ptr, ls_flag_t flag)
/*********** END OF ls_flag_t::SAVE ***************/
/* Obtain the "kind" template */
- object_kind *k_ptr = &k_info[o_ptr->k_idx];
+ object_kind *k_ptr = &k_info.at(o_ptr->k_idx);
/* Obtain tval/sval from k_info */
o_ptr->tval = k_ptr->tval;
@@ -1940,8 +2003,8 @@ static bool do_object_lore(ls_flag_t flag)
{
auto &k_info = game->edit_data.k_info;
- do_array("object kinds", flag, k_info, k_info.size(),
- [](auto k_ptr, auto flag) -> void {
+ do_fixed_map(flag, k_info, do_int,
+ [](object_kind *k_ptr, auto flag) -> void {
do_bool(&k_ptr->aware, flag);
do_bool(&k_ptr->tried, flag);
do_bool(&k_ptr->artifact, flag);
diff --git a/src/monster2.cc b/src/monster2.cc
index 624dca53..4599f8b6 100644
--- a/src/monster2.cc
+++ b/src/monster2.cc
@@ -499,9 +499,9 @@ void delete_monster_idx(int i)
{
random_artifacts[o_ptr->sval].generated = FALSE;
}
- else if (k_info[o_ptr->k_idx].flags & TR_NORM_ART)
+ else if (k_info.at(o_ptr->k_idx).flags & TR_NORM_ART)
{
- k_info[o_ptr->k_idx].artifact = FALSE;
+ k_info.at(o_ptr->k_idx).artifact = FALSE;
}
else
{
@@ -1916,9 +1916,9 @@ void monster_carry(monster_type *m_ptr, int m_idx, object_type *q_ptr)
{
a_info[q_ptr->name1].cur_num = 0;
}
- else if (k_info[q_ptr->k_idx].flags & TR_NORM_ART)
+ else if (k_info.at(q_ptr->k_idx).flags & TR_NORM_ART)
{
- k_info[q_ptr->k_idx].artifact = 0;
+ k_info.at(q_ptr->k_idx).artifact = 0;
}
else if (q_ptr->tval == TV_RANDART)
{
@@ -1957,7 +1957,7 @@ bool_ kind_is_randart(int k_idx)
auto const &k_info = game->edit_data.k_info;
int max;
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
if (!kind_is_legal(k_idx)) return (FALSE);
diff --git a/src/object1.cc b/src/object1.cc
index 3fb2ef26..db7c40c7 100644
--- a/src/object1.cc
+++ b/src/object1.cc
@@ -592,12 +592,9 @@ void flavor_init()
set_complex_rng();
/* Analyze every object */
- for (auto &k_ref: k_info)
+ for (auto &k_entry: k_info)
{
- auto k_ptr = &k_ref;
-
- /* Skip "empty" objects */
- if (!k_ptr->name) continue;
+ auto k_ptr = &k_entry.second;
/* Extract "flavor" (if any) */
k_ptr->flavor = object_flavor(k_ptr);
@@ -652,8 +649,10 @@ void reset_visuals()
}
/* Extract default attr/char code for objects */
- for (auto &k_ref: k_info)
+ for (auto &k_entry: k_info)
{
+ auto &k_ref = k_entry.second;
+
/* Default attr/char */
k_ref.x_attr = k_ref.d_attr;
k_ref.x_char = k_ref.d_char;
@@ -789,7 +788,7 @@ object_flag_set object_flags(object_type const *o_ptr)
auto const &k_info = game->edit_data.k_info;
auto const &a_info = game->edit_data.a_info;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Base object */
auto f = k_ptr->flags;
@@ -823,7 +822,7 @@ int object_power(object_type *o_ptr)
auto const &a_info = game->edit_data.a_info;
auto const &e_info = game->edit_data.e_info;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
int power = -1;
/* Base object */
@@ -874,7 +873,7 @@ object_flag_set object_flags_known(object_type const *o_ptr)
auto const &k_info = game->edit_data.k_info;
auto const &a_info = game->edit_data.a_info;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Must be identified */
if (!object_known_p(o_ptr))
@@ -1032,7 +1031,7 @@ static std::string object_desc_aux(object_type const *o_ptr, int pref, int mode)
bool_ show_weapon = FALSE;
bool_ show_armour = FALSE;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Extract some flags */
auto const flags = object_flags(o_ptr);
@@ -1193,7 +1192,7 @@ static std::string object_desc_aux(object_type const *o_ptr, int pref, int mode)
case TV_ROD_MAIN:
{
- modstr = k_info[lookup_kind(TV_ROD, o_ptr->pval)].name;
+ modstr = k_info.at(lookup_kind(TV_ROD, o_ptr->pval)).name;
break;
}
@@ -2054,7 +2053,7 @@ void object_desc_store(char *buf, object_type *o_ptr, int pref, int mode)
auto &k_info = game->edit_data.k_info;
/* Save the "aware" flag */
- bool_ hack_aware = k_info[o_ptr->k_idx].aware;
+ bool_ hack_aware = k_info.at(o_ptr->k_idx).aware;
/* Save the "known" flag */
bool_ hack_known = (o_ptr->ident & (IDENT_KNOWN)) ? TRUE : FALSE;
@@ -2064,7 +2063,7 @@ void object_desc_store(char *buf, object_type *o_ptr, int pref, int mode)
o_ptr->ident |= (IDENT_KNOWN);
/* Force "aware" for description */
- k_info[o_ptr->k_idx].aware = TRUE;
+ k_info.at(o_ptr->k_idx).aware = TRUE;
/* Describe the object */
@@ -2072,7 +2071,7 @@ void object_desc_store(char *buf, object_type *o_ptr, int pref, int mode)
/* Restore "aware" flag */
- k_info[o_ptr->k_idx].aware = hack_aware;
+ k_info.at(o_ptr->k_idx).aware = hack_aware;
/* Clear the known flag */
if (!hack_known) o_ptr->ident &= ~(IDENT_KNOWN);
@@ -2481,7 +2480,7 @@ bool_ object_out_desc(object_type *o_ptr, FILE *fff, bool_ trim_down, bool_ wait
{
if (o_ptr->k_idx && (!trim_down))
{
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
text_out_c(TERM_ORANGE, k_ptr->text);
text_out("\n");
@@ -6132,13 +6131,13 @@ byte object_attr(object_type const *o_ptr)
{
return random_artifacts[o_ptr->sval].attr;
}
- else if (k_info[o_ptr->k_idx].flavor)
+ else if (k_info.at(o_ptr->k_idx).flavor)
{
- return misc_to_attr[k_info[o_ptr->k_idx].flavor];
+ return misc_to_attr[k_info.at(o_ptr->k_idx).flavor];
}
else
{
- return k_info[o_ptr->k_idx].x_attr;
+ return k_info.at(o_ptr->k_idx).x_attr;
}
}
@@ -6151,13 +6150,13 @@ byte object_attr_default(object_type *o_ptr)
{
return random_artifacts[o_ptr->sval].attr;
}
- else if (k_info[o_ptr->k_idx].flavor)
+ else if (k_info.at(o_ptr->k_idx).flavor)
{
- return misc_to_attr[k_info[o_ptr->k_idx].flavor];
+ return misc_to_attr[k_info.at(o_ptr->k_idx).flavor];
}
else
{
- return k_info[o_ptr->k_idx].d_attr;
+ return k_info.at(o_ptr->k_idx).d_attr;
}
}
@@ -6171,13 +6170,13 @@ char object_char(object_type const *o_ptr)
{
auto const &k_info = game->edit_data.k_info;
- if (k_info[o_ptr->k_idx].flavor)
+ if (k_info.at(o_ptr->k_idx).flavor)
{
- return misc_to_char[k_info[o_ptr->k_idx].flavor];
+ return misc_to_char[k_info.at(o_ptr->k_idx).flavor];
}
else
{
- return k_info[o_ptr->k_idx].x_char;
+ return k_info.at(o_ptr->k_idx).x_char;
}
}
@@ -6185,13 +6184,13 @@ char object_char_default(object_type const *o_ptr)
{
auto const &k_info = game->edit_data.k_info;
- if (k_info[o_ptr->k_idx].flavor)
+ if (k_info.at(o_ptr->k_idx).flavor)
{
- return misc_to_char[k_info[o_ptr->k_idx].flavor];
+ return misc_to_char[k_info.at(o_ptr->k_idx).flavor];
}
else
{
- return k_info[o_ptr->k_idx].d_char;
+ return k_info.at(o_ptr->k_idx).d_char;
}
}
@@ -6206,7 +6205,7 @@ bool artifact_p(object_type const *o_ptr)
(o_ptr->tval == TV_RANDART) ||
(o_ptr->name1 ? true : false) ||
(!o_ptr->artifact_name.empty()) ||
- ((k_info[o_ptr->k_idx].flags & TR_NORM_ART) ? true : false);
+ ((k_info.at(o_ptr->k_idx).flags & TR_NORM_ART) ? true : false);
}
/**
diff --git a/src/object2.cc b/src/object2.cc
index da6c6644..b9841a90 100644
--- a/src/object2.cc
+++ b/src/object2.cc
@@ -270,7 +270,7 @@ void compact_objects(int size)
{
object_type *o_ptr = &o_list[i];
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Skip dead objects */
if (!o_ptr->k_idx) continue;
@@ -402,9 +402,9 @@ void wipe_o_list()
{
game->random_artifacts[o_ptr->sval].generated = FALSE;
}
- else if (k_info[o_ptr->k_idx].flags & TR_NORM_ART)
+ else if (k_info.at(o_ptr->k_idx).flags & TR_NORM_ART)
{
- k_info[o_ptr->k_idx].artifact = FALSE;
+ k_info.at(o_ptr->k_idx).artifact = FALSE;
}
else
{
@@ -594,7 +594,7 @@ s16b get_obj_num(int level)
k_idx = entry.index;
/* Access the actual kind */
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
/* Hack -- prevent embedded chests */
if (opening_chest && (k_ptr->tval == TV_CHEST)) continue;
@@ -732,7 +732,7 @@ bool object_known_p(object_type const *o_ptr)
auto const &k_info = game->edit_data.k_info;
return ((o_ptr->ident & (IDENT_KNOWN)) ||
- (k_info[o_ptr->k_idx].easy_know && k_info[o_ptr->k_idx].aware));
+ (k_info.at(o_ptr->k_idx).easy_know && k_info.at(o_ptr->k_idx).aware));
}
@@ -745,7 +745,7 @@ void object_aware(object_type *o_ptr)
auto &k_info = game->edit_data.k_info;
/* Fully aware of the effects */
- k_info[o_ptr->k_idx].aware = TRUE;
+ k_info.at(o_ptr->k_idx).aware = TRUE;
}
/**
@@ -755,7 +755,7 @@ bool object_aware_p(object_type const *o_ptr)
{
auto const &k_info = game->edit_data.k_info;
- return k_info[o_ptr->k_idx].aware;
+ return k_info.at(o_ptr->k_idx).aware;
}
@@ -767,7 +767,7 @@ void object_tried(object_type *o_ptr)
auto &k_info = game->edit_data.k_info;
/* Mark it as tried (even if "aware") */
- k_info[o_ptr->k_idx].tried = TRUE;
+ k_info.at(o_ptr->k_idx).tried = TRUE;
}
@@ -778,7 +778,7 @@ bool object_tried_p(object_type const *o_ptr)
{
auto const &k_info = game->edit_data.k_info;
- return k_info[o_ptr->k_idx].tried;
+ return k_info.at(o_ptr->k_idx).tried;
}
@@ -791,7 +791,7 @@ static s32b object_value_base(object_type const *o_ptr)
auto const &r_info = game->edit_data.r_info;
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Aware item -- use template cost */
if ((object_aware_p(o_ptr)) && (o_ptr->tval != TV_EGG)) return (k_ptr->cost);
@@ -1098,7 +1098,7 @@ s32b object_value_real(object_type const *o_ptr)
s32b value;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
if (o_ptr->tval == TV_RANDART)
{
@@ -1292,7 +1292,7 @@ s32b object_value_real(object_type const *o_ptr)
if (tip_idx > 0)
{
/* Add its cost */
- value += k_info[tip_idx].cost;
+ value += k_info.at(tip_idx).cost;
}
/* Done */
@@ -1810,12 +1810,12 @@ s16b lookup_kind(int tval, int sval)
{
auto const &k_info = game->edit_data.k_info;
- for (std::size_t k = 1; k < k_info.size(); k++)
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_info[k];
- if ((k_ptr->tval == tval) && (k_ptr->sval == sval))
+ auto const &k_ref = k_entry.second;
+ if ((k_ref.tval == tval) && (k_ref.sval == sval))
{
- return k;
+ return k_entry.first;
}
}
@@ -1865,7 +1865,7 @@ void object_prep(object_type *o_ptr, int k_idx)
{
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
/* Clear the record */
object_wipe(o_ptr);
@@ -2223,10 +2223,10 @@ static bool_ make_artifact_special(object_type *o_ptr)
int k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
/* XXX XXX Enforce minimum "object" level (loosely) */
- if (k_info[k_idx].level > object_level)
+ if (k_info.at(k_idx).level > object_level)
{
/* Acquire the "out-of-depth factor" */
- int d = (k_info[k_idx].level - object_level) * 5;
+ int d = (k_info.at(k_idx).level - object_level) * 5;
/* Roll for out-of-depth creation */
if (rand_int(d) != 0) continue;
@@ -2244,7 +2244,7 @@ static bool_ make_artifact_special(object_type *o_ptr)
/* Hack give a basic exp/exp level to an object that needs it */
if (flags & TR_LEVELS)
{
- init_obj_exp(o_ptr, &k_info[k_idx]);
+ init_obj_exp(o_ptr, &k_info.at(k_idx));
}
/* Success */
@@ -2320,7 +2320,7 @@ static bool_ make_artifact(object_type *o_ptr)
/* Hack give a basic exp/exp level to an object that needs it */
if (flags & TR_LEVELS)
{
- init_obj_exp(o_ptr, &k_info[o_ptr->k_idx]);
+ init_obj_exp(o_ptr, &k_info.at(o_ptr->k_idx));
}
/* Success */
@@ -2342,7 +2342,7 @@ static bool_ make_ego_item(object_type *o_ptr, bool_ good)
auto const &e_info = game->edit_data.e_info;
bool_ ret = FALSE;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
if (artifact_p(o_ptr) || o_ptr->name2) return (FALSE);
@@ -3166,7 +3166,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
auto const &k_info = game->edit_data.k_info;
s32b bonus_lvl, max_lvl;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Very good */
if (power > 1)
@@ -3218,9 +3218,9 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
/* Hack -- random fuel */
if (flags & TR_FUEL_LITE)
{
- if (k_info[o_ptr->k_idx].pval2 > 0)
+ if (k_info.at(o_ptr->k_idx).pval2 > 0)
{
- o_ptr->timeout = randint(k_info[o_ptr->k_idx].pval2);
+ o_ptr->timeout = randint(k_info.at(o_ptr->k_idx).pval2);
}
}
@@ -3361,7 +3361,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
case TV_CHEST:
{
/* Hack -- skip ruined chests */
- if (k_info[o_ptr->k_idx].level <= 0) break;
+ if (k_info.at(o_ptr->k_idx).level <= 0) break;
/* Hack - set pval2 to the number of objects in it */
if (o_ptr->pval)
@@ -3865,7 +3865,7 @@ void apply_magic(object_type *o_ptr, int lev, bool_ okay, bool_ good, bool_ grea
auto const &e_info = game->edit_data.e_info;
int i, rolls;
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Aply luck */
lev += luck( -7, 7);
@@ -4196,7 +4196,7 @@ try_an_other_ego:
/* Examine real objects */
if (o_ptr->k_idx)
{
- object_kind *k_ptr = &k_info[o_ptr->k_idx];
+ object_kind *k_ptr = &k_info.at(o_ptr->k_idx);
/* Hack -- acquire "cursed" flag */
if (k_ptr->flags & TR_CURSED) o_ptr->ident |= (IDENT_CURSED);
@@ -4268,7 +4268,7 @@ static bool kind_is_theme(obj_theme const *theme, int k_idx)
assert(theme != nullptr);
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
s32b prob = 0;
@@ -4452,13 +4452,13 @@ bool_ kind_is_legal(int k_idx)
{
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
if (!kind_is_theme(match_theme, k_idx)) return FALSE;
if (k_ptr->flags & TR_SPECIAL_GENE)
{
- return k_info[k_idx].allow_special;
+ return k_ptr->allow_special;
}
/* No 2 times the same normal artifact */
@@ -4497,7 +4497,7 @@ static bool_ kind_is_good(int k_idx)
{
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
if (!kind_is_legal(k_idx)) return FALSE;
@@ -4595,7 +4595,7 @@ bool_ kind_is_artifactable(int k_idx)
auto const &ra_info = game->edit_data.ra_info;
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
if (kind_is_good(k_idx))
{
// Consider the item artifactable if there is at least one
@@ -4722,10 +4722,10 @@ bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const &
/* Notice "okay" out-of-depth objects */
if (!cursed_p(j_ptr) &&
- (k_info[j_ptr->k_idx].level > dun_level))
+ (k_info.at(j_ptr->k_idx).level > dun_level))
{
/* Rating increase */
- rating += (k_info[j_ptr->k_idx].level - dun_level);
+ rating += (k_info.at(j_ptr->k_idx).level - dun_level);
/* Cheat -- peek at items */
if (options->cheat_peek || p_ptr->precognition)
@@ -4838,9 +4838,9 @@ void place_object(int y, int x, bool_ good, bool_ great, int where)
{
a_info[q_ptr->name1].cur_num = 0;
}
- else if (k_info[q_ptr->k_idx].flags & TR_NORM_ART)
+ else if (k_info.at(q_ptr->k_idx).flags & TR_NORM_ART)
{
- k_info[q_ptr->k_idx].artifact = 0;
+ k_info.at(q_ptr->k_idx).artifact = 0;
}
else if (q_ptr->tval == TV_RANDART)
{
@@ -4865,13 +4865,8 @@ bool_ make_gold(object_type *j_ptr)
{
auto const &k_info = game->edit_data.k_info;
- int i;
-
- s32b base;
-
-
/* Hack -- Pick a Treasure variety */
- i = ((randint(object_level + 2) + 2) / 2) - 1;
+ int i = ((randint(object_level + 2) + 2) / 2) - 1;
/* Apply "extra" magic */
if (rand_int(GREAT_OBJ) == 0)
@@ -4889,7 +4884,7 @@ bool_ make_gold(object_type *j_ptr)
object_prep(j_ptr, OBJ_GOLD_LIST + i);
/* Hack -- Base coin cost */
- base = k_info[OBJ_GOLD_LIST + i].cost;
+ s32b const base = k_info.at(OBJ_GOLD_LIST + i).cost;
/* Determine how much the treasure is "worth" */
j_ptr->pval = (base + (8L * randint(base)) + randint(8));
@@ -5215,9 +5210,9 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
{
a_info[j_ptr->name1].cur_num = 0;
}
- else if (k_info[j_ptr->k_idx].flags & TR_NORM_ART)
+ else if (k_info.at(j_ptr->k_idx).flags & TR_NORM_ART)
{
- k_info[j_ptr->k_idx].artifact = 0;
+ k_info.at(j_ptr->k_idx).artifact = 0;
}
else if (j_ptr->tval == TV_RANDART)
{
diff --git a/src/powers.cc b/src/powers.cc
index f3ffe668..2c4ebe3e 100644
--- a/src/powers.cc
+++ b/src/powers.cc
@@ -887,7 +887,7 @@ static void power_activate(int power)
o_ptr = get_object(item);
- lev = k_info[o_ptr->k_idx].level;
+ lev = k_info.at(o_ptr->k_idx).level;
if (o_ptr->tval == TV_ROD_MAIN)
{
diff --git a/src/q_rand.cc b/src/q_rand.cc
index b643ca39..9cbc19b4 100644
--- a/src/q_rand.cc
+++ b/src/q_rand.cc
@@ -272,9 +272,9 @@ static void do_get_new_obj(int y, int x)
{
game->random_artifacts[o_ptr->sval].generated = FALSE;
}
- else if (k_info[o_ptr->k_idx].flags & TR_NORM_ART)
+ else if (k_info.at(o_ptr->k_idx).flags & TR_NORM_ART)
{
- k_info[o_ptr->k_idx].artifact = FALSE;
+ k_info.at(o_ptr->k_idx).artifact = FALSE;
}
else if (o_ptr->name1)
{
diff --git a/src/spells1.cc b/src/spells1.cc
index 4df1a7c6..ac6a2c09 100644
--- a/src/spells1.cc
+++ b/src/spells1.cc
@@ -1826,7 +1826,7 @@ static int inven_damage(inven_func typ, int perc)
((amt > 1) ? "were" : "was"));
/* Potions smash open */
- if (k_info[o_ptr->k_idx].tval == TV_POTION)
+ if (k_info.at(o_ptr->k_idx).tval == TV_POTION)
{
potion_smash_effect(0, p_ptr->py, p_ptr->px, o_ptr->sval);
}
@@ -4071,7 +4071,7 @@ static bool_ project_o(int who, int r, int y, int x, int dam, int typ)
}
o_sval = o_ptr->sval;
- is_potion = ((k_info[o_ptr->k_idx].tval == TV_POTION) || (k_info[o_ptr->k_idx].tval == TV_POTION2));
+ is_potion = ((k_info.at(o_ptr->k_idx).tval == TV_POTION) || (k_info.at(o_ptr->k_idx).tval == TV_POTION2));
/* Delete the object */
diff --git a/src/spells2.cc b/src/spells2.cc
index c0d435ea..b90a0b9b 100644
--- a/src/spells2.cc
+++ b/src/spells2.cc
@@ -2414,7 +2414,7 @@ bool_ recharge(int power)
auto const flags = object_flags(o_ptr);
/* Extract the object "level" */
- lev = k_info[o_ptr->k_idx].level;
+ lev = k_info.at(o_ptr->k_idx).level;
/* Recharge a rod */
if (o_ptr->tval == TV_ROD_MAIN)
diff --git a/src/spells3.cc b/src/spells3.cc
index 58d42cab..d2f09a0e 100644
--- a/src/spells3.cc
+++ b/src/spells3.cc
@@ -2961,7 +2961,7 @@ casting_result udun_drain()
case TV_STAFF:
case TV_WAND:
{
- auto k_ptr = &k_info[o_ptr->k_idx];
+ auto k_ptr = &k_info.at(o_ptr->k_idx);
/* Generate mana */
increase_mana(o_ptr->pval * k_ptr->level * o_ptr->number);
diff --git a/src/squelch/condition.cc b/src/squelch/condition.cc
index cd7f879c..72ac1c8b 100644
--- a/src/squelch/condition.cc
+++ b/src/squelch/condition.cc
@@ -880,7 +880,7 @@ bool SymbolCondition::is_match(object_type *o_ptr) const
{
auto const &k_info = game->edit_data.k_info;
- return k_info[o_ptr->k_idx].d_char == m_symbol;
+ return k_info.at(o_ptr->k_idx).d_char == m_symbol;
}
std::shared_ptr<Condition> SymbolCondition::from_json(jsoncons::json const &j)
diff --git a/src/squeltch.cc b/src/squeltch.cc
index 9390ca42..e8ac4427 100644
--- a/src/squeltch.cc
+++ b/src/squeltch.cc
@@ -77,7 +77,7 @@ void squeltch_grid()
object_type * o_ptr = &o_list[this_o_idx];
// We've now seen one of these
- if (!k_info[o_ptr->k_idx].flavor)
+ if (!k_info.at(o_ptr->k_idx).flavor)
{
object_aware(o_ptr);
}
diff --git a/src/store.cc b/src/store.cc
index 1c593c7e..29d06d45 100644
--- a/src/store.cc
+++ b/src/store.cc
@@ -1161,12 +1161,12 @@ static bool_ kind_is_storeok(int k_idx)
{
auto const &k_info = game->edit_data.k_info;
- auto k_ptr = &k_info[k_idx];
+ auto k_ptr = &k_info.at(k_idx);
- if (k_info[k_idx].flags & TR_NORM_ART)
+ if (k_info.at(k_idx).flags & TR_NORM_ART)
return ( FALSE );
- if (k_info[k_idx].flags & TR_INSTA_ART)
+ if (k_info.at(k_idx).flags & TR_INSTA_ART)
return ( FALSE );
if (!kind_is_legal(k_idx)) return FALSE;
@@ -1184,7 +1184,7 @@ struct is_artifact_p : public boost::static_visitor<bool> {
bool operator ()(store_item_filter_by_k_idx f) const
{
auto const &k_info = game->edit_data.k_info;
- return bool(k_info[f.k_idx].flags & TR_NORM_ART);
+ return bool(k_info.at(f.k_idx).flags & TR_NORM_ART);
}
bool operator ()(store_item_filter_by_tval) const
@@ -1374,11 +1374,11 @@ static void store_create()
if (!obj_all_done)
{
/* Don't allow k_info artifacts */
- if (k_info[k_idx].flags & TR_NORM_ART)
+ if (k_info.at(k_idx).flags & TR_NORM_ART)
continue;
/* Don't allow artifacts */
- if (k_info[k_idx].flags & TR_INSTA_ART)
+ if (k_info.at(k_idx).flags & TR_INSTA_ART)
continue;
/* Get local object */
@@ -1396,7 +1396,7 @@ static void store_create()
auto const flags = object_flags(q_ptr);
if (flags & TR_FUEL_LITE)
{
- q_ptr->timeout = k_info[q_ptr->k_idx].pval2;
+ q_ptr->timeout = k_info.at(q_ptr->k_idx).pval2;
}
}
diff --git a/src/util.cc b/src/util.cc
index 7d795828..9565cbcc 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -3360,24 +3360,23 @@ int test_mego_name(cptr needle)
/*
* Given item name as string, return the index in k_info array. Name
- * must exactly match (look out for commas and the like!), or else 0 is
+ * must exactly match (look out for commas and the like!), or else -1 is
* returned. Case doesn't matter. -DG-
*/
-
int test_item_name(cptr needle)
{
auto const &k_info = game->edit_data.k_info;
- for (std::size_t i = 0; i < k_info.size(); i++)
+ for (auto const &k_entry: k_info)
{
- auto const &name = k_info[i].name;
+ auto const &name = k_entry.second.name;
if (name && iequals(needle, name))
{
- return i;
+ return k_entry.first;
}
}
- return 0;
+ return -1;
}
/*
diff --git a/src/wizard2.cc b/src/wizard2.cc
index 890c6fbe..fea57687 100644
--- a/src/wizard2.cc
+++ b/src/wizard2.cc
@@ -433,7 +433,7 @@ static void wiz_display_item(object_type *o_ptr)
prt(buf, 2, j);
prt(format("kind = %-5d level = %-4d tval = %-5d sval = %-5d",
- o_ptr->k_idx, k_info[o_ptr->k_idx].level,
+ o_ptr->k_idx, k_info.at(o_ptr->k_idx).level,
o_ptr->tval, o_ptr->sval), 4, j);
prt(format("number = %-3d wgt = %-6d ac = %-5d damage = %dd%d",
@@ -584,10 +584,9 @@ static int wiz_create_itemtype()
/* We have to search the whole itemlist. */
std::vector<std::size_t> choice;
choice.reserve(60);
- std::size_t i;
- for (i = 1; (choice.size() < 60) && (i < k_info.size()); i++)
+ for (auto &k_entry: k_info)
{
- auto k_ptr = &k_info[i];
+ auto k_ptr = &k_entry.second;
/* Analyze matching items */
if (k_ptr->tval == tval)
@@ -602,7 +601,11 @@ static int wiz_create_itemtype()
wci_string(buf, choice.size());
/* Remember the object index */
- choice.push_back(i);
+ choice.push_back(k_entry.first);
+ if (choice.size() >= 60)
+ {
+ break;
+ }
}
}
@@ -1225,7 +1228,7 @@ static void wiz_create_item_2()
int k_idx = atoi(out_val);
/* Return if failed or out-of-bounds */
- if ((k_idx <= 0) || (k_idx >= static_cast<int>(k_info.size())))
+ if (!k_info.count(k_idx))
{
return;
}
@@ -1368,9 +1371,9 @@ static void do_cmd_wiz_learn()
auto const &k_info = game->edit_data.k_info;
/* Scan every object */
- for (std::size_t i = 0; i < k_info.size(); i++)
+ for (auto const &k_entry: k_info)
{
- auto k_ptr = &k_info[i];
+ auto const k_ptr = &k_entry.second;
/* Induce awareness */
if (k_ptr->level <= command_arg)
@@ -1380,7 +1383,7 @@ static void do_cmd_wiz_learn()
auto q_ptr = &forge;
/* Prepare object */
- object_prep(q_ptr, i);
+ object_prep(q_ptr, k_entry.first);
/* Awareness */
object_aware(q_ptr);
diff --git a/src/xtra1.cc b/src/xtra1.cc
index ae797aa8..12dbfdbd 100644
--- a/src/xtra1.cc
+++ b/src/xtra1.cc
@@ -4309,7 +4309,7 @@ void gain_fate(byte fate)
/* Invalidate the cached allocation table */
alloc.kind_table_valid = false;
- auto k_ptr = &k_info[fates[i].o_idx];
+ auto k_ptr = &k_info.at(fates[i].o_idx);
if (!(k_ptr->flags & TR_INSTA_ART) && !(k_ptr->flags & TR_NORM_ART)) break;
}
diff --git a/src/xtra2.cc b/src/xtra2.cc
index d8b87f51..7885cf98 100644
--- a/src/xtra2.cc
+++ b/src/xtra2.cc
@@ -4245,7 +4245,7 @@ static int target_set_aux(int y, int x, int mode, cptr info)
sv = c_ptr->special - SV_POTION_LAST;
}
- info = k_info[lookup_kind(tv, sv)].name;
+ info = k_info.at(lookup_kind(tv, sv)).name;
}
/* Display a message */
@@ -4980,10 +4980,9 @@ static bool_ test_object_wish(char *name, object_type *o_ptr, object_type *forge
int save_aware;
char buf[200];
- /* try all objects, this *IS* a very ugly and slow method :( */
- for (std::size_t i = 0; i < k_info.size(); i++)
+ for (auto &k_entry: k_info)
{
- auto k_ptr = &k_info[i];
+ auto k_ptr = &k_entry.second;
o_ptr = forge;
@@ -4992,7 +4991,7 @@ static bool_ test_object_wish(char *name, object_type *o_ptr, object_type *forge
if (k_ptr->flags & TR_INSTA_ART) continue;
if (k_ptr->tval == TV_GOLD) continue;
- object_prep(o_ptr, i);
+ object_prep(o_ptr, k_entry.first);
o_ptr->name1 = 0;
o_ptr->name2 = 0;
apply_magic(o_ptr, dun_level, FALSE, FALSE, FALSE);
@@ -5066,7 +5065,7 @@ static bool_ test_object_wish(char *name, object_type *o_ptr, object_type *forge
}
}
- object_prep(o_ptr, i);
+ object_prep(o_ptr, k_entry.first);
o_ptr->name1 = 0;
o_ptr->name2 = j;
o_ptr->name2b = jb;