-
Notifications
You must be signed in to change notification settings - Fork 0
/
archer.cpp
162 lines (148 loc) · 4.67 KB
/
archer.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
160
161
162
#include <iostream>
#include <string>
#include<time.h>
using namespace std;
#include "archer.h"
archer::archer(int lv_in, string name_in)
{
role = ar; // enumerate type of job
LV = lv_in;
name = name_in;
// Initialising the character's properties, based on his level
HPmax = 150 + 8 * (LV - 1); // HP increases 8 points per level
HP = HPmax;
MPmax = 75 + 2 * (LV - 1); // MP increases 2 points per level
MP = MPmax;
AP = 50 + 5* (LV - 1); // AP increases 4 points per level
DP = 15 + 3 * (LV - 1); // DP increases 4 points per level
speed = 15 + 2 * (LV - 1); // speed increases 2 points per level
playerdeath = 0;
EXP = LV*LV * 75;
bag.set(lv_in, lv_in,1);
}
void archer::isLevelUp()
{
if (EXP >= LV*LV * 75)
{
LV++;
AP += 5;
DP += 3;
HPmax += 8;
MPmax += 2;
speed += 2;
cout << name << " Level UP!" << endl;
cout << "HP improved 8 points to " << HPmax << endl;
cout << "MP improved 2 points to " << MPmax << endl;
cout << "Speed improved 2 points to " << speed << endl;
cout << "AP improved 5 points to " << AP << endl;
cout << "DP improved 3 points to " << DP << endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
}
bool archer::attack(player &p)
{
double HPtemp = 0; // opponent's HP decrement
double EXPtemp = 0; // player obtained exp
double hit = 1; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time
// If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand() % 120<(speed - p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout << name << "'s quick strike hit " << p.name << ", " << p.name << "'s HP decreased " << HPtemp << endl;
p.HP = int(p.HP - HPtemp);
EXPtemp = (int)(HPtemp*1.2);
}
// If speed smaller than opponent, the opponent has possibility to evade
if (((speed<p.speed) && (rand() % 50<1)) || ((p.bag.nOflu()>0) && (rand() % 4<1)))
{
if ((p.bag.nOflu() > 0) && (rand() % 4 < 1)){
p.bag.useluck();
}
cout << name << "'s attack has been evaded by " << p.name << endl;
system("pause");
return 1;
}
// 10% chance give critical attack
if (rand() % 80 <= 10)
{
hit = 1.5;
cout << "Critical attack: ";
}
// Normal attack
HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10));
cout << name << " uses bash, " << p.name << "'s HP decreases " << HPtemp << endl;
EXPtemp = (int)(EXPtemp + HPtemp*1.2);
p.HP = (int)(p.HP - HPtemp);
cout << name << " obtained " << EXPtemp << " experience." << endl;
EXP = (int)(EXP + EXPtemp);
system("pause");
return 1; // Attack success
}
bool archer::specialatt(player &p)
{
if (MP<40)
{
cout << "You don't have enough magic points!" << endl;
system("pause");
return 0; // Attack failed
}
else
{
MP -= 40; // consume 40 MP to do special attack
//10% chance opponent evades
if ((rand() % 100 <= 10) || ((p.bag.nOflu()>0) && (rand() % 4<1)))
{
if ((p.bag.nOflu() > 0) && (rand() % 4 < 1)){
p.bag.useluck();
}
cout << name << "'s leap attack has been evaded by " << p.name << endl;
system("pause");
return 1;
}
double HPtemp = 0;
double EXPtemp = 0;
//double hit=1;
//srand(time(NULL));
HPtemp = (int)(AP*1.2 + 20); // not related to opponent's DP
EXPtemp = (int)(HPtemp*1.5); // special attack provides more experience
cout << name << " uses leap attack, " << p.name << "'s HP decreases " << HPtemp << endl;
cout << name << " obtained " << EXPtemp << " experience." << endl;
p.HP = (int)(p.HP - HPtemp);
EXP = (int)(EXP + EXPtemp);
system("pause");
}
return 1; // special attack succeed
}
// Computer opponent
void archer::AI(player &p)
{
if ((HP<(int)((1.0*p.AP / DP)*p.AP*1.5)) && (HP + 100 <= 1.1*HPmax) && (bag.nOfHeal()>0) && (HP>(int)((1.0*p.AP / DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if (MP >= 40 && HP>0.5*HPmax && rand() % 100 <= 30)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP<40 && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}