/* Adam Obenauf, ml22 4/17/11 project2.cpp Text based rpg game based on the combat system of eve online */ //Preprocessor directives #include #include #include #include #include #include #include #include #include "prompts.h" #include "attributecalc.h" #include "display.h" using namespace std; //Structures struct ship { int size; int velocity; int damageBonusShield; int damageBonusArmor; int damage; int shieldHitPoints; int armorHitPoints; int hullHitPoints; int accuracyFalloff; int optimalRange; float trackingSpeed; }; //Function prototypes //CalculateShipAttributes //Return type: ship //Parameters: int specialization, int size, int primaryWeaponChoice, int ammoChoice //This function calculates the attributes of a ship based on specialization/size/weapon choice/ammo choice and returns that ship ship CalculateShipAttributes (int specialization, int size, int primaryWeaponChoice, int ammoChoice); //WriteShipFile //Return type: void //Parameters: ship myShip, string shipName //This function is passed a ship and the name of the file and writes the data to that file void WriteShipFile (ship myShip, string shipName); //LoadShip //Return type: ship //Parameters: string shipName //This function is passed the name of a file, it reads the data from that file and returns it as a ship ship LoadShip(string shipName); //DoesFileExist //Return type: bool //Parameters: string shipName //This function is passed the name of a file and checks if the file exists, it returns true/false based on whether it exists or not bool DoesFileExist(string shipName); //RandomNum //Return type: int //Parameters: int lo, int hi //This function is passed two numbers and returns a random number between them (inclusive) int RandomNum(int lo, int hi); //DisplayShipAttributes //Return type: void //Parameters: ship playerShip //This function is passed a ship and displays the attributes of that ship void DisplayShipAttributes(ship playerShip); //IsHit //Return type: bool //Parameters: float accuracyFalloff, float optimalRange, float currentRange, float velocity, float trackingSpeed //This function is passed a bunch of attributes and returns whether or not the person attacking hit the target bool IsHit(float accuracyFalloff, float optimalRange, float currentRange, float velocity, float trackingSpeed); //UpdateShipWeapon //Return type: void //Parameters: int size, int weaponChoice, int ammoChoice, ship myShip passed by reference //This function updates some of a ships's attributes when a weapon change occurs void UpdateShipWeapon(int size, int weaponChoice, int ammoChoice, ship& myShip); //PercHealth //Return type: float //Parameters: int current, string shipName, int healthType //This function calculates the percentage of health remaining by using current health, originalHealth (reads this from a file), and health type float PercHealth (int current, string shipName, int healthType); //PlayerAttacksEnemy //Return type: void //Parameters: ship playerShip passed by reference, ship enemyShip passed by reference, int currentRange //This function updates the enemyShips shield/armor/hull hitpoints based on damage inflicted by the player from a hit (if the hit is successful), it displays a message about whether you hit the enemy or not and how much damage you inflicted to shields/armor/hull void PlayerAttacksEnemy(ship playerShip, ship& enemyShip, int currentRange); //EnemyAttacksPlayer //Return type: void //Parameters: ship playerShip passed by reference, ship enemyShip passed by reference, int currentRange //This function updates the playerShips shield/armor/hull hitpoints based on damage inflicted by the enemy from a hit (if the hit is successful), it displays a message about whether the enemy hit you or not and how much damage it inflicted to your shields/armor/hull void EnemyAttacksPlayer(ship& playerShip, ship enemyShip, int currentRange); //Main function int main() { srand((unsigned)time(0)); //Declare variables int menuChoice = 0; ship playerShip; ship enemyShip; string shipName = ""; bool fileExists; int shipMenu = 0; int specialization = 0; int size = 0; int weaponChoice = 0; int ammoChoice = 0; int currentRange = 0; float percHealth = 0; int combatChoice = 0; cout<=enemyShip.velocity) { currentRange=playerShip.optimalRange; } else { currentRange=enemyShip.optimalRange; } //Display current range and everyone's health cout<enemyShip.velocity) { cout<0 & playerShip.hullHitPoints>0); //Either the enemy or player has died if(enemyShip.hullHitPoints=0) { cout<trackingSpeed) { return false; } //Extremely close range else if (currentRange < accuracyFalloff) { return true; } else if (currentRange >= accuracyFalloff) { //calculate hitChance hitChance = 1.f - abs(optimalRange - currentRange) / accuracyFalloff * 0.5f; //If we have 1/6 chance for example that's 0.15. 0.15 times 1000 is 150, we pick a random number between 0 and 1000 and we'll have a 1/6th chance of it being less than or equal to 150 randomNum=RandomNum(0,1000); if (hitChance*1000 <= randomNum) { return true; } else if(hitChance*1000 > randomNum) { return false; } } } int RandomNum(int lo, int hi) { // note: floor() really only needed if hi < 0 int range = (hi - lo+1); return rand()%range+lo; } ship LoadShip(string shipName) { ship Ship1; ifstream inputFile; inputFile.open(shipName.c_str()); inputFile>>Ship1.size; inputFile>>Ship1.velocity; inputFile>>Ship1.damageBonusShield; inputFile>>Ship1.damageBonusArmor; inputFile>>Ship1.damage; inputFile>>Ship1.shieldHitPoints; inputFile>>Ship1.armorHitPoints; inputFile>>Ship1.hullHitPoints; inputFile>>Ship1.accuracyFalloff; inputFile>>Ship1.optimalRange; inputFile>>Ship1.trackingSpeed; inputFile.close(); return Ship1; } bool DoesFileExist(string shipName) { ifstream inputFile; inputFile.open(shipName.c_str()); if(inputFile.fail()) { return false; } else { return true; } } void DisplayShipAttributes (ship playerShip) { //Display everything cout<<"Velocity: \t\t\t"<0) { enemyShip.shieldHitPoints=enemyShip.shieldHitPoints-(playerShip.damage + playerShip.damageBonusShield); cout<0) { enemyShip.armorHitPoints=enemyShip.armorHitPoints-(playerShip.damage + playerShip.damageBonusArmor); cout<0) { playerShip.shieldHitPoints=playerShip.shieldHitPoints-(enemyShip.damage + enemyShip.damageBonusShield); cout<0) { playerShip.armorHitPoints=playerShip.armorHitPoints-(enemyShip.damage + enemyShip.damageBonusArmor); cout<