DungeonCrawl
Loading...
Searching...
No Matches
damage.h File Reference

Exposes functions for working with damage. More...

Go to the source code of this file.

Data Structures

struct  damage_resistance_t

Macros

#define DAMAGE_TYPE_COUNT   2

Typedefs

typedef enum damage_type_t damage_type_t
typedef enum dice_size_t dice_size_t
typedef struct damage_resistance_t damage_resistance_t

Enumerations

enum  damage_type_t { PHYSICAL , MAGICAL }
enum  dice_size_t {
  D6 = 6 , D8 = 8 , D10 = 10 , D12 = 12 ,
  D20 = 20
}

Functions

bool roll_hit (int attacker_dex, int defender_dex)
 Rolls a D20 to determine if an attack hits.
int roll_damage (const ability_t *ability)
 Rolls damage based on the ability's roll amount and dice size.
int deal_damage (character_t *character, damage_type_t damage_type, int damage)
 Deals damage to a character based on the damage type and amount.
void reset_current_stats (character_t *character)
 Resets the current stats of a character to their base stats.
const char * dice_size_to_string (dice_size_t size)
 Converts a dice size enum to a string representation.
const char * damage_type_to_string (damage_type_t type)
 Converts a damage type enum to a string representation.

Detailed Description

Exposes functions for working with damage.

Definition in file damage.h.

Macro Definition Documentation

◆ DAMAGE_TYPE_COUNT

#define DAMAGE_TYPE_COUNT   2

Definition at line 11 of file damage.h.

Enumeration Type Documentation

◆ damage_type_t

enum damage_type_t

Definition at line 13 of file damage.h.

13 {
14 PHYSICAL,
15 MAGICAL
16} damage_type_t;

◆ dice_size_t

enum dice_size_t

Definition at line 18 of file damage.h.

18 {
19 D6 = 6,
20 D8 = 8,
21 D10 = 10,
22 D12 = 12,
23 D20 = 20
24} dice_size_t;

Function Documentation

◆ damage_type_to_string()

const char * damage_type_to_string ( damage_type_t type)

Converts a damage type enum to a string representation.

Parameters
typeThe damage type to convert.
Returns
The string representation of the damage type.

Definition at line 78 of file damage.c.

78 {
79 switch (type) {
80 case PHYSICAL:
81 return "Physical";
82 case MAGICAL:
83 return "Magic";
84 default:
85 return "Unknown";
86 }
87}

◆ deal_damage()

int deal_damage ( character_t * character,
damage_type_t damage_type,
int damage )

Deals damage to a character based on the damage type and amount.

Parameters
characterPointer to the character receiving damage.
damage_typeThe type of damage being dealt.
damageThe amount of damage to be dealt.
Returns
The actual damage dealt.

Definition at line 42 of file damage.c.

42 {
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}

◆ dice_size_to_string()

const char * dice_size_to_string ( dice_size_t size)

Converts a dice size enum to a string representation.

Parameters
sizeThe dice size to convert.
Returns
The string representation of the dice size.

Definition at line 61 of file damage.c.

61 {
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}

◆ reset_current_stats()

void reset_current_stats ( character_t * character)

Resets the current stats of a character to their base stats.

Parameters
characterPointer to the character whose stats are to be reset.

Resets the current stats of a character to their base stats.

Parameters
characterPointer to the character

Definition at line 56 of file damage.c.

56 {
57 character->current_stats = character->base_stats;
58}

◆ roll_damage()

int roll_damage ( const ability_t * ability)

Rolls damage based on the ability's roll amount and dice size.

Parameters
abilityPointer to the ability used for rolling damage.
Returns
The total damage rolled.

Definition at line 33 of file damage.c.

33 {
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}
int roll_dice(dice_size_t dice_size)
Rolls given dice size and returns the result.
Definition damage.c:20

◆ roll_hit()

bool roll_hit ( int attacker_dex,
int defender_dex )

Rolls a D20 to determine if an attack hits.

Parameters
attacker_dexThe dexterity of the attacker.
defender_dexThe dexterity of the defender.
Returns
The result of the dice roll.

Definition at line 25 of file damage.c.

25 {
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}