// AI for a Monster to make an attack. // Includes identifying the closest character // Moving closer to the character. Teleporting more like // Then attacking the target if within range. public string EnemyTakeTurn(int row, int column) { // Find target in the grid BattleTile target = new BattleTile(); for (int i = 0; i < Grid.Count; i++) { for (int j = 0; j < Grid[i].Count; j++) { // If a Character occupies the current grid access to determine if it is closer than current target. if (Grid[i][j].Type == "Character") { // If the current target is just a tile or empty then assign the new target. if (target.Type == "Tile") { target = Grid[i][j]; } else { // If the current target and locations are both Characters, use ManhattanDistance to determine the closest one. if (ManhattanDistance(target.Row, target.Column, row, column) > ManhattanDistance(j, i, row, column)) { target = Grid[i][j]; } } } } } string battleText = ""; // Move to target in the grid BattleTile current = new BattleTile(Grid[column][row]); BattleTile newLocation = new BattleTile(MoveCloser(target, current)); Grid[column][row] = new BattleTile(row, column); current.Row = newLocation.Row; current.Column = newLocation.Column; Grid[newLocation.Column][newLocation.Row] = current; // Cannot move any closer to the target so the Monster stays put if (row == newLocation.Row && column == newLocation.Column) { battleText += current.Name + " does not move.\n"; } // Has successfully moved closer to the target. else { battleText += current.Name + " moves to Row " + newLocation.Row + " and Column " + newLocation.Column + ".\n"; } // Try to attack target. if (ManhattanDistance(target.Row, target.Column, newLocation.Row, newLocation.Column) <= current.GetRange()) { // Damage is attempted against the Character target. int damage = target.DealtDamage(current); string status = target.DamageStatus; // Monster misses the attack. if (status == "Miss") { battleText += current.Name + " misses attack against " + target.Name + ".\n"; } // Monster hits the target. else if (status == "Hit") { battleText += current.Name + " deals " + damage + " damage to " + target.Name + ".\n"; } // Monster critically misses. else if (status == "Critical Miss") { battleText += current.Name + " CRITICALLY misses attack against " + target.Name + ".\n"; DamageStatus = "Critical Miss"; // Monster Criticaly hits. } else if (status == "Critical Hit") { battleText += current.Name + " deals " + damage + " CRITICAL damage to " + target.Name + ".\n"; DamageStatus = "Critical Hit"; } target.DamageStatus = ""; // If the target dies if (target.GetHealthCurr() <= 0) { // Automatically resurect if the MostlyDead option is turned on. if (BattleSystemViewModel.Instance.MostlyDead && target.MostlyDead) { target.MostlyDead = false; battleText += "Miracle Max pops out of a drainage pipe steps on " + target.Name + " and brings him back to life.\n"; target.Hero.HealthCurr = target.Hero.Health; target.Life = "life" + (int)(((double)(target.Hero.HealthCurr + 1) / (double)(target.Hero.Health + 1)) * 10) + ".png"; } // Otherwise the character dies and the state is saved. else { battleText += target.Name + " dies from the damage.\n"; Grid[target.Column][target.Row] = new BattleTile(target.Row, target.Column); recentlyDeceased = target; } } } // Monster is not within range of its target. else { battleText += current.Name + " does not attack. \n"; } // Returns the messages saved during combat. return(battleText); }