Adilol

lol

Aug 19th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.35 KB | None | 0 0
  1.  
  2. // BATTLE
  3.  
  4. #include "Library.h"
  5.  
  6. void ListMonsters(MONSTER* list,int count);
  7. bool AttackMonster(PLAYER* player,MONSTER* monster);
  8. bool MonsterAttack(MONSTER* monster,PLAYER* player);
  9.  
  10. bool Battle(PLAYER* player,MONSTER* list,int count)
  11. {
  12.     cout << "You clench your " << player->GetWeapon()->GetName() << " tightly and face the enemy.\n\n";
  13.  
  14.     while (player->GetHealth() > 0)
  15.     {
  16.         //player goes first
  17.         bool actionused = false;
  18.  
  19.         //display enemy count
  20.         if (count ==  1)
  21.         {
  22.             cout << "The " << list->GetName() << " stares at you menacingly.\n";
  23.  
  24.             /// TEMPORARY
  25.             cout << "Health: " << list->GetHealth() << endl;
  26.             Wait();
  27.         }
  28.         else
  29.         {
  30.             ListMonsters(list,count);
  31.         }
  32.        
  33.         cout << "\n";
  34.  
  35.         //display player stats
  36.         cout << player->GetName() << endl;
  37.         //health / maxhealth, mana / maxmana
  38.         cout << "Health: " << player->GetHealth() << " / " << player->GetMaxHealth() << endl;
  39.         cout << "Mana: " << player->GetMana() << " / " << player->GetMaxMana() << endl;
  40.  
  41.         cout << endl;
  42.  
  43.         int action = 0;
  44.  
  45.         while (true)
  46.         {
  47.             cout << "1: Attack\n";
  48.             cout << "2: Spell\n";
  49.             cout << "3: Item\n\n";
  50.             cout << "4:Escape\n\n";
  51.            
  52.  
  53.             cout << ">";
  54.  
  55.             cin >> action;
  56.             cin.ignore(10,'\n');
  57.  
  58.             //action?
  59.             if (action == 1)    //attack
  60.             {
  61.                 if (count == 1)
  62.                 {
  63.                     //attack single monster
  64.  
  65.                     AttackMonster(player,list);
  66.  
  67.                     Wait();
  68.  
  69.                     cout << endl;
  70.                 }
  71.                 else
  72.                 {
  73.                     //ask for target
  74.                     int target = 0;
  75.  
  76.                     while (true)
  77.                     {
  78.                         ListMonsters(list,count);
  79.  
  80.                         cout << "\n0: Cancel\n\n";
  81.  
  82.                         cout << "Which monster?\n";
  83.                         cout << ">";
  84.  
  85.                         cin >> target;
  86.                         cin.ignore(10,'\n');
  87.  
  88.                         if ( target > 0 && target <= count)
  89.                         {
  90.                             //check if the target is alive
  91.                             if (list[target-1].GetHealth() > 0)
  92.                             {
  93.                                 //attack the target
  94.                                 AttackMonster(player,&list[target-1]);
  95.  
  96.                                 Wait();
  97.  
  98.                                 cout << endl;
  99.  
  100.                                 //check if alive and award points if not
  101.                                 if (list[target-1].GetHealth() <= 0)
  102.                                 {
  103.                                     cout << "You killed the " << list[target-1].GetName() << "\n";
  104.                                     cout << "You gain " << list[target-1].GetExperience() << " experience and " << list[target-1].GetGold() << " gold.\n";
  105.                                     player->AddExperience(list[target-1].GetExperience());
  106.                                     player->AddGold(list[target-1].GetGold());
  107.  
  108.                                     cout << endl;
  109.                                 }
  110.                                 break;
  111.                             }
  112.                             else
  113.                             {
  114.                                 cout << "Invalid monster.\n\n";
  115.                             }
  116.                         }
  117.                         else if (target == 0)
  118.                         {
  119.                             //cancelled attack
  120.                             action = 0;
  121.                             break;
  122.                         }
  123.                     }   //end while target not chosen
  124.  
  125.                     //
  126.                 }
  127.  
  128.                 if (action == 1)    //followed through with attack
  129.                     break;
  130.             }
  131.             else if (action == 2)   //spell
  132.             {
  133.                 //display players spells
  134.  
  135.                 //get the list
  136.                 SPELL** spelllist = player->GetSpellList();
  137.  
  138.                 for (int loop=0;loop < 10; loop++)
  139.                 {
  140.                     if (spelllist[loop] != NULL)
  141.                     {
  142.                         cout << loop+1 << ": " << spelllist[loop]->GetName() << " MP: " << spelllist[loop]->GetManaCost() << endl;
  143.                     }
  144.                 }
  145.                 cout << "0: Cancel\n\n";
  146.  
  147.                 //check for input
  148.                 int which = 0;
  149.                 while (true)
  150.                 {
  151.                     cout << "Which Spell?\n";
  152.                     cout << ">";
  153.                     cin >> which;
  154.                     cin.ignore(10,'\n');
  155.  
  156.                     if (which == 0)
  157.                     {
  158.                         //cancel
  159.                         break;
  160.                     }
  161.                     else if (which > 0 && which < 11)
  162.                     {
  163.                         //valid?
  164.                         if (spelllist[which-1] != NULL)
  165.                         {
  166.                             //spell is valid
  167.  
  168.                             //enough mp?
  169.                             if (player->GetMana() >= spelllist[which-1]->GetManaCost())
  170.                             {
  171.                                 //cast
  172.                                 player->LoseMana(spelllist[which-1]->GetManaCost());
  173.  
  174.                                 //which target?
  175.                                 if (count > 1)
  176.                                 {
  177.                                     ListMonsters(list,count);
  178.                                 }
  179.                                 else
  180.                                 {
  181.                                     cout << "1: " << list->GetName() << endl;
  182.                                 }
  183.                                 cout << "0: " << "Self\n";
  184.  
  185.                                 int choice = 0;
  186.                                 while (true)
  187.                                 {
  188.                                     cout << "Which target?\n";
  189.                                     cout << ">";
  190.                                     cin >> choice;
  191.                                     cin.clear();
  192.                                     cin.ignore(INT_MAX,'\n');
  193.  
  194.                                     //validate
  195.                                     if (choice == 0)
  196.                                     {
  197.                                         spelllist[which-1]->Cast((ENTITY*)player,1);
  198.                                         actionused = true;
  199.                                         break;
  200.                                     }
  201.                                     else if (count > 1 && choice < count+1)
  202.                                     {
  203.                                         if (list[choice-1].GetHealth() > 0)
  204.                                         {
  205.                                             //cast
  206.                                             spelllist[which-1]->Cast((ENTITY*)(&list[choice-1]),1);
  207.                                             actionused = true;
  208.                                             break;
  209.                                         }
  210.                                         else
  211.                                             cout << "Invalid Target\n";
  212.                                     }
  213.                                     else if (count == 1 && choice == 1)
  214.                                     {
  215.                                         //cast
  216.                                         spelllist[which-1]->Cast((ENTITY*)(list),1);
  217.                                         actionused = true;
  218.                                         break;
  219.                                     }
  220.                                     else
  221.                                     {
  222.                                         cout << "Invalid Target\n";
  223.                                     }
  224.                                 }//end while choose target
  225.  
  226.                                 break;
  227.                             }
  228.                             else
  229.                             {
  230.                                 cout << "Not Enough mana\n";
  231.                             }
  232.                         }
  233.                     }
  234.                 }
  235.  
  236.                 if (actionused)
  237.                     break;
  238.             }
  239.             else if (action == 3)   //item
  240.             {
  241.                 //display players potion list
  242.                
  243.                 //getlist
  244.                 POTION** potionlist = player->GetPotionList();
  245.  
  246.                 for (int loop=0;loop < 15;loop++)
  247.                 {
  248.                     if (potionlist[loop] != NULL)
  249.                     {
  250.                         cout << loop+1 << ": " << potionlist[loop]->GetName() << " (" <<potionlist[loop]->GetQuantity() << ")\n";
  251.                     }
  252.                    
  253.                 }
  254.                 cout << "0: Cancel\n\n";
  255.  
  256.                 //check for input
  257.                 int which = 0;
  258.                 while (true)
  259.                 {
  260.                     cout << "Which potion?\n";
  261.                     cout << ">";
  262.                     cin >> which;
  263.                     cin.ignore(10,'\n');
  264.  
  265.                     if (which == 0)
  266.                     {
  267.                         //cancel
  268.                         break;
  269.                     }
  270.                     else if (which > 0 && which < 16)
  271.                     {
  272.                         //errorcheck
  273.                         if (potionlist[which-1] == NULL)
  274.                             cout << "Invalide Potion.\n";
  275.                         else
  276.                         {
  277.                             if ( potionlist[which-1]->GetHealthRestore() > 0)
  278.                                 cout << "You gain " << potionlist[which-1]->GetHealthRestore() << " health.\n";
  279.                            
  280.                             if (potionlist[which-1]->GetManaRestore() > 0)
  281.                                 cout << "You gain " << potionlist[which-1]->GetManaRestore() << " mana.\n";
  282.  
  283.                             player->AddHealth(potionlist[which-1]->GetHealthRestore());
  284.                             player->AddMana(potionlist[which-1]->GetManaRestore());
  285.  
  286.                             potionlist[which-1]->SubtractQuantity(1);
  287.  
  288.                             //error check
  289.                             if (potionlist[which-1]->GetQuantity() < 1)
  290.                             {
  291.                                 //remove potion
  292.                                 delete potionlist[which-1];
  293.                                 potionlist[which-1] = NULL;
  294.                             }
  295.  
  296.                             actionused = true;
  297.  
  298.                             Wait();
  299.                             cout << endl;
  300.                             break;
  301.                         }
  302.                     }
  303.                 }
  304.  
  305.                 if (actionused)
  306.                     break;
  307.             }
  308.         } // while attacking
  309.  
  310.         //check poison
  311.         if (player->IsPoisoned())
  312.         {
  313.             cout << "Poison does 5 damage to you.\n";
  314.             player->LoseHealth(5);
  315.             Wait();
  316.         }
  317.  
  318.         //monster poisoned?
  319.         if (count == 1)
  320.         {
  321.             if (list->IsPoisoned())
  322.             {
  323.                 cout << list->GetName() << " takes 5 damage from poison.\n";
  324.                 list->LoseHealth(5);
  325.                 Wait();
  326.             }
  327.         }
  328.         else if (count > 1)
  329.         {
  330.             for (int ploop=0;ploop < count;ploop++)
  331.             {
  332.                 if (list[ploop].GetHealth() > 0)
  333.                 {
  334.                     if (list[ploop].IsPoisoned())
  335.                     {
  336.                         cout << list[ploop].GetName() << " takes 5 damage from poison.\n";
  337.                         list[ploop].LoseHealth(5);
  338.                         Wait();
  339.                     }
  340.                 }
  341.             }
  342.         }
  343.  
  344.         //is enemy dead?
  345.         if (count == 1)
  346.         {
  347.             if (list->GetHealth() <= 0)
  348.             {
  349.                 //dead
  350.                 //battle over
  351.  
  352.                 cout << "You win!\n";
  353.                 cout << "You gain " << list->GetExperience() << " experience and " << list->GetGold() << " gold.\n";
  354.                 Wait();
  355.  
  356.                 //award experience
  357.                 player->AddExperience(list->GetExperience());
  358.                 player->AddGold(list->GetGold());
  359.  
  360.                 player->CheckForLevelUp();
  361.  
  362.                 return true;
  363.             }
  364.         }
  365.         else if (count > 1)
  366.         {
  367.             bool alldead = true;
  368.             //are all enemies dead
  369.             for (int check=0;check < count;check++)
  370.             {
  371.                 if (list[check].GetHealth() > 0)
  372.                 {
  373.                     alldead = false;
  374.                     check = count;
  375.                 }
  376.             }
  377.  
  378.             if (alldead)
  379.             {
  380.                 //end the battle
  381.                 cout << "\nYou WIN!\n";
  382.                 Wait();
  383.  
  384.                 player->CheckForLevelUp();
  385.  
  386.                 return true;
  387.             }
  388.         }
  389.  
  390.  
  391.         //enemies turn
  392.  
  393.         if (count == 1)
  394.         {
  395.             //have enemy fight
  396.             MonsterAttack(list,player);
  397.         }
  398.         else
  399.         {
  400.             //loop
  401.             for (int loop=0;loop < count;loop++)
  402.             {
  403.                 if (list[loop].GetHealth() > 0)
  404.                     MonsterAttack(&list[loop],player);
  405.             }
  406.         }
  407.     }
  408.  
  409.     cout << "Thou hast been slain.\n\n";
  410.  
  411.     Wait();
  412.  
  413.     cout << endl;
  414.  
  415.     return false;   //you died
  416. }
  417.  
  418. void ListMonsters(MONSTER* list,int count)
  419. {
  420.     cout << "Enemies\n";
  421.     for (int loopname=0;loopname < count; loopname++)
  422.     {
  423.         if (list[loopname].GetHealth() > 0)
  424.         {
  425.             cout << loopname+1 << ": " << list[loopname].GetName();
  426.  
  427.             //output status effect
  428.             //Goblin - poisoned
  429.  
  430.             cout << " - " << list[loopname].GetHealth();
  431.  
  432.             cout << endl;
  433.         }
  434.     }
  435. }
  436.  
  437. bool AttackMonster(PLAYER* player,MONSTER* monster)
  438. {
  439.     /*
  440.     Damage Taken = ATT – AP
  441.     Damage Given = ( (ATT+Strength) / 2) TO (ATT+Strength) WITH (10-Strength)% Chance miss
  442.     */
  443.  
  444.     //calculate miss?
  445.     if (player->GetStrength() < 10)
  446.     {
  447.         //generate random number
  448.         int random = (rand()%(10-player->GetStrength()))+1;
  449.  
  450.         if (random == 1) //miss
  451.         {
  452.             cout << "You swing as hard as you can but miss horribly\n";
  453.             return false;
  454.         }
  455.     }
  456.  
  457.     //attack single monster
  458.     int pstr = player->GetStrength();
  459.     int damagelow = ( (player->GetWeapon()->GetDamage() + pstr) /2 );
  460.     int damagehigh = ( player->GetWeapon()->GetDamage() + pstr);
  461.  
  462.     //generate damage
  463.     int damagegiven = (rand()%(damagehigh-damagelow))+damagelow;
  464.     int totaldamage = damagegiven - monster->GetArmor();
  465.  
  466.     cout << "You injure the " << monster->GetName() << " for " << totaldamage << " health.\n";
  467.     monster->LoseHealth(totaldamage);
  468.  
  469.     return true;
  470. }
  471.  
  472. bool MonsterAttack(MONSTER* monster,PLAYER* player)
  473. {
  474.     /*
  475.     Damage Taken = ATT – AP
  476.     Damage Given = ATT - (ATT / 5) TO ATT
  477.     */
  478.  
  479.     //slightly modified formula
  480.  
  481.     int damagelow = monster->GetAttack() - (monster->GetAttack() / 5);
  482.     int damagehigh = monster->GetAttack();
  483.  
  484.     int damagegiven = (rand()%damagehigh-damagelow)+damagelow;
  485.     int totaldamage = damagegiven - player->GetArmor()->GetAP();
  486.  
  487.     if (totaldamage < 0)
  488.         totaldamage = 0;
  489.  
  490.     cout << "The " << monster->GetName() << " attacks you for " << totaldamage << " health.\n";
  491.     player->LoseHealth(totaldamage);
  492.  
  493.     Wait();
  494.  
  495.     return true;
  496. }
Advertisement
Add Comment
Please, Sign In to add comment