Initial work on battling
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2019-10-17 14:33:25 +02:00
parent 212a7d1ad6
commit d580d81163
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
21 changed files with 302 additions and 53 deletions

View File

@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.13)
# If a defined C Compiler is not set
if (NOT CMAKE_C_COMPILER)
# If the GCC flag is set, use GCC
if (GCC)
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
# Otherwise default to clang
else()
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
endif(GCC)
endif(NOT CMAKE_C_COMPILER)
# Make warnings trigger errors.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
project(CreatureLib)
@ -17,30 +21,42 @@ project(CreatureLib)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (WINDOWS)
ADD_DEFINITIONS(-D WINDOWS=1)
endif(WINDOWS)
# Create Core library with files in src/Core
file(GLOB_RECURSE CORE_SRC_FILES "src/Core/*.cpp" "src/Core/*.hpp")
add_library(CreatureLibCore SHARED ${CORE_SRC_FILES})
# Create main Library library with files in src/Library
file(GLOB_RECURSE LIBRARY_SRC_FILES "src/Library/*.cpp" "src/Library/*.hpp")
add_library(CreatureLibLibrary SHARED ${LIBRARY_SRC_FILES})
# Create Battling library with files in src/Battling
file(GLOB_RECURSE BATTLING_SRC_FILES "src/Battling/*.cpp" "src/Battling/*.hpp")
add_library(CreatureLibBattling SHARED ${BATTLING_SRC_FILES})
# Create Test executable
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
add_executable(CreatureLibTests ${TEST_FILES} extern/catch.hpp)
target_link_libraries(CreatureLibLibrary -static-libgcc -static-libstdc++)
target_link_libraries(CreatureLibTests -static-libgcc -static-libstdc++)
# Link the core library to the individual other libraries
target_link_libraries(CreatureLibLibrary CreatureLibCore)
target_link_libraries(CreatureLibBattling CreatureLibCore)
# Link the library data to the Battling library
target_link_libraries(CreatureLibBattling CreatureLibLibrary)
target_link_libraries(CreatureLibTests CreatureLibLibrary)
if (WINDOWS)
# Statically link libraries we need in Windows.
target_link_libraries(CreatureLibCore -static -static-libgcc -static-libstdc++)
target_link_libraries(CreatureLibLibrary -static -static-libgcc -static-libstdc++)
target_link_libraries(CreatureLibBattling -static -static-libgcc -static-libstdc++)
target_link_libraries(CreatureLibTests -static -static-libgcc -static-libstdc++)
endif(WINDOWS)
# Add a definition for the test library
target_compile_definitions(CreatureLibTests PRIVATE TESTS_BUILD)

View File

@ -0,0 +1,5 @@
#include "BattleLibrary.hpp"
const CreatureLib::Battling::BattleStatCalculator *CreatureLib::Battling::BattleLibrary::GetStatCalculator() const {
return _statCalculator;
}

View File

@ -0,0 +1,14 @@
#ifndef CREATURELIB_BATTLELIBRARY_HPP
#define CREATURELIB_BATTLELIBRARY_HPP
#include "BattleStatCalculator.hpp"
namespace CreatureLib::Battling {
class BattleLibrary {
BattleStatCalculator* _statCalculator;
public:
const BattleStatCalculator* GetStatCalculator() const;
};
}
#endif //CREATURELIB_BATTLELIBRARY_HPP

View File

