Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 64 additions & 110 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,133 +1,87 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.21)
project(combat_cpp VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


#----------------------------------------------------------------
# Packages
#----------------------------------------------------------------
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_definitions(-DCMAKE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")

#----------------------------------------------------------------
# GTest External Project (from gtest documentation)
# https://bit.ly/3g8iXeM
#----------------------------------------------------------------

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
enable_testing()

#----------------------------------------------------------------
# Protobuf Messages
# Set compiler flags
# (modified from fmtlib: https://github.com/fmtlib/fmt/blob/master/CMakeLists.txt)
#----------------------------------------------------------------
add_subdirectory(messages)
include(CheckCXXCompilerFlag)

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(PEDANTIC_COMPILE_FLAGS -pedantic-errors -Wall -Wextra -pedantic
-Wold-style-cast -Wundef
-Wredundant-decls -Wwrite-strings -Wpointer-arith
-Wcast-qual -Wformat=2 -Wmissing-include-dirs
-Wcast-align
-Wctor-dtor-privacy -Wdisabled-optimization
-Winvalid-pch -Woverloaded-virtual
-Wconversion -Wundef
-Wno-ctor-dtor-privacy -Wno-format-nonliteral)
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
-Wno-dangling-else -Wno-unused-local-typedefs)
endif ()
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wdouble-promotion
-Wtrampolines -Wzero-as-null-pointer-constant -Wuseless-cast
-Wvector-operation-performance -Wsized-deallocation -Wshadow)
endif ()
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wshift-overflow=2
-Wnull-dereference -Wduplicated-cond)
endif ()
endif ()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion -Wundef
-Wdeprecated -Wweak-vtables -Wshadow
-Wno-gnu-zero-variadic-macro-arguments)
check_cxx_compiler_flag(-Wzero-as-null-pointer-constant HAS_NULLPTR_WARNING)
if (HAS_NULLPTR_WARNING)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
-Wzero-as-null-pointer-constant)
endif ()
endif ()

if (MSVC)
set(PEDANTIC_COMPILE_FLAGS /W3)
endif ()

#----------------------------------------------------------------
# Main Sources
# Add messages library
#----------------------------------------------------------------
set(ser_includes
include/Die.hpp
include/Combatant.hpp
include/Utility.hpp
include/Item.hpp
include/Weapon.hpp
include/Armour.hpp
include/States.hpp
include/GameLogic.hpp
)

set(ser_sources
src/Die.cpp
src/Combatant.cpp
src/Utility.cpp
src/Weapon.cpp
src/Armour.cpp
src/GameLogic.cpp
)

set(all_sources
${ser_includes}
${ser_sources}
)

add_subdirectory(proto)

