diff options
author | Bardur Arantsson <bardur@scientician.net> | 2016-09-17 09:58:13 +0200 |
---|---|---|
committer | Bardur Arantsson <bardur@scientician.net> | 2016-09-17 09:58:13 +0200 |
commit | 10c45c9975c076476cf1bcb6a1260c1ee055a6a1 (patch) | |
tree | 54b3d9115f245eb5524e11ee34b685405575a797 /src | |
parent | 0a16384475488d682e33c8d55691e5d9417412ac (diff) |
Change 'abilities' member of player_* to a std::vector<>
Diffstat (limited to 'src')
-rw-r--r-- | src/init1.cc | 119 | ||||
-rw-r--r-- | src/player_class.hpp | 2 | ||||
-rw-r--r-- | src/player_race.hpp | 2 | ||||
-rw-r--r-- | src/player_race_mod.hpp | 2 | ||||
-rw-r--r-- | src/player_spec.hpp | 2 | ||||
-rw-r--r-- | src/skills.cc | 46 |
6 files changed, 66 insertions, 107 deletions
diff --git a/src/init1.cc b/src/init1.cc index 412dde11..def2cdb1 100644 --- a/src/init1.cc +++ b/src/init1.cc @@ -868,16 +868,55 @@ static int read_proto_object(std::vector<object_proto> *protos, cptr buf) /* + * Read an ability assignment + */ +static int read_ability(std::vector<player_race_ability_type> *abilities, char *buf) +{ + int level = 0; + char *name = nullptr; + + // Find the ':' separator + if (!(name = strchr(buf, ':'))) + { + return 1; + } + + // Split the buffer there and advance to point at the ability name + name++; + + // Extract the level + if (1 != sscanf(buf, "%d:", &level)) + { + return 1; + } + + // Try to find the ability by name + int idx = find_ability(name); + if (idx < 0) + { + return 1; + } + + // Insert + player_race_ability_type ability; + ability.ability = idx; + ability.level = level; + abilities->emplace_back(ability); + + return 0; +} + + +/* * Initialize the "player" arrays, by parsing an ascii "template" file */ errr init_player_info_txt(FILE *fp) { - int i = 0, z; + int i = 0; int powers = 0; int lev = 1; int tit_idx = 0; int spec_idx = 0; - int cur_ab = -1; char buf[1024]; char *s, *t; @@ -987,9 +1026,6 @@ errr init_player_info_txt(FILE *fp) rp_ptr->powers[0] = rp_ptr->powers[1] = rp_ptr->powers[2] = rp_ptr->powers[3] = -1; powers = 0; lev = 1; - cur_ab = 0; - for (z = 0; z < 10; z++) - rp_ptr->abilities[z].level = -1; /* Next... */ continue; @@ -1100,22 +1136,10 @@ errr init_player_info_txt(FILE *fp) /* Process 'b' for "abilities" */ if ((buf[0] == 'R') && (buf[2] == 'b')) { - char *sec; - - /* Scan for the values */ - if (NULL == (sec = strchr(buf + 4, ':'))) + if (read_ability(&rp_ptr->abilities, buf + 4)) { - return (1); + return 1; } - *sec = '\0'; - sec++; - if (!*sec) return (1); - - if ((i = find_ability(sec)) == -1) return (1); - - rp_ptr->abilities[cur_ab].ability = i; - rp_ptr->abilities[cur_ab].level = atoi(buf + 4); - cur_ab++; /* Next... */ continue; @@ -1225,9 +1249,6 @@ errr init_player_info_txt(FILE *fp) rmp_ptr->powers[0] = rmp_ptr->powers[1] = rmp_ptr->powers[2] = rmp_ptr->powers[3] = -1; powers = 0; lev = 1; - cur_ab = 0; - for (z = 0; z < 10; z++) - rmp_ptr->abilities[z].level = -1; /* Next... */ continue; @@ -1351,22 +1372,10 @@ errr init_player_info_txt(FILE *fp) /* Process 'b' for "abilities" */ if ((buf[0] == 'S') && (buf[2] == 'b')) { - char *sec; - - /* Scan for the values */ - if (NULL == (sec = strchr(buf + 4, ':'))) + if (read_ability(&rmp_ptr->abilities, buf + 4)) { return (1); } - *sec = '\0'; - sec++; - if (!*sec) return (1); - - if ((i = find_ability(sec)) == -1) return (1); - - rmp_ptr->abilities[cur_ab].ability = i; - rmp_ptr->abilities[cur_ab].level = atoi(buf + 4); - cur_ab++; /* Next... */ continue; @@ -1503,9 +1512,6 @@ errr init_player_info_txt(FILE *fp) c_ptr->powers[0] = c_ptr->powers[1] = c_ptr->powers[2] = c_ptr->powers[3] = -1; powers = 0; lev = 1; - for (z = 0; z < 10; z++) - c_ptr->abilities[z].level = -1; - cur_ab = 0; tit_idx = 0; spec_idx = -1; for (z = 0; z < MAX_SPEC; z++) @@ -1631,22 +1637,10 @@ errr init_player_info_txt(FILE *fp) /* Process 'b' for "abilities" */ if ((buf[0] == 'C') && (buf[2] == 'b')) { - char *sec; - - /* Scan for the values */ - if (NULL == (sec = strchr(buf + 4, ':'))) + if (read_ability(&c_ptr->abilities, buf + 4)) { - return (1); + return 1; } - *sec = '\0'; - sec++; - if (!*sec) return (1); - - if ((i = find_ability(sec)) == -1) return (1); - - c_ptr->abilities[cur_ab].ability = i; - c_ptr->abilities[cur_ab].level = atoi(buf + 4); - cur_ab++; /* Next... */ continue; @@ -1790,11 +1784,6 @@ errr init_player_info_txt(FILE *fp) assert(!s_ptr->title); s_ptr->title = my_strdup(s); - /* Initialize */ - cur_ab = 0; - for (z = 0; z < 10; z++) - s_ptr->abilities[z].level = -1; - /* Next... */ continue; } @@ -1862,22 +1851,10 @@ errr init_player_info_txt(FILE *fp) /* Process 'b' for "abilities" */ if (buf[4] == 'b') { - char *sec; - - /* Scan for the values */ - if (NULL == (sec = strchr(buf + 6, ':'))) + if (read_ability(&s_ptr->abilities, buf + 6)) { - return (1); + return 1; } - *sec = '\0'; - sec++; - if (!*sec) return (1); - - if ((i = find_ability(sec)) == -1) return (1); - - s_ptr->abilities[cur_ab].ability = i; - s_ptr->abilities[cur_ab].level = atoi(buf + 6); - cur_ab++; /* Next... */ continue; diff --git a/src/player_class.hpp b/src/player_class.hpp index e0f88008..57282e27 100644 --- a/src/player_class.hpp +++ b/src/player_class.hpp @@ -51,6 +51,6 @@ struct player_class std::array<player_spec, MAX_SPEC> spec; - std::array<player_race_ability_type, 10> abilities; /* Abilitiers to be gained by level(doesnt take prereqs in account) */ + std::vector<player_race_ability_type> abilities; /* Abilities to be gained by level; ignores prereqs */ }; diff --git a/src/player_race.hpp b/src/player_race.hpp index 844eeb75..36c9109e 100644 --- a/src/player_race.hpp +++ b/src/player_race.hpp @@ -47,5 +47,5 @@ struct player_race std::vector<object_proto> object_protos; - std::array<player_race_ability_type, 10> abilities; /* Abilitiers to be gained by level(doesnt take prereqs in account) */ + std::vector<player_race_ability_type> abilities; /* Abilities to be gained by level; ignores prereqs */ }; diff --git a/src/player_race_mod.hpp b/src/player_race_mod.hpp index 40264ba3..da3ae0ca 100644 --- a/src/player_race_mod.hpp +++ b/src/player_race_mod.hpp @@ -51,6 +51,6 @@ struct player_race_mod std::vector<object_proto> object_protos; - std::array<player_race_ability_type, 10> abilities; /* Abilities to be gained by level; doesnt take prereqs in account */ + std::vector<player_race_ability_type> abilities; /* Abilities to be gained by level; ignores prereqs */ }; diff --git a/src/player_spec.hpp b/src/player_spec.hpp index 3d1c4471..574425f6 100644 --- a/src/player_spec.hpp +++ b/src/player_spec.hpp @@ -25,5 +25,5 @@ struct player_spec player_race_flag_set flags; - std::array<player_race_ability_type, 10> abilities; /* Abilities to be gained by level(doesnt take prereqs in account) */ + std::vector<player_race_ability_type> abilities; /* Abilities to be gained by level; ignores prereqs */ }; diff --git a/src/skills.cc b/src/skills.cc index bc508c54..70665a49 100644 --- a/src/skills.cc +++ b/src/skills.cc @@ -1749,41 +1749,23 @@ void do_cmd_ability() */ void apply_level_abilities(int level) { - int i; - - for (i = 0; i < 10; i++) + auto apply = [level](std::vector<player_race_ability_type> const &abilities) -> void { - if (cp_ptr->abilities[i].level == level) - { - if ((level > 1) && (!ab_info[cp_ptr->abilities[i].ability].acquired)) - { - cmsg_format(TERM_L_GREEN, "You have learned the ability '%s'.", ab_info[cp_ptr->abilities[i].ability].name); - } - ab_info[cp_ptr->abilities[i].ability].acquired = TRUE; - } - if (spp_ptr->abilities[i].level == level) + for (auto const &a: abilities) { - if ((level > 1) && (!ab_info[spp_ptr->abilities[i].ability].acquired)) + if (a.level == level) { - cmsg_format(TERM_L_GREEN, "You have learned the ability '%s'.", ab_info[spp_ptr->abilities[i].ability].name); - } - ab_info[spp_ptr->abilities[i].ability].acquired = TRUE; - } - if (rp_ptr->abilities[i].level == level) - { - if ((level > 1) && (!ab_info[rp_ptr->abilities[i].ability].acquired)) - { - cmsg_format(TERM_L_GREEN, "You have learned the ability '%s'.", ab_info[rp_ptr->abilities[i].ability].name); - } - ab_info[rp_ptr->abilities[i].ability].acquired = TRUE; - } - if (rmp_ptr->abilities[i].level == level) - { - if ((level > 1) && (!ab_info[rmp_ptr->abilities[i].ability].acquired)) - { - cmsg_format(TERM_L_GREEN, "You have learned the ability '%s'.", ab_info[rmp_ptr->abilities[i].ability].name); + if ((level > 1) && (!ab_info[a.ability].acquired)) + { + cmsg_format(TERM_L_GREEN, "You have learned the ability '%s'.", ab_info[a.ability].name); + } + ab_info[a.ability].acquired = TRUE; } - ab_info[rmp_ptr->abilities[i].ability].acquired = TRUE; } - } + }; + + apply(cp_ptr->abilities); + apply(spp_ptr->abilities); + apply(rp_ptr->abilities); + apply(rmp_ptr->abilities); } |