From 39d344759327944068935ce8dbfa283c5a0f96d4 Mon Sep 17 00:00:00 2001 From: Peisong Xiao Date: Sat, 13 Jul 2024 18:22:15 -0400 Subject: [PATCH] work in progress --- src/arguments.cc | 2 ++ src/cc3k.h | 2 ++ src/constants.h | 4 +-- src/game.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ src/game.h | 2 ++ src/map.h | 2 +- src/position.cc | 10 ++++++ src/position.h | 4 +++ 8 files changed, 105 insertions(+), 3 deletions(-) diff --git a/src/arguments.cc b/src/arguments.cc index f51c4fd..6e22392 100644 --- a/src/arguments.cc +++ b/src/arguments.cc @@ -140,6 +140,8 @@ feature proc_args(int argc, char **argv, return FEATURE_PANIC_SEED; rng = std::make_unique(tmp); + } else { + rng = std::make_unique(); } return result; diff --git a/src/cc3k.h b/src/cc3k.h index e702d1a..17663f9 100644 --- a/src/cc3k.h +++ b/src/cc3k.h @@ -3,4 +3,6 @@ class CC3K; +// every time it runs, clear first + #endif diff --git a/src/constants.h b/src/constants.h index 42682dd..786f46a 100644 --- a/src/constants.h +++ b/src/constants.h @@ -9,7 +9,7 @@ const int INF = 0x3F3F3F3F; enum error {none}; // TODO: update result to include subject -enum result {fine, died, go_down, go_up, hit, moved, miss}; +enum result {fine, died, go_down, go_up, hit, moved, miss, terminate}; enum game_status {terminated, main_menu, in_game, options, dead, won, escaped, restart @@ -63,7 +63,7 @@ const int DIRECTION_CNT = 8; // IMPORTANT: east is positive for x and SOUTH is positive for y // initializes all directions to an int enum direction { north = 0, south, east, west, northeast, - northwest, southeast, southest + northwest, southeast, southwest }; const position MOVE[DIRECTION_CNT] = { diff --git a/src/game.cc b/src/game.cc index dc7df97..e3ec72d 100644 --- a/src/game.cc +++ b/src/game.cc @@ -40,10 +40,89 @@ void game::new_level() { rng, enabled_features)); } +result game::player_moves(game_command cmd) { + switch (cmd) { + case game_command_terminate: + return result::terminate; + + case move_east: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(east); + + return player->move(east, levels[curr_level]->get_available_around( + player->get_position())); + } + + case move_west: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(west); + + return player->move(west, levels[curr_level]->get_available_around( + player->get_position())); + } + + case move_north: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(north); + + return player->move(north, levels[curr_level]->get_available_around( + player->get_position())); + } + + case move_south: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(south); + + return player->move(south, levels[curr_level]->get_available_around( + player->get_position())); + } + + case move_northeast: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(northeast); + + return player->move(northeast, + levels[curr_level]->get_available_around( + player->get_position())); + } + + case move_northwest: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(northwest); + + return player->move(northwest, + levels[curr_level]->get_available_around( + player->get_position())); + } + + case move_southeast: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(southeast); + + return player->move(southeast, + levels[curr_level]->get_available_around( + player->get_position())); + } + + case move_southwest: { + if (enabled_features & FEATURE_NCURSES) + return player_move_or_attack(southwest); + + return player->move(southwest, + levels[curr_level]->get_available_around( + player->get_position())); + } + } +} + game_status game::run() { + player->start_turn(); auto res = player_moves(in->get_command()); switch (res) { + case result::terminate: + return terminated; + case result::died: return game_status::dead; @@ -70,7 +149,10 @@ game_status game::run() { break; } + move_enemies(); + if (player->get_HP() <= 0) + return game_status::dead; return game_status::in_game; } diff --git a/src/game.h b/src/game.h index 6221749..7aea250 100644 --- a/src/game.h +++ b/src/game.h @@ -44,6 +44,8 @@ private: result player_moves(game_command cmd); void new_level(); void move_enemies(); + + result player_move_or_attack(const direction &dir); }; #endif diff --git a/src/map.h b/src/map.h index 675a490..e4652b0 100644 --- a/src/map.h +++ b/src/map.h @@ -18,7 +18,7 @@ private: static const int MAX_ROOM_WIDTH = 20; static const int MAX_ROOM_HEIGHT = 5; // setup safezones - static const int ACTUAL_MAP_WIDTH = MAP_WIDTH - 5; + static const int ACTUAL_MAP_WIDTH = MAP_WIDTH - 6; static const int ACTUAL_MAP_HEIGHT = MAP_HEIGHT - 5; static const int MAP_PADDING = 3; static const int MIN_ROOM_SPACING = 3; diff --git a/src/position.cc b/src/position.cc index 452d9c2..092f3ff 100644 --- a/src/position.cc +++ b/src/position.cc @@ -74,3 +74,13 @@ void remove_from_list(std::vector return; } } + +#include "math.h" + +float distance(const position &a, const position &b) { + return sqrt(distance_sqr(a, b)); +} + +int distance_sqr(const position &a, const position &b) { + return pow(a.x - b.x, 2) + pow(a.y - b.y, 2); +} diff --git a/src/position.h b/src/position.h index c796540..5466121 100644 --- a/src/position.h +++ b/src/position.h @@ -26,4 +26,8 @@ std::vector remove_from_list(const std::vector std::vector excluded); void remove_from_list(std::vector &sorted_positions, position &excluded); + +float distance(const position &a, const position &b); +int distance_sqr(const position &a, const position &b); + #endif