-
Problems with my RPG app
I'm working on an RPG program that can be used as an engine; basicly I finish the engine and use it's functions in a different program with all the fluff. The problems right now are:
1. It doesn't seem to be very random. Hmm...
2. If the damage isn't enough to kill you or the monster, it doesn't take down any hp, just leaves it like it is.
3. A buncha other problems that seem to pop up randomly.
Here's the code:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
void cls() // Begin crud WinAPI. Ignore.
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
} // End crud WinAPI.
void Randomize() { // My lazy random routines.
srand(time(NULL));
}
int Rand(int par1) {
return ((rand() % par1) + 1);
}
struct Spell {
int power;
bool enabled;
};
class Enemy {
public:
int str, def, lev;
int ehp, ehpmax;
char ename[12];
void Set(int s, int d, int l);
};
void Enemy::Set(int s, int d, int l) {
Randomize();
ehpmax = Rand((s + d) * 2);
ehp = ehpmax;
}
class Player {
public:
int str, def, mag, lev, luck;
int exp, nlev, expadd, mp, hp, mpmax, hpmax, mexp;
Spell fire1, water1, ice1, fire2, water2, ice2;
char name[12];
void Roll(int skill);
};
Player One; // Global variables.
Enemy Orc;
int dam;
void Player::Roll(int skill) { // Rolls stat and initializes all values
Randomize();
lev = Rand(skill + 4) + 1;
str = Rand(lev) + 1;
def = Rand(lev) + 1;
mag = Rand(lev) + 1;
luck = Rand(lev) + 2;
exp = 0;
nlev = ((100 * lev) + Rand(100));
Randomize();
hpmax = Rand((lev + (str * 2) + (def * 3)));
hp = hpmax;
Randomize();
mpmax = Rand((lev + (mag * 2)));
mp = mpmax;
mexp = 0;
nlev = (lev * 10);
fire1.enabled = true;
fire1.power = 2;
water1.enabled = true;
water1.power = 2;
ice1.enabled = true;
ice1.power = 2;
fire2.enabled = false;
fire2.power = 5;
water2.enabled = false;
water2.power = 5;
ice2.enabled = false;
ice2.power = 5;
}
bool DieEval(int hpleft, int damage) { // Figures out if you're dead.
if (hpleft < damage) {
return true;
} else {
return false;
}
}
void EndBattle(Player Guy, Enemy Dead) { // Award exp and level up, not finished
Randomize();
cout << "Experience Gained:\n";
Guy.expadd = Dead.lev * 5 + Rand(Dead.lev);
cout << "Previous Experience:\n" << Guy.exp << endl;
}
void Death() {
cout << "You have died...\n"; // To be implemented later.
}
void EAtt(Enemy EAtt, Player EDefen) {
Randomize();
dam = 0;
dam = ((EAtt.lev + 2 + EAtt.str - (EDefen.def - 50)));
if (dam <= 0) {
dam = 0;
cout << "Critical miss!\n";
}
EDefen.hp = EDefen.hp - dam;
if (EDefen.hp <= 0) {
Death(); // If you have no more hp, you lose!
} else {
cout << EAtt.ename << " did " << dam << " points of damage.\n\n";
}
}
void PAtt(Player Att, Enemy Defen) {
Randomize();
dam = 0; // Initialize damage
dam = ((Att.lev + 2 + Att.str - (Defen.def - 50))); // Calculate damage
if (dam <= 0) {
dam = 0;
cout << "Critical miss!\n";
}
Defen.ehp = Defen.ehp - dam; // Hurt enemy. Doesn't seem to work though...
if (Defen.ehp <= 0) {
EndBattle(Att, Defen); // If he's dead, you won.
} else {
cout << Att.name << " did " << dam << " points of damage.\n\n"; // If not, tell how much damage you did
EAtt(Orc, One); // Let the enemy attack
}
}
void BMenu(Player Play, Enemy Enem) { // The menu function
int choice;
do {
cout << Play.name << "'s HP: " << Play.hp << "/ " << Play.hpmax << " " << Play.name << "'s MP: " << Play.mp << "/" << Play.mpmax << endl << Enem.ename << "'s HP: " << Enem.ehp << "/" << Enem.ehpmax << endl;
cout << "\nBattle Menu:\n[1] Attack\n[2] Cast Spell\n>";
cin >> choice;
if (choice == 1) {
PAtt(Play, Enem);
}
if (choice == 0) {
exit(0);
}
} while (Play.hp > 0 || Enem.ehp > 0);
}
void main() {
cls();
printf("RPG V0.1\n");
Sleep(1000);
cout << "Enter name (8 letters Max.): ";
cin >> One.name;
One.Roll(81);
Orc.Set(150,150,5);
strcpy(Orc.ename, "Orc");
cls();
BMenu(One, Orc);
}
I just added some comments and I always indent/space it as good as I can.
Thanks,
Valar_King
-
Here are the random functions i use, perhaps it can help you?
#include <stdlib.h>
int getrandi(int low, int high)
{
return low+rand()%(high-low+1);
}
float getrandf(float low, float high)
{
float range = high - low;
float num = range * (float)rand()/(float)RAND_MAX;
return( num + low );
}
double getrandd( double low, double high ) {
double range = high - low;
double num = range * (double)rand()/(double)RAND_MAX;
return( num + low );
}