-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.cpp
159 lines (141 loc) · 3.8 KB
/
character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "character.h"
#include "diceroller.h"
#include <cassert>
#include <QDebug>
Character::Character(bool good, QString name,
int weaponSkill, int ballisticSkill,
int strength, int toughness, int agility,
int attacks, int wounds, int meleeWeapon,
int rangedWeapon, int armor, bool hasEvadeSkill)
: good(good),
hasEvadeSkill(hasEvadeSkill),
weaponSkill(weaponSkill),
ballisticSkill(ballisticSkill),
strength(strength),
toughness(toughness),
agility(agility),
attacks(attacks),
meleeWeapon(meleeWeapon),
rangedWeapon(rangedWeapon),
armor(armor),
name(name),
wounds(std::max(wounds, 0)),
currentHp(this->wounds),
attackingWeapon(ballisticSkill >= weaponSkill ? AttackingWeapon::Ranged : AttackingWeapon::Melee),
weaponLoaded(true), // character starts battle with weapon ready
target(nullptr)
{
newTurn();
}
bool Character::changeAttackingWeapon(AttackingWeapon weapon)
{
if (weapon != attackingWeapon) {
if (!useAction()) {
return false;
}
weaponLoaded = false;
attackingWeapon = weapon;
}
return true;
}
bool Character::loadWeapon()
{
if (attackingWeapon == AttackingWeapon::Ranged) {
if (!useAction()) {
return false;
}
weaponLoaded = true;
}
return true;
}
bool Character::attack(const DiceRoller& dice)
{
assert(target != nullptr);
assert(attackingWeapon != AttackingWeapon::Ranged || weaponLoaded);
// attacking action
if (didAttack || !useAction()) {
return false;
}
didAttack = true;
// always attack with better skill
auto skill = std::max(attackingWeapon == AttackingWeapon::Ranged ? ballisticSkill : weaponSkill, 1);
if (isFocused) {
skill += 10;
}
skill = std::min(skill, 99);
// hit enemy with all attacks if there is extra action
int attackCounter = attacks;
if (attackCounter > 1 && !useAction()) {
attackCounter = 1;
}
for (; attackCounter > 0; --attackCounter) {
const auto roll = dice.roll(100);
if (roll <= skill && (attackingWeapon == AttackingWeapon::Ranged
|| (!target->parry(dice) && !target->evade(dice)))) {
constexpr const auto kDmgRoll = 10;
auto dmg = dice.roll(kDmgRoll);
if (kDmgRoll == dmg) {
// crit
const auto roll = dice.roll(100);
if (roll <= skill) {
do {
dmg += dice.roll(kDmgRoll);
} while (0 == (dmg % kDmgRoll)); // while rolled crit roll another
}
}
// weapon
dmg += (attackingWeapon == AttackingWeapon::Ranged) ? rangedWeapon : ((strength/10) + meleeWeapon);
target->hit(dmg);
}
}
return true;
}
bool Character::parry(const DiceRoller &dice)
{
if (!didParry) {
didParry = true;
return dice.roll(100) < weaponSkill;
}
return false;
}
bool Character::evade(const DiceRoller &dice)
{
if (!didEvade) {
didEvade = true;
return dice.roll(100) < agility;
}
return false;
}
bool Character::focusAttack()
{
if (!isFocused) {
if (!useAction()) {
return false;
}
isFocused = true;
}
return true;
}
bool Character::walk()
{
return useAction();
}
bool Character::useAction(int complexity)
{
if (actionsRemaining > 0) {
actionsRemaining -= complexity;
}
return actionsRemaining >= 0;
}
void Character::newTurn()
{
actionsRemaining = kActionsPerRound;
didParry = false;
didEvade = !hasEvadeSkill;
isFocused = false;
didAttack = false;
}
void Character::hit(int dmg)
{
currentHp -= std::max((dmg - ((toughness/10) + armor)), 0);
}