public static void HeroesAction(int turn, Attackers attackers, Defenders defenders) { bool attackerTurn = IsHeroTurn(turn, attackers.CurrentHeroPower); bool defenderTurn = IsHeroTurn(turn, defenders.CurrentHeroPower); bool hasDiv = false; if (attackerTurn || defenderTurn) { hasDiv = true; Combat.Log.Add("<div class='heroesActions'>"); } while (attackerTurn || defenderTurn) { if (attackerTurn && defenderTurn) { int attackerInitiative = Dice.Throw(attackers.CurrentHero.Speed); int defenderInitiative = Dice.Throw(defenders.CurrentHero.Speed); if (attackerInitiative.BiggerThan(defenderInitiative)) { HeroCombat(attackers, defenders); if (defenders.CurrentHero.Life > 0 && IsHeroTurn(turn, defenders.CurrentHeroPower)) HeroCombat(defenders, attackers); } else { HeroCombat(defenders, attackers); if (attackers.CurrentHero.Life > 0 && IsHeroTurn(turn, attackers.CurrentHeroPower)) HeroCombat(attackers, defenders); } } else { if (attackerTurn) HeroCombat(attackers, defenders); else if (defenderTurn) HeroCombat(defenders, attackers); } if (attackers.Powers.Count > 0 && attackers.CurrentHeroPower.Executed) attackers.Powers.RemoveAt(0); if (defenders.Powers.Count > 0 && defenders.CurrentHeroPower.Executed) defenders.Powers.RemoveAt(0); Combat.Log.Add("<br />"); attackerTurn = IsHeroTurn(turn, attackers.CurrentHeroPower); defenderTurn = IsHeroTurn(turn, defenders.CurrentHeroPower); } if (hasDiv) Combat.Log.Add("</div>"); }
public void HeroesPowers(int turns, Attackers attackers, Defenders defenders) { attackers.Powers = new List<HeroPower>(); defenders.Powers = new List<HeroPower>(); foreach (Hero hero in attackers.Heroes) PowerList(turns, hero.Powers, attackers.Powers); foreach (Hero hero in defenders.Heroes) PowerList(turns, hero.Powers, defenders.Powers); attackers.Powers = attackers.Powers.OrderBy(x => x.Turn).ToList(); defenders.Powers = defenders.Powers.OrderBy(x => x.Turn).ToList(); }
public ActionResult Show() { Combat.Log.Clear(); Generate generate = new Generate(Request); UnitFactory.StartUnitFactory(); DateTime startTime = DateTime.Now; Attackers attackers = new Attackers(generate); Defenders defenders = new Defenders(generate); int turns = attackers.Units.Count.BiggerThan(defenders.Units.Count)? attackers.Units.Count : defenders.Units.Count; int rounds = ReadIntFromRequest("rounds"); generate.HeroesPowers(turns, attackers, defenders); int i = 1; while (i <= rounds && defenders.Units.Count + defenders.Heroes.Count > 0 && attackers.Units.Count + attackers.Heroes.Count > 0) { Combat.Log.Add(string.Format("<h2>Round {0}</h2><p>Atacando: {1} unidades</p><p>Defendendo: {2} unidades</p>", i, attackers.Units.Count, defenders.Units.Count)); Combat.StartCombat(turns, attackers, defenders); #region LOG Combat.Log.Add(attackers.PV()); Combat.Log.Add(defenders.PV()); generate.RoundReport(i, attackers, defenders); #endregion #region RESET generate.RoundResetUnitLives(attackers.Units); generate.RoundResetUnitLives(defenders.Units); attackers.ResetHeroes(); defenders.ResetHeroes(); #endregion turns = attackers.Units.Count.BiggerThan(defenders.Units.Count)? attackers.Units.Count : defenders.Units.Count; generate.HeroesPowers(turns, attackers, defenders); i++; } DateTime finishTime = DateTime.Now; TimeSpan ts = finishTime.Subtract(startTime); ViewData["TimeSpent"] = ts; ViewData["Rounds"] = rounds; return View(); }
public void RoundReport(int turn, Attackers attackers, Defenders defenders) { Combat.Log.Add("<hr>"); Combat.Log.Add(string.Format("<a name='turno{0}'> </a><h2>Obituário do round {0}</h2>", turn)); Combat.Log.Add("<div class='attacker'>Atacantes</div>"); var attackersDied = (from atk in attackers.cemitery orderby atk.Type group atk by atk.Type); foreach (var group in attackersDied) Combat.Log.Add(string.Format("<b>Tipo {1}:</b> {0}", group.Count<Unit>(), group.Key)); Combat.Log.Add("<br />"); Combat.Log.Add("<div class='defender'>Defensores</div>"); var defendersDied = (from dfr in defenders.cemitery orderby dfr.Type group dfr by dfr.Type); foreach (var group in defendersDied) Combat.Log.Add(string.Format("<b>Tipo {1}:</b> {0}", group.Count<Unit>(), group.Key)); Combat.Log.Add("<hr>"); }
//private static void round_OnRoundExecution() //{ // Round.IsRunning = true; // System.Threading.Thread.Sleep(20000); // Round.IsRunning = false; //} private void round_OnRoundExecution() { Round.IsRunning = true; Game.Log.Clear(); World davi = World.Load(1); World golias = World.Load(2); Attackers attackers = new Attackers(); Defenders defenders = new Defenders(); int number = 1; attackers.Units = new List<Unit>(); Unit unit = null; Unit baseUnit = null; foreach (WorldUnits worldUnit in davi.Units) { baseUnit = Unit.Find(worldUnit.UnitId); if (baseUnit != null) { for (int j = 0; j < worldUnit.Quantity; j++) { unit = (Unit)baseUnit.Clone(); unit.Number = number; unit.PositionInCombat = Unit.CombatPosition.attacker; attackers.Units.Add(unit); number += 1; } } } number = 1; defenders.Units = new List<Unit>(); unit = null; baseUnit = null; foreach (WorldUnits worldUnit in golias.Units) { baseUnit = Unit.Find(worldUnit.UnitId); if (baseUnit != null) { for (int j = 0; j < worldUnit.Quantity; j++) { unit = (Unit)baseUnit.Clone(); unit.Number = number; unit.PositionInCombat = Unit.CombatPosition.defender; defenders.Units.Add(unit); number += 1; } } } attackers.Units = Util.ScrambleList<Unit>(attackers.Units); defenders.Units = Util.ScrambleList<Unit>(defenders.Units); Hero hero = null; attackers.Heroes = new List<Hero>(); hero = Hero.Find(davi.HeroId); hero.SetPowers(); attackers.Heroes.Add(hero); defenders.Heroes = new List<Hero>(); hero = Hero.Find(golias.HeroId); hero.SetPowers(); defenders.Heroes.Add(hero); int turns = attackers.Units.Count.BiggerThan(defenders.Units.Count) ? attackers.Units.Count : defenders.Units.Count; int rounds = 1; //round will be always one! refactor later HeroesPowers(turns, attackers, defenders); int i = 1; while (i <= rounds && defenders.Units.Count + defenders.Heroes.Count > 0 && attackers.Units.Count + attackers.Heroes.Count > 0) { Game.Log.Add(string.Format("<h2>Round {0}</h2><p>Atacando: {1} unidades</p><p>Defendendo: {2} unidades</p>", i, attackers.Units.Count, defenders.Units.Count)); Game.StartCombat(turns, attackers, defenders); #region LOG Game.Log.Add(attackers.PV()); Game.Log.Add(defenders.PV()); RoundReport(i, attackers, defenders); #endregion //#region RESET //generate.RoundResetUnitLives(attackers.Units); //generate.RoundResetUnitLives(defenders.Units); //attackers.ResetHeroes(); //defenders.ResetHeroes(); //#endregion turns = attackers.Units.Count.BiggerThan(defenders.Units.Count) ? attackers.Units.Count : defenders.Units.Count; HeroesPowers(turns, attackers, defenders); i++; } Round.IsRunning = false; CombatLog l = new CombatLog() { Log = string.Join("<br/>", Game.Log.ToArray()), CombatId = 1 }; l.Save(); }
public static void StartCombat(int turns, Attackers attackers, Defenders defenders) { attackers.ResetCemitery(); defenders.ResetCemitery(); if (defenders.Units.Count > 0 && attackers.Units.Count > 0) { Unit defender = null; Unit attacker = null; int i = 1; while (i < turns && defenders.Units.Count + defenders.Heroes.Count > 0 && attackers.Units.Count + attackers.Heroes.Count > 0) { HeroesAction(i, attackers, defenders); if (defenders.Units.Count > 0 && attackers.Units.Count > 0) { attacker = attackers.CurrentUnit; defender = defenders.CurrentUnit; attackers.CurrentUnit.Fight(defender); defenders.Units.RemoveAt(0); attackers.Units.RemoveAt(0); if (defender.Life > 0) defenders.Units.Add(defender); else defenders.BuryUnit(defender); if (attacker.Life > 0) attackers.Units.Add(attacker); else attackers.BuryUnit(attacker); Combat.Log.Add("<br />"); } i++; } } }