changed all non-owned unique_ptr access to reference to the object itself

This commit is contained in:
2024-07-11 22:15:15 -04:00
parent 6d3e8229df
commit c68330b3e3
17 changed files with 73 additions and 57 deletions

View File

@ -121,18 +121,19 @@ feature proc_args(int argc, char **argv,
if (result & FEATURE_NCURSES) { if (result & FEATURE_NCURSES) {
curse = std::make_unique<cursor>(); curse = std::make_unique<cursor>();
in = std::make_unique<curses_input>(curse); in = std::make_unique<curses_input>(*curse);
out = std::make_unique<curses_output>(curse); out = std::make_unique<curses_output>(*curse);
} }
if (result & FEATURE_SEED) { if (result & FEATURE_SEED) {
std::istringstream iss {seed}; std::istringstream iss {seed};
unsigned int tmp; unsigned int tmp;
iss >> tmp; iss >> tmp;
if (!iss.good())
if (!iss.good())
return FEATURE_PANIC_SEED; return FEATURE_PANIC_SEED;
rng = std::make_unique<RNG>(tmp); rng = std::make_unique<RNG>(tmp);
} }
return result; return result;
@ -166,9 +167,8 @@ void panic_args(feature panic) {
cerr << "Something must have went really, really, wrong..." cerr << "Something must have went really, really, wrong..."
<< endl; << endl;
} }
} } else if (panic & FEATURE_PANIC_SEED) {
else if (panic & FEATURE_PANIC_SEED) { cerr << "Invalid seed" << endl;
cerr << "Invalid seed" << endl;
} else } else
cerr << "Something must have went really, really, wrong..." cerr << "Something must have went really, really, wrong..."
<< endl; << endl;

View File

@ -2,12 +2,11 @@
#include <algorithm> #include <algorithm>
character::character(const enum race &nrace): character::character(RNG &rng, const enum race &nrace):
rng{rng},
race{nrace}, HP{STARTING_HP[race]}, race{nrace}, HP{STARTING_HP[race]},
ATK{STARTING_ATK[race]}, DEF{STARTING_DEF[race]} {} ATK{STARTING_ATK[race]}, DEF{STARTING_DEF[race]} {}
character::~character() {}
enum race character::get_race() const { enum race character::get_race() const {
return race; return race;
} }

View File

