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

Implements damage rolling, hit checks, and damage application logic. More...

#include "damage.h"
#include "../character/character.h"
#include "../logging/logger.h"
#include "ability.h"
#include <stdlib.h>

Go to the source code of this file.

Functions

int roll_dice (const dice_size_t dice_size)
 Rolls given dice size and returns the result.
bool roll_hit (const int attacker_dex, const 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, const 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 values.
const char * dice_size_to_string (const dice_size_t size)
 Converts a dice size enum to a string representation.
const char * damage_type_to_string (const damage_type_t type)
 Converts a damage type enum to a string representation.

Detailed Description

Implements damage rolling, hit checks, and damage application logic.

Definition in file damage.c.

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 values.

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_dice()

int roll_dice ( const dice_size_t dice_size)

Rolls given dice size and returns the result.

Parameters
dice_sizeThe dice that is rolled.
Returns
The result of the dice roll.

Definition at line 20 of file damage.c.

20 {
21 /* Seed random number generator */
22 return rand() % dice_size + 1;
23}

◆ 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}