66 lines
1.2 KiB
C++
66 lines
1.2 KiB
C++
#include "dice_data.h"
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace active_dice {
|
|
struct DieState {
|
|
Die type;
|
|
uint8_t value;
|
|
};
|
|
constexpr size_t maxActiveDice{16};
|
|
DieState activeDice[maxActiveDice];
|
|
size_t numActiveDice{0};
|
|
|
|
void Add(Die die) {
|
|
if(numActiveDice < maxActiveDice) {
|
|
activeDice[numActiveDice].type = die;
|
|
activeDice[numActiveDice].value = RollDie(die);
|
|
++numActiveDice;
|
|
}
|
|
}
|
|
|
|
void Remove(size_t at) {
|
|
if(at >= numActiveDice) {
|
|
return;
|
|
}
|
|
if(at < numActiveDice - 1) {
|
|
std::memmove(activeDice + at, activeDice + at + 1, sizeof(DieState) * (numActiveDice - at));
|
|
}
|
|
--numActiveDice;
|
|
}
|
|
|
|
void Clear() {
|
|
numActiveDice = 0;
|
|
}
|
|
|
|
Die Get(size_t at) {
|
|
if(at < numActiveDice) {
|
|
return activeDice[at].type;
|
|
} else return D_COIN;
|
|
}
|
|
|
|
uint8_t GetValue(size_t at) {
|
|
if(at < numActiveDice) {
|
|
return activeDice[at].value;
|
|
} else return 0;
|
|
}
|
|
|
|
size_t Count() {
|
|
return numActiveDice;
|
|
}
|
|
|
|
void ReRoll() {
|
|
for(size_t i{0}; i < numActiveDice; ++i) {
|
|
activeDice[i].value = RollDie(activeDice[i].type);
|
|
}
|
|
}
|
|
}
|
|
|
|
uint8_t RollDie(Die die) {
|
|
if(die == D_COIN) {
|
|
return std::rand() % 2;
|
|
}
|
|
return 1 + std::rand() % (die-1);
|
|
}
|