removed all C-style casts

This commit is contained in:
2024-07-18 17:56:00 -04:00
parent 8f00e903e8
commit 1a9c04ad5a
36 changed files with 212 additions and 92 deletions

View File

@ -8,7 +8,7 @@
########## Variables ##########
CXX = g++ # compiler
CXXFLAGS = -std=c++20 -g -Wall -MMD -O0 # compiler flags
CXXFLAGS = -std=c++20 -g -Wall -Wold-style-cast -MMD -O0 # compiler flags
MAKEFILE_NAME = ${firstword ${MAKEFILE_LIST}} # makefile name
SOURCES = $(wildcard *.cc) $(wildcard */*.cc) # source files (*.cc)

View File

@ -25,7 +25,8 @@ game_status CC3K::run() {
if (tmp != -1) {
curr_menu.reset();
curr_game = std::make_unique<game>((race)tmp,
curr_game =
std::make_unique<game>(static_cast<race>(tmp),
enabled_features,
in, out, rng);
out->clear();

View File

@ -46,39 +46,46 @@ enum game_command : int {game_command_terminate = 0,
};
// Character generation related
static const int RACE_CNT = 17;
static const int RACE_CNT = 23;
enum race : int {rshade = 0, rdrow, rvampire, rtroll,
rgoblin, rhuman, rdwarf, relf,
rorc, rmerchant, rdragon, rhalfling,
rt_800, rmr_goose, rmonk, rbrawler,
rassassin
rassassin, rviking, rswordsman,
rleprechaun, rwitch, rhacker, rbaby_dragon
};
static const char CHAR_REP[RACE_CNT] = {
's', 'd', 'v', 't', 'g', 'H', 'W', 'E', 'O', 'M', 'D', 'L',
't', 'g', 'm', 'b', 'a'
't', 'g', 'm', 'b', 'a',
'V', 'S', 'l', 'W', 'h', 'B'
};
static const int MAX_HP[RACE_CNT] = {
125, 150, INF, 120, 110, 140, 100, 140, 180, 30, 150, 100,
800, 130, 150, 120, 100
800, 130, 150, 120, 100,
150, 100, 60, 100, 90, 140
};
static const int STARTING_HP[RACE_CNT] = {
125, 150, 50, 120, 110, 140, 100, 140, 180, 30, 150, 100,
800, 130, 150, 120, 100
800, 130, 150, 120, 100,
150, 100, 60, 100, 90, 140
};
static const int STARTING_ATK[RACE_CNT] = {
25, 25, 25, 25, 15, 20, 20, 30, 30, 70, 20, 15,
40, 25, 80, 15, 30
40, 25, 80, 15, 30,
30, 25, 10, 20, 15, 25
};
static const int STARTING_DEF[RACE_CNT] = {
25, 15, 25, 15, 20, 20, 30, 10, 25, 5, 20, 20,
50, 20, 0, 20, 10
50, 20, 0, 20, 10,
25, 15, 15, 15, 30, 40
};
static const fraction STARTING_HR[RACE_CNT] = {
{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1},
{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
{1, 1}, {1, 1}, {100, 1}, {3, 4}, {1, 1}
{1, 1}, {1, 1}, {100, 1}, {3, 4}, {1, 1},
{1, 3}, {1, 1}, {1, 2}, {1, 2}, {1, 2}, {1, 3}
};

View File

@ -6,10 +6,6 @@ dragon::dragon(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, rdragon, pos, gen_room_num, "D"} {}
void dragon::print(output *out) {
out->print_char(pos, 'D', COLOR_PAIR(COLOR_RED));
}
const char *dragon::get_race_name() const {
return "Dragon";
}
@ -20,3 +16,7 @@ long_result dragon::act(level *lvl, character *pc, bool hostile) {
return {fine, ""};
}
int dragon::dies(level *lvl) {
return 0;
}

View File

@ -7,9 +7,9 @@ class dragon final: public enemy_base {
public:
dragon(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
void print(output *out) override;
const char *get_race_name() const override;
long_result act(level *lvl, character *pc, bool hostile) override;
int dies(level *lvl) override;
};
#endif

View File

@ -6,10 +6,6 @@ dwarf::dwarf(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, rdwarf, pos, gen_room_num, "W"} {}
void dwarf::print(output *out) {
out->print_char(pos, 'W', COLOR_PAIR(COLOR_RED));
}
const char *dwarf::get_race_name() const {
return "Dwarf";
}

View File

@ -7,7 +7,6 @@ class dwarf final: public enemy_base {
public:
dwarf(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
void print(output *out) override;
const char *get_race_name() const override;
};

View File

@ -6,10 +6,6 @@ elf::elf(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, relf, pos, gen_room_num, "E"} {}
void elf::print(output *out) {
out->print_char(pos, 'E', COLOR_PAIR(COLOR_RED));
}
const char *elf::get_race_name() const {
return "Elf";
}

View File

@ -7,7 +7,6 @@ class elf final: public enemy_base {
public:
elf(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
void print(output *out) override;
const char *get_race_name() const override;
long_result attack(character *ch) override;
};

View File

@ -9,10 +9,6 @@ halfling::halfling(RNG *rng, const feature enabled_features,
const int gen_room_num):
enemy_base{rng, enabled_features, rhalfling, pos, gen_room_num, "L"} {}
void halfling::print(output *out) {
out->print_char(pos, 'L', COLOR_PAIR(COLOR_RED));
}
const char *halfling::get_race_name() const {
return "Halfling";
}

View File

@ -8,10 +8,9 @@ class halfling final: public enemy_base {
public:
halfling(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
void print(output *out) override;
const char *get_race_name() const override;
long_result get_hit(character *ch, const int tATK,
const fraction &hit_rate);
const fraction &hit_rate) override;
};
#endif

View File

@ -1,15 +1,19 @@
#include "human.h"
#include "../constants.h"
#include "../level.h"
human::human(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, rhuman, pos, gen_room_num, "H"} {}
void human::print(output *out) {
out->print_char(pos, 'H', COLOR_PAIR(COLOR_RED));
}
const char *human::get_race_name() const {
return "Human";
}
int human::dies(level *lvl) {
lvl->add_gold(gold{pos, GOLD_NORMAL});
auto plist = lvl->get_available_around_all(pos);
lvl->add_gold(gold{rng->get_rand_in_vector(plist), GOLD_NORMAL});
return 0;
}

View File

@ -7,8 +7,8 @@ class human final: public enemy_base {
public:
human(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
void print(output *out) override;
const char *get_race_name() const override;
int dies(level *lvl) override;
};
#endif

32
src/enemies/leprechaun.cc Normal file
View File

@ -0,0 +1,32 @@
#include "leprechaun.h"
#include "../constants.h"
#include "../player.h"
leprechaun::leprechaun(RNG *rng, const feature enabled_features,
const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, rleprechaun, pos, gen_room_num, "l"} {}
const char *leprechaun::get_race_name() const {
return "Leprechaun";
}
long_result leprechaun::attack(character *ch) {
long_result res;
if (static_cast<player_base * >(ch)->get_gold() < STEAL_GOLD) {
res = ch->get_hit(this, BOOSTED_ATK, base_hit_rate);
res.msg = "l is angered because PC is too poor. " + res.msg;
} else {
res = ch->get_hit(this, ATK, base_hit_rate);
if (res.res == hit) {
res.msg += "l steals " + std::to_string(STEAL_GOLD) +
" pieces of gold from PC. ";
static_cast<player_base *>(ch)->add_gold(-STEAL_GOLD);
}
}
return res;
}

18
src/enemies/leprechaun.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef __LEPRECHAUN_H__
#define __LEPRECHAUN_H__
#include "../enemy.h"
class leprechaun final: public enemy_base {
static const int STEAL_GOLD = 3;
static const int BOOSTED_ATK = 30;
static const int STARTING_GOLD = 5;
int gold_cnt;
public:
leprechaun(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
const char *get_race_name() const override;
virtual long_result attack(character *ch) override;
};
#endif

View File

@ -1,15 +1,18 @@
#include "merchant.h"
#include "../constants.h"
#include "../gold.h"
#include "../level.h"
merchant::merchant(RNG *rng, const feature enabled_features,
const position &pos, const int gen_room_num):
enemy_base{rng, enabled_features, rmerchant, pos, gen_room_num, "M"} {}
void merchant::print(output *out) {
out->print_char(pos, 'M', COLOR_PAIR(COLOR_RED));
}
const char *merchant::get_race_name() const {
return "Merchant";
}
int merchant::dies(level *lvl) {
lvl->add_gold(gold{pos, GOLD_MERCHANT});
return 0;
}

View File

@ -7,8 +7,8 @@ class merchant final: public enemy_base {
public:
merchant(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
void print(output *out) override;
const char *get_race_name() const override;
int dies(level *lvl) override;
};
#endif

View File

@ -6,10 +6,6 @@ orc::orc(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, rorc, pos, gen_room_num, "O"} {}
void orc::print(output *out) {
out->print_char(pos, 'O', COLOR_PAIR(COLOR_RED));
}
const char *orc::get_race_name() const {
return "Orc";
}

View File

@ -7,7 +7,6 @@ class orc final: public enemy_base {
public:
orc(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
void print(output *out) override;
const char *get_race_name() const override;
};

12
src/enemies/swordsman.cc Normal file
View File

@ -0,0 +1,12 @@
#include "swordsman.h"
#include "../constants.h"
swordsman::swordsman(RNG *rng, const feature enabled_features,
const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, rswordsman, pos, gen_room_num, "S"} {}
const char *swordsman::get_race_name() const {
return "Swordsman";
}

13
src/enemies/swordsman.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef __SWORDSMAN_H__
#define __SWORDSMAN_H__
#include "../enemy.h"
class swordsman final: public enemy_base {
public:
swordsman(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
const char *get_race_name() const override;
};
#endif

27
src/enemies/viking.cc Normal file
View File

@ -0,0 +1,27 @@
#include "viking.h"
#include "../constants.h"
viking::viking(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num):
enemy_base{rng, enabled_features, rviking, pos, gen_room_num, "V"} {}
const char *viking::get_race_name() const {
return "Viking";
}
long_result viking::attack(character *ch) {
auto res1 = ch->get_hit(this, ATK, base_hit_rate);
if (res1.res == result::died)
return res1;
auto res2 = ch->get_hit(this, ATK, base_hit_rate);
if (res1.res == miss && res2.res == miss)
return {miss, res1.msg + res2.msg};
else if (res2.res == died)
return {died, res1.msg + res2.msg};
else
return {hit, res1.msg + res2.msg};
}

14
src/enemies/viking.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __VIKING_H__
#define __VIKING_H__
#include "../enemy.h"
class viking final: public enemy_base {
public:
viking(RNG *rng, const feature enabled_features, const position &pos,
const int gen_room_num);
const char *get_race_name() const override;
long_result attack(character *ch) override;
};
#endif

View File

@ -87,18 +87,6 @@ long_result enemy_base::get_hit(character *ch, const int tATK,
}
int enemy_base::dies(level *lvl) {
if (race == race::rdragon) {
return 0;
} else if (race == race::rmerchant) {
lvl->add_gold(gold{pos, GOLD_MERCHANT});
return 0;
} else if (race == race::rhuman) {
lvl->add_gold(gold{pos, GOLD_NORMAL});
auto plist = lvl->get_available_around_all(pos);
lvl->add_gold(gold{rng->get_rand_in_vector(plist), GOLD_NORMAL});
return 0;
}
return rand_gold_drop(rng);
}
@ -109,3 +97,7 @@ enemy_base *get_enemy_at(const position &pos, const enemy_list &elist) {
return nullptr;
}
void enemy_base::print(output *out) {
out->print_char(pos, CHAR_REP[race], COLOR_PAIR(COLOR_RED));
}

View File

@ -24,6 +24,8 @@ public:
virtual long_result get_hit(character *ch, const int tATK,
const fraction &hit_rate) override;
void print(output *out) override;
virtual std::string get_abbrev() const override;
virtual int dies(level *lvl);

View File

@ -33,7 +33,7 @@ fraction &fraction::simplify() {
}
float fraction::real() const {
return (float)numerator / denominator;
return static_cast<float>(numerator) / denominator;
}
int fraction::gcd(int a, int b) {

View File

@ -178,7 +178,7 @@ game_result game::run() {
if (killer == nullptr)
return {dead, "You died: killed by your own stupidity!"};
return {dead, (std::string)"You died: killed by a(n) " +
return {dead, std::string{"You died: killed by a(n) "} +
killer->get_race_name()};
}

View File

@ -10,7 +10,7 @@ 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;
return static_cast<game_command>(i);
return game_command_panic;
}

View File

@ -30,11 +30,14 @@ game_command console_input::get_command() {
auto tmp = get_direction(cmd);
if (cmdtmp == "u")
return (game_command)(tmp + apply_north);
return static_cast<game_command>
(tmp + apply_north);
else if (cmdtmp == "a")
return (game_command)(tmp + attack_north);
return static_cast<game_command>
(tmp + attack_north);
else
return (game_command)(tmp + throw_north);
return static_cast<game_command>
(tmp + throw_north);
} else if (cmd == "yes") {
return game_command::enter;
} else if (cmd == "i") {
@ -43,7 +46,8 @@ game_command console_input::get_command() {
auto tmp = get_direction(cmd);
if (tmp != game_command_panic)
return (game_command)(tmp + move_north);
return static_cast<game_command>
(tmp + move_north);
}
return game_command_pass;

View File

@ -68,28 +68,36 @@ game_command curses_input::get_command() {
switch (curse->getcmd()) {
case 'h':
return (game_command)(tmp + west);
return static_cast<game_command>
(tmp + west);
case 'j':
return (game_command)(tmp + south);
return static_cast<game_command>
(tmp + south);
case 'k':
return (game_command)(tmp + north);
return static_cast<game_command>
(tmp + north);
case 'l':
return (game_command)(tmp + east);
return static_cast<game_command>
(tmp + east);
case 'y':
return (game_command)(tmp + northwest);
return static_cast<game_command>
(tmp + northwest);
case 'u':
return (game_command)(tmp + northeast);
return static_cast<game_command>
(tmp + northeast);
case 'b':
return (game_command)(tmp + southwest);
return static_cast<game_command>
(tmp + southwest);
case 'n':
return (game_command)(tmp + southeast);
return static_cast<game_command>
(tmp + southeast);
default:
return game_command_panic;

View File

@ -30,11 +30,11 @@ game_command file_input::get_command() {
auto tmp = get_direction(cmd);
if (cmdtmp == "u")
return (game_command)(tmp + apply_north);
return static_cast<game_command>(tmp + apply_north);
else if (cmdtmp == "a")
return (game_command)(tmp + attack_north);
return static_cast<game_command>(tmp + attack_north);
else
return (game_command)(tmp + throw_north);
return static_cast<game_command>(tmp + throw_north);
} else if (cmd == "yes") {
return game_command::enter;
} else if (cmd == "i") {
@ -43,7 +43,8 @@ game_command file_input::get_command() {
auto tmp = get_direction(cmd);
if (tmp != game_command_panic)
return tmp;
return static_cast<game_command>
(tmp + move_north);
}
return game_command_pass;

View File

@ -136,7 +136,8 @@ void level::gen_potions(RNG *rng, std::vector<position_list> &tiles) {
pplist.reserve(cnt);
for (int i = 0; i < cnt; ++i) {
pplist.push_back(new_potion((potion_type)rng->rand_under(max_type),
pplist.push_back(new_potion(static_cast<potion_type>
(rng->rand_under(max_type)),
get_rand_pos(rng, tiles)));
plist.push_back(pplist[i].get());
}

View File

@ -269,8 +269,8 @@ std::vector<game_map::room> game_map::distr_rooms(RNG *rng,
// and the max height of the layer
for (std::size_t i = 0; i < room_dims.size(); ++i)
if (room_dims[i].second == layer) {
l = std::min(l, (int)i);
r = std::max(r, (int)i);
l = std::min(l, static_cast<int>(i));
r = std::max(r, static_cast<int>(i));
layer_height = std::max(layer_height,
room_dims[i].first.y);
}

View File

@ -74,7 +74,7 @@ void console_output::render() {
out << std::endl;
out << get_code(contents[idx])
<< (char)(contents[idx] ? contents[idx] : ' ');
<< static_cast<char>(contents[idx] ? contents[idx] : ' ');
}
out << std::endl;

View File

@ -12,7 +12,7 @@ void file_output::render() {
if (idx % DISPLAY_WIDTH == 0 && idx)
out << std::endl;
out << (char)(contents[idx] ? contents[idx] : ' ');
out << static_cast<char>(contents[idx] ? contents[idx] : ' ');
}
out << std::endl;

View File

@ -72,7 +72,7 @@ long_result player_base::move(level *lvl,
pos = p;
return {result::moved, ""};
} else if ((tmp = get_enemy_at(p, lvl->get_elist())) != nullptr) {
auto res = attack((character *)tmp);
auto res = attack(static_cast<character *>(tmp));
if (tmp->is_dead()) {
int g = tmp->dies(lvl);
@ -170,7 +170,7 @@ long_result player_base::interpret_command(level *lvl, game_command cmd) {
} else {
inv.enabled = false;
return throw_potion(lvl, std::move(res.first),
(direction)res.second);
static_cast<direction>(res.second));
}
}
}
@ -213,7 +213,7 @@ long_result player_base::interpret_command(level *lvl, game_command cmd) {
enemy_base *tmp = get_enemy_at(pos + MOVE[cmd - attack_north],
lvl->get_elist());
auto res = attack((character *)tmp);
auto res = attack(static_cast<character *>(tmp));
if (tmp != nullptr && tmp->is_dead()) {
int g = tmp->dies(lvl);
@ -284,7 +284,8 @@ std::pair<std::unique_ptr<potion>, int> player_base::inventory::run(
if (owns.size()) {
std::unique_ptr<potion> tmp = std::move(owns[curr]);
owns.erase(owns.begin() + curr);
return {std::move(tmp), (direction)(cmd - throw_north)};
return {std::move(tmp),
static_cast<direction>(cmd - throw_north)};
}
return {nullptr, DIRECTION_CNT};
@ -322,14 +323,14 @@ void player_base::inventory::print(output *out, unsigned long known_potions) {
for (size_t i = 0; i < owns.size(); ++i) {
out->print_str({INV_LEFT + INV_POTION_OFFSET,
(int) i + INV_TOP + INV_Y_OFFSET},
static_cast<int>(i) + INV_TOP + INV_Y_OFFSET},
"potion of " +
(known_potions & (1 << owns[i]->get_type()) ?
owns[i]->get_name() : (std::string)"??"));
owns[i]->get_name() : std::string{"??"}));
}
if (owns.size())
out->print_str({INV_LEFT + INV_CURSOR_OFFSET,
(int)curr + INV_TOP + INV_Y_OFFSET},
static_cast<int>(curr) + INV_TOP + INV_Y_OFFSET},
"->");
}