I'm a C++ student who's making an RPG game with the limited knowledge I have (just learned about classes today) and I've got most of the details worked out. One thing has been making my head itch, though, and it's the fact that when I'm having a party member's health values changed by their own team, the numbers constantly go many digits into the negatives. What should be basic subtraction is a mess that's driving me insane!
Some context: "player" is the one doing the attack, while "players" is the one taking the attack. These can heal too.
Expected result: player2.health -= player.att1.damage - player2.armor
if player2.health = 15 and player.att1.damage = 3, player2.health should be 12
instead, it can reach new lows of -858993450 HP
Code:
void pvp(Party &player, Party &player2) {
int select, flinch = 2;
if (player.effect.flinch)
flinch = rand() % 2;
moveshow(player);
cin >> select;
switch (select) {
case 1: {
srand(time(NULL));
string re;
int t = rand() % 100 + 1;
int r = rand() % player.att1.hitcount + 1;
if (r == 1)
re = " time";
else
re = " times";
if (t < (player.att1.accuracy - player.att1.blindness + player.att1.deadeye) & flinch != 1) {
if (player.att1.damage < 0) {
cout << player.name << player.att1.action << player2.name << r << re << ", healing " << abs(player.att1.damage * r) << " health.\n";
}
else if (player.att1.damage == 0) {
cout << player.name << player.att1.action << player2.name << r << re << endl;
}
else
cout << player.name << player.att1.action << player2.name << r << re << ", dealing " << player.att1.damage * r << " damage.\n";
player2.health -= (player.att1.damage * r) - player2.armor;
cout << player2.name << ": " << player2.health << " HP\n";
pvpinflict(player, player2, select);
player.att1.pp--;
}
else if (flinch == 1)
cout << player.name << " flinched and couldn't move!\n";
else {
cout << player.name << " missed!\n";
player.att1.pp--;
}
break;
}
case 2: {
srand(time(NULL));
string re;
int t = rand() % 100 + 1;
int r = rand() % player.att2.hitcount + 1;
if (r == 1)
re = " time";
else
re = " times";
if (t < (player.att2.accuracy - player.att2.blindness + player.att2.deadeye) & flinch != 1) {
if (player.att2.damage < 0) {
cout << player.name << player.att2.action << player2.name << r << re << ", healing " << abs(player.att2.damage * r) << " health.\n";
}
else if (player.att2.damage == 0) {
cout << player.name << player.att2.action << player2.name << r << re << endl;
}
else
cout << player.name << player.att2.action << player2.name << r << re << ", dealing " << player.att2.damage * r << " damage.\n";
player2.health -= (player.att2.damage * r) - player2.armor;
cout << player2.name << ": " << player2.health << " HP\n";
pvpinflict(player, player2, select);
player.att2.pp--;
}
else if (flinch == 1)
cout << player.name << " flinched and couldn't move!\n";
else {
cout << player.name << " missed!\n";
player.att2.pp--;
}
break;
}
case 3: {
srand(time(NULL));
string re;
int t = rand() % 100 + 1;
int r = rand() % player.att3.hitcount + 1;
if (r == 1)
re = " time";
else
re = " times";
if (t < (player.att3.accuracy - player.att3.blindness + player.att3.deadeye) & flinch != 1) {
if (player.att3.damage < 0) {
cout << player.name << player.att3.action << player2.name << r << re << ", healing " << abs(player.att3.damage * r) << " health.\n";
}
else if (player.att3.damage == 0) {
cout << player.name << player.att3.action << player2.name << r << re << endl;
}
else
cout << player.name << player.att3.action << player2.name << r << re << ", dealing " << player.att3.damage * r << " damage.\n";
player2.health -= (player.att3.damage * r) - player2.armor;
cout << player2.name << ": " << player2.health << " HP\n";
pvpinflict(player, player2, select);
player.att3.pp--;
}
else if (flinch == 1)
cout << player.name << " flinched and couldn't move!\n";
else {
cout << player.name << " missed!\n";
player.att3.pp--;
}
break;
}
}
}```