예제 #1
0
        public void Update(MTG mtg)
        {
            BoardInfo info = new BoardInfo();

            info.Import(mtg);

            BoardViewerController <BoardInfo> .Update(info);
        }
예제 #2
0
            public void Import(MTG mtg)
            {
                objects = new Dictionary <string, ObjectInfo>();
                foreach (var kvp in mtg.objects)
                {
                    var obj = new ObjectInfo();
                    obj.Import(kvp.Value);
                    objects.Add(kvp.Key.GuidString, obj);
                }

                players = new List <PlayerInfo>();
                foreach (Player player in mtg.players)
                {
                    var info = new PlayerInfo();
                    info.Import(player);
                    players.Add(info);
                }

                battlefield = GetZoneInfo(mtg.battlefield);
                theStack    = GetZoneInfo(mtg.theStack);
                exile       = GetZoneInfo(mtg.exile);
            }
예제 #3
0
        static void Main(string[] args)
        {
            var ogre = new MTGLib.MTGObject.BaseCardAttributes()
            {
                name     = "Onakke Ogre",
                manaCost = new ManaCost(
                    2, ManaSymbol.Red
                    ),
                power     = 4,
                toughness = 2,
                cardTypes = new HashSet <MTGLib.MTGObject.CardType> {
                    MTGLib.MTGObject.CardType.Creature
                },
                subTypes = new HashSet <MTGLib.MTGObject.SubType> {
                    MTGLib.MTGObject.SubType.Ogre, MTGLib.MTGObject.SubType.Warrior
                }
            };

            var knight = new MTGObject.BaseCardAttributes()
            {
                name     = "Fireborn Knight",
                manaCost = new ManaCost(
                    ManaSymbol.HybridBoros,
                    ManaSymbol.HybridBoros,
                    ManaSymbol.HybridBoros,
                    ManaSymbol.HybridBoros
                    ),
                power     = 2,
                toughness = 3,
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Creature
                },
                subTypes = new HashSet <MTGObject.SubType> {
                    MTGObject.SubType.Human, MTGObject.SubType.Knight
                },
                // double strike goes here
                activatedAbilities = new List <ActivatedAbility>
                {
                    new ActivatedAbility(
                        new CostEvent.CostGen[]
                    {
                        () => { return(EventContainerPayManaCost.Auto(new ManaCost(
                                                                          ManaSymbol.HybridBoros,
                                                                          ManaSymbol.HybridBoros,
                                                                          ManaSymbol.HybridBoros,
                                                                          ManaSymbol.HybridBoros
                                                                          ))); }
                    },
                        new EffectEvent.Effect[]
                    {
                        (source, targets, callback) =>
                        {
                            MTG mtg_ = MTG.Instance;

                            mtg_.continuousEffects.Add(
                                new ContinuousEffect(
                                    source,
                                    ContinuousEffect.Duration.EndOfTurn,
                                    new ContinuousEffect.DurationData
                            {
                                turn = mtg_.turn.turnCount
                            },
                                    new Modification[]
                            {
                                new PowerMod
                                {
                                    value       = 1,
                                    operation   = Modification.Operation.Add,
                                    specificOID = source
                                },
                                new ToughnessMod
                                {
                                    value       = 1,
                                    operation   = Modification.Operation.Add,
                                    specificOID = source
                                }
                            }
                                    )
                                );
                        }
                    }
                        )
                }
            };

            var landfall = new MTGObject.BaseCardAttributes()
            {
                name      = "Landfall Enchantment",
                manaCost  = new ManaCost(),
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Enchantment
                },
                triggeredAbilities = new List <TriggeredAbility>
                {
                    new TriggeredAbility <MoveZoneEvent>(
                        (source, mtgevent) =>
                    {
                        if (mtgevent.newZone != MTG.Instance.battlefield)
                        {
                            return(false);
                        }
                        var trigobj   = MTG.Instance.objects[mtgevent.oid];
                        var sourceobj = MTG.Instance.objects[source];
                        return(trigobj.attr.controller == sourceobj.attr.controller);
                    },
                        new EffectEvent.Effect[]
                    {
                        (source, targets, callback) =>
                        {
                            var damage = 1;
                            var target = targets[0].SetTargets[0];
                            callback(new DealDamageEvent(source, target, damage));
                        }
                    },
                        new Target[] { Target.AnyTarget }
                        )
                }
            };

            var island = new MTGLib.MTGObject.BaseCardAttributes()
            {
                name      = "Island",
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Land
                },
                superTypes = new HashSet <MTGObject.SuperType> {
                    MTGObject.SuperType.Basic
                },
                subTypes = new HashSet <MTGObject.SubType> {
                    MTGObject.SubType.Island
                },
                activatedAbilities = new List <ActivatedAbility>
                {
                    new ManaAbility(
                        new CostEvent.CostGen[]
                    {
                        () => { return(new TapSelfCostEvent()); }
                    },
                        new EffectEvent.Effect[] {
                        (source, targets, callback) => {
                            int controller = MTG.Instance.objects[source].attr.controller;
                            callback(EventContainerAddMana.Auto(source, controller, ManaSymbol.Blue));
                        }
                    }
                        )
                }
            };
            var mountain = new MTGLib.MTGObject.BaseCardAttributes()
            {
                name      = "Mountain",
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Land
                },
                superTypes = new HashSet <MTGObject.SuperType> {
                    MTGObject.SuperType.Basic
                },
                subTypes = new HashSet <MTGObject.SubType> {
                    MTGObject.SubType.Mountain
                },
                activatedAbilities = new List <ActivatedAbility>
                {
                    new ManaAbility(
                        new CostEvent.CostGen[]
                    {
                        () => { return(new TapSelfCostEvent()); }
                    },
                        new EffectEvent.Effect[] {
                        (source, targets, callback) => {
                            int controller = MTG.Instance.objects[source].attr.controller;
                            callback(EventContainerAddMana.Auto(source, controller, ManaSymbol.Red));
                        }
                    }
                        )
                }
            };

            var crab = new MTGLib.MTGObject.BaseCardAttributes()
            {
                name     = "Wishcoin Crab",
                manaCost = new ManaCost(
                    3, ManaSymbol.Blue
                    ),
                power     = 2,
                toughness = 5,
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGLib.MTGObject.CardType.Creature
                },
                subTypes = new HashSet <MTGObject.SubType> {
                    MTGLib.MTGObject.SubType.Crab
                }
            };

            var izzetSignet = new MTGObject.BaseCardAttributes()
            {
                name      = "Izzet Signet",
                manaCost  = new ManaCost(2),
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Artifact
                },
                activatedAbilities = new List <ActivatedAbility>
                {
                    new ManaAbility(
                        new CostEvent.CostGen[]
                    {
                        () => { return(new TapSelfCostEvent()); },
                        () => { return(EventContainerPayManaCost.Auto(new ManaCost(1))); }
                    },
                        new EffectEvent.Effect[]
                    {
                        (source, targets, callback) =>
                        {
                            int controller = MTG.Instance.objects[source].attr.controller;
                            callback(EventContainerAddMana.Auto(source, controller, ManaSymbol.Red, ManaSymbol.Blue));
                        }
                    }
                        )
                }
            };

            var bolt = new MTGObject.BaseCardAttributes()
            {
                name      = "Lightning Bolt",
                manaCost  = new ManaCost(ManaSymbol.Red),
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Instant
                },
                spellAbilities = new List <ResolutionAbility>
                {
                    new ResolutionAbility(
                        new EffectEvent.Effect[]
                    {
                        (source, targets, callback) =>
                        {
                            var damage = 3;
                            var target = targets[0].SetTargets[0];
                            callback(new DealDamageEvent(source, target, damage));
                        }
                    },
                        new Target[] { Target.AnyTarget }
                        )
                }
            };

            var lib1 = new List <MTGLib.MTGObject.BaseCardAttributes>();
            var lib2 = new List <MTGLib.MTGObject.BaseCardAttributes>();

            for (int i = 0; i < 26; i++)
            {
                lib1.Add(mountain);
                lib2.Add(island);
            }

            for (int i = 0; i < 17; i++)
            {
                lib1.Add(ogre);
                lib1.Add(landfall);
                lib2.Add(crab);
                lib2.Add(izzetSignet);
            }

            var mtg = new MTG(lib1, lib2);

            mtg.Start();

            BoardViewer boardViewer       = new BoardViewer();
            Thread      boardViewerThread = new Thread(boardViewer.Run);

            boardViewerThread.Name = "Board Viewer Thread";
            boardViewerThread.Start();
            boardViewer.Update(mtg);

            Thread gameLoopThread = new Thread(mtg.GameLoop);

            gameLoopThread.Name = "Game Loop Thread";
            gameLoopThread.Start();

            while (true)
            {
                mtg.ChoiceNewEvent.WaitOne();
                boardViewer.Update(mtg);

                Choice choice = mtg.CurrentUnresolvedChoice;

                if (choice is PriorityChoice cast)
                {
                    if (cast.Options.Count == 1)
                    {
                        Console.WriteLine("Single choice - autoresolving.");
                        cast.Resolve(new List <PriorityOption>(cast.Options));
                    }
                }

                if (!choice.Resolved)
                {
                    choice.ConsoleResolve();
                }

                mtg.ResolveChoice(choice);
            }
        }
