Lighter library with a rework on characteristics.

This commit is contained in:
Pcornat 2024-10-29 23:23:56 +01:00
parent f412b1b997
commit 7e613ca2a0
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD
10 changed files with 404 additions and 142 deletions

View File

@ -95,7 +95,11 @@ namespace character {
void pop_object(CharacterSheet &sheet) noexcept;
// void insert_weapon(weapons weapon, CharacterSheet &sheet) noexcept;
[[nodiscard]] static ankerl::svector<bool, 3> check_conformity(const CharacterSheet &sheet) noexcept;
[[nodiscard]] const std::plus<std::uint32_t> &get_plus_operation() const { return plus; }
[[nodiscard]] const std::minus<std::uint32_t> &get_minus_operation() const { return minus; }
private:
std::plus<std::uint32_t> plus;
@ -124,6 +128,10 @@ namespace character {
characteristic::Characteristic &localEndurance,
characteristic::Characteristic &localChance,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) noexcept;
static void check_dagger_conditions(const CharacterSheet &sheet,
characteristic::Characteristic &localHabilete,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation);
};
}

View File

@ -1,18 +1,10 @@
#ifndef BILLYSHEET_CHARACTER_SHEET_HPP
#define BILLYSHEET_CHARACTER_SHEET_HPP
#include "characteristic/adresse.hpp"
#include "characteristic/endurance.hpp"
#include "characteristic/chance.hpp"
#include "characteristic/habilete.hpp"
#include "characteristic/characteristic.hpp"
#include "billy_objects.hpp"
#include <random>
#include <unordered_set>
namespace gui {
class Gui;
}
namespace character {
enum class classe : std::uint8_t {
Guerrier = 0,
@ -23,23 +15,20 @@ namespace character {
class CharacterSheet final {
private:
friend gui::Gui;
friend BillyObjects;
std::mt19937_64 engine{ std::random_device{ "rdseed" }() };
std::string caractere{};
//TODO: bad design pour les caractéristiques. Je dois trouver autre chose.
characteristic::Adresse adresse;
characteristic::Characteristic adresse{ characteristic::characType::Adresse };
characteristic::Endurance endurance;
characteristic::Characteristic endurance{ characteristic::characType::Endurance };
characteristic::Chance chance;
characteristic::Characteristic chance{ characteristic::characType::Chance };
characteristic::Habilete habilete;
characteristic::Characteristic habilete{ characteristic::characType::Habilete };
BillyObjects::container objects;
@ -80,13 +69,13 @@ namespace character {
[[nodiscard]] const std::string &get_caractere() const { return caractere; }
[[nodiscard]] const characteristic::Adresse &get_adresse() const { return adresse; }
[[nodiscard]] const characteristic::Characteristic &get_adresse() const { return adresse; }
[[nodiscard]] const characteristic::Endurance &get_endurance() const { return endurance; }
[[nodiscard]] const characteristic::Characteristic &get_endurance() const { return endurance; }
[[nodiscard]] const characteristic::Chance &get_chance() const { return chance; }
[[nodiscard]] const characteristic::Characteristic &get_chance() const { return chance; }
[[nodiscard]] const characteristic::Habilete &get_habilete() const { return habilete; }
[[nodiscard]] const characteristic::Characteristic &get_habilete() const { return habilete; }
[[nodiscard]] const BillyObjects::container &get_objects() const { return objects; }

View File

@ -1,30 +0,0 @@
#ifndef BILLYSHEET_ADRESSE_HPP
#define BILLYSHEET_ADRESSE_HPP
#include "characteristic.hpp"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace character::characteristic {
class Adresse final : public Characteristic {
public:
Adresse() noexcept: Characteristic(1, 0, 0, 0) {}
Adresse(const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) :
Characteristic(1, carac, materiel, additional) {}
~Adresse() noexcept final = default;
friend void from_json(const json &j, Adresse &adresse) {
nlohmann::from_json(j, static_cast<Characteristic &>(adresse));
}
};
static void to_json(json &j, const Adresse &adresse) {
to_json(j, static_cast<Characteristic>(adresse));
}
}
#endif //BILLYSHEET_ADRESSE_HPP

View File

@ -1,27 +0,0 @@
#ifndef BILLYSHEET_CHANCE_HPP
#define BILLYSHEET_CHANCE_HPP
#include "characteristic.hpp"
namespace character::characteristic {
class Chance final : public Characteristic {
public:
Chance() noexcept: Characteristic(3, 0, 0, 0) {}
Chance(const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) :
Characteristic(3, carac, materiel, additional) {}
~Chance() noexcept final = default;
friend void from_json(const json &j, Chance &chance) {
nlohmann::from_json(j, static_cast<Characteristic &>(chance));
}
};
static void to_json(json &j, const Chance &chance) {
to_json(j, static_cast<Characteristic>(chance));
}
}
#endif //BILLYSHEET_CHANCE_HPP

View File

@ -11,6 +11,13 @@ namespace character {
}
namespace character::characteristic {
enum class characType : std::uint8_t {
Adresse = 0,
Endurance = 1,
Chance = 2,
Habilete = 3
};
class Characteristic {
protected:
friend character::BillyObjects;
@ -21,19 +28,40 @@ namespace character::characteristic {
std::uint32_t materiel{ 0 };
std::uint32_t additional{ 0 };
static std::uint32_t get_base(const characType inType) noexcept {
switch (inType) {
case characType::Adresse:
return 1;
case characType::Chance:
return 3;
case characType::Endurance:
case characType::Habilete:
return 2;
}
}
public:
const characType type{ characType::Adresse };
Characteristic() noexcept = default;
Characteristic(const std::uint32_t base,
Characteristic(const characType inType,
const std::uint32_t carac,
const std::uint32_t materiel,
const std::uint32_t additional) noexcept:
base(base), carac(carac), materiel(materiel), additional(additional) { (void) get_total(); }
const std::uint32_t additional) :
base(get_base(inType)),
carac(carac),
materiel(materiel),
additional(additional),
type(inType) { (void) get_total(); }
explicit Characteristic(const characType inType) : Characteristic(inType, 0, 0, 0) {}
Characteristic(const Characteristic &charac) noexcept = default;
Characteristic &operator=(const Characteristic &charac) noexcept {
const_cast<std::uint32_t &>(base) = charac.base;
const_cast<characType &>(type) = charac.type;
carac = charac.carac;
materiel = charac.materiel;
additional = charac.additional;
@ -63,15 +91,17 @@ namespace character::characteristic {
friend void from_json(const json &j, Characteristic &charac) {
const_cast<std::uint32_t &>(charac.base) = j.at("base").get<std::uint32_t>();
const_cast<characType &>(charac.type) = static_cast<characType>(j.at("type").get<std::uint8_t>());
charac.carac = j.at("carac").get<std::uint32_t>();
charac.materiel = j.at("materiel").get<std::uint32_t>();
charac.additional = j.at("additional").get<std::uint32_t>();
charac.total = j.at("total").get<std::uint32_t>();
charac.total = j.at("total").get<std::int32_t>();
}
};
static void to_json(json &j, const Characteristic &charac) {
j["base"] = charac.get_base();
j["type"] = charac.type;
j["carac"] = charac.get_carac();
j["materiel"] = charac.get_materiel();
j["additional"] = charac.get_additional();

View File

@ -1,32 +0,0 @@
#ifndef BILLYSHEET_ENDURANCE_HPP
#define BILLYSHEET_ENDURANCE_HPP
#include "characteristic.hpp"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace character::characteristic {
class Endurance final : public Characteristic {
public:
Endurance() noexcept: Characteristic(2, 0, 0, 0) {};
Endurance(const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) noexcept:
Characteristic(2, carac, materiel, additional) {}
~Endurance() noexcept final = default;
[[nodiscard]] std::uint32_t get_max_lp() const noexcept;
friend void from_json(const json &j, Endurance &endurance) {
nlohmann::from_json(j, static_cast<Characteristic &>(endurance));
}
};
static void to_json(json &j, const Endurance &endurance) {
to_json(j, static_cast<Characteristic>(endurance));
}
}
#endif //BILLYSHEET_ENDURANCE_HPP

View File

@ -1,28 +0,0 @@
#ifndef BILLYSHEET_HABILETE_HPP
#define BILLYSHEET_HABILETE_HPP
#include <cstdint>
#include "characteristic.hpp"
namespace character::characteristic {
class Habilete final : public Characteristic {
public:
Habilete() noexcept: Characteristic(2, 0, 0, 0) {};
Habilete(const uint32_t carac, const uint32_t materiel, const uint32_t additional) noexcept:
Characteristic(2, carac, materiel, additional) {}
~Habilete() noexcept final = default;
friend void from_json(const json &j, Habilete &habilete) {
nlohmann::from_json(j, static_cast<Characteristic &>(habilete));
}
};
static void to_json(json &j, const Habilete &habilete) {
to_json(j, static_cast<Characteristic>(habilete));
}
}
#endif //BILLYSHEET_HABILETE_HPP

144
include/generic_object.hpp Normal file
View File

@ -0,0 +1,144 @@
//
// Created by postaron on 01/09/24.
//
#ifndef BILLYSHEET_GENERIC_OBJECT_HPP
#define BILLYSHEET_GENERIC_OBJECT_HPP
#include <cstdint>
#include <variant>
#include <string>
#include <string_view>
#include "characteristic/characteristic.hpp"
// helper type for the visitor
template<typename... Ts>
struct overloaded : Ts ... { using Ts::operator()...; };
template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
namespace character {
using namespace std::string_literals;
enum class weapons : std::uint8_t {
Sword = 0,
Lance = 1,
Morgenstern = 2,
Bow = 3
};
enum class equipments : std::uint8_t {
Chainmail = 4,
CookingPot = 5,
PamphletTourist = 6,
MedicKit = 7
};
enum class tools : std::uint8_t {
Fourche = 8,
Dagger = 9,
RockClimbingKit = 10,
SackOfGrain = 11
};
class Weapons;
class Equipments;
class Tools;
using billyObjects = std::variant<Weapons, Equipments, Tools>;
using billyEnums = std::variant<weapons, equipments, tools>;
static billyObjects new_object(const billyEnums &inputObject);
class GenericObject {
public:
virtual ~GenericObject() = default;
[[nodiscard]] virtual std::uint32_t add_armor() const noexcept = 0;
[[nodiscard]] virtual std::uint32_t add_critique() const noexcept = 0;
[[nodiscard]] virtual std::uint32_t add_damage() const noexcept = 0;
[[nodiscard]] virtual std::uint32_t add_materiel(const characteristic::characType inType) const noexcept = 0;
[[nodiscard]] virtual std::string_view to_string() const noexcept = 0;
};
class Weapons final : virtual public GenericObject {
public:
const weapons type{ weapons::Sword };
friend billyObjects new_object(const billyEnums &inputObject);
Weapons() = delete;
~Weapons() final = default;
[[nodiscard]] std::uint32_t add_armor() const noexcept final;
[[nodiscard]] std::uint32_t add_critique() const noexcept final;
[[nodiscard]] std::uint32_t add_damage() const noexcept final;
[[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final;
[[nodiscard]] std::string_view to_string() const noexcept override;
private:
explicit Weapons(const weapons type) : type(type) {}
};
class Equipments final : virtual public GenericObject {
public:
const equipments type{ equipments::Chainmail };
friend billyObjects new_object(const billyEnums &inputObject);
Equipments() = delete;
~Equipments() final = default;
[[nodiscard]] std::uint32_t add_armor() const noexcept final;
[[nodiscard]] std::uint32_t add_critique() const noexcept final;
[[nodiscard]] std::uint32_t add_damage() const noexcept final;
[[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final;
[[nodiscard]] std::string_view to_string() const noexcept override;
private:
explicit Equipments(const equipments type) : type(type) {}
};
class Tools final : virtual public GenericObject {
public:
const tools type{ tools::Fourche };
friend billyObjects new_object(const billyEnums &inputObject);
Tools() = delete;
~Tools() final = default;
[[nodiscard]] std::uint32_t add_armor() const noexcept final;
[[nodiscard]] std::uint32_t add_critique() const noexcept final;
[[nodiscard]] std::uint32_t add_damage() const noexcept final;
[[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final;
[[nodiscard]] std::string_view to_string() const noexcept override;
private:
explicit Tools(const tools type) : type(type) {}
};
} // character
#endif //BILLYSHEET_GENERIC_OBJECT_HPP

View File

@ -179,6 +179,24 @@ namespace character {
}
}
void BillyObjects::check_dagger_conditions(const CharacterSheet &sheet,
Characteristic &localHabilete,
const std::function<std::uint32_t(std::uint32_t, std::uint32_t)> &operation) {
int count_weapons = 0;
bool is_there_bow = false;
std::for_each(sheet.objects.cbegin(), sheet.objects.cend(), [&](const billyObject &object) -> void {
if (const weapons *p = std::get_if<weapons>(std::addressof(object)); p != nullptr) {
++count_weapons;
if (*p == weapons::Bow) {
is_there_bow = true;
}
}
});
if (count_weapons < 2 && !is_there_bow) {
localHabilete.materiel = operation(localHabilete.materiel, 1);
}
}
std::string_view BillyObjects::billy_object_to_string(const billyObject &object) noexcept {
return std::visit(overloaded{
[](const weapons &arg) {
@ -253,4 +271,24 @@ namespace character {
}, object);
}
}
ankerl::svector<bool, 3> BillyObjects::check_conformity(const CharacterSheet &sheet) noexcept {
ankerl::svector<bool, 3> output;
std::transform(sheet.get_objects().cbegin(),
sheet.get_objects().cend(), std::back_inserter(output),
[&sheet](const billyObject &object) -> bool {
return std::visit(overloaded{
[](const weapons weapon) { return false; },
[](const equipments equipment) { return false; },
[](const tools tool) { return false; },
}, object);
});
const int total = std::accumulate(sheet.get_objects().cbegin(),
sheet.get_objects().cend(),
0,
[](const int a, const billyObject &object) -> int {
return 0;
});
return output;
}
}

170
src/generic_object.cpp Normal file
View File

@ -0,0 +1,170 @@
//
// Created by postaron on 01/09/24.
//
#include "generic_object.hpp"
namespace character {
using characteristic::characType;
static constexpr std::string_view jsonKey{ "billy_objects" };
static constexpr std::string_view sword{ "Sword" };
static constexpr std::string_view lance{ "Lance" };
static constexpr std::string_view morgenstern{ "Morgenstern" };
static constexpr std::string_view bow{ "Bow" };
static constexpr std::string_view chainmail{ "Chainmail" };
static constexpr std::string_view cookingPot{ "Cooking pot" };
static constexpr std::string_view pamphletTourist{ "Touristic pamphlet" };
static constexpr std::string_view medicKit{ "Medic kit" };
static constexpr std::string_view fourche{ "Fourche" };
static constexpr std::string_view dagger{ "Dagger" };
static constexpr std::string_view rockClimbingKit{ "Rock climbing kit" };
static constexpr std::string_view sackOfGrain{ "Sack of grain" };
std::uint32_t Weapons::add_armor() const noexcept { return 0; }
std::uint32_t Weapons::add_critique() const noexcept { return 0; }
std::uint32_t Weapons::add_damage() const noexcept { return type == weapons::Morgenstern ? 1 : 0; }
std::uint32_t Weapons::add_materiel(const characteristic::characType inType) const noexcept {
switch (type) {
case weapons::Sword:
if (inType == characType::Habilete) {
return 4;
} else {
return 0;
}
case weapons::Lance:
switch (inType) {
case characType::Adresse:
return 1;
case characType::Habilete:
return 3;
default:
return 0;
}
case weapons::Morgenstern:
if (inType == characType::Endurance || inType == characType::Habilete) {
return 1;
} else {
return 0;
}
case weapons::Bow:
switch (inType) {
case characType::Habilete:
return 3;
case characType::Adresse:
return 1;
default:
return 0;
}
}
}
std::string_view Weapons::to_string() const noexcept {
switch (type) {
case weapons::Sword:
return sword;
case weapons::Lance:
return lance;
case weapons::Morgenstern:
return morgenstern;
case weapons::Bow:
return bow;
}
}
std::uint32_t Equipments::add_armor() const noexcept {
switch (type) {
case equipments::Chainmail:
return 2;
case equipments::CookingPot:
return 1;
default:
return 0;
}
}
std::uint32_t Equipments::add_critique() const noexcept { return 0; }
std::uint32_t Equipments::add_damage() const noexcept { return 0; }
std::uint32_t Equipments::add_materiel(const characteristic::characType inType) const noexcept {
switch (type) {
case equipments::Chainmail:
return inType == characType::Habilete ||
inType == characType::Adresse ||
inType == characType::Endurance ? 1 : 0;
case equipments::CookingPot:
return inType == characType::Endurance ? 2 : 0;
case equipments::PamphletTourist:
return inType == characType::Chance ? 4 : 0;
case equipments::MedicKit:
return inType == characType::Chance ? 1 : 0;
}
}
std::string_view Equipments::to_string() const noexcept {
switch (type) {
case equipments::Chainmail:
return chainmail;
case equipments::CookingPot:
return cookingPot;
case equipments::PamphletTourist:
return pamphletTourist;
case equipments::MedicKit:
return medicKit;
}
}
std::uint32_t Tools::add_armor() const noexcept { return 0; }
std::uint32_t Tools::add_critique() const noexcept { return type == tools::Dagger ? 6 : 0; }
std::uint32_t Tools::add_damage() const noexcept { return 0; }
std::uint32_t Tools::add_materiel(const characteristic::characType inType) const noexcept {
switch (type) {
case tools::Fourche:
switch (inType) {
case characType::Habilete:
return 1;
case characType::Endurance:
return 3;
default:
return 0;
}
case tools::Dagger:
return 0;
case tools::RockClimbingKit:
return inType == characType::Adresse ? 1 : 0;
case tools::SackOfGrain:
return inType == characType::Endurance || inType == characType::Chance ? 2 : 0;
}
}
std::string_view Tools::to_string() const noexcept {
switch (type) {
case tools::Fourche:
return fourche;
case tools::Dagger:
return dagger;
case tools::RockClimbingKit:
return rockClimbingKit;
case tools::SackOfGrain:
return sackOfGrain;
}
}
billyObjects new_object(const billyEnums &inputObject) {
return std::visit(overloaded{
[](const weapons input) { return billyObjects{ Weapons{ input }}; },
[](const equipments input) { return billyObjects{ Equipments{ input }}; },
[](const tools input) { return billyObjects{ Tools{ input }}; }
}, inputObject);
}
}