diff --git a/src/arguments.cc b/src/arguments.cc index e22170d..d45a863 100644 --- a/src/arguments.cc +++ b/src/arguments.cc @@ -1,2 +1,159 @@ #include "arguments.h" +#include +#include +#include +#include +#include "file_input.h" +#include "file_output.h" +#include "console_input.h" +#include "console_output.h" +#include "curses_input.h" +#include "curses_output.h" + +feature proc_args(int argc, char **argv, + std::unique_ptr &curse, + std::unique_ptr &in, + std::unique_ptr &out, + std::unique_ptr &log) { + feature result = 0; + std::string str; + std::string fn_fin; + std::string fn_fout; + std::string fn_lout; + std::string fn_save; + std::string seed; + + for (int i = 1; i < argc; ++i) { + str = argv[i]; + + if (str == "-n") { + if (result & (FEATURE_IN_FILE | FEATURE_OUT_FILE)) + return FEATURE_CONFLICT | i; + + result |= FEATURE_NCURSES; + } else if (str == "-r") { + result |= FEATURE_RAND_MAP; + } else if (str == "-m") { + result |= FEATURE_MENU; + } else if (str == "-c") { + result |= FEATURE_ENEMIES_CHASE; + } else if (str == "-i") { + result |= FEATURE_INVENTORY; + } else if (str == "-t") { + result |= FEATURE_THROW; + } else if (str == "-R") { + result |= FEATURE_REVISIT; + } else if (str == "-s") { + ++i; + str = argv[i]; + + if (!seed.empty() && seed != str) + return FEATURE_CONFLICT | i; + + seed = str; + result |= FEATURE_SEED; + } else if (str == "-L") { + ++i; + str = argv[i]; + + if (!fn_lout.empty() && fn_lout != str) + return FEATURE_CONFLICT | i; + + fn_lout = str; + result |= FEATURE_LOG; + } else if (str == "-I") { + ++i; + str = argv[i]; + + if (!fn_fin.empty() && fn_fin != str) + return FEATURE_CONFLICT | i; + + fn_fin = str; + result |= FEATURE_IN_FILE; + } else if (str == "-O") { + ++i; + str = argv[i]; + + if (!fn_fout.empty() && fn_fout != str) + return FEATURE_CONFLICT | i; + + fn_fout = str; + result |= FEATURE_OUT_FILE; + } else if (str == "-h" || str == "--help") { + return FEATURE_LIST_ARGS; + } else { + return FEATURE_PANIC | i; + } + } + + if (result & FEATURE_IN_FILE) { + std::ifstream fin(fn_fin); + + if (!fin.good()) + return FEATURE_PANIC_FILE | FEATURE_IN_FILE; + + in = std::make_unique(std::move(fin)); + } else if (!(result & FEATURE_NCURSES)) + in = std::make_unique(std::cin); + + if (result & FEATURE_OUT_FILE) { + std::ofstream fout(fn_fout); + + if (!fout.good()) + return FEATURE_PANIC_FILE | FEATURE_OUT_FILE; + + out = std::make_unique(std::move(fout)); + } else if (!(result & FEATURE_NCURSES)) + out = std::make_unique(std::cout); + + if (result & FEATURE_LOG) { + std::ofstream lout(fn_lout); + + if (!lout.good()) + return FEATURE_PANIC_FILE | FEATURE_LOG; + + log = std::make_unique(std::move(lout)); + } + + if (result & FEATURE_NCURSES) { + curse = std::make_unique(); + in = std::make_unique(curse); + out = std::make_unique(curse); + } + + return result; +} + +// IMPORTANT: This is meant for already panicking +void panic_args(feature panic) { + using namespace std; + + if (panic & FEATURE_PANIC) + cerr << "Invalid argument No. " << (panic ^ FEATURE_PANIC) + << " !" << endl; + else if (panic & FEATURE_CONFLICT) + cerr << "Argument No. " << (panic ^ FEATURE_CONFLICT) + << " conflicts with a previous argument!" << endl; + else if (panic & FEATURE_PANIC_FILE) { + switch (panic ^ FEATURE_PANIC_FILE) { + case FEATURE_IN_FILE: + cerr << "Cannot open specified input file!" << endl; + break; + + case FEATURE_OUT_FILE: + cerr << "Cannot open specified output file!" << endl; + break; + + case FEATURE_LOG: + cerr << "Cannot open specified log file!" << endl; + break; + + default: + cerr << "Something must have went really, really, wrong..." + << endl; + } + } else + cerr << "Something must have went really, really, wrong..." + << endl; +} diff --git a/src/arguments.h b/src/arguments.h index 163e23f..d211f0e 100644 --- a/src/arguments.h +++ b/src/arguments.h @@ -16,17 +16,20 @@ -i : Enable inventory -t : Enable throw -R : Enable revisiting levels --S [seed] : Sets initial seed to seed --s [file] : Enable saves +-s [seed] : Sets initial seed to seed -L [file] : Enable logging to file -I [file] : Reads commands from file. CANNOT BE USED WITH -n. -O [file] : Outputs to file. CANNOT BE USED WITH -n. +-h/--help : Displays options list (doesn't start a game) */ +// IMPORTANT: Errors include the index that caused them (or'ed into them) feature proc_args(int argc, char **argv, std::unique_ptr &curse, std::unique_ptr &in, std::unique_ptr &out, std::unique_ptr &log); +void panic_args(feature panic); + #endif diff --git a/src/characters.cc b/src/characters.cc index e85f07e..f610ff9 100644 --- a/src/characters.cc +++ b/src/characters.cc @@ -103,8 +103,8 @@ void character_list::print() const { void character_list::print(display &display) const { for (auto &ch : characters) - display.print_position(ch->get_position(), - CHARACTER_REP[ch->get_race()]); + display.print_char(ch->get_position(), + CHARACTER_REP[ch->get_race()]); } std::vector>::const_iterator character_list::begin() diff --git a/src/console_output.cc b/src/console_output.cc index 0c8915f..9beef14 100644 --- a/src/console_output.cc +++ b/src/console_output.cc @@ -5,7 +5,7 @@ #include -console_output::console_output(std::ostream &new_out): out{new_out} {} +console_output::console_output(std::ostream &cout): out{cout} {} /* Attributes black 30 40 diff --git a/src/console_output.h b/src/console_output.h index 9610e17..a6b5d44 100644 --- a/src/console_output.h +++ b/src/console_output.h @@ -9,7 +9,7 @@ private: std::ostream &out; std::string get_code(const int attr); public: - console_output(std::ostream &new_out); + console_output(std::ostream &cout); void render() override; void clear() override; diff --git a/src/constants.h b/src/constants.h index 0695388..43c56c2 100644 --- a/src/constants.h +++ b/src/constants.h @@ -77,9 +77,17 @@ const feature FEATURE_INVENTORY = 1 << 3; const feature FEATURE_THROW = 1 << 4; const feature FEATURE_REVISIT = 1 << 5; const feature FEATURE_LOG = 1 << 6; -const feature FEATURE_SAVE = 1 << 7; +const feature FEATURE_SEED = 1 << 7; const feature FEATURE_MENU = 1 << 8; +const feature FEATURE_IN_FILE = 1 << 9; +const feature FEATURE_OUT_FILE = 1 << 10; +const feature FEATURE_PANIC_FILE = 1 << 28; const feature FEATURE_PANIC = 1 << 29; +const feature FEATURE_CONFLICT = 1 << 30; +const feature FEATURE_LIST_ARGS = 1 << 31; + +const int RETURN_FINE = 0; +const int RETURN_PANICKED = 1; typedef std::vector position_list; typedef std::vector direction_list; diff --git a/src/file_output.cc b/src/file_output.cc index 58ddb74..1ed9fa2 100644 --- a/src/file_output.cc +++ b/src/file_output.cc @@ -2,8 +2,8 @@ #include -file_output::file_output(std::ofstream &&new_out): - out{std::move(new_out)} {} +file_output::file_output(std::ofstream &&ofs): + out{std::move(ofs)} {} void file_output::render() { for (std::size_t idx = 0; idx < contents.size(); ++idx) { diff --git a/src/file_output.h b/src/file_output.h index 95fab40..ff96ce5 100644 --- a/src/file_output.h +++ b/src/file_output.h @@ -8,7 +8,7 @@ class file_output final : public display { private: std::ofstream out; public: - file_output(std::ofstream &&new_out); + file_output(std::ofstream &&ofs); void render() override; void clear() override; diff --git a/src/log.h b/src/log.h index f55bda9..b82e1f9 100644 --- a/src/log.h +++ b/src/log.h @@ -1,6 +1,26 @@ #ifndef __LOG_H__ #define __LOG_H__ +#include +#include +#include +#include "constants.h" +#include "characters.h" +#include "objects.h" -class logger; +class logger final { +private: + std::ofstream out; +public: + 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(std::unique_ptr &player); + void print_chlist(const character_list &chlist); + void print_potions(const potion_list &potions); +}; #endif diff --git a/src/main.cc b/src/main.cc index e6eb13e..10e8df9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,12 +1,6 @@ #include "game.h" -#include "cursor.h" -#include "display.h" -#include "input.h" #include "arguments.h" -// The way things are designed to work: -// to be filled... - int main(int argc, char **argv) { std::unique_ptr curse; std::unique_ptr in; @@ -15,13 +9,16 @@ int main(int argc, char **argv) { feature enabled_features = proc_args(argc, argv, curse, in, out, log); - if (enabled_features & FEATURE_PANIC) - std::cerr << "Wrong arguments you dumbass :)" << std::endl; + if (enabled_features & + (FEATURE_PANIC | FEATURE_PANIC_FILE | FEATURE_CONFLICT)) { + panic_args(enabled_features); + return RETURN_PANICKED; + } game game_proc(enabled_features, in, out, log); while (game_proc.run() != game_status::terminated) out->render(); - return 0; + return RETURN_FINE; } diff --git a/src/map.cc b/src/map.cc index 777c5e3..a7d186d 100644 --- a/src/map.cc +++ b/src/map.cc @@ -49,7 +49,7 @@ void game_map::print() const { void game_map::print(display &display) const { for (int i = 0; i < MAP_HEIGHT; ++i) - display.print_line(map[i] + "\n", i); + display.print_str(position{0, i}, map[i]); } void game_map::apply_potion(character *who, const stat_name statn,