dice-gui/src/dice.c
2025-09-17 22:45:32 +02:00

59 lines
1.5 KiB
C

#include "dice.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int roll_die(enum die_type die) {
int const max = die - 1;
return (rand() % max) + 1;
}
static int current_active_count = 0;
static enum die_type active_dice_set[MAX_ACTIVE_DICE];
static struct roll_result_type roll_results[MAX_ACTIVE_DICE];
static struct roll_result_type roll_total = {
.roll = 0, .string_len = 0
};
enum die_type const *get_active_dice_set(size_t *out_length) {
if (out_length != nullptr) {
*out_length = current_active_count;
}
return active_dice_set;
}
size_t add_die_to_active(enum die_type die) {
if (current_active_count >= MAX_ACTIVE_DICE) {
return MAX_ACTIVE_DICE;
}
active_dice_set[current_active_count] = die;
return current_active_count++;
}
void remove_die_from_active(size_t index) {
memcpy(active_dice_set + index, active_dice_set + index + 1, MAX_ACTIVE_DICE - index - 1);
--current_active_count;
}
void roll_active_dice_set(enum die_type die) {
for (size_t i = 0; i < current_active_count; ++i) {
roll_results[i].roll = roll_die(active_dice_set[i]);
snprintf(roll_results[i].string, MAX_ROLL_STR_LEN, "%d", roll_results[i].roll);
roll_total.roll += roll_results[i].roll;
}
}
Clay_String die_to_str(enum die_type die) {
switch (die) {
case COIN: return CLAY_STRING("C");
case D4: return CLAY_STRING("4");
case D6: return CLAY_STRING("6");
case D8: return CLAY_STRING("8");
case D10: return CLAY_STRING("10");
case D12: return CLAY_STRING("12");
case D20: return CLAY_STRING("20");
case D100: return CLAY_STRING("100");
}
}