@ -19,16 +19,13 @@
// #include "inventory.h" // Reserved for later // #include "inventory.h" // Reserved for later
class character; // forward declaration class character; // forward declaration
extern RNG rng;
// Note: player should not be in the character list // Note: player should not be in the character list
typedef std::vector<character> character_list; typedef std::vector<character> character_list;
class character { class character {
public: public:
character(const race &nrace); // fills out the starting stats character(RNG &rng, const race &nrace); // fills out the starting stats
~character(); // placeholder
// usually I wouldn't do this but considering that the map has // usually I wouldn't do this but considering that the map has
// a super small size an O(n) solution is acceptable // a super small size an O(n) solution is acceptable
@ -69,6 +66,7 @@ public:
// void apply_buff(const stat_name statn, const float mul); // void apply_buff(const stat_name statn, const float mul);
// reserved for later // reserved for later
protected: protected:
RNG &rng;
const enum race race; const enum race race;
int HP; int HP;

View File

@ -1,10 +1,10 @@
#include "curses_input.h" #include "curses_input.h"
curses_input::curses_input(const std::unique_ptr<cursor> &new_curse): curses_input::curses_input(cursor &new_curse):
curse{new_curse} {} curse{new_curse} {}
game_command curses_input::get_command() { game_command curses_input::get_command() {
switch (curse->getcmd()) { switch (curse.getcmd()) {
case 'h': case 'h':
return game_command::move_west; return game_command::move_west;
@ -51,7 +51,7 @@ game_command curses_input::get_command() {
return game_command_pass; return game_command_pass;
} }
switch (curse->getcmd()) { switch (curse.getcmd()) {
case 'h': case 'h':
return game_command::apply_west; return game_command::apply_west;

View File

@ -8,9 +8,9 @@
class curses_input final: public input { class curses_input final: public input {
private: private:
const std::unique_ptr<cursor> &curse; cursor &curse;
public: public:
curses_input(const std::unique_ptr<cursor> &new_curse); curses_input(cursor &new_curse);
game_command get_command() override; game_command get_command() override;
}; };

View File

@ -1,14 +1,14 @@
#include "curses_output.h" #include "curses_output.h"
curses_output::curses_output(const std::unique_ptr<cursor> &new_curse): curses_output::curses_output(cursor &new_curse):
curse{new_curse} {} curse{new_curse} {}
void curses_output::render() { void curses_output::render() {
curse->show(); curse.show();
} }
void curses_output::clear() { void curses_output::clear() {
curse->clear(); curse.clear();
} }
void curses_output::print_char(const position &pos, const char ch, void curses_output::print_char(const position &pos, const char ch,
@ -16,7 +16,7 @@ void curses_output::print_char(const position &pos, const char ch,
if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
return; return;
curse->print_char(pos, ch, attrs); curse.print_char(pos, ch, attrs);
} }
void curses_output::print_str(const position &pos, const std::string &str, void curses_output::print_str(const position &pos, const std::string &str,
@ -27,7 +27,7 @@ void curses_output::print_str(const position &pos, const std::string &str,
position tmp = pos; position tmp = pos;
for (std::size_t i = 0; i < str.length(); ++i) { for (std::size_t i = 0; i < str.length(); ++i) {
curse->print_char(tmp, str[i], attrs); curse.print_char(tmp, str[i], attrs);
tmp += {1, 0}; tmp += {1, 0};
if (tmp.x >= DISPLAY_WIDTH) if (tmp.x >= DISPLAY_WIDTH)

View File

@ -8,9 +8,9 @@
class curses_output final : public display { class curses_output final : public display {
private: private:
const std::unique_ptr<cursor> &curse; cursor &curse;
public: public:
curses_output(const std::unique_ptr<cursor> &new_curse); curses_output(cursor &new_curse);
void render() override; void render() override;
void clear() override; void clear() override;

View File

@ -13,17 +13,17 @@
class game final { class game final {
private: private:
feature features; feature features;
const std::unique_ptr<input> &in; input &in;
const std::unique_ptr<display> &out; display &out;
const std::unique_ptr<logger> &log; logger &log;
const std::unique_ptr<RNG> &rng; RNG &rng;
std::unique_ptr<character> player; std::unique_ptr<character> player;
public: public:
game(const feature enabled_features, game(const feature enabled_features,
const std::unique_ptr<input> &new_in, input &new_in,
const std::unique_ptr<display> &new_out, display &new_out,
const std::unique_ptr<logger> &new_log, logger &new_log,
const std::unique_ptr<RNG> &new_rng); RNG &new_rng);
game_status run(); game_status run();
private: private:
int getcmd() const; int getcmd() const;

View File

@ -2,16 +2,24 @@
#include <algorithm> #include <algorithm>
#include <math.h> #include <math.h>
goblin::goblin(RNG &rng, const position_list &available_positions):
character{rng, race::rshade} {
pos = available_positions[rng.rand_under(available_positions.size())];
gold = 0;
hostile = true;
}
result goblin::attack(const direction dir, const character_list &chlist) { result goblin::attack(const direction dir, character_list &chlist) {
position tmp{pos + MOVE[dir]}; position tmp{pos + MOVE[dir]};
for (auto &ch : chlist) for (auto &ch : chlist)
if (tmp == ch->get_position()) { if (tmp == ch.get_position()) {
auto res = ch->get_hit(race, ATK, base_hit_rate); auto res = ch.get_hit(race, ATK, base_hit_rate);
if (res == result::died) { if (res == result::died) {
gold += GAIN_GOLD; gold += GAIN_GOLD;
} }
return res; return res;
} }
@ -19,8 +27,8 @@ result goblin::attack(const direction dir, const character_list &chlist) {
} }
result goblin::get_hit(const enum race &race, const int atk, result goblin::get_hit(const enum race &race, const int atk,
const float hitrate) { const float hitrate) {
if (rng.rand_num() <= hitrate * (float)RAND_MAX) if (rng.rand_num() <= hitrate * (float)RAND_MAX)
HP = std::max(HP - calc_dmg(atk, DEF), 0); HP = std::max(HP - calc_dmg(atk, DEF), 0);
if (HP == 0) if (HP == 0)

View File

@ -6,11 +6,12 @@ const int GAIN_GOLD = 5;
class goblin final: public character { class goblin final: public character {
public: public:
goblin(const position_list &available_positions); // spawn at a random place goblin(RNG &rng,
const position_list &available_positions);
virtual result attack(const direction dir, virtual result attack(const direction dir,
const character_list &chlist) override; character_list &chlist) override;
virtual result get_hit(const enum race &race, const int atk, virtual result get_hit(const enum race &race, const int atk,
const float hit_rate) override; const float hit_rate) override;
}; };
#endif #endif

View File

@ -19,7 +19,7 @@ int main(int argc, char **argv) {
return RETURN_PANICKED; return RETURN_PANICKED;
} }
game game_proc(enabled_features, in, out, log, rng); game game_proc(enabled_features, *in, *out, *log, *rng);
while (game_proc.run() != game_status::terminated) while (game_proc.run() != game_status::terminated)
out->render(); out->render();

View File

@ -1,7 +1,7 @@
#ifndef __RESTORE_HEALTH_H__ #ifndef __RESTORE_HEALTH_H__
#define __RESTORE_HEALTH_H__ #define __RESTORE_HEALTH_H__
#include "../potion.h" #include "potion.h"
class restore_health final: public potion { class restore_health final: public potion {
public: public:

View File

@ -3,8 +3,8 @@
#include <algorithm> #include <algorithm>
#include <math.h> #include <math.h>
shade::shade(const position_list &available_positions): shade::shade(RNG &rng, const position_list &available_positions):
character{race::rshade} { character{rng, race::rshade} {
pos = available_positions[rng.rand_under(available_positions.size())]; pos = available_positions[rng.rand_under(available_positions.size())];
gold = 0; gold = 0;
hostile = true; hostile = true;

View File

@ -5,7 +5,8 @@
class shade final: public character { class shade final: public character {
public: public:
shade(const position_list &available_positions); // spawn at a random place shade(RNG &rng, const position_list
&available_positions); // spawn at a random place
virtual result attack(const direction dir, virtual result attack(const direction dir,
character_list &chlist) override; character_list &chlist) override;
virtual result get_hit(const enum race &race, const int atk, virtual result get_hit(const enum race &race, const int atk,

View File

@ -2,16 +2,24 @@
#include <algorithm> #include <algorithm>
#include <math.h> #include <math.h>
vampire::vampire(RNG &rng, const position_list &available_positions):
character{rng, race::rshade} {
pos = available_positions[rng.rand_under(available_positions.size())];
gold = 0;
hostile = true;
}
result vampire::attack(const direction dir, const character_list &chlist) { result vampire::attack(const direction dir, character_list &chlist) {
position tmp{pos + MOVE[dir]}; position tmp{pos + MOVE[dir]};
for (auto &ch : chlist) for (auto &ch : chlist)
if (tmp == ch->get_position()) { if (tmp == ch.get_position()) {
auto res = ch->get_hit(race, ATK, base_hit_rate); auto res = ch.get_hit(race, ATK, base_hit_rate);
if (res != result::miss) { if (res != result::miss) {
HP += GAIN_HP; HP += GAIN_HP;
} }
return res; return res;
} }
@ -19,8 +27,8 @@ result vampire::attack(const direction dir, const character_list &chlist) {
} }
result vampire::get_hit(const enum race &race, const int atk, result vampire::get_hit(const enum race &race, const int atk,
const float hitrate) { const float hitrate) {
if (rng.rand_num() <= hitrate * (float)RAND_MAX) if (rng.rand_num() <= hitrate * (float)RAND_MAX)
HP = std::max(HP - calc_dmg(atk, DEF), 0); HP = std::max(HP - calc_dmg(atk, DEF), 0);
if (HP == 0) if (HP == 0)

View File

@ -6,11 +6,12 @@ const int GAIN_HP = 5;
class vampire final: public character { class vampire final: public character {
public: public:
vampire(const position_list &available_positions); // spawn at a random place vampire(RNG &rng,
const position_list &available_positions);
virtual result attack(const direction dir, virtual result attack(const direction dir,
const character_list &chlist) override; character_list &chlist) override;
virtual result get_hit(const enum race &race, const int atk, virtual result get_hit(const enum race &race, const int atk,
const float hit_rate) override; const float hit_rate) override;
}; };
#endif #endif