Skip to content

Commit e39f7ed

Browse files
committed
Attack system & Enemy AI Overhaul
1 parent 74f4a4f commit e39f7ed

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

main.cpp

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,67 +32,47 @@ void EndBattle()
3232
}
3333

3434
// Used to return a bool which decides if attacks miss or not in attack functions
35-
bool MissChance(int min, int max, int missNumber)
35+
bool MissChance(int accuracy)
3636
{
37-
int rng = RandomNumber(min, max);
38-
if (rng == missNumber)
37+
int rng = RandomNumber(1,100);
38+
if (rng > accuracy)
3939
{
4040
return true;
4141
}
4242
else
4343
return false;
4444
}
4545

46-
4746
//----------------//
4847
// Battle Actions //
4948
//----------------//
5049

51-
// Basic attack which deals low damage & doesn't cost MP
52-
void Attack(const string& user, int& HPTarget)
53-
{
54-
if (MissChance(1,10,6))
55-
{
56-
cout << user << " missed!" << endl << endl;
57-
}
58-
else
59-
{
60-
int damage = RandomNumber(5, 10);
61-
HPTarget -= damage;
62-
cout << user << " attack dealt " << damage << " HP!" << endl << endl;
63-
if (HPTarget <= 0)
64-
EndBattle();
65-
}
66-
67-
}
6850

69-
// Stronger attack which deals higher damage at the cost of some MP
70-
void Magic(const string& user, int& HPTarget, int& MPSource) // Perform a stronger attack at the cost of MP
51+
void Attack(const string& attackName, const string& user, int& hpTarget, int hpDamageMin, int hpDamageMax, int mpCost, int& mpSource, int accuracy)
7152
{
72-
if (MPSource < 10)
73-
{
74-
cout << user << " doesn't have enough MP to cast a spell!" << endl << endl;
75-
}
76-
77-
else if (MissChance(1, 5, 3))
53+
if (mpSource < mpCost)
54+
cout << user << " doesn't have enough MP to use " << attackName << "!" << endl << endl;
55+
else if (MissChance(accuracy))
7856
{
79-
cout << user << " missed!" << endl << endl;
57+
mpSource -= mpCost;
58+
cout << user << " used " << attackName << "... but missed!" << endl << endl;
8059
}
8160
else
8261
{
83-
int damage = RandomNumber(12, 22);
84-
HPTarget -= damage;
85-
MPSource -= 10;
86-
cout << user << " magic attack dealt " << damage << " HP!" << endl << endl;
87-
if (HPTarget <= 0)
62+
mpSource -= mpCost;
63+
int damage = RandomNumber(hpDamageMin, hpDamageMax);
64+
hpTarget -= damage;
65+
cout << user << " used " << attackName << " and dealt " << damage << " damage!" << endl << endl;
66+
if (hpTarget <= 0)
8867
EndBattle();
8968
}
9069
}
9170

71+
9272
// Healing lets the user recover HP at the cost of some MP
9373
void Heal(const string& user, int& HPTarget, int& MPSource) // Heal HP at the cost of MP
9474
{
95-
if (MPSource < 20)
75+
if (MPSource < 10)
9676
{
9777
cout << user << " doesn't have enough MP to heal!" << endl << endl;
9878
}
@@ -102,13 +82,13 @@ void Heal(const string& user, int& HPTarget, int& MPSource) // Heal HP at the co
10282
}
10383
else
10484
{
105-
int amount = RandomNumber(10, 20);
85+
int amount = RandomNumber(25, 35);
10686
HPTarget += amount;
10787
if (HPTarget > 100)
10888
{
10989
HPTarget = 100;
11090
}
111-
MPSource -= 20;
91+
MPSource -= 10;
11292
cout << user << " healed " << amount << " HP!" << endl << endl;
11393
}
11494
}
@@ -119,12 +99,35 @@ void Heal(const string& user, int& HPTarget, int& MPSource) // Heal HP at the co
11999
//-----------------------//
120100
void EnemyTurn()
121101
{
122-
switch (int enemyAction = RandomNumber(1, 3))
102+
int enemyActions[] = {1,2,3};
103+
104+
// Enemy avoids magic if they don't have enough MP
105+
if (enemyMP < 10)
106+
enemyActions[2] = 0;
107+
if (enemyMP < 15)
108+
enemyActions[1] = 0;
109+
110+
// Enemy avoids healing if they have high HP
111+
if (enemyHP > 80)
112+
enemyActions[2] = 0;
113+
114+
// Selects a random non-disabled action
115+
int enemyAction = 0;
116+
while (enemyAction == 0)
117+
{
118+
int rng = enemyActions[RandomNumber(0,2)];
119+
if (rng != 0)
120+
{
121+
enemyAction = rng;
122+
break;
123+
}
124+
}
125+
switch (enemyAction)
123126
{
124-
case 1: Attack("Enemy", playerHP);
127+
case 1: Attack("Bite", "Enemy", playerHP, 8, 18, 0, enemyMP, 80);
125128
break;
126129

127-
case 2: Magic("Enemy", playerHP, enemyMP);
130+
case 2: Attack("Magic", "Enemy", playerHP, 20, 30, 15, enemyMP, 60);
128131
break;
129132

130133
case 3: Heal("Enemy", enemyHP, enemyMP);
@@ -155,8 +158,8 @@ void BattleMenu()
155158

156159
// Battle Options
157160
cout << "1. Attack" << endl;
158-
cout << "2. Magic (Cost: 10MP)" << endl;
159-
cout << "3. Heal (Cost: 20MP)" << endl;
161+
cout << "2. Magic (Cost: 15MP)" << endl;
162+
cout << "3. Heal (Cost: 10MP)" << endl;
160163
cout << "Your choice: ";
161164

162165
int action;
@@ -170,15 +173,15 @@ void BattleMenu()
170173
// Player's turn
171174
switch (action)
172175
{
173-
case 1:
174-
Attack("Player", enemyHP);
176+
case 1: // Basic Attack
177+
Attack("Punch", "Player", enemyHP, 5, 12, 0, playerMP, 80);
175178
break;
176179

177-
case 2:
178-
Magic("Player", enemyHP, playerMP);
180+
case 2: // Magic Attack
181+
Attack("Magic", "Player", enemyHP, 15, 24, 15, playerMP, 60);
179182
break;
180183

181-
case 3:
184+
case 3: // Healing
182185
Heal("Player", playerHP, playerMP);
183186
break;
184187

0 commit comments

Comments
 (0)