using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConqueringOneself { public static class Combat { static Main main; static int turn = 1; static Dictionary> effects = new Dictionary>(); public static void Initialize(Main main) { Combat.main = main; } public static void StartCombat() { main.yourAttacks = new List(); main.selectedItem = 0; main.player.HP = main.player.maxHP; main.enemy.HP = main.enemy.maxHP; main.player.MP = main.player.maxMP; main.enemy.MP = main.enemy.maxMP; effects = new Dictionary>(); effects.Add(main.player.id, new List()); effects.Add(main.enemy.id, new List()); Graphics.DisplayMoves(); Graphics.DisplayCombatStuff(); Graphics.ShowBackground(); Graphics.ShowDemonLord(); Audio.PlayMusic("TheFateOfTheWorld"); main.battleState = Main.BattleState.Normal; turn = 1; } public static void HandleTurn() { Graphics.Msg("!icon turn"); Graphics.Msg("---TURN " + turn + "---"); Character player = main.player; Character enemy = main.enemy; Attack yourMove = player.availableAttacks[main.selectedItem]; Attack enemyMove = main.enemyPattern[main.enemyPtnCounter]; //Decide who goes first if (yourMove.priority < enemyMove.priority) { PerformMove(player, enemy, yourMove); PerformMove(enemy, player, enemyMove); } else { PerformMove(enemy, player, enemyMove); PerformMove(player, enemy, yourMove); } //--End of turn-- //Move enemy pattern counter main.enemyPtnCounter++; if (main.enemyPtnCounter >= main.enemyPattern.Count) main.enemyPtnCounter = 0; //Effects wear off foreach (List l in effects.Values) { RemoveThese(l, new string[] { "defend", "counterphysical", "countermagical", "nullify" }); } if (player.HP <= 0) { player.HP = 0; main.battleState = Main.BattleState.Defeat; Graphics.Msg("!defeat"); Graphics.Msg("You collapse to the ground..."); Graphics.Msg("--YOU HAVE BEEN DEFEATED!--"); } else if (enemy.HP <= 0) { //Remove attack if player has 8 attacks if (player.availableAttacks.Count == 8) { int[] attackCounts = new int[player.availableAttacks.Count]; for (int i = 0; i < main.yourAttacks.Count; i++) { int foundAt = -1; for (int ii = 0; ii < player.availableAttacks.Count; ii++) { if (player.availableAttacks[ii].id == main.yourAttacks[i].id) foundAt = ii; } if (foundAt != -1) { attackCounts[foundAt]++; } } int highestMoveNo = 0; int highestMoveAmount = -1; for (int i = 0; i < attackCounts.Length; i++) { if (attackCounts[i] > highestMoveAmount) { highestMoveNo = i; highestMoveAmount = attackCounts[i]; } } Attack removedAttack = player.availableAttacks[highestMoveNo]; player.availableAttacks.RemoveAt(highestMoveNo); Graphics.Msg("With his last breath, the Demon Lord casts a strange spell!"); Graphics.Msg("!sound forget"); Graphics.Msg("!icon ohno"); Graphics.Msg("You have forgotten the attack '" + removedAttack.name + "'!"); } enemy.HP = 0; main.battleState = Main.BattleState.Victorious; Graphics.Msg("!victory"); Graphics.Msg("The Demon Lord collapses to the ground!"); Graphics.Msg("--YOU ARE VICTORIOUS!--"); } Graphics.Msg("(Press any key.)"); if(player.HP > 0 && enemy.HP > 0) turn++; main.selectedItem = 0; } public static List RemoveThese(List input, string[] toFilter) { for (int i = 0; i < toFilter.Length; i++) { while (input.Contains(toFilter[i])) input.Remove(toFilter[i]); } return input; } public static void PerformMove(Character user, Character victim, Attack move) { if (user.HP > 0) { //First, start-of-move effects if (effects[user.id].Contains("regeneration")) { long regHP = HealUser(user, victim, 10, true); if (regHP > 0) { Graphics.Msg("!icon healing"); Graphics.Msg("!sound regenerate"); Graphics.Msg(user.name + " regenerates " + regHP + " HP!"); } } if (effects[user.id].Contains("aura")) { long regMP = MeditateUser(user, victim, 10, true); if (regMP > 0) { Graphics.Msg("!icon restoration"); Graphics.Msg("!sound auraeffect"); Graphics.Msg(user.name + "'s aura grants him " + regMP + " MP!"); } } Graphics.Msg("!sound " + move.name); Graphics.Msg("!icon " + move.name); Graphics.Msg(user.name + " uses " + move.name + "!"); if (user.PayMP(move.mpCost)) { if (effects[victim.id].Contains("nullify") && move.effect != AttackEffect.UnblockableDamage) { Graphics.Msg("!sound fizzle"); Graphics.Msg("!icon fizzle"); Graphics.Msg("But his actions are nullified by the mana floating around!"); } else { switch (move.effect) { case AttackEffect.Damage: DealDamage(user, victim, move); break; case AttackEffect.UnblockableDamage: DealDamage(user, victim, move, true); break; case AttackEffect.Defend: effects[user.id].Add("defend"); Graphics.Msg(user.name + " is shielding himself from attacks!"); break; case AttackEffect.CounterPhysical: effects[user.id].Add("counterphysical"); Graphics.Msg(user.name + " takes a defensive stance!"); break; case AttackEffect.CounterMagical: effects[user.id].Add("countermagical"); Graphics.Msg(user.name + " closes his eyes!"); break; case AttackEffect.Heal: HealUser(user, victim, move.strength); break; case AttackEffect.Drain: int damage = DealDamage(user, victim, move); HealUser(user, victim, damage); break; case AttackEffect.Berserker: if (DealDamage(user, victim, move) > 0) { if (victim.id == 0) Graphics.Msg("!sound hit"); else Graphics.Msg("!sound gethit"); int recoil = (int)Math.Round((float)move.strength / 2f); user.HP -= recoil; Graphics.Msg("!icon damage"); Graphics.Msg(user.name + " receives " + recoil + " recoil damage!"); } break; case AttackEffect.Nullify: effects[user.id].Add("nullify"); Graphics.Msg("The environment is filled with mana!"); break; case AttackEffect.RestoreMP: MeditateUser(user, victim, move.strength); break; case AttackEffect.MPDamage: MPDamage(user, victim, move); break; case AttackEffect.Regeneration: Graphics.Msg(user.name + "'s body starts regenerating!"); effects[user.id].Add("regeneration"); break; case AttackEffect.Aura: Graphics.Msg("A magical aura surrounds " + user.name + "!"); effects[user.id].Add("aura"); break; case AttackEffect.Reality: Graphics.Msg("All lasting effects vanish!"); effects = new Dictionary>(); effects.Add(main.player.id, new List()); effects.Add(main.enemy.id, new List()); break; } user.lastMove = move; } } else { Graphics.Msg("!sound fizzle"); Graphics.Msg("!icon fizzle"); Graphics.Msg("But the spell fizzles due to lack of mana!"); } } } public static int DealDamage(Character user, Character victim, Attack move, bool unblockable = false) { int damage = move.strength; if (effects[victim.id].Contains("defend") && !unblockable) { damage = (int)Math.Round((float)damage / 2f); Graphics.Msg(victim.name + " manages to partially block the attack!"); } else if (!unblockable) { if (CheckCounter(user, victim, move, damage)) damage = 0; } if (damage < 0) damage = 0; victim.HP -= damage; if (damage > 0) { if (victim.id == 1) Graphics.Msg("!sound hit"); else Graphics.Msg("!sound gethit"); Graphics.Msg("!icon damage"); Graphics.Msg(damage + " damage dealt to " + victim.name + "!"); } return damage; } public static long HealUser(Character user, Character victim, int amount, bool silent = false) { long healed = user.maxHP - user.HP; if (healed > amount) healed = amount; if (healed > 0) { user.HP += healed; if(!silent) Graphics.Msg("!sound healing"); if(!silent) Graphics.Msg("!icon healing"); if(!silent) Graphics.Msg(user.name + " heals " + healed + " HP!"); } return healed; } public static long MeditateUser(Character user, Character victim, int amount, bool silent = false) { long recovered = user.maxMP - user.MP; if (recovered > amount) recovered = amount; if (recovered > 0) { user.MP += recovered; if(!silent) Graphics.Msg("!sound healing"); if(!silent) Graphics.Msg("!icon restoration"); if(!silent) Graphics.Msg(user.name + " recovers " + recovered + " MP!"); } return recovered; } public static bool CheckCounter(Character user, Character victim, Attack move, int damage) { int counterDamage = 0; bool countered = false; if (effects[victim.id].Contains("counterphysical") && move.type == AttackType.Physical && damage > 0) { counterDamage = victim.lastMove.strength; Graphics.Msg("!sound counter"); Graphics.Msg("!icon disrupt"); Graphics.Msg("But " + victim.name + " manages to counterattack!"); countered = true; } else if (effects[victim.id].Contains("countermagical") && move.type == AttackType.Magic && damage > 0) { counterDamage = victim.lastMove.strength; Graphics.Msg("!sound counter"); Graphics.Msg("!icon disrupt"); Graphics.Msg("But " + victim.name + " manages to disrupt the flow of mana!"); countered = true; } user.HP -= counterDamage; if (counterDamage > 0) { if (victim.id == 0) Graphics.Msg("!sound hit"); else Graphics.Msg("!sound gethit"); Graphics.Msg("!icon damage"); Graphics.Msg(user.name + " receives " + counterDamage + " damage!"); } return countered; } public static int MPDamage(Character user, Character victim, Attack move) { int damage = move.strength; if (CheckCounter(user, victim, move, damage)) damage = 0; if (damage < 0) damage = 0; victim.MP -= damage; if (victim.MP < 0) victim.MP = 0; if (damage > 0) { if (victim.id == 1) Graphics.Msg("!sound hit"); else Graphics.Msg("!sound Drain"); Graphics.Msg("!icon mpdamage"); Graphics.Msg(victim.name + " lost " + damage + " MP!"); } return damage; } } }