Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BATTLE
- #include "Library.h"
- void ListMonsters(MONSTER* list,int count);
- bool AttackMonster(PLAYER* player,MONSTER* monster);
- bool MonsterAttack(MONSTER* monster,PLAYER* player);
- bool Battle(PLAYER* player,MONSTER* list,int count)
- {
- cout << "You clench your " << player->GetWeapon()->GetName() << " tightly and face the enemy.\n\n";
- while (player->GetHealth() > 0)
- {
- //player goes first
- bool actionused = false;
- //display enemy count
- if (count == 1)
- {
- cout << "The " << list->GetName() << " stares at you menacingly.\n";
- /// TEMPORARY
- cout << "Health: " << list->GetHealth() << endl;
- Wait();
- }
- else
- {
- ListMonsters(list,count);
- }
- cout << "\n";
- //display player stats
- cout << player->GetName() << endl;
- //health / maxhealth, mana / maxmana
- cout << "Health: " << player->GetHealth() << " / " << player->GetMaxHealth() << endl;
- cout << "Mana: " << player->GetMana() << " / " << player->GetMaxMana() << endl;
- cout << endl;
- int action = 0;
- while (true)
- {
- cout << "1: Attack\n";
- cout << "2: Spell\n";
- cout << "3: Item\n\n";
- cout << "4:Escape\n\n";
- cout << ">";
- cin >> action;
- cin.ignore(10,'\n');
- //action?
- if (action == 1) //attack
- {
- if (count == 1)
- {
- //attack single monster
- AttackMonster(player,list);
- Wait();
- cout << endl;
- }
- else
- {
- //ask for target
- int target = 0;
- while (true)
- {
- ListMonsters(list,count);
- cout << "\n0: Cancel\n\n";
- cout << "Which monster?\n";
- cout << ">";
- cin >> target;
- cin.ignore(10,'\n');
- if ( target > 0 && target <= count)
- {
- //check if the target is alive
- if (list[target-1].GetHealth() > 0)
- {
- //attack the target
- AttackMonster(player,&list[target-1]);
- Wait();
- cout << endl;
- //check if alive and award points if not
- if (list[target-1].GetHealth() <= 0)
- {
- cout << "You killed the " << list[target-1].GetName() << "\n";
- cout << "You gain " << list[target-1].GetExperience() << " experience and " << list[target-1].GetGold() << " gold.\n";
- player->AddExperience(list[target-1].GetExperience());
- player->AddGold(list[target-1].GetGold());
- cout << endl;
- }
- break;
- }
- else
- {
- cout << "Invalid monster.\n\n";
- }
- }
- else if (target == 0)
- {
- //cancelled attack
- action = 0;
- break;
- }
- } //end while target not chosen
- //
- }
- if (action == 1) //followed through with attack
- break;
- }
- else if (action == 2) //spell
- {
- //display players spells
- //get the list
- SPELL** spelllist = player->GetSpellList();
- for (int loop=0;loop < 10; loop++)
- {
- if (spelllist[loop] != NULL)
- {
- cout << loop+1 << ": " << spelllist[loop]->GetName() << " MP: " << spelllist[loop]->GetManaCost() << endl;
- }
- }
- cout << "0: Cancel\n\n";
- //check for input
- int which = 0;
- while (true)
- {
- cout << "Which Spell?\n";
- cout << ">";
- cin >> which;
- cin.ignore(10,'\n');
- if (which == 0)
- {
- //cancel
- break;
- }
- else if (which > 0 && which < 11)
- {
- //valid?
- if (spelllist[which-1] != NULL)
- {
- //spell is valid
- //enough mp?
- if (player->GetMana() >= spelllist[which-1]->GetManaCost())
- {
- //cast
- player->LoseMana(spelllist[which-1]->GetManaCost());
- //which target?
- if (count > 1)
- {
- ListMonsters(list,count);
- }
- else
- {
- cout << "1: " << list->GetName() << endl;
- }
- cout << "0: " << "Self\n";
- int choice = 0;
- while (true)
- {
- cout << "Which target?\n";
- cout << ">";
- cin >> choice;
- cin.clear();
- cin.ignore(INT_MAX,'\n');
- //validate
- if (choice == 0)
- {
- spelllist[which-1]->Cast((ENTITY*)player,1);
- actionused = true;
- break;
- }
- else if (count > 1 && choice < count+1)
- {
- if (list[choice-1].GetHealth() > 0)
- {
- //cast
- spelllist[which-1]->Cast((ENTITY*)(&list[choice-1]),1);
- actionused = true;
- break;
- }
- else
- cout << "Invalid Target\n";
- }
- else if (count == 1 && choice == 1)
- {
- //cast
- spelllist[which-1]->Cast((ENTITY*)(list),1);
- actionused = true;
- break;
- }
- else
- {
- cout << "Invalid Target\n";
- }
- }//end while choose target
- break;
- }
- else
- {
- cout << "Not Enough mana\n";
- }
- }
- }
- }
- if (actionused)
- break;
- }
- else if (action == 3) //item
- {
- //display players potion list
- //getlist
- POTION** potionlist = player->GetPotionList();
- for (int loop=0;loop < 15;loop++)
- {
- if (potionlist[loop] != NULL)
- {
- cout << loop+1 << ": " << potionlist[loop]->GetName() << " (" <<potionlist[loop]->GetQuantity() << ")\n";
- }
- }
- cout << "0: Cancel\n\n";
- //check for input
- int which = 0;
- while (true)
- {
- cout << "Which potion?\n";
- cout << ">";
- cin >> which;
- cin.ignore(10,'\n');
- if (which == 0)
- {
- //cancel
- break;
- }
- else if (which > 0 && which < 16)
- {
- //errorcheck
- if (potionlist[which-1] == NULL)
- cout << "Invalide Potion.\n";
- else
- {
- if ( potionlist[which-1]->GetHealthRestore() > 0)
- cout << "You gain " << potionlist[which-1]->GetHealthRestore() << " health.\n";
- if (potionlist[which-1]->GetManaRestore() > 0)
- cout << "You gain " << potionlist[which-1]->GetManaRestore() << " mana.\n";
- player->AddHealth(potionlist[which-1]->GetHealthRestore());
- player->AddMana(potionlist[which-1]->GetManaRestore());
- potionlist[which-1]->SubtractQuantity(1);
- //error check
- if (potionlist[which-1]->GetQuantity() < 1)
- {
- //remove potion
- delete potionlist[which-1];
- potionlist[which-1] = NULL;
- }
- actionused = true;
- Wait();
- cout << endl;
- break;
- }
- }
- }
- if (actionused)
- break;
- }
- } // while attacking
- //check poison
- if (player->IsPoisoned())
- {
- cout << "Poison does 5 damage to you.\n";
- player->LoseHealth(5);
- Wait();
- }
- //monster poisoned?
- if (count == 1)
- {
- if (list->IsPoisoned())
- {
- cout << list->GetName() << " takes 5 damage from poison.\n";
- list->LoseHealth(5);
- Wait();
- }
- }
- else if (count > 1)
- {
- for (int ploop=0;ploop < count;ploop++)
- {
- if (list[ploop].GetHealth() > 0)
- {
- if (list[ploop].IsPoisoned())
- {
- cout << list[ploop].GetName() << " takes 5 damage from poison.\n";
- list[ploop].LoseHealth(5);
- Wait();
- }
- }
- }
- }
- //is enemy dead?
- if (count == 1)
- {
- if (list->GetHealth() <= 0)
- {
- //dead
- //battle over
- cout << "You win!\n";
- cout << "You gain " << list->GetExperience() << " experience and " << list->GetGold() << " gold.\n";
- Wait();
- //award experience
- player->AddExperience(list->GetExperience());
- player->AddGold(list->GetGold());
- player->CheckForLevelUp();
- return true;
- }
- }
- else if (count > 1)
- {
- bool alldead = true;
- //are all enemies dead
- for (int check=0;check < count;check++)
- {
- if (list[check].GetHealth() > 0)
- {
- alldead = false;
- check = count;
- }
- }
- if (alldead)
- {
- //end the battle
- cout << "\nYou WIN!\n";
- Wait();
- player->CheckForLevelUp();
- return true;
- }
- }
- //enemies turn
- if (count == 1)
- {
- //have enemy fight
- MonsterAttack(list,player);
- }
- else
- {
- //loop
- for (int loop=0;loop < count;loop++)
- {
- if (list[loop].GetHealth() > 0)
- MonsterAttack(&list[loop],player);
- }
- }
- }
- cout << "Thou hast been slain.\n\n";
- Wait();
- cout << endl;
- return false; //you died
- }
- void ListMonsters(MONSTER* list,int count)
- {
- cout << "Enemies\n";
- for (int loopname=0;loopname < count; loopname++)
- {
- if (list[loopname].GetHealth() > 0)
- {
- cout << loopname+1 << ": " << list[loopname].GetName();
- //output status effect
- //Goblin - poisoned
- cout << " - " << list[loopname].GetHealth();
- cout << endl;
- }
- }
- }
- bool AttackMonster(PLAYER* player,MONSTER* monster)
- {
- /*
- Damage Taken = ATT AP
- Damage Given = ( (ATT+Strength) / 2) TO (ATT+Strength) WITH (10-Strength)% Chance miss
- */
- //calculate miss?
- if (player->GetStrength() < 10)
- {
- //generate random number
- int random = (rand()%(10-player->GetStrength()))+1;
- if (random == 1) //miss
- {
- cout << "You swing as hard as you can but miss horribly\n";
- return false;
- }
- }
- //attack single monster
- int pstr = player->GetStrength();
- int damagelow = ( (player->GetWeapon()->GetDamage() + pstr) /2 );
- int damagehigh = ( player->GetWeapon()->GetDamage() + pstr);
- //generate damage
- int damagegiven = (rand()%(damagehigh-damagelow))+damagelow;
- int totaldamage = damagegiven - monster->GetArmor();
- cout << "You injure the " << monster->GetName() << " for " << totaldamage << " health.\n";
- monster->LoseHealth(totaldamage);
- return true;
- }
- bool MonsterAttack(MONSTER* monster,PLAYER* player)
- {
- /*
- Damage Taken = ATT AP
- Damage Given = ATT - (ATT / 5) TO ATT
- */
- //slightly modified formula
- int damagelow = monster->GetAttack() - (monster->GetAttack() / 5);
- int damagehigh = monster->GetAttack();
- int damagegiven = (rand()%damagehigh-damagelow)+damagelow;
- int totaldamage = damagegiven - player->GetArmor()->GetAP();
- if (totaldamage < 0)
- totaldamage = 0;
- cout << "The " << monster->GetName() << " attacks you for " << totaldamage << " health.\n";
- player->LoseHealth(totaldamage);
- Wait();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment