From 3b164bbe9ff987af704f8083b2737cf4c79ca7a3 Mon Sep 17 00:00:00 2001 From: Peisong Xiao Date: Tue, 16 Jul 2024 23:00:10 -0400 Subject: [PATCH] fixed: dragon generation breaking the game seed validation wrong --- src/arguments.cc | 3 +-- src/enemies.cc | 43 +++++++++++++++++++++++++------------------ src/enemies.h | 13 +++++++------ src/game.cc | 2 +- src/level.cc | 20 +++++++++----------- src/pc.cc | 20 ++++++++++---------- src/pc.h | 4 ++-- src/potions.cc | 17 +++++++++-------- src/potions.h | 3 +-- 9 files changed, 65 insertions(+), 60 deletions(-) diff --git a/src/arguments.cc b/src/arguments.cc index 8d52f2c..e5d98b3 100644 --- a/src/arguments.cc +++ b/src/arguments.cc @@ -117,9 +117,8 @@ feature proc_args(int argc, char **argv, if (result & FEATURE_SEED) { std::istringstream iss {seed}; unsigned int tmp; - iss >> tmp; - if (!iss.good()) + if (!(iss >> tmp)) return FEATURE_PANIC_SEED; rng = std::make_unique(tmp); diff --git a/src/enemies.cc b/src/enemies.cc index 1665f1a..0ed2561 100644 --- a/src/enemies.cc +++ b/src/enemies.cc @@ -9,17 +9,18 @@ #include "enemies/merchant.h" #include "enemies/orc.h" -void new_dragon(RNG *rng, std::unique_ptr &p, - const position &pos, const position &fallback, - const feature enabled_features, int which_room) { +std::unique_ptr new_dragon(RNG *rng, const position &pos, + const position &fallback, + const feature enabled_features, + int which_room) { const position nil{0, 0}; if (pos != nil) - p = std::make_unique(rng, enabled_features, - pos, which_room); + return std::make_unique(rng, enabled_features, + pos, which_room); else - p = std::make_unique(rng, enabled_features, - fallback, which_room); + return std::make_unique(rng, enabled_features, + fallback, which_room); } const int EXCNT = 6; @@ -44,9 +45,9 @@ enum race get_normal_race(RNG *rng) { return CHOICES[rng->rand_under(CNT)]; } -void new_enemy(RNG *rng, std::unique_ptr &p, - const position &pos, const feature enabled_features, - int which_room) { +std::unique_ptr new_enemy(RNG *rng, const position &pos, + const feature enabled_features, + int which_room) { using std::make_unique; enum race r; @@ -56,34 +57,40 @@ void new_enemy(RNG *rng, std::unique_ptr &p, else r = get_normal_race(rng); - p = nullptr; - switch (r) { case rdwarf: - p = make_unique(rng, enabled_features, pos, which_room); + return make_unique(rng, enabled_features, + pos, which_room); break; case rhuman: - p = make_unique(rng, enabled_features, pos, which_room); + return make_unique(rng, enabled_features, + pos, which_room); break; case relf: - p = make_unique(rng, enabled_features, pos, which_room); + return make_unique(rng, enabled_features, + pos, which_room); break; case rorc: - p = make_unique(rng, enabled_features, pos, which_room); + return make_unique(rng, enabled_features, + pos, which_room); break; case rmerchant: - p = make_unique(rng, enabled_features, pos, which_room); + return make_unique(rng, enabled_features, + pos, which_room); break; case rhalfling: - p = make_unique(rng, enabled_features, pos, which_room); + return make_unique(rng, enabled_features, + pos, which_room); break; default: break; } + + return nullptr; } diff --git a/src/enemies.h b/src/enemies.h index ea44300..14183c8 100644 --- a/src/enemies.h +++ b/src/enemies.h @@ -4,12 +4,13 @@ #include #include "enemy.h" -void new_dragon(RNG *rng, std::unique_ptr &p, - const position &pos, const position &fallback, - const feature enabled_features, int which_room); +std::unique_ptr new_dragon(RNG *rng, const position &pos, + const position &fallback, + const feature enabled_features, + int which_room); -void new_enemy(RNG *rng, std::unique_ptr &p, - const position &pos, const feature enabled_features, - int which_room); +std::unique_ptr new_enemy(RNG *rng, const position &pos, + const feature enabled_features, + int which_room); #endif diff --git a/src/game.cc b/src/game.cc index ef69219..bd2297c 100644 --- a/src/game.cc +++ b/src/game.cc @@ -15,7 +15,7 @@ game::game(const enum race starting_race, the_world = false; // TODO: add the other races - init_player(rng, player, enabled_features, starting_race); + player = init_player(rng, enabled_features, starting_race); if (enabled_features & FEATURE_EXTRA_LEVELS) max_level = rng->rand_between(MIN_LEVEL_CNT, MAX_LEVEL_CNT + 1); diff --git a/src/level.cc b/src/level.cc index 1a4ff56..0995abd 100644 --- a/src/level.cc +++ b/src/level.cc @@ -72,11 +72,11 @@ void level::gen_enemies(RNG *rng, std::vector &tiles) { spots.push_back(dhoard[i].pos + MOVE[i]); } - pelist.push_back(nullptr); - auto pos = rng->get_rand_in_vector(spots); - new_dragon(rng, pelist[i], pos, - dhoard[i].pos, enabled_features, - map.which_room(dhoard[i].pos)); + auto pos = spots.size() ? rng->get_rand_in_vector(spots) : + dhoard[i].pos; + pelist.push_back(new_dragon(rng, pos, dhoard[i].pos, + enabled_features, + map.which_room(dhoard[i].pos))); int room = map.which_room(pos); remove_from_list(tiles[room], pos); @@ -88,9 +88,9 @@ void level::gen_enemies(RNG *rng, std::vector &tiles) { for (int i = dhoard.size(); i < cnt; ++i) { - pelist.push_back(nullptr); auto p = get_rand_pos(rng, tiles); - new_enemy(rng, pelist[i], p, enabled_features, map.which_room(p)); + pelist.push_back(new_enemy(rng, p, enabled_features, + map.which_room(p))); elist.push_back(pelist[i].get()); } } @@ -136,10 +136,8 @@ void level::gen_potions(RNG *rng, std::vector &tiles) { pplist.reserve(cnt); for (int i = 0; i < cnt; ++i) { - pplist.push_back(nullptr); - new_potion(pplist[i], - (potion_type)rng->rand_under(max_type), - get_rand_pos(rng, tiles)); + pplist.push_back(new_potion((potion_type)rng->rand_under(max_type), + get_rand_pos(rng, tiles))); plist.push_back(pplist[i].get()); } } diff --git a/src/pc.cc b/src/pc.cc index b87b1cd..572e23b 100644 --- a/src/pc.cc +++ b/src/pc.cc @@ -8,38 +8,38 @@ #include "player/vampire.h" #include "player/t_800.h" -void init_player(RNG *rng, std::unique_ptr &pc, - const feature enabled_features, const enum race &r) { +std::unique_ptr init_player(RNG *rng, + const feature enabled_features, const enum race &r) { using std::make_unique; - pc = nullptr; - switch (r) { case rgoblin: - pc = make_unique(rng, enabled_features); + return make_unique(rng, enabled_features); break; case rdrow: - pc = make_unique(rng, enabled_features); + return make_unique(rng, enabled_features); break; case rshade: - pc = make_unique(rng, enabled_features); + return make_unique(rng, enabled_features); break; case rtroll: - pc = make_unique(rng, enabled_features); + return make_unique(rng, enabled_features); break; case rvampire: - pc = make_unique(rng, enabled_features); + return make_unique(rng, enabled_features); break; case rt_800: - pc = make_unique(rng, enabled_features); + return make_unique(rng, enabled_features); break; default: break; } + + return nullptr; } diff --git a/src/pc.h b/src/pc.h index 7c67f2c..fbede40 100644 --- a/src/pc.h +++ b/src/pc.h @@ -4,7 +4,7 @@ #include #include "player.h" -void init_player(RNG *rng, std::unique_ptr &pc, - const feature enabled_features, const enum race &r); +std::unique_ptr init_player(RNG *rng, + const feature enabled_features, const enum race &r); #endif diff --git a/src/potions.cc b/src/potions.cc index 4034745..ae8725e 100644 --- a/src/potions.cc +++ b/src/potions.cc @@ -8,34 +8,35 @@ #include "potions/wound_atk.h" #include "potions/wound_def.h" -void new_potion(std::unique_ptr &pp, potion_type type, - const position &pos) { +std::unique_ptr new_potion(potion_type type, const position &pos) { switch (type) { case restore_health: - pp = std::make_unique(pos); + return std::make_unique(pos); break; case boost_atk: - pp = std::make_unique(pos); + return std::make_unique(pos); break; case boost_def: - pp = std::make_unique(pos); + return std::make_unique(pos); break; case poison_health: - pp = std::make_unique(pos); + return std::make_unique(pos); break; case wound_atk: - pp = std::make_unique(pos); + return std::make_unique(pos); break; case wound_def: - pp = std::make_unique(pos); + return std::make_unique(pos); break; default: break; } + + return nullptr; } diff --git a/src/potions.h b/src/potions.h index ae83786..e4c8651 100644 --- a/src/potions.h +++ b/src/potions.h @@ -6,7 +6,6 @@ #include #include -void new_potion(std::unique_ptr &pp, potion_type type, - const position &pos); +std::unique_ptr new_potion(potion_type type, const position &pos); #endif