@ -0,0 +1,39 @@
#include "BattleStatCalculator.hpp"
#include "../Models/BattleCreature.hpp"
using namespace CreatureLib;
Core::StatisticSet<uint32_t>
Battling::BattleStatCalculator::CalculateStats(Battling::BattleCreature *creature) const {
return Core::StatisticSet<uint32_t>(
CalculateStat(creature, Core::Statistic::Health),
CalculateStat(creature, Core::Statistic::PhysicalAttack),
CalculateStat(creature, Core::Statistic::PhysicalDefense),
CalculateStat(creature, Core::Statistic::MagicalAttack),
CalculateStat(creature, Core::Statistic::MagicalDefense),
CalculateStat(creature, Core::Statistic::Speed)
);
}
uint32_t CalculateHealthStat(Battling::BattleCreature *creature){
auto base = creature->GetBackingCreature();
auto level = creature->GetLevel();
auto a = (base->GetBaseStat(Core::Statistic::Health) + base->GetStatPotential(Core::Statistic::Health)) * 2 +
floor(sqrt(base->GetStatExperience(Core::Statistic::Health) / 4)) * level;
return floor(a / 100) + level + 10;
}
uint32_t CalculateOtherStat(Battling::BattleCreature *creature, Core::Statistic stat){
auto base = creature->GetBackingCreature();
auto level = creature->GetLevel();
auto a = (base->GetBaseStat(stat) + base->GetStatPotential(stat)) * 2 +
floor(sqrt(base->GetStatExperience(stat) / 4)) * level;
return floor(a / 100) + 10;
}
uint32_t Battling::BattleStatCalculator::CalculateStat(Battling::BattleCreature *creature, Core::Statistic stat) const{
if (stat == Core::Statistic::Health)
return CalculateHealthStat(creature);
return CalculateOtherStat(creature, stat);
}

View File

@ -0,0 +1,17 @@
#ifndef CREATURELIB_BATTLESTATCALCULATOR_HPP
#define CREATURELIB_BATTLESTATCALCULATOR_HPP
#include "../../Core/StatisticSet.hpp"
namespace CreatureLib::Battling {
//predeclare BattleCreature class
class BattleCreature;
class BattleStatCalculator {
public:
virtual Core::StatisticSet<uint32_t > CalculateStats(BattleCreature* creature) const;
virtual uint32_t CalculateStat(BattleCreature* creature, Core::Statistic stat) const;
};
}
#endif //CREATURELIB_BATTLESTATCALCULATOR_HPP

View File

@ -0,0 +1,5 @@
#include "Battle.hpp"
const CreatureLib::Battling::BattleLibrary *CreatureLib::Battling::Battle::GetLibrary() const {
return _library;
}

View File

@ -0,0 +1,18 @@
#ifndef CREATURELIB_BATTLE_HPP
#define CREATURELIB_BATTLE_HPP
#include <vector>
#include "BattleSide.hpp"
#include "../Library/BattleLibrary.hpp"
namespace CreatureLib::Battling {
class Battle {
const BattleLibrary* _library;
std::vector<BattleSide*> _sides;
public:
const BattleLibrary* GetLibrary() const;
};
}
#endif //CREATURELIB_BATTLE_HPP

View File

@ -0,0 +1,37 @@
#include "BattleCreature.hpp"
#include "../Models/Battle.hpp"
using namespace CreatureLib;
Battling::BattleCreature::BattleCreature(Battling::Battle *battle,
Library::Creature *creature)
:__Battle(battle),
__Level(creature->GetLevel()),
_creature(creature),
_statBoost(Core::StatisticSet<int8_t >())
{
}
const Library::Creature* Battling::BattleCreature::GetBackingCreature() {
return _creature;
}
void Battling::BattleCreature::ApplyPostBattleEffects() {
}
uint32_t Battling::BattleCreature::GetStat(Core::Statistic stat) {
return _fullStats.GetStat(stat);
}
void Battling::BattleCreature::RecalculateStats() {
this->_fullStats = this->__Battle->GetLibrary()->GetStatCalculator()->CalculateStats(this);
}
void Battling::BattleCreature::RecalculateStat(Core::Statistic stat) {
auto s = this->__Battle->GetLibrary()->GetStatCalculator()->CalculateStat(this, stat);
this->_fullStats.SetStat(stat, s);
}

View File

