-
Notifications
You must be signed in to change notification settings - Fork 0
/
battle.hpp
230 lines (190 loc) · 5.81 KB
/
battle.hpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
The MIT License (MIT)
Copyright (c) 2013 Daniel Mansfield
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef BATTLE_HPP
#define BATTLE_HPP
#include "dialogue.hpp"
#include "creature.hpp"
#include "weapon.hpp"
#include <iostream>
class Battle
{
public:
// Dialogue used to ask the player battle choices
Dialogue dialogue;
// Creatures in combat. creatures[0] is the player
Creature* creatures[2];
Battle()
{
}
Battle(Creature* player, Creature* b)
{
// Start a battle with the player and another creature
this->creatures[0] = player;
this->creatures[1] = b;
// Set up the dialogue. Defending offers no tactical advangtages
// in this battle system
this->dialogue = Dialogue("What will you do?",
{
"Attack",
"Defend"
});
}
// Creature a attacks creature b, and b takes damage accordingly
void attack(Creature* a, Creature* b)
{
std::cout << a->name << " attacks!\n";
// Damage that a will inflict on b
int damage = 0;
// Cumulative modifier to hitRate
double hitRate = a->hitRate;
// If a has equipped a weapon, then add the weapon damage on
// to the current damage and add the hit rate of the weapon on to
// the current hit rate
if(a->equippedWeapon != nullptr)
{
damage += a->equippedWeapon->damage;
hitRate += a->equippedWeapon->hitRate;
}
// Increase the damage by half the attacker's strength
damage += a->str / 2;
// Damage that b will block
int defense = 0;
// Decrease the damage by the damage blocked, then ensure that
// damage is always inflicted (we do not want battles to last
// forever, nor to we want attacks to heal the wounded!)
damage -= defense;
if(damage < 1) damage = 1;
// Add the hit rate to the base hit rate and subract the target's
// dexterity from it. Instead of halving it to normalise it into
// a percentage, we just double the range of randomly generated
// values
if(rand() % 201 <= 170 + hitRate - b->dex)
{
// The attack hit, so subtract the damage
std::cout << b->name << " takes " << damage << " damage!\n";
b->health -= damage;
}
else
{
// The attack missed
std::cout << a->name << " missed!\n";
}
return;
}
// Allow the player to act
void playerTurn()
{
// Activate the dialogue and allow the player to choose their
// battle option
int result = this->dialogue.activate();
switch(result)
{
// Attack the enemy
case 1:
attack(creatures[0], creatures[1]);
break;
// Defend, skipping to the enemy's turn
case 2:
std::cout << creatures[0]->name << " defends!\n";
break;
default:
break;
}
return;
}
// Allow the enemy to attack
void enemyTurn()
{
// Battle system does not currently allow for any kind of
// tactics, so make the enemy attack blindly
attack(creatures[1], creatures[0]);
return;
}
// Return true if the creature is dead. Split into it's own function
// to allow easy addition of effects which simulate death, such as
// petrifaction or banishment
bool isdead(Creature* creature)
{
if(creature->health <= 0)
{
return true;
}
return false;
}
// Run a round of the battle
bool activate()
{
// The creature with the highest dexterity attacks first, with
// preference to the player
if(creatures[0]->dex >= creatures[1]->dex)
{
// Run each turn and check if the foe is dead at the end of
// each
this->playerTurn();
if(isdead(creatures[1]))
{
std::cout << creatures[1]->name << " was vanquished!\n";
return true;
}
this->enemyTurn();
if(isdead(creatures[0]))
{
std::cout << creatures[0]->name << " was vanquished!\n";
return true;
}
}
else
{
this->enemyTurn();
if(isdead(creatures[0]))
{
std::cout << creatures[0]->name << " was vanquished!\n";
return true;
}
this->playerTurn();
if(isdead(creatures[1]))
{
std::cout << creatures[1]->name << " was vanquished!\n";
return true;
}
}
return false;
}
// Begin the battle
void run()
{
std::cout << creatures[1]->name << " appears!" << std::endl;
// Run the battle until one creature dies
while(!this->activate());
// If the enemy is dead, then allocate experience to the player
if(isdead(creatures[1]))
{
// Give experience to the player equal to one eigth of the
// experience the enemy gained to reach it's next level
unsigned int expGain = creatures[1]->expToLevel(creatures[1]->level+1) / 8;
std::cout << "Gained " << expGain << " exp!\n";
creatures[0]->exp += expGain;
// Repeatedly level up the player until they are the highest
// level they can be for their experience
while(creatures[0]->levelUp());
}
return;
}
};
#endif /* BATTLE_HPP */