Example #1
0
        public virtual void Calculate(Battle battle, Random random)
        {
            BattleWaves waves = SplitByWaves(battle);

            for (int j = 0; j < IterationCount; j++)
            {
                BattleComplete(battle, BattleHelper.Instance.CalculateBattle2(battle, waves, random));
            }
        }
Example #2
0
        public virtual void Calculate(Battle battle, double targetAttackerChance, double targetDefenderChance)
        {
            BattleWaves waves = SplitByWaves(battle);

            MinMaxBattleHelper battleHelper = new MinMaxBattleHelper();

            battleHelper.TargetAttackerChance = targetAttackerChance;
            battleHelper.TargetDefenderChance = targetDefenderChance;
            BattleComplete(battle, battleHelper.CalculateBattle2(battle, waves, new Random()));
        }
Example #3
0
        internal static BattleWaves SplitByWaves(Battle battle)
        {
            BattleWaves result = new BattleWaves();
            int         i      = 0;

            foreach (Unit unit in battle.Units)
            {
                switch (unit.AttackPriority)
                {
                case AttackPriority.AvantGarde:
                    result.Avantgard.Add(i);
                    break;

                case AttackPriority.Normal:
                    result.Normal.Add(i);
                    break;

                case AttackPriority.RearGuard:
                    result.RearGuard.Add(i);
                    break;

                default:
                    break;
                }
                i++;
            }

            i = 0;
            foreach (Unit unit in battle.EnemyUnits)
            {
                switch (unit.AttackPriority)
                {
                case AttackPriority.AvantGarde:
                    result.EnemyAvantgard.Add(i);
                    break;

                case AttackPriority.Normal:
                    result.EnemyNormal.Add(i);
                    break;

                case AttackPriority.RearGuard:
                    result.EnemyRearGuard.Add(i);
                    break;

                default:
                    break;
                }
                i++;
            }

            return(result);
        }
 public ThreadParameter(
     int index,
     int iterationCount,
     Battle battle,
     BattleWaves battleWaves,
     Random random)
 {
     m_index          = index;
     m_battleWaves    = battleWaves;
     m_battle         = battle;
     m_iterationCount = iterationCount;
     m_random         = random;
 }
        public override void Calculate(Battle battle, Random random)
        {
            BattleWaves battleWaves = SplitByWaves(battle);

            int threadCounts = Environment.ProcessorCount;

            m_battles = new Queue <List <BattleStep> >(IterationCount / threadCounts);
            m_threads = new Thread[threadCounts];
            m_events  = new ManualResetEvent[threadCounts];
            for (int i = 0; i < m_events.Length; i++)
            {
                m_events[i]  = new ManualResetEvent(false);
                m_threads[i] = new Thread(ThreadMethod);
                m_threads[i].Start(new ThreadParameter(
                                       i,
                                       IterationCount / threadCounts,
                                       battle,
                                       battleWaves,
                                       new Random(random.Next())));
            }

            while (true)
            {
                while (true)
                {
                    List <BattleStep> result;
                    lock (m_battles)
                    {
                        if (m_battles.Count > 0)
                        {
                            result = m_battles.Dequeue();
                        }
                        else
                        {
                            break;
                        }
                    }

                    BattleComplete(battle, result);
                }

                bool threadStopped = WaitHandle.WaitAll(m_events, 50);
                if (threadStopped)
                {
                    if (m_battles.Count == 0)
                    {
                        return;
                    }
                }
            }
        }
        internal List <BattleStep> CalculateBattle2(
            Battle battle,
            BattleWaves waves,
            Random random,
            RoundStateHandler roundStateHandler,
            UnitAttackHandler unitAttackHandler)
        {
            List <BattleStep> steps = new List <BattleStep>();

            steps.Add(new BattleStep(battle));
            int     roundIndex       = 0;
            General avantgardGeneral = null;
            General general          = null;
            General rearguardGeneral = null;

            if (battle.General != null)
            {
                switch (battle.General.AttackPriority)
                {
                case AttackPriority.AvantGarde:
                    avantgardGeneral = battle.General;
                    break;

                case AttackPriority.Normal:
                    general = battle.General;
                    break;

                case AttackPriority.RearGuard:
                    rearguardGeneral = battle.General;
                    break;
                }
            }

            General avantgardEnemyGeneral = null;
            General enemyGeneral          = null;
            General rearguardEnemyGeneral = null;

            if (battle.EnemyGeneral != null)
            {
                switch (battle.EnemyGeneral.AttackPriority)
                {
                case AttackPriority.AvantGarde:
                    avantgardEnemyGeneral = battle.EnemyGeneral;
                    break;

                case AttackPriority.Normal:
                    enemyGeneral = battle.EnemyGeneral;
                    break;

                case AttackPriority.RearGuard:
                    rearguardEnemyGeneral = battle.EnemyGeneral;
                    break;
                }
            }

            while (true)
            {
                if (!Attack2(battle, waves.Avantgard, waves.EnemyAvantgard, steps, avantgardGeneral, avantgardEnemyGeneral, random, unitAttackHandler))
                {
                    break;
                }

                if (roundStateHandler != null)
                {
                    roundStateHandler(null, new RoundStateArgs(steps[steps.Count - 1], roundIndex++));
                }

                if (!Attack2(battle, waves.Normal, waves.EnemyNormal, steps, general, enemyGeneral, random, unitAttackHandler))
                {
                    break;
                }

                if (roundStateHandler != null)
                {
                    roundStateHandler(null, new RoundStateArgs(steps[steps.Count - 1], roundIndex++));
                }

                if (!Attack2(battle, waves.RearGuard, waves.EnemyRearGuard, steps, rearguardGeneral, rearguardEnemyGeneral, random, unitAttackHandler))
                {
                    break;
                }

                if (roundStateHandler != null)
                {
                    roundStateHandler(null, new RoundStateArgs(steps[steps.Count - 1], roundIndex++));
                }
            }

            if (roundStateHandler != null)
            {
                roundStateHandler(null, new RoundStateArgs(steps[steps.Count - 1], roundIndex));
                roundStateHandler(null, new RoundStateArgs(steps[steps.Count - 1], -1));
            }

            return(steps);
        }
 internal List <BattleStep> CalculateBattle2(Battle battle, BattleWaves waves, Random random)
 {
     return(CalculateBattle2(battle, waves, random, null, null));
 }