@ -0,0 +1,34 @@
#ifndef CREATURELIB_BATTLECREATURE_HPP
#define CREATURELIB_BATTLECREATURE_HPP
#include "../../GenericTemplates.cpp"
#include "../../Library/Creature.hpp"
namespace CreatureLib::Battling{
// Forward declare battle class
class Battle;
class BattleCreature {
GetProperty(Battle*, Battle);
GetProperty(uint8_t, Level);
private:
Library::Creature* _creature;
Core::StatisticSet<int8_t > _statBoost;
Core::StatisticSet<uint32_t > _fullStats;
public:
BattleCreature(Battle* battle, Library::Creature* creature);
const Library::Creature* GetBackingCreature();
void ApplyPostBattleEffects();
uint32_t GetStat(Core::Statistic stat);
void RecalculateStats();
void RecalculateStat(Core::Statistic);
};
}
#endif //CREATURELIB_BATTLECREATURE_HPP

View File

@ -0,0 +1 @@
#include "BattleSide.hpp"

View File

@ -0,0 +1,14 @@
#ifndef CREATURELIB_BATTLESIDE_HPP
#define CREATURELIB_BATTLESIDE_HPP
#include <vector>
#include "BattleCreature.hpp"
namespace CreatureLib::Battling{
class BattleSide {
std::vector<BattleCreature*> _creatures;
};
}
#endif //CREATURELIB_BATTLESIDE_HPP

View File

@ -0,0 +1 @@
#include "BattleTeam.hpp"

View File

@ -0,0 +1,10 @@
#ifndef CREATURELIB_BATTLETEAM_HPP
#define CREATURELIB_BATTLETEAM_HPP
class BattleTeam {
};
#endif //CREATURELIB_BATTLETEAM_HPP

View File

@ -1,13 +1,10 @@
#include <exception>
#include "StatisticSet.hpp"
CreatureLib::Core::StatisticSet::StatisticSet(uint32_t health, uint32_t physicalAttack, uint32_t physicalDefense,
uint32_t magicalAttack, uint32_t magicalDefense, uint32_t speed)
:
__Health(health),
__PhysicalAttack(physicalAttack),
__PhysicalDefense(physicalDefense),
__MagicalAttack(magicalAttack),
__MagicalDefense(magicalDefense),
__Speed(speed)
{}
template <class T>
CreatureLib::Core::StatisticSet<T>::StatisticSet()
: _health(0), _physicalAttack(0), _physicalDefense(0), _magicalAttack(0), _magicalDefense(0), _speed(0)
{
}

View File

