From 1eb13ec5a357c43c5366c276dce87fba6f713fc6 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Tue, 13 Jun 2017 18:24:42 +0200 Subject: Move alloc_* tables to Game struct We also change the arrays to std::vector<> --- src/alloc.hpp | 29 ++++++++++++++++++++++++++ src/alloc_entry.hpp | 10 ++++----- src/game.hpp | 6 ++++++ src/generate.cc | 3 ++- src/init2.cc | 49 +++++++++++++++++++------------------------- src/monster2.cc | 57 ++++++++++++++++++++++++++++----------------------- src/object2.cc | 59 +++++++++++++++++++++++++++++------------------------ src/store.cc | 5 +++-- src/variable.cc | 28 ------------------------- src/variable.hpp | 5 ----- src/xtra1.cc | 3 ++- 11 files changed, 131 insertions(+), 123 deletions(-) create mode 100644 src/alloc.hpp (limited to 'src') diff --git a/src/alloc.hpp b/src/alloc.hpp new file mode 100644 index 00000000..1d4ce815 --- /dev/null +++ b/src/alloc.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "h-basic.h" +#include "alloc_entry.hpp" + +#include + +/** + * Allocations of object kinds and monster races. + */ +struct Alloc { + + /* + * The entries in the "kind allocator table" + */ + std::vector kind_table; + + /* + * The flag to tell if kind_table contains valid entries + * for normal (i.e. kind_is_legal) object allocation + */ + bool kind_table_valid = false; + + /* + * The entries in the "race allocator table" + */ + std::vector race_table; + +}; diff --git a/src/alloc_entry.hpp b/src/alloc_entry.hpp index 41ec3b66..5e0e547f 100644 --- a/src/alloc_entry.hpp +++ b/src/alloc_entry.hpp @@ -11,10 +11,10 @@ */ struct alloc_entry { - s16b index; /* The actual index */ + s16b index = -1; /* The actual index */ - byte level; /* Base dungeon level */ - byte prob1; /* Probability, pass 1 */ - byte prob2; /* Probability, pass 2 */ - byte prob3; /* Probability, pass 3 */ + byte level = 0; /* Base dungeon level */ + byte prob1 = 0; /* Probability, pass 1 */ + byte prob2 = 0; /* Probability, pass 2 */ + byte prob3 = 0; /* Probability, pass 3 */ }; diff --git a/src/game.hpp b/src/game.hpp index 37da1cc0..6aaa5d47 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -2,6 +2,7 @@ #include "game_fwd.hpp" +#include "alloc.hpp" #include "birther.hpp" #include "game_edit_data.hpp" #include "grid.hpp" @@ -49,6 +50,11 @@ struct Game { */ std::vector random_artifacts; + /** + * Allocations + */ + Alloc alloc; + /** * Player's un-adjusted HP at every level. * Stored to avoid shenanigans with draininging levels diff --git a/src/generate.cc b/src/generate.cc index 5286532d..3a99a7fa 100644 --- a/src/generate.cc +++ b/src/generate.cc @@ -7552,6 +7552,7 @@ static bool_ cave_gen() auto const &r_info = game->edit_data.r_info; auto const &a_info = game->edit_data.a_info; auto &k_info = game->edit_data.k_info; + auto &alloc = game->alloc; auto d_ptr = &d_info[dungeon_type]; @@ -7783,7 +7784,7 @@ static bool_ cave_gen() get_obj_num_hook = kind_is_legal; /* Invalidate the allocation table */ - alloc_kind_table_valid = FALSE; + alloc.kind_table_valid = false; /* Get a local object */ q_ptr = &forge; diff --git a/src/init2.cc b/src/init2.cc index e79ba87c..6e130ac4 100644 --- a/src/init2.cc +++ b/src/init2.cc @@ -702,8 +702,7 @@ static errr init_alloc() { auto const &r_info = game->edit_data.r_info; auto const &k_info = game->edit_data.k_info; - - alloc_entry *table; + auto &alloc = game->alloc; s16b num[MAX_DEPTH_MONSTER]; @@ -717,10 +716,8 @@ static errr init_alloc() /* Clear the "num" array */ memset(num, 0, MAX_DEPTH_MONSTER * sizeof(s16b)); - /* Size of "alloc_kind_table" */ - alloc_kind_size = 0; - /* Scan the objects */ + std::size_t kind_size = 0; for (auto const &k_ref: k_info) { auto k_ptr = &k_ref; @@ -732,7 +729,7 @@ static errr init_alloc() if (k_ptr->chance[j]) { /* Count the entries */ - alloc_kind_size++; + kind_size++; /* Group by level */ num[k_ptr->locale[j]]++; @@ -754,10 +751,8 @@ static errr init_alloc() /*** Initialise object allocation info ***/ /* Allocate the alloc_kind_table */ - alloc_kind_table = make_array(alloc_kind_size); - - /* Access the table entry */ - table = alloc_kind_table; + alloc.kind_table.clear(); + alloc.kind_table.resize(kind_size); /* Scan the objects */ for (std::size_t i = 1; i < k_info.size(); i++) @@ -785,11 +780,12 @@ static errr init_alloc() z = y + aux[x]; /* Load the entry */ - table[z].index = i; - table[z].level = x; - table[z].prob1 = p; - table[z].prob2 = p; - table[z].prob3 = p; + auto &entry = alloc.kind_table[z]; + entry.index = i; + entry.level = x; + entry.prob1 = p; + entry.prob2 = p; + entry.prob3 = p; /* Another entry complete for this locale */ aux[x]++; @@ -806,10 +802,8 @@ static errr init_alloc() /* Clear the "num" array */ memset(num, 0, MAX_DEPTH_MONSTER * sizeof(s16b)); - /* Size of "alloc_race_table" */ - alloc_race_size = 0; - /* Scan the monsters */ + std::size_t race_size = 0; for (auto &r_ref: r_info) { /* Get the i'th race */ @@ -819,7 +813,7 @@ static errr init_alloc() if (r_ptr->rarity) { /* Count the entries */ - alloc_race_size++; + race_size++; /* Group by level */ num[r_ptr->level]++; @@ -840,10 +834,8 @@ static errr init_alloc() /*** Initialise monster allocation info ***/ /* Allocate the alloc_race_table */ - alloc_race_table = make_array(alloc_race_size); - - /* Access the table entry */ - table = alloc_race_table; + alloc.race_table.clear(); + alloc.race_table.resize(race_size); /* Scan the monsters */ for (std::size_t i = 1; i < r_info.size(); i++) @@ -869,11 +861,12 @@ static errr init_alloc() z = y + aux[x]; /* Load the entry */ - table[z].index = i; - table[z].level = x; - table[z].prob1 = p; - table[z].prob2 = p; - table[z].prob3 = p; + auto &entry = alloc.race_table[z]; + entry.index = i; + entry.level = x; + entry.prob1 = p; + entry.prob2 = p; + entry.prob3 = p; /* Another entry complete for this locale */ aux[x]++; diff --git a/src/monster2.cc b/src/monster2.cc index 0cd07c64..624dca53 100644 --- a/src/monster2.cc +++ b/src/monster2.cc @@ -816,27 +816,24 @@ s16b m_pop() */ errr get_mon_num_prep() { - int i; + auto &alloc = game->alloc; /* Scan the allocation table */ - for (i = 0; i < alloc_race_size; i++) + for (auto &&entry: alloc.race_table) { - /* Get the entry */ - alloc_entry *entry = &alloc_race_table[i]; - /* Accept monsters which pass the restriction, if any */ - if ((!get_mon_num_hook || (*get_mon_num_hook)(entry->index)) && - (!get_mon_num2_hook || (*get_mon_num2_hook)(entry->index))) + if ((!get_mon_num_hook || (*get_mon_num_hook)(entry.index)) && + (!get_mon_num2_hook || (*get_mon_num2_hook)(entry.index))) { /* Accept this monster */ - entry->prob2 = entry->prob1; + entry.prob2 = entry.prob1; } /* Do not use this monster */ else { /* Decline this monster */ - entry->prob2 = 0; + entry.prob2 = 0; } } @@ -950,12 +947,12 @@ bool_ summon_hack = FALSE; s16b get_mon_num(int level) { auto const &r_info = game->edit_data.r_info; + auto &alloc = game->alloc; - int i, j, p; - int r_idx; - long value, total; - - alloc_entry *table = alloc_race_table; + std::size_t i, j; + int p; + int r_idx; + long value, total; int in_tome; @@ -991,16 +988,18 @@ s16b get_mon_num(int level) in_tome = strcmp(game_module, "ToME") == 0; /* Process probabilities */ - for (i = 0; i < alloc_race_size; i++) + for (i = 0; i < alloc.race_table.size(); i++) { + auto &entry = alloc.race_table[i]; + /* Monsters are sorted by depth */ - if (table[i].level > level) break; + if (entry.level > level) break; /* Default */ - table[i].prob3 = 0; + entry.prob3 = 0; /* Access the "r_idx" of the chosen monster */ - r_idx = table[i].index; + r_idx = entry.index; /* Access the actual race */ auto r_ptr = &r_info[r_idx]; @@ -1031,10 +1030,10 @@ s16b get_mon_num(int level) if (!summon_hack && !restrict_monster_to_dungeon(r_idx) && dun_level) continue; /* Accept */ - table[i].prob3 = table[i].prob2; + entry.prob3 = entry.prob2; /* Total */ - total += table[i].prob3; + total += entry.prob3; } /* No legal monsters */ @@ -1045,19 +1044,24 @@ s16b get_mon_num(int level) value = rand_int(total); /* Find the monster */ - for (i = 0; i < alloc_race_size; i++) + for (i = 0; i < alloc.race_table.size(); i++) { + auto &entry = alloc.race_table[i]; + /* Found the entry */ - if (value < table[i].prob3) break; + if (value < entry.prob3) break; /* Decrement */ - value = value - table[i].prob3; + value = value - entry.prob3; } /* Power boost */ p = rand_int(100); + /* Shorthand */ + auto &table = alloc.race_table; + /* Try for a "harder" monster once (50%) or twice (10%) */ if (p < 60) { @@ -1068,7 +1072,7 @@ s16b get_mon_num(int level) value = rand_int(total); /* Find the monster */ - for (i = 0; i < alloc_race_size; i++) + for (i = 0; i < table.size(); i++) { /* Found the entry */ if (value < table[i].prob3) break; @@ -1091,7 +1095,7 @@ s16b get_mon_num(int level) value = rand_int(total); /* Find the monster */ - for (i = 0; i < alloc_race_size; i++) + for (i = 0; i < table.size(); i++) { /* Found the entry */ if (value < table[i].prob3) break; @@ -1990,6 +1994,7 @@ static s16b hack_m_idx_ii = 0; s16b place_monster_one(int y, int x, int r_idx, int ego, bool_ slp, int status) { auto &r_info = game->edit_data.r_info; + auto &alloc = game->alloc; int i; char dummy[5]; @@ -2316,7 +2321,7 @@ s16b place_monster_one(int y, int x, int r_idx, int ego, bool_ slp, int status) } /* Invalidate the cached allocation table */ - alloc_kind_table_valid = FALSE; + alloc.kind_table_valid = false; if (tries) { diff --git a/src/object2.cc b/src/object2.cc index 32ab6de1..2d6ea672 100644 --- a/src/object2.cc +++ b/src/object2.cc @@ -512,26 +512,23 @@ s16b o_pop() */ errr get_obj_num_prep() { - int i; - - /* Get the entry */ - alloc_entry *table = alloc_kind_table; + auto &alloc = game->alloc; /* Scan the allocation table */ - for (i = 0; i < alloc_kind_size; i++) + for (auto &&entry: alloc.kind_table) { /* Accept objects which pass the restriction, if any */ - if (!get_obj_num_hook || (*get_obj_num_hook)(table[i].index)) + if (!get_obj_num_hook || (*get_obj_num_hook)(entry.index)) { /* Accept this object */ - table[i].prob2 = table[i].prob1; + entry.prob2 = entry.prob1; } /* Do not use this object */ else { /* Decline this object */ - table[i].prob2 = 0; + entry.prob2 = 0; } } @@ -559,11 +556,12 @@ errr get_obj_num_prep() s16b get_obj_num(int level) { auto const &k_info = game->edit_data.k_info; + auto &alloc = game->alloc; - int i, j, p; + std::size_t i, j; + int p; int k_idx; long value, total; - alloc_entry *table = alloc_kind_table; /* Boost level */ @@ -582,16 +580,18 @@ s16b get_obj_num(int level) total = 0L; /* Process probabilities */ - for (i = 0; i < alloc_kind_size; i++) + for (i = 0; i < alloc.kind_table.size(); i++) { + auto &entry = alloc.kind_table[i]; + /* Objects are sorted by depth */ - if (table[i].level > level) break; + if (entry.level > level) break; /* Default */ - table[i].prob3 = 0; + entry.prob3 = 0; /* Access the index */ - k_idx = table[i].index; + k_idx = entry.index; /* Access the actual kind */ auto k_ptr = &k_info[k_idx]; @@ -600,33 +600,36 @@ s16b get_obj_num(int level) if (opening_chest && (k_ptr->tval == TV_CHEST)) continue; /* Accept */ - table[i].prob3 = table[i].prob2; + entry.prob3 = entry.prob2; /* Total */ - total += table[i].prob3; + total += entry.prob3; } /* No legal objects */ if (total <= 0) return (0); - /* Pick an object */ value = rand_int(total); /* Find the object */ - for (i = 0; i < alloc_kind_size; i++) + for (i = 0; i < alloc.kind_table.size(); i++) { + auto &entry = alloc.kind_table[i]; + /* Found the entry */ - if (value < table[i].prob3) break; + if (value < entry.prob3) break; /* Decrement */ - value = value - table[i].prob3; + value = value - entry.prob3; } - /* Power boost */ p = rand_int(100); + /* Shorthand */ + auto &table = alloc.kind_table; + /* Try for a "better" object once (50%) or twice (10%) */ if (p < 60) { @@ -637,8 +640,9 @@ s16b get_obj_num(int level) value = rand_int(total); /* Find the monster */ - for (i = 0; i < alloc_kind_size; i++) + for (i = 0; i < table.size(); i++) { + /* Found the entry */ if (value < table[i].prob3) break; @@ -660,7 +664,7 @@ s16b get_obj_num(int level) value = rand_int(total); /* Find the object */ - for (i = 0; i < alloc_kind_size; i++) + for (i = 0; i < table.size(); i++) { /* Found the entry */ if (value < table[i].prob3) break; @@ -4631,6 +4635,7 @@ bool_ kind_is_artifactable(int k_idx) bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const &theme) { auto const &k_info = game->edit_data.k_info; + auto &alloc = game->alloc; int invprob, base; @@ -4649,7 +4654,7 @@ bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const & if (init_match_theme(theme)) { /* Invalidate the cached allocation table */ - alloc_kind_table_valid = FALSE; + alloc.kind_table_valid = false; } /* Good objects */ @@ -4663,7 +4668,7 @@ bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const & } /* Normal objects -- only when the cache is invalidated */ - else if (!alloc_kind_table_valid) + else if (!alloc.kind_table_valid) { /* Activate normal restriction */ get_obj_num_hook = kind_is_legal; @@ -4672,7 +4677,7 @@ bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const & get_obj_num_prep(); /* The table is synchronised */ - alloc_kind_table_valid = TRUE; + alloc.kind_table_valid = true; } /* Pick a random object */ @@ -4688,7 +4693,7 @@ bool_ make_object(object_type *j_ptr, bool_ good, bool_ great, obj_theme const & get_obj_num_prep(); /* The table is synchronised */ - alloc_kind_table_valid = TRUE; + alloc.kind_table_valid = true; } /* Handle failure */ diff --git a/src/store.cc b/src/store.cc index fea51410..a11a3ea8 100644 --- a/src/store.cc +++ b/src/store.cc @@ -1191,6 +1191,7 @@ static void store_create() { auto const &st_info = game->edit_data.st_info; auto const &k_info = game->edit_data.k_info; + auto &alloc = game->alloc; int i = 0, tries, level = 0; @@ -1265,7 +1266,7 @@ static void store_create() i = get_obj_num(level); /* Invalidate the cached allocation table */ - alloc_kind_table_valid = FALSE; + alloc.kind_table_valid = false; /* Handle failure */ if (!i) continue; @@ -1317,7 +1318,7 @@ static void store_create() i = get_obj_num(level); /* Invalidate the cached allocation table */ - alloc_kind_table_valid = FALSE; + alloc.kind_table_valid = false; } if (!i) continue; diff --git a/src/variable.cc b/src/variable.cc index 1bcab738..8a6a71c8 100644 --- a/src/variable.cc +++ b/src/variable.cc @@ -340,34 +340,6 @@ u16b max_real_towns; */ town_type *town_info; -/* - * The size of "alloc_kind_table" (at most max_k_idx * ALLOCATIONS_MAX) - */ -s16b alloc_kind_size; - -/* - * The entries in the "kind allocator table" - */ -alloc_entry *alloc_kind_table; - -/* - * The flag to tell if alloc_kind_table contains valid entries - * for normal (i.e. kind_is_legal) object allocation - */ -bool_ alloc_kind_table_valid = FALSE; - - -/* - * The size of "alloc_race_table" (at most max_r_idx) - */ -s16b alloc_race_size; - -/* - * The entries in the "race allocator table" - */ -alloc_entry *alloc_race_table; - - /* * Specify attr/char pairs for visual special effects * Be sure to use "index & 0x7F" to avoid illegal access diff --git a/src/variable.hpp b/src/variable.hpp index 3cb54d3a..734e7773 100644 --- a/src/variable.hpp +++ b/src/variable.hpp @@ -127,11 +127,6 @@ extern monster_type *km_list; extern u16b max_real_towns; extern u16b max_towns; extern town_type *town_info; -extern s16b alloc_kind_size; -extern alloc_entry *alloc_kind_table; -extern bool_ alloc_kind_table_valid; -extern s16b alloc_race_size; -extern alloc_entry *alloc_race_table; extern byte misc_to_attr[256]; extern char misc_to_char[256]; extern byte tval_to_attr[128]; diff --git a/src/xtra1.cc b/src/xtra1.cc index dcfb4b3d..2da31dbb 100644 --- a/src/xtra1.cc +++ b/src/xtra1.cc @@ -4220,6 +4220,7 @@ static int get_artifact_idx(int level) void gain_fate(byte fate) { auto const &k_info = game->edit_data.k_info; + auto &alloc = game->alloc; int i; int level; @@ -4309,7 +4310,7 @@ void gain_fate(byte fate) fates[i].o_idx = get_obj_num(max_dlv[dungeon_type] + randint(10)); /* Invalidate the cached allocation table */ - alloc_kind_table_valid = FALSE; + alloc.kind_table_valid = false; auto k_ptr = &k_info[fates[i].o_idx]; -- cgit v1.2.3