#----------------------------------------------------------------
# Executables
# Add main library
#----------------------------------------------------------------
add_executable(exampleMain
src/main.cpp
${all_sources}
)
file(GLOB COMBAT_CPP_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
file(GLOB COMBAT_CPP_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/combat_cpp/*.hpp")

target_compile_features(exampleMain
PRIVATE
cxx_std_17
)
add_library(${PROJECT_NAME} ${COMBAT_CPP_SOURCES} ${COMBAT_CPP_HEADERS})

target_include_directories(exampleMain
PUBLIC
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)

target_link_libraries(exampleMain
PUBLIC
messages
)

add_executable(runAllTests
src/all_tests.cpp
${all_sources}
)

target_compile_features(runAllTests
target_compile_features(${PROJECT_NAME}
PRIVATE
cxx_std_17
)

target_include_directories(runAllTests
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
target_compile_options(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${PEDANTIC_COMPILE_FLAGS}
)

target_link_libraries(runAllTests
PUBLIC
gtest_main
messages
)
#----------------------------------------------------------------
# Add Apps
#----------------------------------------------------------------
add_subdirectory(app)

enable_testing()
add_test(GTest runAllTests)
#----------------------------------------------------------------
# Add Tests
#----------------------------------------------------------------
add_subdirectory(test)
15 changes: 0 additions & 15 deletions CMakeLists.txt.in

This file was deleted.

25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ Possible actions are: Fight, Check Health, Rest, Look Around, and Exit. While fi

The game ends when all enemies are defeated, or the user chooses to exit. I hope you enjoy playing this little game!

Want to try it out? All you need to do is clone, configure + build with CMake, and run exampleMain :D
It has passed build actions for windows-latest, ubuntu-latest, and macos-latest, however I have only personally tested on windows and ubuntu (in WSL for now).

Last but not least, please report any bugs you find! Any and all feedback is always welcome as I plan to continue casual improvements of this.

Note: Enemy names and any references are meant simply as a tribute to stories I have enjoyed in the past. I will gladly modify as required should anyone have any issues with it

Note: The majority of rules are based on this source: https://roll20.net/compendium/dnd5e/CategoryIndex%3ARules#content
## Pre-requisites and Setup

1. Make sure your c++ dev environment is setup
a. On windows, Visual Studio makes set up very simple (2022 was used in this project)
b. On Linux/MacOS, be sure to install both `build-essential` and `ninja-build`
c. Make sure to install CMake version 3.21 or newer
2. Populate submodules with `git submodule update --init`
3. Build with your favorite CMake methods (either GUI or command line)
4. Run tests if you feel like it / are developing
5. Try the game by running `main_game`

## Notices

* Enemy names and any references are meant simply as a tribute to stories I have enjoyed in the past. I will gladly modify as required should anyone have any issues with it
* The majority of rules are based on this source: https://roll20.net/compendium/dnd5e/CategoryIndex%3ARules#content
* It has passed build actions for windows-latest, ubuntu-latest, and macos-latest, however I have only personally tested on windows and ubuntu.
* Please report any bugs you find! Any and all feedback is always welcome as I plan to continue casual improvements of this.
21 changes: 21 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
message("-- Configuring applications")
add_executable(main_game
main.cpp
)

target_compile_features(main_game
PRIVATE
cxx_std_17
)

target_link_libraries(main_game
PRIVATE
combat_cpp
)

target_compile_options(main_game
PRIVATE
${PEDANTIC_COMPILE_FLAGS}
)

message("-- DONE configuring application")
2 changes: 1 addition & 1 deletion src/main.cpp → app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>

#include "GameLogic.hpp"
#include "combat_cpp/GameLogic.hpp"

int main()
{
Expand Down
2 changes: 1 addition & 1 deletion include/Armour.hpp → include/combat_cpp/Armour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <unordered_map>
#include <utility>

#include "Item.hpp"
#include "combat_cpp/Item.hpp"

enum class ArmourType
{
Expand Down
8 changes: 4 additions & 4 deletions include/Combatant.hpp → include/combat_cpp/Combatant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include <cstdint>

#include "Utility.hpp"
#include "Die.hpp"
#include "Weapon.hpp"
#include "Armour.hpp"
#include "combat_cpp/Utility.hpp"
#include "combat_cpp/Die.hpp"
#include "combat_cpp/Weapon.hpp"
#include "combat_cpp/Armour.hpp"

struct AttackResult
{
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions include/GameLogic.hpp → include/combat_cpp/GameLogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <memory>
#include <unordered_map>

#include "Combatant.hpp"
#include "Die.hpp"
#include "States.hpp"
#include "combat_cpp/Combatant.hpp"
#include "combat_cpp/Die.hpp"
#include "combat_cpp/States.hpp"

class GameLogic
{
Expand Down
File renamed without changes.
10 changes: 7 additions & 3 deletions include/States.hpp → include/combat_cpp/States.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <variant>
#include <optional>

#include "Combatant.hpp"
#include "combat_cpp/Combatant.hpp"

struct GameState
{
Expand All @@ -14,8 +14,8 @@ struct GameState
struct Combat
{
Combat() : target(nullptr), target_key(), player_initiative(0) {};
Combat(Combatant* const target, const uint8_t initiative)
: target(target)
Combat(Combatant* const target_, const uint8_t initiative)
: target(target_)
, target_key()
, player_initiative(initiative)
{
Expand Down Expand Up @@ -52,6 +52,7 @@ struct GameState
}
if (new_state.has_value()) { state_ = new_state.value(); };
};

void FinishFight()
{
std::optional<State> new_state;
Expand Down Expand Up @@ -79,6 +80,7 @@ struct GameState
}
if (new_state.has_value()) { state_ = new_state.value(); };
};

void FinishRest()
{
std::optional<State> new_state;
Expand Down Expand Up @@ -106,6 +108,7 @@ struct GameState
}
if (new_state.has_value()) { state_ = new_state.value(); };
};

void FinishLook()
{
std::optional<State> new_state;
Expand Down Expand Up @@ -133,6 +136,7 @@ struct GameState
}
if (new_state.has_value()) { state_ = new_state.value(); };
};

void FinishSelfCheck()
{
std::optional<State> new_state;
Expand Down
2 changes: 1 addition & 1 deletion include/Utility.hpp → include/combat_cpp/Utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ namespace Utility

void ConditionStringInPlace(std::string& str, const bool remove_whitespace, const bool to_lower);
std::string ConditionString(std::string str, const bool remove_whitespace, const bool to_lower);
};
}

#endif // !COMBAT_CPP_UTILITY_HPP_
6 changes: 3 additions & 3 deletions include/Weapon.hpp → include/combat_cpp/Weapon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <unordered_map>
#include <utility>

#include "Item.hpp"
#include "Die.hpp"
#include "combat_cpp/Item.hpp"
#include "combat_cpp/Die.hpp"

struct WeaponInfo
{
WeaponInfo(const std::string& description, const int die_n, const int num_dice)
WeaponInfo(const std::string& description, const uint8_t die_n, const uint8_t num_dice)
{
Description = description;
DieN = die_n;
Expand Down
Loading