@ -1,30 +1,55 @@
#ifndef CREATURELIB_STATISTICSET_HPP
#define CREATURELIB_STATISTICSET_HPP
#include <stdint.h>
#include <exception>
#include "Statistic.hpp"
#include "../GenericTemplates.cpp"
namespace CreatureLib::Core{
template <class T>
class StatisticSet {
protected:
T _health;
T _physicalAttack;
T _physicalDefense;
T _magicalAttack;
T _magicalDefense;
T _speed;
public:
StatisticSet(uint32_t health, uint32_t physicalAttack, uint32_t physicalDefense, uint32_t magicalAttack,
uint32_t magicalDefense, uint32_t speed);
StatisticSet(T health, T physicalAttack, T physicalDefense, T magicalAttack, T magicalDefense, T speed)
: _health(health), _physicalAttack(physicalAttack),
_physicalDefense(physicalDefense), _magicalAttack(magicalAttack),
_magicalDefense(magicalDefense), _speed(speed){}
StatisticSet();
GetSetProperty(uint32_t, Health)
GetSetProperty(uint32_t, PhysicalAttack)
GetSetProperty(uint32_t, PhysicalDefense)
GetSetProperty(uint32_t, MagicalAttack)
GetSetProperty(uint32_t, MagicalDefense)
GetSetProperty(uint32_t, Speed)
inline T GetHealth() const{ return _health; }
inline T GetPhysicalAttack() const{ return _physicalAttack; }
inline T GetPhysicalDefense() const{ return _physicalDefense; }
inline T GetMagicalAttack() const{ return _magicalAttack; }
inline T GetMagicalDefense() const{ return _magicalDefense; }
inline T GetSpeed() const{ return _speed; }
[[nodiscard]] inline uint32_t GetStat(Statistic stat) const{
[[nodiscard]] inline T GetStat(Statistic stat) const{
switch (stat){
case Health: return __Health;
case PhysicalAttack: return __PhysicalAttack;
case PhysicalDefense: return __PhysicalDefense;
case MagicalAttack: return __MagicalAttack;
case MagicalDefense: return __MagicalDefense;
case Speed: return __Speed;
case Health: return _health;
case PhysicalAttack: return _physicalAttack;
case PhysicalDefense: return _physicalDefense;
case MagicalAttack: return _magicalAttack;
case MagicalDefense: return _magicalDefense;
case Speed: return _speed;
}
throw std::exception();
}
inline void SetStat(Statistic stat, T value){
switch (stat){
case Health: _health = value;
case PhysicalAttack: _physicalAttack = value;
case PhysicalDefense: _physicalDefense = value;
case MagicalAttack: _magicalAttack = value;
case MagicalDefense: _magicalDefense = value;
case Speed: _speed = value;
}
throw std::exception();
}

View File

@ -13,19 +13,19 @@ namespace CreatureLib::Library {
uint8_t _level;
std::string _nickname = "";
uint32_t _healthPotential = 0;
uint32_t _physAttackPotential = 0;
uint32_t _physDefensePotential = 0;
uint32_t _magAttackPotential = 0;
uint32_t _magDefensePotential = 0;
uint32_t _speedPotential = 0;
uint8_t _healthPotential = 0;
uint8_t _physAttackPotential = 0;
uint8_t _physDefensePotential = 0;
uint8_t _magAttackPotential = 0;
uint8_t _magDefensePotential = 0;
uint8_t _speedPotential = 0;
uint32_t _healthExperience = 0;
uint32_t _physAttackExperience = 0;
uint32_t _physDefenseExperience = 0;
uint32_t _magAttackExperience = 0;
uint32_t _magDefenseExperience = 0;
uint32_t _speedExperience = 0;
uint8_t _healthExperience = 0;
uint8_t _physAttackExperience = 0;
uint8_t _physDefenseExperience = 0;
uint8_t _magAttackExperience = 0;
uint8_t _magDefenseExperience = 0;
uint8_t _speedExperience = 0;
std::string _talent = "";
Gender _gender = static_cast<Gender>(-1);

View File

@ -4,8 +4,8 @@ CreatureLib::Library::Creature::Creature(const CreatureLib::Library::DataLibrary
const CreatureLib::Library::CreatureSpecies *species,
const CreatureLib::Library::SpeciesVariant *variant, uint8_t level,
std::string nickname, int8_t talentIndex,
CreatureLib::Core::StatisticSet statExperience,
CreatureLib::Core::StatisticSet statPotential,
CreatureLib::Core::StatisticSet<uint8_t > statExperience,
CreatureLib::Core::StatisticSet<uint8_t > statPotential,
uint32_t identifier, CreatureLib::Library::Gender gender, uint8_t coloring,
const CreatureLib::Library::Item *heldItem)
:
@ -52,8 +52,20 @@ int32_t CalculateStat(const CreatureLib::Library::Creature* creature, CreatureLi
}
int32_t CreatureLib::Library::Creature::CalculateFullStat(CreatureLib::Core::Statistic stat) const {
uint32_t CreatureLib::Library::Creature::CalculateFullStat(CreatureLib::Core::Statistic stat) const {
if (stat == Core::Statistic::Health)
return CalculateHealth(this);
return CalculateStat(this, stat);
}
uint16_t CreatureLib::Library::Creature::GetBaseStat(CreatureLib::Core::Statistic stat) const {
return this->GetVariant()->GetStatistic(stat);
}
uint8_t CreatureLib::Library::Creature::GetStatPotential(CreatureLib::Core::Statistic stat) const {
return this->__StatPotential.GetStat(stat);
}
uint8_t CreatureLib::Library::Creature::GetStatExperience(CreatureLib::Core::Statistic stat) const {
return this->__StatExperience.GetStat(stat);
}

View File

@ -13,8 +13,8 @@ namespace CreatureLib::Library{
GetProperty(const SpeciesVariant*, Variant);
GetProperty(uint8_t, Level);
GetProperty(uint32_t, Experience);
GetProperty(Core::StatisticSet, StatExperience);
GetProperty(Core::StatisticSet, StatPotential);
GetProperty(Core::StatisticSet<uint8_t >, StatExperience);
GetProperty(Core::StatisticSet<uint8_t >, StatPotential);
GetProperty(uint32_t, UniqueIdentifier);
GetProperty(Gender, Gender);
GetProperty(uint8_t, Coloring);
@ -26,15 +26,18 @@ namespace CreatureLib::Library{
int8_t _talentIndex;
public:
Creature(const DataLibrary* library, const CreatureSpecies* species, const SpeciesVariant* variant,
uint8_t level, std::string nickname, int8_t talentIndex, Core::StatisticSet statExperience,
Core::StatisticSet statPotential, uint32_t identifier, Gender gender,
uint8_t level, std::string nickname, int8_t talentIndex, Core::StatisticSet<uint8_t > statExperience,
Core::StatisticSet<uint8_t > statPotential, uint32_t identifier, Gender gender,
uint8_t coloring, const Item* heldItem);
virtual ~Creature(){}
std::string GetTalent() const;
std::string GetNickname() const;
virtual int32_t CalculateFullStat(Core::Statistic stat) const;
virtual uint32_t CalculateFullStat(Core::Statistic stat) const;
virtual uint16_t GetBaseStat(Core::Statistic stat) const;
virtual uint8_t GetStatPotential(Core::Statistic stat) const;
virtual uint8_t GetStatExperience(Core::Statistic stat) const;
};
}

View File

@ -44,7 +44,7 @@ int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(CreatureLib::Core::
CreatureLib::Library::SpeciesVariant::SpeciesVariant(std::string name, float height, float weight,
uint32_t baseExperience, std::vector<std::string> types,
CreatureLib::Core::StatisticSet baseStats,
CreatureLib::Core::StatisticSet<uint16_t > baseStats,
std::vector<std::string> talents,
std::vector<std::string> secretTalents)
: __Name(name),

View File

@ -19,13 +19,13 @@ namespace CreatureLib::Library {
GetProperty(uint32_t, BaseExperience);
private:
std::vector<std::string> _types;
const Core::StatisticSet _baseStatistics;
const Core::StatisticSet<uint16_t > _baseStatistics;
std::vector<std::string> _talents;
std::vector<std::string> _secretTalents;
//CreatureMoves _moves;
public:
SpeciesVariant(std::string name, float height, float weight, uint32_t baseExperience,
std::vector<std::string> types, Core::StatisticSet baseStats, std::vector<std::string> talents,
std::vector<std::string> types, Core::StatisticSet<uint16_t > baseStats, std::vector<std::string> talents,
std::vector<std::string> secretTalents);
[[nodiscard]] size_t GetTypeCount() const;

View File

@ -9,7 +9,8 @@ static DataLibrary* __library = nullptr;
static SpeciesLibrary* BuildSpeciesLibrary(){
auto l = new SpeciesLibrary();
l->LoadSpecies("testSpecies1", new CreatureSpecies(0, "testSpecies1",
new SpeciesVariant("default", 1,1, 10, {"fire", "water"}, StatisticSet(10,10,10,10,10,10),
new SpeciesVariant("default", 1,1, 10, {"fire", "water"},
StatisticSet<uint16_t >(10,10,10,10,10,10),
{"testTalent"}, {"testSecretTalent"}),
0.5f, "testGrowthRate", 5, 100));
return l;