DungeonCrawl
Loading...
Searching...
No Matches
damage.c
Go to the documentation of this file.
1
5#include "damage.h"
6
8#include "../logging/logger.h"
9#include "ability.h"
10
11#include <stdlib.h>
12
13int roll_dice(dice_size_t dice_size);
14
20int roll_dice(const dice_size_t dice_size) {
21 /* Seed random number generator */
22 return rand() % dice_size + 1;
23}
24
25bool roll_hit(const int attacker_dex, const int defender_dex) {
26 const int attacker_roll = roll_dice(D20);
27 const int defender_roll = roll_dice(D20);
28 bool hit = false;
29
30 return attacker_roll + (attacker_dex / 2) > defender_roll + (defender_dex / 2);
31}
32
33int roll_damage(const ability_t* ability) {
34 int roll = 0;
35 // Roll the dice several times
36 for (int i = 0; i < ability->roll_amount; i++) {
37 roll += roll_dice(ability->dice_size);
38 }
39 return roll;
40}
41
42int deal_damage(character_t* character, damage_type_t damage_type, const int damage) {
43 // TODO critical hits are ignored
44 // negative damage resistance leads to more damage
45 // damage += character->resistance[damage_type].value;
46 // damage -= character->current_stats.armor;
47
48 if (damage < character->current_resources.health) {
49 character->current_resources.health -= damage;
50 } else {
51 character->current_resources.health = 0;
52 }
53 return damage;
54}
55
57 character->current_stats = character->base_stats;
58}
59
60// Helper function to convert dice_size_t to string
61const char* dice_size_to_string(const dice_size_t size) {
62 switch (size) {
63 case D6:
64 return "D6";
65 case D8:
66 return "D8";
67 case D10:
68 return "D10";
69 case D12:
70 return "D12";
71 case D20:
72 return "D20";
73 default:
74 return "Unknown";
75 }
76}
77
78const char* damage_type_to_string(const damage_type_t type) {
79 switch (type) {
80 case PHYSICAL:
81 return "Physical";
82 case MAGICAL:
83 return "Magic";
84 default:
85 return "Unknown";
86 }
87}
Exposes functions for working with abilities.
Exposes functions for working working with the character.
const char * dice_size_to_string(const dice_size_t size)
Converts a dice size enum to a string representation.
Definition damage.c:61
bool roll_hit(const int attacker_dex, const int defender_dex)
Rolls a D20 to determine if an attack hits.
Definition damage.c:25
int roll_dice(dice_size_t dice_size)
Rolls given dice size and returns the result.
Definition damage.c:20
int deal_damage(character_t *character, damage_type_t damage_type, const int damage)
Deals damage to a character based on the damage type and amount.
Definition damage.c:42
void reset_current_stats(character_t *character)
Resets the current stats of a character to their base values.
Definition damage.c:56
const char * damage_type_to_string(const damage_type_t type)
Converts a damage type enum to a string representation.
Definition damage.c:78
int roll_damage(const ability_t *ability)
Rolls damage based on the ability's roll amount and dice size.
Definition damage.c:33
Exposes functions for working with damage.
Header file for logging functionality of the game.