Skip to content

Commit

Permalink
Added clang-format and clang-tidy.
Browse files Browse the repository at this point in the history
Rearranged CMakeLists.txt and made all compiler warnings appear as errors.
  • Loading branch information
JackAshwell11 committed Oct 23, 2023
1 parent 1c52efc commit bb504f7
Show file tree
Hide file tree
Showing 36 changed files with 708 additions and 1,013 deletions.
2 changes: 2 additions & 0 deletions src/hades_extensions/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: Google
ColumnLimit: 120
23 changes: 23 additions & 0 deletions src/hades_extensions/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Checks:
- "*"
- "clang-analyzer-*"
- "bugprone-*"
- "modernize-*"
- "cppcoreguidelines-*"
- "readability-*"
- "misc-*"
- "google-*"
- "performance-*"
- "hicpp-*"
- "fuchsia-*"
- "llvm-*"
- "cert-*"
- "mpi-*"
- "openmp-*"
- "zircon-*"
- "abseil-*"
- "llvmlibc-*"
- "android-*"

WarningsAsErrors:
- "*"
18 changes: 10 additions & 8 deletions src/hades_extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ cmake_minimum_required(VERSION 3.18.2)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Get a path to the include directory as well as all the source and test files
# Define the module names and initialise the project
set(PY_MODULE hades_extensions)
set(CPP_LIB hades_extensions_lib)
set(TEST_MODULE hades_extensions_tests)
project(${PY_MODULE} LANGUAGES CXX)

# Get the various paths that will be used as well as all files
set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
set(TEST_DIR ${CMAKE_SOURCE_DIR}/test)
Expand Down Expand Up @@ -58,15 +65,10 @@ set(TEST_FILES
${TEST_DIR}/generation/test_primitives.cpp
)

# Define the module names and initialise the project
set(PY_MODULE hades_extensions)
set(CPP_LIB hades_extensions_lib)
set(TEST_MODULE hades_extensions_tests)
project(${PY_MODULE} LANGUAGES CXX)

# Add the src and test directories to the project
include(FetchContent)
add_subdirectory(src)
add_subdirectory(test)

# TODO: Look at maybe adding clang-tidy and clang-format files
# TODO: Try and see if include formatting can be improved
# TODO: Try and have clang-tidy and cppcheck in CI
48 changes: 21 additions & 27 deletions src/hades_extensions/include/game_objects/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct SystemBase {
/// Process update logic for a system.
///
/// @param delta_time - The time interval since the last time the function was called.
virtual void update(double delta_time) {};
virtual void update(double delta_time){};
};

