Support for cloning battles for AI purposes.
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
2021-04-11 15:20:50 +02:00
parent a3b7002cd4
commit 84a14cff2b
19 changed files with 236 additions and 30 deletions

View File

@@ -4,13 +4,14 @@
#include "../../Library/Exceptions/CreatureException.hpp"
#include "HistoryElements/HistoryElement.hpp"
template <class T> concept HistoryElementType = std::is_base_of<CreatureLib::Battling::HistoryElement, T>::value;
template <class T>
concept HistoryElementType = std::is_base_of<CreatureLib::Battling::HistoryElement, T>::value;
namespace CreatureLib::Battling {
class HistoryHolder {
size_t _offset;
size_t _capacity;
uint8_t* _memory = nullptr;
u8* _memory = nullptr;
HistoryElement* _top = nullptr;
static constexpr size_t initialSize = 2048;
@@ -22,7 +23,7 @@ namespace CreatureLib::Battling {
if (ptr == nullptr) {
THROW("Out of memory.");
}
_memory = static_cast<uint8_t*>(ptr);
_memory = static_cast<u8*>(ptr);
}
HistoryHolder(const HistoryHolder&) = delete;
HistoryHolder& operator=(const HistoryHolder&) = delete;
@@ -33,19 +34,25 @@ namespace CreatureLib::Battling {
free(_memory);
}
void Resize() {
_capacity += stepSize;
auto newPtr = realloc(_memory, _capacity);
if (newPtr == nullptr) {
THROW("Out of memory.");
}
_memory = static_cast<u8*>(newPtr);
}
template <HistoryElementType T, class... parameters> void Register(parameters... args) {
if (_offset + sizeof(T) >= _capacity) {
_capacity += stepSize;
auto newPtr = realloc(_memory, _capacity);
if (newPtr == nullptr) {
THROW("Out of memory.");
}
_memory = static_cast<uint8_t*>(newPtr);
Resize();
}
uint8_t* ptr = _memory + _offset;
u8* ptr = _memory + _offset;
T* element = new (ptr) T(args...);
_offset += sizeof(T);
element->_previous = _top;
if (_top != nullptr) {
element->_previousOffset = (u8*)sizeof(T);
}
_top = element;
}
@@ -56,10 +63,23 @@ namespace CreatureLib::Battling {
while (c != nullptr) {
if (c->GetKind() == HistoryElementKind::AttackUse)
return c;
c = c->_previous;
c = c->GetPrevious();
}
return {};
}
void CloneOnto(HistoryHolder& other) {
if (other._top != nullptr) {
other._top->Clear();
}
free(other._memory);
other._offset = _offset;
other._capacity = _capacity;
other._memory = static_cast<uint8_t*>(malloc(_capacity));
if (_top != nullptr) {
other._top = reinterpret_cast<HistoryElement*>(other._memory + ((u8*)_top - _memory));
}
}
};
}