private void attack(BattleEngine.Player.Player source, BattleEngine.Player.Player target) { string details = string.Format("<u>Round {0}</u>: <i>{1}</i> is <b>attacking</b> <i>{2}</i>", this.round, source.Name, target.Name); this.BattleGui.ShowAttackDialog(details); Location sloc = this.GameMap.GetPlayerOnMap(source); Location tloc = this.GameMap.GetPlayerOnMap(target); double dist = sloc.DistanceTo(tloc); int range = source.GetAttribute("Range").Current; if ((double)dist <= range) { int attackroll = source.ThrowForAttack(); int defenseroll = target.ThrowForDefense(); this.Console.ConsoleWrite(string.Format("\t\"{0}\" attack rolls {1} at a distance of {2} with range {3}", source.Name, attackroll, dist, range)); this.Console.ConsoleWrite(string.Format(" : \"{0}\" defense rolls {1}", target.Name, defenseroll)); if (attackroll >= defenseroll) { this.Console.ConsoleWrite(" HIT"); int power = source.RollDamage(); this.Console.ConsoleWrite(string.Format(" for {0} damage", power)); int armor = target.GetAttribute("Armor").Current; int damage = armor - power; if (damage < 0) { target.TakeDamage(damage); this.Console.ConsoleWriteLine(string.Format(" \"{0}\" took {1} damage leaving {2} armor", target.Name, damage, target.GetAttribute("Armor").Current)); } else { this.Console.ConsoleWriteLine(string.Format(" \"{0}\" took no damage \"{1}\" could not penetrate armor of {2}", target.Name, source.Name, target.GetAttribute("Armor").Current)); } } else { this.Console.ConsoleWriteLine(" MISSED"); } } else { this.Console.ConsoleWriteLine(string.Format("\tOUT OF RANGE: \"{0}\" is {1} meters away from \"{2}\"", target.Name, dist, source.Name)); int move = source.GetAttribute("Speed").Current; this.Console.ConsoleWriteLine(string.Format("{0} closing {1} meters", source.Name, move)); this.GameMap.MovePlayerToTarget(source, move, target); } }
private BattleEngine.Player.Player pick_target_by_shortest_distance(BattleEngine.Player.Player source) { List <BattleEngine.Player.Player> potential_targets = new List <BattleEngine.Player.Player>(); foreach (BattleEngine.Player.Player p in this.players.Values) { if ((source != p) && (!p.IsDead())) { potential_targets.Add(p); } } if (potential_targets.Count < 1) { throw new Exception("No potential targets!"); } Location myloc = this.GameMap.GetPlayerOnMap(source); Dictionary <BattleEngine.Player.Player, double> distances = new Dictionary <BattleEngine.Player.Player, double>(); foreach (BattleEngine.Player.Player target in potential_targets) { Location tgtloc = this.GameMap.GetPlayerOnMap(target); distances.Add(target, myloc.DistanceTo(tgtloc)); } double lowest_dist = (double)(this.GameMap.Length + this.GameMap.Width + this.GameMap.Height) * 2; BattleEngine.Player.Player target_player = null; foreach (KeyValuePair <BattleEngine.Player.Player, double> kvp in distances) { this.Console.ConsoleWriteLine(string.Format("\"{0}\" distance to \"{1}\" is {2}", source.Name, kvp.Key.Name, kvp.Value)); if (kvp.Value < lowest_dist) { lowest_dist = kvp.Value; target_player = kvp.Key; } } return(target_player); }