From 36542ee11d592e3df65647f54427c742aae68025 Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Sat, 8 Jun 2024 19:49:06 -0700 Subject: [PATCH] clean up config file and placement of executables --- .gitignore | 7 ++- CMakeLists.txt | 5 ++- config.json | 47 +++++++++----------- dependencies/json/CMakeLists.txt | 4 +- include/server/game/servergamestate.hpp | 14 ------ include/shared/utilities/config.hpp | 48 +++++++-------------- src/client/CMakeLists.txt | 1 + src/client/client.cpp | 42 +++++++++--------- src/client/lobbyfinder.cpp | 2 +- src/server/game/introcutscene.cpp | 7 +-- src/server/game/mazegenerator.cpp | 4 +- src/server/game/servergamestate.cpp | 17 +++----- src/server/lobbybroadcaster.cpp | 2 +- src/server/server.cpp | 2 +- src/shared/utilities/config.cpp | 57 ++++--------------------- src/shared/utilities/root_path.cpp | 9 +--- 16 files changed, 91 insertions(+), 177 deletions(-) diff --git a/.gitignore b/.gitignore index ee5600e7..4248f92b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,9 @@ docs/* !docs/.gitkeep maps/generated/** -!maps/generated/.gitkeep \ No newline at end of file +!maps/generated/.gitkeep + +client +server +client.exe +server.exe \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 099bfccc..ff6608bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ endif() # CMake Variables set(CMAKE_CXX_STANDARD 20) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/bin) +set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}) # Generate a "compile_commands.json" file for VScode to use set(CMAKE_EXPORT_COMPILE_COMMANDS True) @@ -36,7 +36,7 @@ add_subdirectory(dependencies/google-test) # If we need any compiler / linker flags, add them here - SET(GCC_COMPILE_FLAGS "") +SET(GCC_COMPILE_FLAGS "") SET(GCC_LINK_FLAGS "") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COMPILE_FLAGS}") @@ -47,6 +47,7 @@ project(game VERSION 1.0) # Custom Variables SET(INCLUDE_DIRECTORY ${PROJECT_SOURCE_DIR}/include) + # Dependencies # For some reason I had to add this in two places to make it work on certain lab computers??? diff --git a/config.json b/config.json index 2359dd97..e7b4dfba 100644 --- a/config.json +++ b/config.json @@ -1,30 +1,23 @@ { - "game": { - "maze": { - "directory": "maps", - "procedural": false, - "maze_file": "demo/game1_player_pov.maze" + "server": { + "port": 2355, + "lobby_name": "Funny Lobby Name Here", + "lobby_broadcast": true, + "max_players": 1, + "disable_dm": false, + "skip_intro": false, + "disable_enemies": false, + "maze": { + "directory": "maps", + "procedural": true, + "maze_file": "demo/game1_player_pov.maze" + } }, - "disable_enemies": false - }, - "network": { - "server_ip": "localhost", - "server_port": 2355 - }, - "server": { - "lobby_name": "Funny Lobby Name Here", - "lobby_broadcast": true, - "max_players": 4, - "disable_dm": false, - "skip_intro": false - }, - "client": { - "default_name": "Conan O'Brien", - "lobby_discovery": true, - "fullscreen": true, - "draw_bboxes": false, - "fps_counter": true, - "presentation": false, - "render": 80 - } + "client": { + "lobby_discovery": true, + "fullscreen": true, + "fps_counter": true, + "presentation": false, + "render": 80 + } } diff --git a/dependencies/json/CMakeLists.txt b/dependencies/json/CMakeLists.txt index 7d962626..a05ef9a0 100644 --- a/dependencies/json/CMakeLists.txt +++ b/dependencies/json/CMakeLists.txt @@ -1,4 +1,4 @@ - include(FetchContent) +include(FetchContent) # orignally used the snippet from here # https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent @@ -6,4 +6,4 @@ # https://github.com/owodzeg/V4Hero/issues/91 FetchContent_Declare(json GIT_REPOSITORY https://github.com/nlohmann/json GIT_TAG v3.11.2) - FetchContent_MakeAvailable(json) \ No newline at end of file +FetchContent_MakeAvailable(json) \ No newline at end of file diff --git a/include/server/game/servergamestate.hpp b/include/server/game/servergamestate.hpp index 0151f522..433383d1 100644 --- a/include/server/game/servergamestate.hpp +++ b/include/server/game/servergamestate.hpp @@ -53,20 +53,6 @@ class ServerGameState { */ std::unique_ptr spawner; - /** - * @brief Creates a ServerGameState instance. The intial GamePhase is set to - * Lobby. - */ - ServerGameState(); - - /** - * @brief Creats a ServerGameState instance and sets the initial game phase - * to the given GamePhase. - * @param start_phase GamePhase that the new ServerGameState instance will - * start in. - */ - explicit ServerGameState(GamePhase start_phase); - ServerGameState(GamePhase start_phase, const GameConfig& config); /** diff --git a/include/shared/utilities/config.hpp b/include/shared/utilities/config.hpp index 5a7f0c8d..d8f5820e 100644 --- a/include/shared/utilities/config.hpp +++ b/include/shared/utilities/config.hpp @@ -10,8 +10,20 @@ * to know the exact string indices to use to index into the nlohmann::json object. */ struct GameConfig { - /// @brief Game config options + /// @brief Config settings for the server struct { + /// @brief port the server should run on + int port; + /// @brief Name of the server's lobby + std::string lobby_name; + /// @brief Whether or not the server should broadcast that the server is + bool lobby_broadcast; + /// @brief max number of players this server allows + int max_players; + /// @brief whether or not the server will spawn a DM + bool disable_dm; + /// @brief whether or not to skip the intro cutscene + bool skip_intro; struct { /** * @brief Path of the directory (contained in the repository @@ -31,37 +43,15 @@ struct GameConfig { std::string maze_file; } maze; + /// @brief whether or not to disable enemy spawns bool disable_enemies; - } game; - /// @brief Shared config settings for the network - struct { - /// @brief IP that the server is being hosted on. E.g. "127.0.0.1" for localhost. - std::string server_ip; - /// @brief Port that the server is running on. This should be a value between - /// 2302-2400, or 6073 so that it can be accepted through the firewall on lab computers - int server_port; - } network; - /// @brief Config settings for the server - struct { - /// @brief Name of the server's lobby - std::string lobby_name; - /// @brief Whether or not the server should broadcast that the server is - bool lobby_broadcast; - /// @brief max number of players this server allows - int max_players; - /// @brief whether or not the server will spawn a DM - bool disable_dm; - /// @brief whether or not to skip the intro cutscene - bool skip_intro; } server; /// @brief Config settings for the client struct { - /// @brief Default name of the client - std::string default_name; /// @brief Whether or not the client should listen for server lobby broadcasts bool lobby_discovery; + /// @brief whether or not the client should open in fullscreen bool fullscreen; - bool draw_bboxes; bool fps_counter; bool presentation; int render; @@ -89,11 +79,3 @@ struct GameConfig { */ static GameConfig parse(int argc, char** argv); }; - - -/** - * @brief Generates a GameConfig with default values. - * Note: Not using a constructor as then aggregate initialization will not be - * possible for GameConfig structs - */ -GameConfig getDefaultConfig(); diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index b96b8351..36670b40 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -29,6 +29,7 @@ set(FILES bone.cpp ) + # OpenGL set(OpenGL_GL_PREFERENCE GLVND) find_package(OpenGL REQUIRED) diff --git a/src/client/client.cpp b/src/client/client.cpp index 4de23bec..dc2f663a 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -102,21 +102,16 @@ AudioManager* Client::getAudioManager() { } bool Client::connect(std::string ip_addr) { - this->endpoints = resolver.resolve(ip_addr, std::to_string(config.network.server_port)); + this->endpoints = resolver.resolve(ip_addr, std::to_string(config.server.port)); this->session = std::make_shared(std::move(this->socket), - SessionInfo(this->config.client.default_name, {}, {})); + SessionInfo("name", {}, {})); if (!this->session->connectTo(this->endpoints)) { return false; } - auto name = this->gui.getCapturedKeyboardInput(); - if (name == "") { - name = config.client.default_name; - } - auto packet = PackagedPacket::make_shared(PacketType::ClientDeclareInfo, - ClientDeclareInfoPacket { .player_name = name }); + ClientDeclareInfoPacket { .player_name = "name" }); this->session->sendPacket(packet); @@ -1518,20 +1513,23 @@ void Client::lightingPass() { } void Client::drawBbox(boost::optional object) { - if (this->config.client.draw_bboxes) { - auto bbox_pos = object->physics.corner; - // for some reason the y axis of the bbox is off by half - // the dimension of the object. when trying getCenterPosition - // it was off on the x axis. - bbox_pos.y += object->physics.dimensions.y / 2.0f; - - item_model->setDimensions(object->physics.dimensions); - item_model->translateAbsolute(bbox_pos); - item_model->rotateAbsolute(glm::normalize(object->physics.facing), true); - item_model->draw(this->deferred_geometry_shader.get(), - this->cam->getPos(), - true); - } + // disabled because it doesn't really render the bboxes in the right location + // so no real reason to users to want to turn this on + + // if (this->config.client.draw_bboxes) { + // auto bbox_pos = object->physics.corner; + // // for some reason the y axis of the bbox is off by half + // // the dimension of the object. when trying getCenterPosition + // // it was off on the x axis. + // bbox_pos.y += object->physics.dimensions.y / 2.0f; + + // item_model->setDimensions(object->physics.dimensions); + // item_model->translateAbsolute(bbox_pos); + // item_model->rotateAbsolute(glm::normalize(object->physics.facing), true); + // item_model->draw(this->deferred_geometry_shader.get(), + // this->cam->getPos(), + // true); + // } } // callbacks - for Interaction diff --git a/src/client/lobbyfinder.cpp b/src/client/lobbyfinder.cpp index ce2e88c9..02b6a744 100644 --- a/src/client/lobbyfinder.cpp +++ b/src/client/lobbyfinder.cpp @@ -12,7 +12,7 @@ LobbyFinder::LobbyFinder(boost::asio::io_context& io_context, const GameConfig& config): lobby_discovery_socket(io_context, - udp::endpoint(address_v4::any(), config.network.server_port)), + udp::endpoint(address_v4::any(), config.server.port)), keep_searching(false), lobby_info_buf() { diff --git a/src/server/game/introcutscene.cpp b/src/server/game/introcutscene.cpp index 791679fb..c45d26a9 100644 --- a/src/server/game/introcutscene.cpp +++ b/src/server/game/introcutscene.cpp @@ -10,9 +10,10 @@ GameConfig getCutsceneConfig() { - GameConfig config = getDefaultConfig(); - config.game.maze.maze_file = "cutscene/intro.maze"; - config.game.maze.procedural = false; + GameConfig config = GameConfig {}; + config.server.maze.directory = "maps"; + config.server.maze.maze_file = "cutscene/intro.maze"; + config.server.maze.procedural = false; return config; } diff --git a/src/server/game/mazegenerator.cpp b/src/server/game/mazegenerator.cpp index 794fce7b..e6315bfc 100644 --- a/src/server/game/mazegenerator.cpp +++ b/src/server/game/mazegenerator.cpp @@ -35,8 +35,8 @@ MazeGenerator::MazeGenerator(GameConfig config) { } // has to happen after the for loop loading in the type vectors - if (!config.game.maze.procedural) { - auto path = getRepoRoot() / config.game.maze.directory / config.game.maze.maze_file; + if (!config.server.maze.procedural) { + auto path = getRepoRoot() / config.server.maze.directory / config.server.maze.maze_file; this->_loadRoom(path, false); return; } diff --git a/src/server/game/servergamestate.cpp b/src/server/game/servergamestate.cpp index ef9687d3..5d275f47 100644 --- a/src/server/game/servergamestate.cpp +++ b/src/server/game/servergamestate.cpp @@ -39,8 +39,6 @@ /* Constructors and Destructors */ -ServerGameState::ServerGameState() : ServerGameState(getDefaultConfig()) {} - ServerGameState::ServerGameState(GameConfig config) : config(config) { this->phase = GamePhase::LOBBY; this->timestep = FIRST_TIMESTEP; @@ -48,8 +46,8 @@ ServerGameState::ServerGameState(GameConfig config) : config(config) { this->lobby.max_players = config.server.max_players; this->lobby.name = config.server.lobby_name; - this->maps_directory = config.game.maze.directory; - this->maze_file = config.game.maze.maze_file; + this->maps_directory = config.server.maze.directory; + this->maze_file = config.server.maze.maze_file; // Initialize game instance match phase data // Match begins in MazeExploration phase (no timer) @@ -85,10 +83,10 @@ ServerGameState::ServerGameState(GameConfig config) : config(config) { attempts++; } - if (config.game.maze.procedural) { + if (config.server.maze.procedural) { std::cout << "Took " << attempts << " attempts to generate a full procedural maze\n"; std::string filename = std::to_string(getMsSinceEpoch()) + ".maze"; - auto path = getRepoRoot() / config.game.maze.directory / "generated" / filename; + auto path = getRepoRoot() / config.server.maze.directory / "generated" / filename; std::cout << "Saving procedural maze to " << path << std::endl; grid->writeToFile(path.string()); } @@ -99,11 +97,6 @@ ServerGameState::ServerGameState(GameConfig config) : config(config) { this->loadMaze(*grid); } -ServerGameState::ServerGameState(GamePhase start_phase) - : ServerGameState(getDefaultConfig()) { - this->phase = start_phase; -} - ServerGameState::ServerGameState(GamePhase start_phase, const GameConfig& config) : ServerGameState(config) { this->phase = start_phase; @@ -739,7 +732,7 @@ void ServerGameState::update(const EventList& events) { handleDeaths(); handleRespawns(); deleteEntities(); - if (!this->config.game.disable_enemies) { + if (!this->config.server.disable_enemies) { spawnEnemies(); } handleTickVelocity(); diff --git a/src/server/lobbybroadcaster.cpp b/src/server/lobbybroadcaster.cpp index fb737f97..413dc3ce 100644 --- a/src/server/lobbybroadcaster.cpp +++ b/src/server/lobbybroadcaster.cpp @@ -64,7 +64,7 @@ void LobbyBroadcaster::_lobbyBroadcastWorker() { this->socket.set_option(udp::socket::reuse_address(true)); this->socket.set_option(boost::asio::socket_base::broadcast(true)); - udp::endpoint endpt(address_v4::broadcast(), this->config.network.server_port); + udp::endpoint endpt(address_v4::broadcast(), this->config.server.port); // Don't bother with packet headers here, because there is only one packet being sent over UDP // so we don't need to distinguish them. diff --git a/src/server/server.cpp b/src/server/server.cpp index 87e103d5..159dd702 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -46,7 +46,7 @@ using namespace boost::asio::ip; Server::Server(boost::asio::io_context& io_context, GameConfig config) :lobby_broadcaster(io_context, config), - acceptor(io_context, tcp::endpoint(tcp::v4(), config.network.server_port)), + acceptor(io_context, tcp::endpoint(tcp::v4(), config.server.port)), socket(io_context), world_eid(0), state(ServerGameState(GamePhase::LOBBY, config)), diff --git a/src/shared/utilities/config.cpp b/src/shared/utilities/config.cpp index 25c9f614..a14b648f 100644 --- a/src/shared/utilities/config.cpp +++ b/src/shared/utilities/config.cpp @@ -16,7 +16,7 @@ GameConfig GameConfig::parse(int argc, char** argv) { // cppcheck-suppress const exit(1); } - boost::filesystem::path filepath = getRepoRoot() / "config.json"; + boost::filesystem::path filepath = "config.json"; if (argc == 2) { filepath = argv[1]; } @@ -36,30 +36,23 @@ GameConfig GameConfig::parse(int argc, char** argv) { // cppcheck-suppress const try { return GameConfig { - .game = { - .maze = { - .directory = json.at("game").at("maze").at("directory"), - .procedural = json.at("game").at("maze").at("procedural"), - .maze_file = json.at("game").at("maze").at("maze_file") - }, - .disable_enemies = json.at("game").at("disable_enemies") - }, - .network = { - .server_ip = json.at("network").at("server_ip"), - .server_port = json.at("network").at("server_port") - }, .server = { + .port = json.at("server").at("port"), .lobby_name = json.at("server").at("lobby_name"), .lobby_broadcast = json.at("server").at("lobby_broadcast"), .max_players = json.at("server").at("max_players"), .disable_dm = json.at("server").at("disable_dm"), - .skip_intro = json.at("server").at("skip_intro") + .skip_intro = json.at("server").at("skip_intro"), + .maze = { + .directory = json.at("server").at("maze").at("directory"), + .procedural = json.at("server").at("maze").at("procedural"), + .maze_file = json.at("server").at("maze").at("maze_file") + }, + .disable_enemies = json.at("server").at("disable_enemies") }, .client = { - .default_name = json.at("client").at("default_name"), .lobby_discovery = json.at("client").at("lobby_discovery"), .fullscreen = json.at("client").at("fullscreen"), - .draw_bboxes = json.at("client").at("draw_bboxes"), .fps_counter = json.at("client").at("fps_counter"), .presentation = json.at("client").at("presentation"), .render = json.at("client").at("render") @@ -70,35 +63,3 @@ GameConfig GameConfig::parse(int argc, char** argv) { // cppcheck-suppress const std::exit(1); } } - -GameConfig getDefaultConfig() { - return GameConfig{ - .game = { - .maze = { - .directory = "maps", - .procedural = true, - .maze_file = "default_maze.maze" - }, - .disable_enemies = false - }, - .network = { - .server_ip = "localhost", - .server_port = 2355 - }, - .server = { - .lobby_name = "My Test Lobby", - .lobby_broadcast = false, - .max_players = 1, - .disable_dm = false, - .skip_intro = false - }, - .client = { - .default_name = "Player", - .lobby_discovery = false, - .fullscreen = false, - .draw_bboxes = false, - .fps_counter = false, - .presentation = false - } - }; -} \ No newline at end of file diff --git a/src/shared/utilities/root_path.cpp b/src/shared/utilities/root_path.cpp index 3a28f7ed..e24370c7 100644 --- a/src/shared/utilities/root_path.cpp +++ b/src/shared/utilities/root_path.cpp @@ -4,12 +4,5 @@ #include boost::filesystem::path getRepoRoot() { - /** - * build - * bin - * client - * server - * ... - */ - return boost::dll::program_location().parent_path().parent_path().parent_path(); + return boost::dll::program_location().parent_path(); } \ No newline at end of file