private List<StateNode> CreateStates(IEnumerable<Action> before, bool getsPriority)
        {
            var nodes = new List<StateNode>();

              StateNode startOfStep = null,
                priorityActiveStart = null,
                priorityPassiveStart = null,
                priorityActive = null,
                priorityPassive = null,
                priorityAfter = null,
                priorityBeginResolve = null,
                priorityFinishResolve = null,
                pushTriggeredActive = null,
                pushTriggeredPassive = null;

              if (getsPriority)
              {
            priorityActiveStart = new StateNode(
              100,
              () => Enqueue(new PlaySpellOrAbility(Players.Active)),
              () =>
            {
              if (WasPriorityPassed)
              {
                return priorityPassive;
              }

              return pushTriggeredActive;
            }
              );

            nodes.Add(priorityActiveStart);

            priorityActive = new StateNode(
              101,
              () => Enqueue(new PlaySpellOrAbility(Players.Active)),
              () =>
            {
              if (WasPriorityPassed)
              {
                return priorityBeginResolve;
              }

              return pushTriggeredActive;
            });

            nodes.Add(priorityActive);

            priorityPassive = new StateNode(
              102,
              () => Enqueue(new PlaySpellOrAbility(Players.Passive)),
              () =>
            {
              if (WasPriorityPassed)
              {
                return Stack.IsEmpty
                  ? priorityAfter
                  : priorityBeginResolve;
              }

              return pushTriggeredPassive;
            }
              );

            nodes.Add(priorityPassive);

            priorityPassiveStart = new StateNode(
              103,
              () => Enqueue(new PlaySpellOrAbility(Players.Passive)),
              () =>
            {
              if (WasPriorityPassed)
              {
                return priorityActive;
              }

              return pushTriggeredPassive;
            }
              );

            nodes.Add(priorityPassiveStart);

            priorityBeginResolve = new StateNode(
              104,
              () => Stack.BeginResolve(),
              () => priorityFinishResolve);

            nodes.Add(priorityBeginResolve);

            priorityFinishResolve = new StateNode(
              105,
              () => Stack.FinishResolve(),
              () =>
            {
              // this executes after all decisions are processed
              // e.g after creature type for engineered plague has been
              // selected

              Players.MoveDeadCreaturesToGraveyard();
              return pushTriggeredActive;
            });

            nodes.Add(priorityFinishResolve);
              }

              priorityAfter = new StateNode(
            106,
            () =>
              {
            Players.Player1.EmptyManaPool();
            Players.Player2.EmptyManaPool();

            Publish(new StepFinishedEvent(Turn.Step));
              },
            () => null);

              nodes.Add(priorityAfter);

              pushTriggeredActive = new StateNode(
              107,
              () => Stack.PushTriggered(),
              () =>
              {
            if (Stack.HasTriggered)
            {
              return pushTriggeredActive;
            }

            return priorityActiveStart;
              });

              nodes.Add(pushTriggeredActive);

              pushTriggeredPassive = new StateNode(
              108,
              () => Stack.PushTriggered(),
              () =>
              {
            if (Stack.HasTriggered)
            {
              return pushTriggeredPassive;
            }

            return priorityPassiveStart;
              });

              nodes.Add(pushTriggeredPassive);

              var nextNode = getsPriority ? pushTriggeredActive : priorityAfter;

              // create and connect step custom statenodes
              var id = 1;
              foreach (var action in before.Reverse())
              {
            var currentNextNode = nextNode;
            nextNode = new StateNode(id, action, () => currentNextNode);

            nodes.Add(nextNode);
            id++;
              }

              startOfStep = new StateNode(
            0,
            () => Publish(new StepStartedEvent(Turn.Step)),
            () => nextNode);

              nodes.Add(startOfStep);

              return nodes;
        }