// ----- EXCEPTIONS ------------------------------
Expand All @@ -53,27 +53,24 @@ class RegistryException : public std::runtime_error {
/// @param not_registered_type - The type of item that is not registered.
/// @param value - The value that is not registered.
/// @param error - The error raised by the registry.
template<typename T>
RegistryException(const std::string &not_registered_type,
const T &value,
const std::string &error = "is not registered with the registry") : std::runtime_error(
"The " + not_registered_type + " `" + to_string(value) + "` " + error + ".") {};
template <typename T>
RegistryException(const std::string &not_registered_type, const T &value,
const std::string &error = "is not registered with the registry")
: std::runtime_error("The " + not_registered_type + " `" + to_string(value) + "` " + error + "."){};

private:
/// Convert a value to a string.
///
/// @param value - The value to convert to a string.
/// @return The value as a string.
static inline std::string to_string(const char *value) {
return {value};
}
static inline std::string to_string(const char *value) { return {value}; }

/// Convert a value to a string.
///
/// @tparam T - The type of value to convert to a string.
/// @param value - The value to convert to a string.
/// @return The value as a string.
template<typename T>
template <typename T>
static inline std::string to_string(const T &value) {
return std::to_string(value);
}
Expand Down Expand Up @@ -111,8 +108,8 @@ class Registry {
/// @param game_object_id - The game object ID.
/// @param args - The arguments to pass to the component constructor.
/// @throws RegistryException - If the game object is not registered or if the game object already has the component.
template<typename ComponentType, typename ... Args>
void add_component(GameObjectID game_object_id, Args &&... args) {
template <typename ComponentType, typename... Args>
void add_component(GameObjectID game_object_id, Args &&...args) {
// Check if the game object is registered or not
if (!game_objects_.contains(game_object_id)) {
throw RegistryException("game object", game_object_id);
Expand All @@ -125,16 +122,17 @@ class Registry {

// Add the component to the registry
components_[typeid(ComponentType)].insert(game_object_id);
game_objects_[game_object_id][typeid(ComponentType)] = std::make_shared<ComponentType>(std::forward<Args>(args) ...);
game_objects_[game_object_id][typeid(ComponentType)] = std::make_shared<ComponentType>(std::forward<Args>(args)...);
}

/// Get a component from the registry.
///
/// @tparam T - The type of component to get.
/// @param game_object_id - The game object ID.
/// @throws RegistryException - If the game object is not registered or if the game object does not have the component.
/// @throws RegistryException - If the game object is not registered or if the game object does not have the
/// component.
/// @return The component from the registry.
template<typename T>
template <typename T>
inline std::shared_ptr<T> get_component(GameObjectID game_object_id) const {
return std::static_pointer_cast<T>(get_component(game_object_id, typeid(T)));
}
Expand All @@ -151,10 +149,10 @@ class Registry {
///
/// @tparam Ts - The types of components to find.
/// @return A vector of tuples containing the game object ID and the required components.
template<typename ... Ts>
std::vector<std::tuple<GameObjectID, std::tuple<std::shared_ptr<Ts> ...>>> find_components() const {
template <typename... Ts>
std::vector<std::tuple<GameObjectID, std::tuple<std::shared_ptr<Ts>...>>> find_components() const {
// Create a vector of tuples to store the components
std::vector<std::tuple<GameObjectID, std::tuple<std::shared_ptr<Ts> ...>>> components;
std::vector<std::tuple<GameObjectID, std::tuple<std::shared_ptr<Ts>...>>> components;

// Iterate over all game objects
for (auto &[game_object_id, game_object_components] : game_objects_) {
Expand All @@ -165,7 +163,7 @@ class Registry {

// Game object has all the components, so cast them to T and add them to
// the vector
auto components_result = std::make_tuple(std::static_pointer_cast<Ts>(game_object_components.at(typeid(Ts))) ...);
auto components_result = std::make_tuple(std::static_pointer_cast<Ts>(game_object_components.at(typeid(Ts)))...);
components.emplace_back(game_object_id, components_result);
}

Expand All @@ -177,7 +175,7 @@ class Registry {
///
/// @tparam SystemType - The type of system to add.
/// @throws RegistryException - If the system is already registered.
template<typename SystemType>
template <typename SystemType>
void add_system() {
// Check if the system is already registered
const std::type_info &system_type = typeid(SystemType);
Expand All @@ -194,7 +192,7 @@ class Registry {
/// @tparam T - The type of system to find.
/// @throws RegistryException - If the system is not registered.
/// @return The system.
template<typename T>
template <typename T>
std::shared_ptr<T> find_system() const {
// Check if the system is registered
auto system_result = systems_.find(typeid(T));
Expand Down Expand Up @@ -226,16 +224,12 @@ class Registry {
/// Add a wall to the registry.
///
/// @param wall - The wall to add to the registry.
inline void add_wall(const Vec2d &wall) {
walls_.emplace(wall);
}
inline void add_wall(const Vec2d &wall) { walls_.emplace(wall); }

/// Get the walls in the registry.
///
/// @return The walls in the registry.
[[nodiscard]] inline const std::unordered_set<Vec2d> &get_walls() const {
return walls_;
}
[[nodiscard]] inline const std::unordered_set<Vec2d> &get_walls() const { return walls_; }

private:
/// The next game object ID to use.
Expand Down
8 changes: 2 additions & 6 deletions src/hades_extensions/include/game_objects/stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ class Stat : public ComponentBase {
/// Get the value of the stat.
///
/// @return The value of the stat.
[[nodiscard]] inline double get_value() const {
return value_;
}
[[nodiscard]] inline double get_value() const { return value_; }

/// Set the value of the stat.
///
/// @param new_value - The new value of the stat.
inline void set_value(double new_value) {
value_ = std::max(std::min(new_value, max_value), 0.0);
}
inline void set_value(double new_value) { value_ = std::max(std::min(new_value, max_value), 0.0); }

protected:
/// The current value of the variable.
Expand Down
37 changes: 10 additions & 27 deletions src/hades_extensions/include/game_objects/steering.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,23 @@ constexpr double SPRITE_SIZE = 128 * SPRITE_SCALE;
// ----- STRUCTURES ------------------------------
/// Represents a 2D vector.
struct Vec2d {
inline bool operator==(const Vec2d &vec) const {
return x == vec.x && y == vec.y;
}
inline bool operator==(const Vec2d &vec) const { return x == vec.x && y == vec.y; }

inline bool operator!=(const Vec2d &vec) const {
return x != vec.x || y != vec.y;
}
inline bool operator!=(const Vec2d &vec) const { return x != vec.x || y != vec.y; }

inline Vec2d operator+(const Vec2d &vec) const {
return {x + vec.x, y + vec.y};
}
inline Vec2d operator+(const Vec2d &vec) const { return {x + vec.x, y + vec.y}; }

inline Vec2d operator+=(const Vec2d &vec) {
x += vec.x;
y += vec.y;
return *this;
}

inline Vec2d operator-(const Vec2d &vec) const {
return {x - vec.x, y - vec.y};
}
inline Vec2d operator-(const Vec2d &vec) const { return {x - vec.x, y - vec.y}; }

inline Vec2d operator*(const double val) const {
return {x * val, y * val};
}
inline Vec2d operator*(const double val) const { return {x * val, y * val}; }

inline Vec2d operator/(const double val) const {
return {std::floor(x / val), std::floor(y / val)};
}
inline Vec2d operator/(const double val) const { return {std::floor(x / val), std::floor(y / val)}; }

/// The x value of the vector.
double x;
Expand All @@ -62,9 +50,7 @@ struct Vec2d {
/// Get the magnitude of the vector.
///
/// @return The magnitude of the vector.
[[nodiscard]] inline double magnitude() const {
return std::hypot(x, y);
}
[[nodiscard]] inline double magnitude() const { return std::hypot(x, y); }

/// Normalise the vector
///
Expand Down Expand Up @@ -100,9 +86,7 @@ struct Vec2d {
///
/// @param other - The vector to get the distance to.
/// @return The distance to the other vector.
[[nodiscard]] inline double distance_to(const Vec2d &other) const {
return std::hypot(x - other.x, y - other.y);
}
[[nodiscard]] inline double distance_to(const Vec2d &other) const { return std::hypot(x - other.x, y - other.y); }
};

/// Stores various data about a game object for use in physics-related operations.
Expand All @@ -118,7 +102,7 @@ struct KinematicObject {
};

// ----- HASHES ------------------------------
template<>
template <>
struct std::hash<Vec2d> {
size_t operator()(const Vec2d &vec) const {
size_t res = 0;
Expand Down Expand Up @@ -165,8 +149,7 @@ Vec2d follow_path(const Vec2d &current_position, std::vector<Vec2d> &path_list);
/// @param current_velocity - The velocity of the game object.
/// @param walls - The set of walls in the game.
/// @return The new steering force from this behaviour.
Vec2d obstacle_avoidance(const Vec2d &current_position,
const Vec2d &current_velocity,
Vec2d obstacle_avoidance(const Vec2d &current_position, const Vec2d &current_velocity,
const std::unordered_set<Vec2d> &walls);

/// Allow a game object to seek towards another game object's predicted position.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum class AttackAlgorithms {
// ----- STRUCTURES ------------------------------
/// Holds the result of an attack.
struct AttackResult {
/// The result of a ranged attack.
/// The result of a ranged attack.
std::optional<std::tuple<Vec2d, double, double>> ranged_attack;

/// The default constructor.
Expand Down
22 changes: 8 additions & 14 deletions src/hades_extensions/include/game_objects/systems/effects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ struct StatusEffectData {
/// @param increase_function - The increase function to apply.
/// @param duration_function - The duration function to apply.
/// @param interval_function - The interval function to apply.
StatusEffectData(StatusEffectType status_effect_type,
ActionFunction increase_function,
ActionFunction duration_function,
ActionFunction interval_function)
StatusEffectData(StatusEffectType status_effect_type, ActionFunction increase_function,
ActionFunction duration_function, ActionFunction interval_function)
: status_effect_type(status_effect_type),
increase_function(std::move(increase_function)),
duration_function(std::move(duration_function)),
Expand All @@ -88,8 +86,8 @@ struct EffectApplier : public ComponentBase {
/// @param instant_effects - The instant effects the game object provides.
/// @param status_effects - The status effects the game object provides.
EffectApplier(std::unordered_map<std::type_index, ActionFunction> instant_effects,
std::unordered_map<std::type_index, StatusEffectData> status_effects) : instant_effects(std::move(
instant_effects)), status_effects(std::move(status_effects)) {}
std::unordered_map<std::type_index, StatusEffectData> status_effects)
: instant_effects(std::move(instant_effects)), status_effects(std::move(status_effects)) {}
};

/// Allows a game object to have status effects applied to it.
Expand Down Expand Up @@ -119,10 +117,8 @@ struct EffectSystem : public SystemBase {
/// @param level - The level of the effect to apply.
/// @throws RegistryException - If the game object does not exist or does not have the target component.
/// @return Whether the instant effect was applied or not.
bool apply_instant_effect(GameObjectID game_object_id,
const std::type_index &target_component,
const ActionFunction &increase_function,
int level);
bool apply_instant_effect(GameObjectID game_object_id, const std::type_index &target_component,
const ActionFunction &increase_function, int level);

/// Apply a status effect to a game object.
///
Expand All @@ -132,8 +128,6 @@ struct EffectSystem : public SystemBase {
/// @param level - The level of the effect to apply.
/// @throws RegistryException - If the game object does not exist or does not have the target component.
/// @return Whether the status effect was applied or not.
bool apply_status_effect(GameObjectID game_object_id,
const std::type_index &target_component,
const StatusEffectData &status_effect_data,
int level);
bool apply_status_effect(GameObjectID game_object_id, const std::type_index &target_component,
const StatusEffectData &status_effect_data, int level);
};
10 changes: 4 additions & 6 deletions src/hades_extensions/include/game_objects/systems/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class InventorySpaceException : public std::runtime_error {
/// Initialise the object.
///
/// @param full - Whether the inventory is full or not.
explicit InventorySpaceException(const bool full) : std::runtime_error(
std::string("The inventory is ") + (full ? "full" : "empty") + ".") {}
explicit InventorySpaceException(const bool full)
: std::runtime_error(std::string("The inventory is ") + (full ? "full" : "empty") + ".") {}
};

// ----- COMPONENTS ------------------------------
Expand All @@ -41,9 +41,7 @@ struct Inventory : public ComponentBase {
/// Get the capacity of the inventory.
///
/// @return The capacity of the inventory.
[[nodiscard]] inline int get_capacity() const {
return width * height;
}
[[nodiscard]] inline int get_capacity() const { return width * height; }
};

// ----- SYSTEMS --------------------------------
Expand All @@ -59,7 +57,7 @@ struct InventorySystem : public SystemBase {
/// @param game_object_id - The ID of the game object to add the item to.
/// @param item - The item to add to the inventory.
/// @throws RegistryException - If the game object does not exist or does not have an inventory component.
/// @throws InventorySpaceException - If the inventory is full.
/// @throws InventorySpaceException - If the inventory is full.
void add_item_to_inventory(GameObjectID game_object_id, GameObjectID item);

/// Remove an item from the inventory of a game object.
Expand Down
2 changes: 0 additions & 2 deletions src/hades_extensions/include/game_objects/systems/upgrade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "game_objects/registry.hpp"

// ----- COMPONENTS ------------------------------
// TODO: Maybe move this

/// Allows a game object to record the amount of money it has.
struct Money : public ComponentBase {
/// The amount of money the game object has.
Expand Down
Loading

0 comments on commit bb504f7

Please sign in to comment.