diff --git a/src/enemy.cc b/src/enemy.cc index 89e8246..2e43b2f 100644 --- a/src/enemy.cc +++ b/src/enemy.cc @@ -90,13 +90,12 @@ int enemy_base::dies(level *lvl) { if (race == race::rdragon) { return 0; } else if (race == race::rmerchant) { - lvl->add_gold({GOLD_MERCHANT, pos}); + lvl->add_gold(gold{pos, GOLD_MERCHANT}); return 0; } else if (race == race::rhuman) { - lvl->add_gold({GOLD_NORMAL, pos}); + lvl->add_gold(gold{pos, GOLD_NORMAL}); auto plist = lvl->get_available_around_all(pos); - lvl->add_gold({GOLD_NORMAL, - rng->get_rand_in_vector(plist)}); + lvl->add_gold(gold{rng->get_rand_in_vector(plist), GOLD_NORMAL}); return 0; } diff --git a/src/game.cc b/src/game.cc index 2caa2ed..9e0cf0b 100644 --- a/src/game.cc +++ b/src/game.cc @@ -78,7 +78,7 @@ character *game::move_enemies() { levels[curr_level]->erase_enemy(ch); if (g) - levels[curr_level]->add_gold({g, ch->get_pos()}); + levels[curr_level]->add_gold(gold{ch->get_pos(), g}); } msg += res.msg; diff --git a/src/gold.cc b/src/gold.cc index 05e8846..b9c3c7f 100644 --- a/src/gold.cc +++ b/src/gold.cc @@ -20,10 +20,10 @@ int rand_gold_pile(RNG *rng) { } gold get_gold_at(const position &pos, gold_list &glist) { - gold ret = {0, {0, 0}}; + gold ret({0, 0}, 0); for (size_t i = 0; i < glist.size(); ++i) - if (glist[i].pos == pos) { + if (glist[i].get_pos() == pos) { ret = glist[i]; glist.erase(i + glist.begin()); return ret; @@ -38,3 +38,17 @@ int rand_gold_drop(RNG *rng) { return GOLD_SMALL; } + +gold::gold(const position &pos, const int amount): item{pos}, amount{amount} {} + +void gold::print(output *out) const { + out->print_char(pos, 'G', COLOR_PAIR(COLOR_YELLOW)); +} + +int gold::get_amount() const { + return amount; +} + +void gold::set_amount(const int new_amount) { + amount = new_amount; +} diff --git a/src/gold.h b/src/gold.h index 9eaf053..a3e6564 100644 --- a/src/gold.h +++ b/src/gold.h @@ -4,10 +4,17 @@ #include #include "position.h" #include "rng.h" +#include "item.h" -struct gold { +class gold : public item { +private: int amount; - position pos; +public: + gold(const position &pos, const int amount); + void print(output *out) const override; + + int get_amount() const; + void set_amount(const int new_amount); }; typedef std::vector gold_list; diff --git a/src/level.cc b/src/level.cc index 9d1d20a..ee1bc89 100644 --- a/src/level.cc +++ b/src/level.cc @@ -47,8 +47,9 @@ gold_list level::dragon_hoard() { gold_list result; for (auto g : glist) - if (g.amount == GOLD_DRAGON) - result.push_back({g.amount, g.pos}); + if (g.get_amount() == GOLD_DRAGON) + result.push_back(g); + return result; } @@ -65,17 +66,17 @@ void level::gen_enemies(RNG *rng, std::vector &tiles) { position_list spots; for (int dir = 0; dir < DIRECTION_CNT; ++dir) { - position tmp = dhoard[i].pos + MOVE[dir]; + position tmp = dhoard[i].get_pos() + MOVE[dir]; if (map.which_room(tmp) != -1) - spots.push_back(dhoard[i].pos + MOVE[dir]); + spots.push_back(dhoard[i].get_pos() + MOVE[dir]); } auto pos = spots.size() ? rng->get_rand_in_vector(spots) : - dhoard[i].pos; - pelist.push_back(new_dragon(rng, pos, dhoard[i].pos, + dhoard[i].get_pos(); + pelist.push_back(new_dragon(rng, pos, dhoard[i].get_pos(), enabled_features, - map.which_room(dhoard[i].pos))); + map.which_room(dhoard[i].get_pos()))); int room = map.which_room(pos); remove_from_list(tiles[room], pos); @@ -116,7 +117,7 @@ void level::gen_gold(RNG *rng, std::vector &tiles) { glist.reserve(GOLD_CNT); for (int i = 0; i < GOLD_CNT; ++i) - glist.push_back({rand_gold_pile(rng), get_rand_pos(rng, tiles)}); + glist.push_back(gold{get_rand_pos(rng, tiles), rand_gold_pile(rng)}); } void level::gen_potions(RNG *rng, std::vector &tiles) { @@ -148,7 +149,7 @@ void level::print(output *out) const { plist[i]->print(out); for (size_t i = 0; i < glist.size(); ++i) - out->print_char(glist[i].pos, 'G', COLOR_PAIR(COLOR_YELLOW)); + glist[i].print(out); for (size_t i = 0; i < elist.size(); ++i) elist[i]->print(out); @@ -172,7 +173,7 @@ bool level::is_available(const position &pos, bool is_player) const { if (!(enabled_features & FEATURE_WALK_OVER) && !is_player) for (size_t i = 0; i < glist.size(); ++i) - if (pos == glist[i].pos) + if (pos == glist[i].get_pos()) return false; return true; @@ -198,7 +199,7 @@ bool level::is_available_all(const position &pos) const { return false; for (size_t i = 0; i < glist.size(); ++i) - if (pos == glist[i].pos) + if (pos == glist[i].get_pos()) return false; return true; diff --git a/src/output.h b/src/output.h index 217aa75..1a8549f 100644 --- a/src/output.h +++ b/src/output.h @@ -1,5 +1,5 @@ -#ifndef __DISPLAY_H__ -#define __DISPLAY_H__ +#ifndef __OUTPUT_H__ +#define __OUTPUT_H__ #include #include diff --git a/src/player.cc b/src/player.cc index d801532..b120014 100644 --- a/src/player.cc +++ b/src/player.cc @@ -181,10 +181,10 @@ long_result player_base::interpret_command(level *lvl, game_command cmd) { auto res = move(lvl, pos + MOVE[cmd - move_north]); gold g = get_gold_at(pos, lvl->get_glist()); - gold_cnt += g.amount; + gold_cnt += g.get_amount(); - if (g.amount) - res.msg += "PC picked up " + std::to_string(g.amount) + + if (g.get_amount()) + res.msg += "PC picked up " + std::to_string(g.get_amount()) + " pieces of gold. "; if (enabled_features & FEATURE_INVENTORY) { diff --git a/src/potion.cc b/src/potion.cc index accd252..5cbabbe 100644 --- a/src/potion.cc +++ b/src/potion.cc @@ -3,7 +3,7 @@ #include "constants.h" potion::potion(const potion_type type, const int duration, const position &pos): - type{type}, remaining_duration{duration}, pos{pos} {} + item{pos}, type{type}, remaining_duration{duration} {} potion_type potion::get_type() const { return type; @@ -13,37 +13,7 @@ int potion::get_duration() const { return remaining_duration; } -position potion::get_pos() const { - return pos; -} - -void potion::set_pos(const position &npos) { - pos = npos; -} - -potion::potion(const potion &p): - type(p.type), remaining_duration(p.remaining_duration), - pos{p.pos} {} - -potion::potion(potion &&p): - type(p.type), remaining_duration(p.remaining_duration), - pos{p.pos} {} - -potion &potion::operator=(const potion &p) { - type = p.type; - remaining_duration = p.remaining_duration; - pos = p.pos; - return *this; -} - -potion &potion::operator=(potion &&p) { - type = p.type; - remaining_duration = p.remaining_duration; - pos = p.pos; - return *this; -} - -void potion::print(output *out) { +void potion::print(output *out) const { out->print_char(pos, 'P', COLOR_PAIR(COLOR_GREEN)); } diff --git a/src/potion.h b/src/potion.h index 191fba6..7ebcc4d 100644 --- a/src/potion.h +++ b/src/potion.h @@ -5,6 +5,7 @@ #include #include "fraction.h" #include "output.h" +#include "item.h" enum potion_type : int; enum race : int; @@ -12,7 +13,7 @@ enum race : int; // IMPORTANT: pop all potions of duration == 0, and when entering a // new level, pop all potions of duration == -1 -class potion { +class potion : public item { protected: // Use -1 to denote that the effect will last until leaving the level // Otherwise, use a positive number @@ -22,10 +23,6 @@ protected: position pos; constexpr static const float DROW_POTION_MUL = 1.5f; public: - potion(const potion &p); - potion(potion &&p); - potion &operator=(const potion &p); - potion &operator=(potion &&p); potion(const potion_type type, const int duration, const position &pos); // apply decrements remaining_duration if it's positive, and // won't do anything if it's non-negative @@ -34,11 +31,9 @@ public: virtual int get_priority() const = 0; potion_type get_type() const; int get_duration() const; - position get_pos() const; - void set_pos(const position &npos); virtual const char *get_name() const = 0; - virtual void print(output *out); + void print(output *out) const override; }; typedef std::vector potion_list;