diff --git a/src/arguments.h b/src/arguments.h index 76ef406..e4e5837 100644 --- a/src/arguments.h +++ b/src/arguments.h @@ -7,7 +7,6 @@ #include "input.h" #include "rng.h" - #include "constants.h" // IMPORTANT: Errors include the index that caused them (or'ed into them) diff --git a/src/boost_atk.cc b/src/boost_atk.cc new file mode 100644 index 0000000..18da5d9 --- /dev/null +++ b/src/boost_atk.cc @@ -0,0 +1,25 @@ +#include "boost_atk.h" + +#include + +const int BOOST_ATK = 5; +const int BOOST_ATK_DROW = 7; + +boost_atk::boost_atk(const position &pos): + potion{potion_type::boost_atk, -1, pos} {} + +void boost_atk::apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) { + if (remaining_duration > 0) { + if (race == rdrow) + ATK += BOOST_ATK_DROW; + else + ATK += BOOST_ATK; + + --remaining_duration; + } +} + +int boost_atk::get_priority() const { + return CALC_ADD_BASE; +} diff --git a/src/boost_atk.h b/src/boost_atk.h new file mode 100644 index 0000000..bd54e9e --- /dev/null +++ b/src/boost_atk.h @@ -0,0 +1,14 @@ +#ifndef __BOOST_ATK_H__ +#define __BOOST_ATK_H__ + +#include "potion.h" + +class boost_atk final: public potion { +public: + boost_atk(const position &pos); + void apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) override; + int get_priority() const override; +}; + +#endif diff --git a/src/boost_def.cc b/src/boost_def.cc new file mode 100644 index 0000000..d4b1f38 --- /dev/null +++ b/src/boost_def.cc @@ -0,0 +1,26 @@ +#include "boost_def.h" + +#include + +// TODO: move into class def as static constants +const int BOOST_DEF = 5; +const int BOOST_DEF_DROW = 7; + +boost_def::boost_def(const position &pos): + potion{potion_type::boost_def, -1, pos} {} + +void boost_def::apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) { + if (remaining_duration > 0) { + if (race == rdrow) + DEF += BOOST_DEF_DROW; + else + DEF += BOOST_DEF; + + --remaining_duration; + } +} + +int boost_def::get_priority() const { + return CALC_ADD_BASE; +} diff --git a/src/boost_def.h b/src/boost_def.h new file mode 100644 index 0000000..1a5eb74 --- /dev/null +++ b/src/boost_def.h @@ -0,0 +1,14 @@ +#ifndef __BOOST_DEF_H__ +#define __BOOST_DEF_H__ + +#include "potion.h" + +class boost_def final: public potion { +public: + boost_def(const position &pos); + void apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) override; + int get_priority() const override; +}; + +#endif diff --git a/src/characters.cc b/src/characters.cc index 9eb09f6..cc0e56f 100644 --- a/src/characters.cc +++ b/src/characters.cc @@ -54,6 +54,13 @@ int character::get_room_num() const { return room_num; } +<<< <<< < HEAD +== == == = +bool character::is_hostile() const { + return hostile; +} + +>>> >>> > AL - races void character::set_room_num(const int room) { room_num = room; } @@ -82,6 +89,39 @@ void character::set_hitrate(const fraction nhitrate) { base_hit_rate = nhitrate; } +<<< <<< < HEAD +== == == = +void character::set_hostile(const bool is_hostile) { + hostile = is_hostile; +} + +void character::apply_buff(const stat_name statn, const int amount) { + // TODO: add checks for bounds + switch (statn) { + case stat_name::HP: + HP += amount; + break; + + case stat_name::ATK: + ATK += amount; + break; + + case stat_name::DEF: + DEF += amount; + break; + + case stat_name::hostile: { + if (amount > 0) + hostile = true; + else + hostile = false; + + break; + } + } +} + +>>> >>> > AL - races direction_list character::moveable(const position_list &available_positions) const { direction_list result; @@ -164,4 +204,3 @@ result character::move(const direction dir, int calc_dmg(const int ATK, const int DEF) { return ceil((100.0f / (100.0f + DEF)) * ATK); } - diff --git a/src/console_input.cc b/src/console_input.cc index 566fab3..0de1201 100644 --- a/src/console_input.cc +++ b/src/console_input.cc @@ -3,40 +3,40 @@ #include console_input::console_input(std::istream &cin): - in{cin} {} + in{cin} {} game_command console_input::get_command() { - std::string cmd; + std::string cmd; + in >> cmd; + game_command tmp; + + if (in.eof()) + return game_command_terminate; + + if (cmd == "q") + return game_command_terminate; + else if (cmd == "f") + return the_world; + else if (cmd == "r") + return game_restart; + else if (cmd == "u" || cmd == "a") { + bool use = cmd == "u"; + in >> cmd; - game_command tmp; if (in.eof()) - return game_command_terminate; + return game_command_panic; - if (cmd == "q") - return game_command_terminate; - else if (cmd == "f") - return the_world; - else if (cmd == "r") - return game_restart; - else if (cmd == "u" || cmd == "a") { - bool use = cmd == "u"; + return (game_command)((tmp = get_direction(cmd)) == + game_command_panic + ? tmp : tmp - move_north + + (use ? apply_north : attack_north)); + } else { + auto tmp = get_direction(cmd); - in >> cmd; + if (tmp != game_command_panic) + return tmp; + } - if (in.eof()) - return game_command_panic; - - return (game_command)((tmp = get_direction(cmd)) == - game_command_panic - ? tmp : tmp - move_north + - (use ? apply_north : attack_north)); - } else { - auto tmp = get_direction(cmd); - - if (tmp != game_command_panic) - return tmp; - } - - return game_command_pass; + return game_command_pass; } diff --git a/src/console_input.h b/src/console_input.h index 4ec4ffe..c2c9b46 100644 --- a/src/console_input.h +++ b/src/console_input.h @@ -6,11 +6,11 @@ class console_input final : public input { private: - std::istream ∈ + std::istream ∈ public: - // This is for cin - console_input(std::istream &cin); - game_command get_command() override; + // This is for cin + console_input(std::istream &cin); + game_command get_command() override; }; #endif diff --git a/src/console_output.cc b/src/console_output.cc index 91d89ae..2be7fef 100644 --- a/src/console_output.cc +++ b/src/console_output.cc @@ -4,7 +4,6 @@ #include #include - console_output::console_output(std::ostream &cout): out{cout} {} /* Attributes @@ -30,70 +29,70 @@ console_output::console_output(std::ostream &cout): out{cout} {} */ std::string console_output::get_code(const int attrs) { - if (attrs < 255) - return "\033[0m"; + if (attrs < 255) + return "\033[0m"; - std::string result = "\033[0m\033["; + std::string result = "\033[0m\033["; - if (attrs & A_BOLD) - result += "1;"; + if (attrs & A_BOLD) + result += "1;"; - if (attrs & A_UNDERLINE) - result += "4;"; + if (attrs & A_UNDERLINE) + result += "4;"; - if (attrs & A_STANDOUT) - result += "7;"; + if (attrs & A_STANDOUT) + result += "7;"; - if ((attrs & COLOR_PAIR(COLOR_WHITE)) == COLOR_PAIR(COLOR_WHITE)) - result += "37;"; - else if ((attrs & COLOR_PAIR(COLOR_CYAN)) == COLOR_PAIR(COLOR_CYAN)) - result += "36;"; - else if ((attrs & COLOR_PAIR(COLOR_MAGENTA)) == COLOR_PAIR(COLOR_MAGENTA)) - result += "35;"; - else if ((attrs & COLOR_PAIR(COLOR_BLUE)) == COLOR_PAIR(COLOR_BLUE)) - result += "34;"; - else if ((attrs & COLOR_PAIR(COLOR_YELLOW)) == COLOR_PAIR(COLOR_YELLOW)) - result += "33;"; - else if ((attrs & COLOR_PAIR(COLOR_RED)) == COLOR_PAIR(COLOR_RED)) - result += "31;"; - else if ((attrs & COLOR_PAIR(COLOR_GREEN)) == COLOR_PAIR(COLOR_GREEN)) - result += "32;"; - else if ((attrs & COLOR_PAIR(COLOR_BLACK_ON_WHITE)) == COLOR_BLACK_ON_WHITE) - result += "30;47;"; + if ((attrs & COLOR_PAIR(COLOR_WHITE)) == COLOR_PAIR(COLOR_WHITE)) + result += "37;"; + else if ((attrs & COLOR_PAIR(COLOR_CYAN)) == COLOR_PAIR(COLOR_CYAN)) + result += "36;"; + else if ((attrs & COLOR_PAIR(COLOR_MAGENTA)) == COLOR_PAIR(COLOR_MAGENTA)) + result += "35;"; + else if ((attrs & COLOR_PAIR(COLOR_BLUE)) == COLOR_PAIR(COLOR_BLUE)) + result += "34;"; + else if ((attrs & COLOR_PAIR(COLOR_YELLOW)) == COLOR_PAIR(COLOR_YELLOW)) + result += "33;"; + else if ((attrs & COLOR_PAIR(COLOR_RED)) == COLOR_PAIR(COLOR_RED)) + result += "31;"; + else if ((attrs & COLOR_PAIR(COLOR_GREEN)) == COLOR_PAIR(COLOR_GREEN)) + result += "32;"; + else if ((attrs & COLOR_PAIR(COLOR_BLACK_ON_WHITE)) == COLOR_BLACK_ON_WHITE) + result += "30;47;"; - result[result.length() - 1] = 'm'; - return result; + result[result.length() - 1] = 'm'; + return result; } void console_output::render() { - out << "\x1B[2J\x1B[H"; + out << "\x1B[2J\x1B[H"; - for (std::size_t idx = 0; idx < contents.size(); ++idx) { - if (idx % DISPLAY_WIDTH == 0 && idx) - out << std::endl; + for (std::size_t idx = 0; idx < contents.size(); ++idx) { + if (idx % DISPLAY_WIDTH == 0 && idx) + out << std::endl; - out << get_code(contents[idx]) - << (char)(contents[idx] ? contents[idx] : ' '); - } + out << get_code(contents[idx]) + << (char)(contents[idx] ? contents[idx] : ' '); + } - out << std::endl; + out << std::endl; } void console_output::print_char(const position &pos, const char ch, const int attrs) { - if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) - return; + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; - contents[pos.y * DISPLAY_WIDTH + pos.x] = attrs | ch; + contents[pos.y * DISPLAY_WIDTH + pos.x] = attrs | ch; } void console_output::print_str(const position &pos, const std::string &str, const int attrs) { - if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) - return; + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; - int head = pos.y * DISPLAY_WIDTH + pos.x; + int head = pos.y * DISPLAY_WIDTH + pos.x; - for (std::size_t i = 0; i < str.length(); ++i) - contents[i + head] = attrs | str[i]; + for (std::size_t i = 0; i < str.length(); ++i) + contents[i + head] = attrs | str[i]; } diff --git a/src/console_output.h b/src/console_output.h index 677d40b..91a8138 100644 --- a/src/console_output.h +++ b/src/console_output.h @@ -6,16 +6,16 @@ class console_output final : public display { private: - std::ostream &out; - std::string get_code(const int attrs); + std::ostream &out; + std::string get_code(const int attrs); public: - console_output(std::ostream &cout); + console_output(std::ostream &cout); - void render() override; - void print_char(const position &pos, - const char ch, const int attrs) override; - void print_str(const position &pos, - const std::string &str, const int attrs) override; + void render() override; + void print_char(const position &pos, + const char ch, const int attrs) override; + void print_str(const position &pos, + const std::string &str, const int attrs) override; }; #endif diff --git a/src/constants.h b/src/constants.h index 4d54c13..02c2e45 100644 --- a/src/constants.h +++ b/src/constants.h @@ -67,8 +67,6 @@ const int CALC_ADD_LATER = 2; const int CALC_MUL_LATER = 3; const int CALC_ADD_FIXED = 4; - - const int DIRECTION_CNT = 8; // IMPORTANT: east is positive for x and SOUTH is positive for y // initializes all directions to an int @@ -77,8 +75,8 @@ enum direction { north = 0, south, east, west, northeast, }; const position MOVE[DIRECTION_CNT] = { - {0, -1}, {0, 1}, {1, 0}, {-1, 0}, - {1, -1}, {-1, -1}, {1, 1}, {-1, 1} + {0, -1}, {0, 1}, {1, 0}, {-1, 0}, + {1, -1}, {-1, -1}, {1, 1}, {-1, 1} }; const int MAP_HEIGHT = 25; diff --git a/src/curses_input.cc b/src/curses_input.cc index dfcab87..d6da0ce 100644 --- a/src/curses_input.cc +++ b/src/curses_input.cc @@ -8,77 +8,77 @@ game_command curses_input::get_command() { case 'h': return game_command::move_west; - case 'j': - return game_command::move_south; + case 'j': + return game_command::move_south; - case 'k': - return game_command::move_north; + case 'k': + return game_command::move_north; - case 'l': - return game_command::move_east; + case 'l': + return game_command::move_east; - case 'y': - return game_command::move_northwest; + case 'y': + return game_command::move_northwest; - case 'u': - return game_command::move_northeast; + case 'u': + return game_command::move_northeast; - case 'b': - return game_command::move_southwest; + case 'b': + return game_command::move_southwest; - case 'n': - return game_command::move_southeast; + case 'n': + return game_command::move_southeast; - case 'a': - break; // wait for another command + case 'a': + break; // wait for another command - case '<': - return game_command::up_stairs; + case '<': + return game_command::up_stairs; - case '>': - return game_command::down_stairs; + case '>': + return game_command::down_stairs; - case 'q': - return game_command_terminate; + case 'q': + return game_command_terminate; - case 'f': - return game_command::the_world; + case 'f': + return game_command::the_world; - case 'r': - return game_restart; + case 'r': + return game_restart; - default: - return game_command_pass; - } + default: + return game_command_pass; + } switch (curse->getcmd()) { case 'h': return game_command::apply_west; - case 'j': - return game_command::apply_south; + case 'j': + return game_command::apply_south; - case 'k': - return game_command::apply_north; + case 'k': + return game_command::apply_north; - case 'l': - return game_command::apply_east; + case 'l': + return game_command::apply_east; - case 'y': - return game_command::apply_northwest; + case 'y': + return game_command::apply_northwest; - case 'u': - return game_command::apply_northeast; + case 'u': + return game_command::apply_northeast; - case 'b': - return game_command::apply_southwest; + case 'b': + return game_command::apply_southwest; - case 'n': - return game_command::apply_southeast; + case 'n': + return game_command::apply_southeast; - default: - return game_command::apply_panic; - } + default: + return game_command::apply_panic; + } - return game_command::game_command_panic; + return game_command::game_command_panic; } diff --git a/src/curses_output.cc b/src/curses_output.cc index e85a278..06409a7 100644 --- a/src/curses_output.cc +++ b/src/curses_output.cc @@ -13,24 +13,24 @@ void curses_output::clear() { void curses_output::print_char(const position &pos, const char ch, const int attrs) { - if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) - return; + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; curse->print_char(pos, ch, attrs); } void curses_output::print_str(const position &pos, const std::string &str, const int attrs) { - if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) - return; + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; - position tmp = pos; + position tmp = pos; for (std::size_t i = 0; i < str.length(); ++i) { curse->print_char(tmp, str[i], attrs); tmp += {1, 0}; - if (tmp.x >= DISPLAY_WIDTH) - tmp = {0, tmp.y + 1}; - } + if (tmp.x >= DISPLAY_WIDTH) + tmp = {0, tmp.y + 1}; + } } diff --git a/src/curses_output.h b/src/curses_output.h index 123bd67..13dbd06 100644 --- a/src/curses_output.h +++ b/src/curses_output.h @@ -12,12 +12,12 @@ private: public: curses_output(cursor *new_curse); - void render() override; - void clear() override; - void print_char(const position &pos, - const char ch, const int attrs) override; - void print_str(const position &pos, - const std::string &str, const int attrs) override; + void render() override; + void clear() override; + void print_char(const position &pos, + const char ch, const int attrs) override; + void print_str(const position &pos, + const std::string &str, const int attrs) override; }; #endif diff --git a/src/cursor.cc b/src/cursor.cc index c3d04f8..fcf9bdf 100644 --- a/src/cursor.cc +++ b/src/cursor.cc @@ -2,53 +2,53 @@ #include "constants.h" cursor::cursor() { - initscr(); + initscr(); - if (has_colors()) { - start_color(); - init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK); - init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); - init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); - init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); - init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); - init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); - init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); - init_pair(COLOR_BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE); - } + if (has_colors()) { + start_color(); + init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK); + init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); + init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); + init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); + init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); + init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); + init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); + init_pair(COLOR_BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE); + } } cursor::~cursor() { - endwin(); + endwin(); } int cursor::getcmd() const { - return getch(); + return getch(); } void cursor::show() const { - refresh(); + refresh(); } void cursor::clear() const { - ::clear(); + ::clear(); } void cursor::print_char(const position &pos, const char ch, const int attrs) const { - attrset(attrs); - mvaddch(pos.y, pos.x, ch); + attrset(attrs); + mvaddch(pos.y, pos.x, ch); } void cursor::print_str(const position &pos, const std::string str, const int attrs) const { - attrset(attrs); - mvaddstr(pos.y, pos.x, str.c_str()); + attrset(attrs); + mvaddstr(pos.y, pos.x, str.c_str()); } bool check_terminal_size() { - int max_x; - int max_y; - getmaxyx(stdscr, max_y, max_x); + int max_x; + int max_y; + getmaxyx(stdscr, max_y, max_x); - return max_x >= DISPLAY_WIDTH && max_y >= DISPLAY_HEIGHT; + return max_x >= DISPLAY_WIDTH && max_y >= DISPLAY_HEIGHT; } diff --git a/src/cursor.h b/src/cursor.h index c0e064a..52fde78 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -37,20 +37,20 @@ class cursor final { private: public: - cursor(); + cursor(); - ~cursor(); + ~cursor(); - int getcmd() const; + int getcmd() const; - void show() const; + void show() const; - void clear() const ; + void clear() const ; - void print_char(const position &pos, const char ch, const int attrs) const; + void print_char(const position &pos, const char ch, const int attrs) const; - void print_str(const position &head, const std::string str, - const int attrs) const; + void print_str(const position &head, const std::string str, + const int attrs) const; }; // IMPORTANT: this will fail when terminal size changes diff --git a/src/display.cc b/src/display.cc index 4b9d63d..5fd0463 100644 --- a/src/display.cc +++ b/src/display.cc @@ -1,15 +1,15 @@ #include "display.h" display::display(): - contents{DISPLAY_BUFFER_SIZE, 0} { - for (int i = 0; i < DISPLAY_BUFFER_SIZE; ++i) - contents.push_back(0); + contents{DISPLAY_BUFFER_SIZE, 0} { + for (int i = 0; i < DISPLAY_BUFFER_SIZE; ++i) + contents.push_back(0); } void display::clear() { - contents.clear(); - contents.reserve(DISPLAY_BUFFER_SIZE); + contents.clear(); + contents.reserve(DISPLAY_BUFFER_SIZE); - for (int i = 0; i < DISPLAY_BUFFER_SIZE; ++i) - contents.push_back(0); + for (int i = 0; i < DISPLAY_BUFFER_SIZE; ++i) + contents.push_back(0); } diff --git a/src/display.h b/src/display.h index 1e1ad1d..9a4a4d5 100644 --- a/src/display.h +++ b/src/display.h @@ -8,28 +8,28 @@ class display { protected: - // use an array of ints to keep track of what's on the screen - // This will have a bit of buffer for the last line - // just in case it overflows - std::vector contents; + // use an array of ints to keep track of what's on the screen + // This will have a bit of buffer for the last line + // just in case it overflows + std::vector contents; public: - display(); + display(); - virtual ~display() = default; + virtual ~display() = default; - // Only call this to refresh the entire output - virtual void render() = 0; + // Only call this to refresh the entire output + virtual void render() = 0; - // Clears the contents buffer - virtual void clear(); + // Clears the contents buffer + virtual void clear(); - virtual void print_char(const position &pos, const char ch, - const int attrs = - A_NORMAL | COLOR_PAIR(COLOR_WHITE)) = 0; - virtual void print_str(const position &pos, const std::string &str, - const int attrs = - A_NORMAL | COLOR_PAIR(COLOR_WHITE)) = 0; - // default arguments are to be set in the base class's declaration + virtual void print_char(const position &pos, const char ch, + const int attrs = + A_NORMAL | COLOR_PAIR(COLOR_WHITE)) = 0; + virtual void print_str(const position &pos, const std::string &str, + const int attrs = + A_NORMAL | COLOR_PAIR(COLOR_WHITE)) = 0; + // default arguments are to be set in the base class's declaration }; #endif diff --git a/src/drow.cc b/src/drow.cc new file mode 100644 index 0000000..6557362 --- /dev/null +++ b/src/drow.cc @@ -0,0 +1,31 @@ +#include "drow.h" +#include +#include + +drow::drow(RNG *rng, const position &pos): + character{rng, race::rdrow, pos} { + gold = 0; + hostile = true; +} + +result drow::attack(const direction dir, character_list &chlist) { + position tmp{pos + MOVE[dir]}; + + for (auto &ch : chlist) + if (tmp == ch->get_position()) { + return ch->get_hit(race, ATK, base_hit_rate); + } + + return result::fine; +} + +result drow::get_hit(const enum race &race, const int atk, + const fraction hitrate) { + if (rng->trial(hitrate)) + HP = std::max(HP - calc_dmg(atk, DEF), 0); + + if (HP == 0) + return result::died; + + return result::hit; +} diff --git a/src/drow.h b/src/drow.h new file mode 100644 index 0000000..6e7b39e --- /dev/null +++ b/src/drow.h @@ -0,0 +1,15 @@ +#ifndef __DROW_H__ +#define __DROW_H__ +#include "characters.h" + +class drow final: public character { +public: + drow(RNG *rng, + const position &pos); + virtual result attack(const direction dir, + character_list &chlist) override; + virtual result get_hit(const enum race &race, const int atk, + const fraction hit_rate) override; +}; + +#endif diff --git a/src/file_input.cc b/src/file_input.cc index ba9a762..5cbfc86 100644 --- a/src/file_input.cc +++ b/src/file_input.cc @@ -3,40 +3,40 @@ #include file_input::file_input(std::ifstream &&ifs): - in{std::move(ifs)} {} + in{std::move(ifs)} {} game_command file_input::get_command() { - std::string cmd; + std::string cmd; + in >> cmd; + game_command tmp; + + if (in.eof()) + return game_command_terminate; + + if (cmd == "q") + return game_command_terminate; + else if (cmd == "f") + return the_world; + else if (cmd == "r") + return game_restart; + else if (cmd == "u" || cmd == "a") { + bool use = cmd == "u"; + in >> cmd; - game_command tmp; if (in.eof()) - return game_command_terminate; + return game_command_panic; - if (cmd == "q") - return game_command_terminate; - else if (cmd == "f") - return the_world; - else if (cmd == "r") - return game_restart; - else if (cmd == "u" || cmd == "a") { - bool use = cmd == "u"; + return (game_command)((tmp = get_direction(cmd)) == + game_command_panic + ? tmp : tmp - move_north + + (use ? apply_north : attack_north)); + } else { + auto tmp = get_direction(cmd); - in >> cmd; + if (tmp != game_command_panic) + return tmp; + } - if (in.eof()) - return game_command_panic; - - return (game_command)((tmp = get_direction(cmd)) == - game_command_panic - ? tmp : tmp - move_north + - (use ? apply_north : attack_north)); - } else { - auto tmp = get_direction(cmd); - - if (tmp != game_command_panic) - return tmp; - } - - return game_command_pass; + return game_command_pass; } diff --git a/src/file_input.h b/src/file_input.h index afcb220..b3b92f4 100644 --- a/src/file_input.h +++ b/src/file_input.h @@ -6,11 +6,11 @@ class file_input final : public input { private: - std::ifstream in; + std::ifstream in; public: - // This is for cin - file_input(std::ifstream &&ifs); - game_command get_command() override; + // This is for cin + file_input(std::ifstream &&ifs); + game_command get_command() override; }; #endif diff --git a/src/file_output.cc b/src/file_output.cc index 2dfce06..710b00a 100644 --- a/src/file_output.cc +++ b/src/file_output.cc @@ -3,34 +3,34 @@ #include file_output::file_output(std::ofstream &&ofs): - out{std::move(ofs)} {} + out{std::move(ofs)} {} void file_output::render() { - for (std::size_t idx = 0; idx < contents.size(); ++idx) { - if (idx % DISPLAY_WIDTH == 0 && idx) - out << std::endl; + for (std::size_t idx = 0; idx < contents.size(); ++idx) { + if (idx % DISPLAY_WIDTH == 0 && idx) + out << std::endl; - out << (char)(contents[idx] ? contents[idx] : ' '); - } + out << (char)(contents[idx] ? contents[idx] : ' '); + } - out << std::endl; + out << std::endl; } void file_output::print_char(const position &pos, const char ch, const int attrs) { - if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) - return; + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; - contents[pos.y * DISPLAY_WIDTH + pos.x] = ch; + contents[pos.y * DISPLAY_WIDTH + pos.x] = ch; } void file_output::print_str(const position &pos, const std::string &str, const int attrs) { - if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) - return; + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; - int head = pos.y * DISPLAY_WIDTH + pos.x; + int head = pos.y * DISPLAY_WIDTH + pos.x; - for (std::size_t i = 0; i < str.length(); ++i) - contents[i + head] = str[i]; + for (std::size_t i = 0; i < str.length(); ++i) + contents[i + head] = str[i]; } diff --git a/src/file_output.h b/src/file_output.h index a6ef81e..8acf522 100644 --- a/src/file_output.h +++ b/src/file_output.h @@ -6,15 +6,15 @@ class file_output final : public display { private: - std::ofstream out; + std::ofstream out; public: - file_output(std::ofstream &&ofs); + file_output(std::ofstream &&ofs); - void render() override; - void print_char(const position &pos, - const char ch, const int attrs) override; - void print_str(const position &pos, - const std::string &str, const int attrs) override; + void render() override; + void print_char(const position &pos, + const char ch, const int attrs) override; + void print_str(const position &pos, + const std::string &str, const int attrs) override; }; #endif diff --git a/src/fraction.cc b/src/fraction.cc index c31f061..595d33f 100644 --- a/src/fraction.cc +++ b/src/fraction.cc @@ -1,35 +1,35 @@ #include "fraction.h" fraction fraction::operator+(const fraction &frac) { - fraction tmp = *this; - tmp.numerator = tmp.numerator * frac.denominator + - tmp.denominator * frac.numerator; - tmp.denominator = tmp.denominator * frac.denominator; - return tmp.simplify(); + fraction tmp = *this; + tmp.numerator = tmp.numerator * frac.denominator + + tmp.denominator * frac.numerator; + tmp.denominator = tmp.denominator * frac.denominator; + return tmp.simplify(); } fraction fraction::operator*(const fraction &frac) { - fraction tmp = *this; - tmp.numerator = tmp.numerator * frac.numerator; - tmp.denominator = tmp.denominator * frac.denominator; - return tmp.simplify(); + fraction tmp = *this; + tmp.numerator = tmp.numerator * frac.numerator; + tmp.denominator = tmp.denominator * frac.denominator; + return tmp.simplify(); } bool fraction::operator==(const fraction &frac) { - this->simplify(); - return gcd(frac.numerator, this->numerator) == this->numerator && - gcd(frac.denominator, this->denominator) == this->denominator; + this->simplify(); + return gcd(frac.numerator, this->numerator) == this->numerator && + gcd(frac.denominator, this->denominator) == this->denominator; } bool fraction::operator!=(const fraction &frac) { - return !(*this == frac); + return !(*this == frac); } fraction &fraction::simplify() { - int g = gcd(numerator, denominator); - numerator /= g; - denominator /= g; - return *this; + int g = gcd(numerator, denominator); + numerator /= g; + denominator /= g; + return *this; } float fraction::real() const { @@ -37,8 +37,8 @@ float fraction::real() const { } int fraction::gcd(int a, int b) { - if (a % b == 0) - return b; + if (a % b == 0) + return b; - return gcd(b, a % b); + return gcd(b, a % b); } diff --git a/src/fraction.h b/src/fraction.h index 5c5a61e..65e686e 100644 --- a/src/fraction.h +++ b/src/fraction.h @@ -2,8 +2,8 @@ #define __FRACTION_H__ struct fraction final { - int numerator; - int denominator; + int numerator; + int denominator; fraction operator+(const fraction &frac); fraction operator*(const fraction &frac); @@ -12,7 +12,7 @@ struct fraction final { fraction &simplify(); float real() const; private: - int gcd(int a, int b); + int gcd(int a, int b); }; #endif diff --git a/src/input.cc b/src/input.cc index 2336630..59f0368 100644 --- a/src/input.cc +++ b/src/input.cc @@ -1,15 +1,14 @@ #include "input.h" - const char *COMMANDS[] = { - "no", "so", "ea", "we", "ne", "nw", "se", "sw" + "no", "so", "ea", "we", "ne", "nw", "se", "sw" }; const int COMMANDS_CNT = 8; game_command get_direction(std::string &str) { - for (int i = 0; i < COMMANDS_CNT; ++i) - if (str == COMMANDS[i]) - return (game_command)i; + for (int i = 0; i < COMMANDS_CNT; ++i) + if (str == COMMANDS[i]) + return (game_command)i; - return game_command_panic; + return game_command_panic; } diff --git a/src/input.h b/src/input.h index 8fd5d97..aa9d120 100644 --- a/src/input.h +++ b/src/input.h @@ -5,8 +5,8 @@ class input { public: - virtual ~input() = default; - virtual game_command get_command() = 0; + virtual ~input() = default; + virtual game_command get_command() = 0; }; game_command get_direction(std::string &str); diff --git a/src/log.cc b/src/log.cc index a990d9f..fe31d0b 100644 --- a/src/log.cc +++ b/src/log.cc @@ -3,7 +3,7 @@ #include logger::logger(std::ofstream &&new_out): - out{std::move(new_out)} {} + out{std::move(new_out)} {} void logger::render() { diff --git a/src/log.h b/src/log.h index 3b03754..887dcd7 100644 --- a/src/log.h +++ b/src/log.h @@ -9,20 +9,20 @@ class logger final { private: - std::ofstream out; - std::vector contents; + std::ofstream out; + std::vector contents; public: - logger(std::ofstream &&new_out); + logger(std::ofstream &&new_out); - // called once every turn - void render(); - void print_char(const position &pos, const char ch); - void print_str(const position &pos, const std::string &str); - void print_turn(const unsigned turn); - void print_player(const character &player); - void print_chlist(const character_list &chlist); - void print_gold(const gold_list &gold_piles); - void print_potions(const potion_list &potions); + // called once every turn + void render(); + void print_char(const position &pos, const char ch); + void print_str(const position &pos, const std::string &str); + void print_turn(const unsigned turn); + void print_player(const character &player); + void print_chlist(const character_list &chlist); + void print_gold(const gold_list &gold_piles); + void print_potions(const potion_list &potions); }; #endif diff --git a/src/main.cc b/src/main.cc index f33033e..5cff79f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,17 +4,16 @@ #include "arguments.h" int main(int argc, char **argv) { - std::unique_ptr curse; - std::unique_ptr in; - std::unique_ptr out; - std::unique_ptr log; - std::unique_ptr rng; + std::unique_ptr curse; + std::unique_ptr in; + std::unique_ptr out; + std::unique_ptr log; + std::unique_ptr rng; + feature enabled_features = proc_args(argc, argv, + curse, in, out, log, rng); - feature enabled_features = proc_args(argc, argv, - curse, in, out, log, rng); - - if (enabled_features & + if (enabled_features & (FEATURE_PANIC | FEATURE_PANIC_FILE | FEATURE_CONFLICT | FEATURE_PANIC_SEED)) { panic_args(enabled_features); @@ -31,5 +30,5 @@ int main(int argc, char **argv) { // out->clear(); // } - return RETURN_FINE; + return RETURN_FINE; } diff --git a/src/poison_health.cc b/src/poison_health.cc new file mode 100644 index 0000000..b6f4fbb --- /dev/null +++ b/src/poison_health.cc @@ -0,0 +1,22 @@ +#include "poison_health.h" + +#include + +poison_health::poison_health(const position &pos): + potion{potion_type::poison_health, 1, pos} {} + +void poison_health::apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) { + if (remaining_duration > 0) { + if (race == rdrow) + HP = std::max(HP - 7, 0); + else + HP = std::max(HP - 5, 0); + + --remaining_duration; + } +} + +int poison_health::get_priority() const { + return CALC_ADD_BASE; +} diff --git a/src/poison_health.h b/src/poison_health.h new file mode 100644 index 0000000..f970f7c --- /dev/null +++ b/src/poison_health.h @@ -0,0 +1,14 @@ +#ifndef __POISON_HEALTH_H__ +#define __POISON_HEALTH_H__ + +#include "potion.h" + +class poison_health final: public potion { +public: + poison_health(const position &pos); + void apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) override; + int get_priority() const override; +}; + +#endif diff --git a/src/position.cc b/src/position.cc index baf3aa5..75eb9e6 100644 --- a/src/position.cc +++ b/src/position.cc @@ -5,43 +5,43 @@ position::position(): x{0}, y{0} {} position::position(int nx, int ny): x{nx}, y{ny} {} position position::operator+(const position &other) const { - position result{x + other.x, y + other.y}; - return result; + position result{x + other.x, y + other.y}; + return result; } position &position::operator=(const position &other) { - x = other.x; - y = other.y; - return *this; + x = other.x; + y = other.y; + return *this; } position &position::operator+=(const position &other) { - return *this = *this + other; + return *this = *this + other; } bool position::operator==(const position &other) const { - return x == other.x && y == other.y; + return x == other.x && y == other.y; } bool position::operator!=(const position &other) const { - return x != other.x || y != other.y; + return x != other.x || y != other.y; } bool position::operator<(const position &other) const { - return y < other.y || x < other.x; + return y < other.y || x < other.x; } #include size_t find(const std::vector &sorted_list, const position &target) { - // TODO: implement binary searching + // TODO: implement binary searching - for (size_t i = 0; i < sorted_list.size(); ++i) - if (sorted_list[i] == target) - return i; + for (size_t i = 0; i < sorted_list.size(); ++i) + if (sorted_list[i] == target) + return i; - return sorted_list.size(); + return sorted_list.size(); } #include diff --git a/src/position.h b/src/position.h index 8d2edf6..e468144 100644 --- a/src/position.h +++ b/src/position.h @@ -3,18 +3,18 @@ #include typedef struct position { - int x; - int y; + int x; + int y; - position(); - position(int nx, int ny); + position(); + position(int nx, int ny); - position operator+(const position &other) const; - position &operator=(const position &other); - position &operator+=(const position &other); - bool operator==(const position &other) const; - bool operator!=(const position &other) const; - bool operator<(const position &other) const; + position operator+(const position &other) const; + position &operator=(const position &other); + position &operator+=(const position &other); + bool operator==(const position &other) const; + bool operator!=(const position &other) const; + bool operator<(const position &other) const; } position; std::size_t find(const std::vector &sorted_list, diff --git a/src/potion.cc b/src/potion.cc index 2026021..4326a0c 100644 --- a/src/potion.cc +++ b/src/potion.cc @@ -4,11 +4,11 @@ potion::potion(const potion_type type, const int duration, const position &pos): type{type}, remaining_duration{duration}, pos{pos} {} potion_type potion::get_type() const { - return type; + return type; } int potion::get_duration() const { - return remaining_duration; + return remaining_duration; } position potion::get_pos() const { diff --git a/src/restore_health.cc b/src/restore_health.cc index 97cd861..0734331 100644 --- a/src/restore_health.cc +++ b/src/restore_health.cc @@ -13,10 +13,10 @@ void restore_health::apply(const enum race &race, int &HP, int &ATK, int &DEF, else HP = std::min(HP + 5, MAX_HP[race]); - --remaining_duration; - } + --remaining_duration; + } } int restore_health::get_priority() const { - return CALC_ADD_BASE; + return CALC_ADD_BASE; } diff --git a/src/rng.h b/src/rng.h index 0abd2c2..575311e 100644 --- a/src/rng.h +++ b/src/rng.h @@ -11,20 +11,20 @@ // IMPORTANT: pass all RNG objects as references class RNG final { private: - const unsigned int init_seed; - int curr_rand_num; + const unsigned int init_seed; + int curr_rand_num; public: - RNG(); // use time(0) as the seed - RNG(const unsigned int seed); // use a seed + RNG(); // use time(0) as the seed + RNG(const unsigned int seed); // use a seed - int rand_num(); // returns a random number between 0 and RAND_MAX + int rand_num(); // returns a random number between 0 and RAND_MAX - // IMPORTANT: all upper bounds are not included, all lower bounds are - int rand_between(const int lower_bound, const int upper_bound); - // returns a random number between 0 and upper_bound - int rand_under(const int upper_bound); - unsigned int get_init_seed() const; - int get_curr_rand_num() const; + // IMPORTANT: all upper bounds are not included, all lower bounds are + int rand_between(const int lower_bound, const int upper_bound); + // returns a random number between 0 and upper_bound + int rand_under(const int upper_bound); + unsigned int get_init_seed() const; + int get_curr_rand_num() const; bool trial(const fraction &psuccess); diff --git a/src/room.h b/src/room.h new file mode 100644 index 0000000..1c4196e --- /dev/null +++ b/src/room.h @@ -0,0 +1,4 @@ +#ifndef __ROOM_H__ +#define __ROOM_H__ + +#endif diff --git a/src/shade.cc b/src/shade.cc index 19bc0dc..6d58d56 100644 --- a/src/shade.cc +++ b/src/shade.cc @@ -6,7 +6,6 @@ shade::shade(RNG *rng, const position &pos): character{rng, race::rshade, pos} { gold = 0; - hostile = true; } character::attack_result shade::attack(const direction dir, @@ -51,4 +50,3 @@ character::hit_result shade::get_hit(const enum race &race, const int atk, return {result::miss, 0, HP}; } - diff --git a/src/vampire.cc b/src/vampire.cc index 705a69f..6725522 100644 --- a/src/vampire.cc +++ b/src/vampire.cc @@ -9,20 +9,20 @@ vampire::vampire(RNG *rng, const position &pos): } result vampire::attack(const direction dir, character_list &chlist) { - position tmp{pos + MOVE[dir]}; + position tmp{pos + MOVE[dir]}; for (auto &ch : chlist) if (tmp == ch->get_position()) { auto res = ch->get_hit(race, ATK, base_hit_rate); - if (res != result::miss) { - HP += GAIN_HP; - } + if (res != result::miss) { + HP += GAIN_HP; + } - return res; - } + return res; + } - return result::fine; + return result::fine; } result vampire::get_hit(const enum race &race, const int atk, @@ -30,8 +30,8 @@ result vampire::get_hit(const enum race &race, const int atk, if (rng->trial(hitrate)) HP = std::max(HP - calc_dmg(atk, DEF), 0); - if (HP == 0) - return result::died; + if (HP == 0) + return result::died; - return result::hit; + return result::hit; } diff --git a/src/wound_atk.cc b/src/wound_atk.cc new file mode 100644 index 0000000..e30ffd2 --- /dev/null +++ b/src/wound_atk.cc @@ -0,0 +1,25 @@ +#include "wound_atk.h" + +#include + +const int WOUND_ATK = 5; +const int WOUND_ATK_DROW = 7; + +wound_atk::wound_atk(const position &pos): + potion{potion_type::wound_atk, -1, pos} {} + +void wound_atk::apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) { + if (remaining_duration > 0) { + if (race == rdrow) + ATK = std::max(ATK - WOUND_ATK_DROW, 0); + else + ATK = std::max(ATK - WOUND_ATK, 0); + + --remaining_duration; + } +} + +int wound_atk::get_priority() const { + return CALC_ADD_BASE; +} diff --git a/src/wound_atk.h b/src/wound_atk.h new file mode 100644 index 0000000..1d95411 --- /dev/null +++ b/src/wound_atk.h @@ -0,0 +1,14 @@ +#ifndef __WOUND_ATK_H__ +#define __WOUND_ATK_H__ + +#include "potion.h" + +class wound_atk final: public potion { +public: + wound_atk(const position &pos); + void apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) override; + int get_priority() const override; +}; + +#endif diff --git a/src/wound_def.cc b/src/wound_def.cc new file mode 100644 index 0000000..1c3561f --- /dev/null +++ b/src/wound_def.cc @@ -0,0 +1,25 @@ +#include "wound_def.h" + +#include + +const int WOUND_DEF = 5; +const int WOUND_DEF_DROW = 7; + +wound_def::wound_def(const position &pos): + potion{potion_type::wound_def, -1, pos} {} + +void wound_def::apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) { + if (remaining_duration > 0) { + if (race == rdrow) + DEF = std::max(DEF - WOUND_DEF_DROW, 0); + else + DEF = std::max(DEF - WOUND_DEF, 0); + + --remaining_duration; + } +} + +int wound_def::get_priority() const { + return CALC_ADD_BASE; +} diff --git a/src/wound_def.h b/src/wound_def.h new file mode 100644 index 0000000..62cd528 --- /dev/null +++ b/src/wound_def.h @@ -0,0 +1,14 @@ +#ifndef __WOUND_DEF_H__ +#define __WOUND_DEF_H__ + +#include "potion.h" + +class wound_def final: public potion { +public: + wound_def(const position &pos); + void apply(const enum race &race, int &HP, int &ATK, int &DEF, + fraction &base_hit_rate) override; + int get_priority() const override; +}; + +#endif