예제 #4
0
        public void ReverseCastTest()
        {
            var mountain = new MTGLib.MTGObject.BaseCardAttributes
            {
                name      = "Mountain",
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Land
                },
                superTypes = new HashSet <MTGObject.SuperType> {
                    MTGObject.SuperType.Basic
                },
                subTypes = new HashSet <MTGObject.SubType> {
                    MTGObject.SubType.Mountain
                },
                activatedAbilities = new List <ActivatedAbility>
                {
                    new ManaAbility(
                        new CostEvent.CostGen[]
                    {
                        () => { return(new TapSelfCostEvent()); }
                    },
                        new EffectEvent.Effect[] {
                        (source, targets, callback) => {
                            int controller = MTG.Instance.objects[source].attr.controller;
                            callback(EventContainerAddMana.Auto(source, controller, ManaSymbol.Red));
                        }
                    }
                        )
                }
            };

            var weirdartifact = new MTGLib.MTGObject.BaseCardAttributes
            {
                name      = "Weird",
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Artifact
                },
                activatedAbilities = new List <ActivatedAbility>
                {
                    new ManaAbility(
                        new CostEvent.CostGen[]
                    {
                        () => { return(new TapSelfCostEvent()); },
                        () => { return(EventContainerPayManaCost.Auto(new ManaCost(1))); }
                    },
                        new EffectEvent.Effect[]
                    {
                        (source, targets, callback) =>
                        {
                            int controller = MTG.Instance.objects[source].attr.controller;
                            var mana       = new ManaCost(3);
                            callback(EventContainerAddMana.Auto(source, controller, mana));
                            callback(EventContainerDrawCards.Auto(source, controller, 2));
                        }
                    }
                        )
                }
            };

            var megacreature = new MTGLib.MTGObject.BaseCardAttributes
            {
                name      = "Mega Creature",
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Creature
                },
                power     = 20,
                toughness = 20,
                manaCost  = new ManaCost(10)
            };

            var lib = new List <MTGObject.BaseCardAttributes>();

            for (int x = 0; x < 15; x++)
            {
                lib.Add(mountain);
                lib.Add(weirdartifact);
                lib.Add(megacreature);
            }

            var mtg = new MTG(lib, lib);

            var mountain1OID = mtg.DebugTutor("Mountain", 0, mtg.battlefield);
            var mountain2OID = mtg.DebugTutor("Mountain", 0, mtg.battlefield);
            var weirdOID     = mtg.DebugTutor("Weird", 0, mtg.battlefield);
            var creatureOID  = mtg.DebugTutor("Mega Creature", 0, mtg.players[0].hand);

            mtg.Start();

            Thread gameLoopThread = new Thread(mtg.GameLoop);

            gameLoopThread.Start();

            var pass = new PriorityOption {
                type = PriorityOption.OptionType.PassPriority
            };

            mtg.ChoiceNewEvent.WaitOne();

            Choice choice;

            // Pass until first main
            {
                while (mtg.turn.phase.type != Phase.PhaseType.Main1)
                {
                    choice = mtg.CurrentUnresolvedChoice;
                    if (choice is PriorityChoice cast)
                    {
                        cast.Resolve(pass);
                    }
                    else
                    {
                        Assert.Fail();
                    }
                    mtg.ResolveChoice(choice);
                    mtg.ChoiceNewEvent.WaitOne();
                }
                Assert.AreEqual(mtg.turn.phase.type, Phase.PhaseType.Main1);
            }

            // main1 priority choice - cast spell
            {
                choice = mtg.CurrentUnresolvedChoice;

                if (choice is PriorityChoice cast)
                {
                    foreach (var option in cast.Options)
                    {
                        if (option.type == PriorityOption.OptionType.CastSpell &&
                            option.source == creatureOID)
                        {
                            cast.Resolve(option);
                            break;
                        }
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();

            // Mana payment choice {1} for cast - activate weird
            {
                choice = mtg.CurrentUnresolvedChoice;

                if (choice is ManaChoice cast)
                {
                    foreach (var option in cast.Options)
                    {
                        if (option.type == ManaChoiceOption.OptionType.ActivateManaAbility &&
                            option.manaAbilitySource == weirdOID)
                        {
                            cast.Resolve(option);
                            break;
                        }
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();

            // Mana payment choice {1} for weird artifact - activate mountain 1
            {
                choice = mtg.CurrentUnresolvedChoice;

                if (choice is ManaChoice cast)
                {
                    foreach (var option in cast.Options)
                    {
                        if (option.type == ManaChoiceOption.OptionType.ActivateManaAbility &&
                            option.manaAbilitySource == mountain1OID)
                        {
                            cast.Resolve(option);
                        }
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();

            // Mana payment choice {1} for weird artifact - use {R}
            {
                choice = mtg.CurrentUnresolvedChoice;

                if (choice is ManaChoice cast)
                {
                    foreach (var option in cast.Options)
                    {
                        if (option.type == ManaChoiceOption.OptionType.UseMana &&
                            option.manaSymbol == ManaSymbol.Red)
                        {
                            cast.Resolve(option);
                        }
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            int cardcount = mtg.players[0].hand.Count;

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();

            // Weird artifact resolves and draws cards
            Assert.IsTrue(mtg.objects[weirdOID].permanentStatus.tapped);
            Assert.IsTrue(mtg.objects[mountain1OID].permanentStatus.tapped);
            Assert.IsFalse(mtg.objects[mountain2OID].permanentStatus.tapped);

            Assert.AreEqual(mtg.players[0].hand.Count, cardcount + 2);

            // Mana paymanet choice {1} - use mana from weird
            {
                choice = mtg.CurrentUnresolvedChoice;
                if (choice is ManaChoice cast)
                {
                    foreach (var option in cast.Options)
                    {
                        if (option.type == ManaChoiceOption.OptionType.UseMana &&
                            option.manaSymbol == ManaSymbol.Generic)
                        {
                            cast.Resolve(option);
                            break;
                        }
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();

            Assert.IsTrue(mtg.objects[weirdOID].permanentStatus.tapped);
            Assert.IsTrue(mtg.objects[mountain1OID].permanentStatus.tapped);
            Assert.IsFalse(mtg.objects[mountain2OID].permanentStatus.tapped);
            Assert.AreEqual(mtg.players[0].manaPool.Count, 2);

            // *NEW* Mana payment choice {1} - activate mountain 2
            {
                choice = mtg.CurrentUnresolvedChoice;
                if (choice is ManaChoice cast)
                {
                    foreach (var option in cast.Options)
                    {
                        if (option.type == ManaChoiceOption.OptionType.ActivateManaAbility &&
                            option.manaAbilitySource == mountain2OID)
                        {
                            cast.Resolve(option);
                        }
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();

            // Mana payment choice {1} - use {R}
            {
                choice = mtg.CurrentUnresolvedChoice;
                if (choice is ManaChoice cast)
                {
                    foreach (var option in cast.Options)
                    {
                        if (option.type == ManaChoiceOption.OptionType.UseMana &&
                            option.manaSymbol == ManaSymbol.Red)
                        {
                            cast.Resolve(option);
                            break;
                        }
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();

            Assert.IsTrue(mtg.objects[weirdOID].permanentStatus.tapped);
            Assert.IsTrue(mtg.objects[mountain1OID].permanentStatus.tapped);
            Assert.IsTrue(mtg.objects[mountain2OID].permanentStatus.tapped);
            Assert.AreEqual(mtg.players[0].hand.Count, cardcount + 2);

            // Mana payment for {1} - cancel
            {
                choice = mtg.CurrentUnresolvedChoice;

                if (choice is ManaChoice cast)
                {
                    cast.Cancel();
                }
                else
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(choice.Resolved);
            mtg.ResolveChoice(choice);
            mtg.ChoiceNewEvent.WaitOne();


            // Mountain 2 gets reverted
            Assert.IsFalse(mtg.objects[mountain2OID].permanentStatus.tapped);

            // Weird oid is not reverted - it drew cards
            Assert.IsTrue(mtg.objects[weirdOID].permanentStatus.tapped);
            Assert.IsTrue(mtg.objects[mountain1OID].permanentStatus.tapped);
            // The card draw isn't reverted, and the spell is returned to hand
            Assert.AreEqual(mtg.players[0].hand.Count, cardcount + 3);

            // We still have 3 mana
            Assert.AreEqual(mtg.players[0].manaPool.Count, 3);
        }
예제 #5
0
        public void TestMTG()
        {
            var mtg = new MTG();

            Assert.ReferenceEquals(MTG.Instance, mtg);
        }
예제 #6
0
        public void TestModifications()
        {
            var       mtg     = new MTG();
            MTGObject testobj = new MTGObject(new MTGObject.BaseCardAttributes
            {
                name      = "Test Creature",
                cardTypes = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Creature
                },
                manaCost  = new ManaCost(2, ManaSymbol.Blue, ManaSymbol.Red),
                power     = 1,
                toughness = 1
            });;

            mtg.CreateObject(testobj);
            mtg.CalculateBoardState();
            Assert.AreEqual(testobj.attr.power, 1);
            Assert.AreEqual(testobj.attr.toughness, 1);

            var contEffect = new ContinuousEffect(null, ContinuousEffect.Duration.Infinite);

            // All creatures get +2/+0
            contEffect.AddModification(
                new PowerMod {
                value     = 2, operation = Modification.Operation.Add,
                condition = (obj) =>
                {
                    if (!obj.attr.cardTypes.Contains(MTGObject.CardType.Creature))
                    {
                        return(false);
                    }
                    return(true);
                }
            }
                );
            // All creatures get -0/-1
            contEffect.AddModification(
                new ToughnessMod
            {
                value     = 1,
                operation = Modification.Operation.Subtract,
                condition = (obj) =>
                {
                    if (!obj.attr.cardTypes.Contains(MTGObject.CardType.Creature))
                    {
                        return(false);
                    }
                    return(true);
                }
            }
                );

            mtg.continuousEffects.Add(contEffect);
            mtg.CalculateBoardState();
            Assert.AreEqual(testobj.attr.power, 3);
            Assert.AreEqual(testobj.attr.toughness, 0);

            // All blue creatures are green
            contEffect = new ContinuousEffect(null, ContinuousEffect.Duration.Infinite);
            contEffect.AddModification(
                new ColorMod
            {
                value     = Color.Green,
                operation = Modification.Operation.Override,
                condition = (obj) =>
                {
                    if (!obj.attr.cardTypes.Contains(MTGObject.CardType.Creature))
                    {
                        return(false);
                    }
                    if (!obj.attr.color.HasColor(Color.Blue))
                    {
                        return(false);
                    }
                    return(true);
                }
            }
                );
            mtg.continuousEffects.Clear();
            mtg.continuousEffects.Add(contEffect);
            mtg.CalculateBoardState();
            Assert.AreEqual(testobj.identity, Color.Green);

            // All creatures are artifacts
            contEffect.AddModification(
                new CardTypeMod
            {
                value = new HashSet <MTGObject.CardType> {
                    MTGObject.CardType.Artifact
                },
                operation = Modification.Operation.Override
            }
                );
            mtg.continuousEffects.Clear();
            mtg.continuousEffects.Add(contEffect);

            // Type is before color, so the color mod should fail as
            // the object is now an artifact
            mtg.CalculateBoardState();
            Assert.AreEqual(testobj.identity, Color.Izzet);
        }