Exemple #2
0
        private List <StateNode> CreateStates(IEnumerable <Action> before, bool getsPriority)
        {
            var nodes = new List <StateNode>();

            StateNode startOfStep           = null,
                      priorityActiveStart   = null,
                      priorityPassiveStart  = null,
                      priorityActive        = null,
                      priorityPassive       = null,
                      priorityAfter         = null,
                      priorityBeginResolve  = null,
                      priorityFinishResolve = null,
                      pushTriggeredActive   = null,
                      pushTriggeredPassive  = null;


            if (getsPriority)
            {
                priorityActiveStart = new StateNode(
                    100,
                    () => Enqueue(new PlaySpellOrAbility(Players.Active)),
                    () =>
                {
                    if (WasPriorityPassed)
                    {
                        return(priorityPassive);
                    }

                    return(pushTriggeredActive);
                }
                    );

                nodes.Add(priorityActiveStart);

                priorityActive = new StateNode(
                    101,
                    () => Enqueue(new PlaySpellOrAbility(Players.Active)),
                    () =>
                {
                    if (WasPriorityPassed)
                    {
                        return(priorityBeginResolve);
                    }

                    return(pushTriggeredActive);
                });

                nodes.Add(priorityActive);

                priorityPassive = new StateNode(
                    102,
                    () => Enqueue(new PlaySpellOrAbility(Players.Passive)),
                    () =>
                {
                    if (WasPriorityPassed)
                    {
                        return(Stack.IsEmpty
                  ? priorityAfter
                  : priorityBeginResolve);
                    }

                    return(pushTriggeredPassive);
                }
                    );

                nodes.Add(priorityPassive);

                priorityPassiveStart = new StateNode(
                    103,
                    () => Enqueue(new PlaySpellOrAbility(Players.Passive)),
                    () =>
                {
                    if (WasPriorityPassed)
                    {
                        return(priorityActive);
                    }

                    return(pushTriggeredPassive);
                }
                    );

                nodes.Add(priorityPassiveStart);


                priorityBeginResolve = new StateNode(
                    104,
                    () => Stack.BeginResolve(),
                    () => priorityFinishResolve);

                nodes.Add(priorityBeginResolve);

                priorityFinishResolve = new StateNode(
                    105,
                    () => Stack.FinishResolve(),
                    () =>
                {
                    // this executes after all decisions are processed
                    // e.g after creature type for engineered plague has been
                    // selected

                    Players.MoveDeadCreaturesToGraveyard();
                    return(pushTriggeredActive);
                });

                nodes.Add(priorityFinishResolve);
            }

            priorityAfter = new StateNode(
                106,
                () =>
            {
                Players.Player1.EmptyManaPool();
                Players.Player2.EmptyManaPool();

                Publish(new StepFinishedEvent(Turn.Step));
            },
                () => null);

            nodes.Add(priorityAfter);

            pushTriggeredActive = new StateNode(
                107,
                () => Stack.PushTriggered(),
                () =>
            {
                if (Stack.HasTriggered)
                {
                    return(pushTriggeredActive);
                }

                return(priorityActiveStart);
            });

            nodes.Add(pushTriggeredActive);

            pushTriggeredPassive = new StateNode(
                108,
                () => Stack.PushTriggered(),
                () =>
            {
                if (Stack.HasTriggered)
                {
                    return(pushTriggeredPassive);
                }

                return(priorityPassiveStart);
            });

            nodes.Add(pushTriggeredPassive);

            var nextNode = getsPriority ? pushTriggeredActive : priorityAfter;

            // create and connect step custom statenodes
            var id = 1;

            foreach (var action in before.Reverse())
            {
                var currentNextNode = nextNode;
                nextNode = new StateNode(id, action, () => currentNextNode);

                nodes.Add(nextNode);
                id++;
            }

            startOfStep = new StateNode(
                0,
                () => Publish(new StepStartedEvent(Turn.Step)),
                () => nextNode);

            nodes.Add(startOfStep);

            return(nodes);
        }