Example #1
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            GlobalRules.Perform<PossibleMatch, Actor>("after acting")
                .Do((match, actor) =>
                {
                    if (actor.GetProperty<MudObject>("active-quest") != null)
                    {
                        var quest = actor.GetProperty<MudObject>("active-quest");

                        if (GlobalRules.ConsiderValueRule<bool>("quest complete?", actor, quest))
                        {
                            actor.RemoveProperty("active-quest");
                            GlobalRules.ConsiderPerformRule("quest completed", actor, quest);
                        }
                        else if (GlobalRules.ConsiderValueRule<bool>("quest failed?", actor, quest))
                        {
                            actor.RemoveProperty("active-quest");
                            GlobalRules.ConsiderPerformRule("quest failed", actor, quest);
                        }
                    }

                    return PerformResult.Continue;
                })
                .Name("Check quest status after acting rule.");

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("quest reset", "[quest, thing] : The quest is being reset. Quests can call this on objects they interact with.", "quest", "item");

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("quest accepted", "[actor, quest] : Handle accepting a quest.", "actor", "quest");
            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("quest completed", "[actor, quest] : Handle when a quest is completed.", "actor", "quest");
            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("quest failed", "[actor, quest] : Handle when a quest is failed.", "actor", "quest");
            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("quest abandoned", "[actor, quest] : Handle when a quest is abandoned.", "actor", "quest");

            GlobalRules.Perform<MudObject, MudObject>("quest abandoned")
                .Last
                .Do((actor, quest) =>
                {
                    return GlobalRules.ConsiderPerformRule("quest failed", actor, quest);
                })
                .Name("Abandoning a quest is failure rule.");

            GlobalRules.DeclareValueRuleBook<MudObject, MudObject, bool>("quest available?", "[actor, quest -> bool] : Is the quest available to this actor?", "actor", "quest");

            GlobalRules.Value<MudObject, MudObject, bool>("quest available?")
                .Do((Actor, quest) => false)
                .Name("Quests unavailable by default rule.");

            GlobalRules.DeclareValueRuleBook<MudObject, MudObject, bool>("quest complete?", "[actor, quest -> bool] : Has this actor completed this quest?", "actor", "quest");

            GlobalRules.Value<MudObject, MudObject, bool>("quest complete?")
                .Do((actor, quest) => false)
                .Name("Quests incomplete by default rule.");

            GlobalRules.DeclareValueRuleBook<MudObject, MudObject, bool>("quest failed?", "[actor, quest -> bool] : Has this actor failed this quest?", "actor", "quest");

            GlobalRules.Value<MudObject, MudObject, bool>("quest failed?")
                .Do((actor, quest) => false)
                .Name("Quests can't fail by default rule.");
        }
Example #2
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            GlobalRules.Perform <PossibleMatch, Actor>("after acting")
            .Do((match, actor) =>
            {
                if (actor.GetProperty <MudObject>("active-quest") != null)
                {
                    var quest = actor.GetProperty <MudObject>("active-quest");

                    if (GlobalRules.ConsiderValueRule <bool>("quest complete?", actor, quest))
                    {
                        actor.RemoveProperty("active-quest");
                        GlobalRules.ConsiderPerformRule("quest completed", actor, quest);
                    }
                    else if (GlobalRules.ConsiderValueRule <bool>("quest failed?", actor, quest))
                    {
                        actor.RemoveProperty("active-quest");
                        GlobalRules.ConsiderPerformRule("quest failed", actor, quest);
                    }
                }

                return(PerformResult.Continue);
            })
            .Name("Check quest status after acting rule.");

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("quest reset", "[quest, thing] : The quest is being reset. Quests can call this on objects they interact with.", "quest", "item");

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("quest accepted", "[actor, quest] : Handle accepting a quest.", "actor", "quest");
            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("quest completed", "[actor, quest] : Handle when a quest is completed.", "actor", "quest");
            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("quest failed", "[actor, quest] : Handle when a quest is failed.", "actor", "quest");
            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("quest abandoned", "[actor, quest] : Handle when a quest is abandoned.", "actor", "quest");

            GlobalRules.Perform <MudObject, MudObject>("quest abandoned")
            .Last
            .Do((actor, quest) =>
            {
                return(GlobalRules.ConsiderPerformRule("quest failed", actor, quest));
            })
            .Name("Abandoning a quest is failure rule.");

            GlobalRules.DeclareValueRuleBook <MudObject, MudObject, bool>("quest available?", "[actor, quest -> bool] : Is the quest available to this actor?", "actor", "quest");

            GlobalRules.Value <MudObject, MudObject, bool>("quest available?")
            .Do((Actor, quest) => false)
            .Name("Quests unavailable by default rule.");

            GlobalRules.DeclareValueRuleBook <MudObject, MudObject, bool>("quest complete?", "[actor, quest -> bool] : Has this actor completed this quest?", "actor", "quest");

            GlobalRules.Value <MudObject, MudObject, bool>("quest complete?")
            .Do((actor, quest) => false)
            .Name("Quests incomplete by default rule.");

            GlobalRules.DeclareValueRuleBook <MudObject, MudObject, bool>("quest failed?", "[actor, quest -> bool] : Has this actor failed this quest?", "actor", "quest");

            GlobalRules.Value <MudObject, MudObject, bool>("quest failed?")
            .Do((actor, quest) => false)
            .Name("Quests can't fail by default rule.");
        }
Example #3
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            GlobalRules.Perform <MudObject, MudObject>("describe")
            .When((viewer, item) => item.GetProperty <bool>("portal?"))
            .Do((viewer, item) =>
            {
                if (item.GetProperty <bool>("openable?") && !item.GetProperty <bool>("open?"))
                {
                    return(PerformResult.Stop);
                }

                var destination = Core.GetObject(item.GetProperty <String>("link destination"));
                if (destination == null)
                {
                    Core.SendMessage(viewer, "@bad link");
                    return(PerformResult.Stop);
                }

                if (item.GetProperty <bool>("openable?"))
                {
                    Core.SendMessage(viewer, "@through the link you see", item);
                }
                else
                {
                    var direction = item.GetProperty <Direction>("link direction");
                    Core.SendMessage(viewer, "@through the cardinal link you see", Link.FriendlyRelativeMessage(direction));
                }

                GlobalRules.ConsiderPerformRule("describe locale", viewer, destination);
                return(PerformResult.Continue);
            })
            .Name("Look through a link rule.");
        }
Example #4
0
        /// <summary>
        /// Start the mud engine.
        /// </summary>
        /// <param name="Flags">Flags control engine functions</param>
        /// <param name="Database"></param>
        /// <param name="Assemblies">Modules to integrate</param>
        /// <returns></returns>
        public static bool Start(StartupFlags Flags, WorldDataService Database, params ModuleAssembly[] Assemblies)
        {
            ShuttingDown = false;
            Core.Flags = Flags;

            try
            {
                // Setup the rule engine and some basic rules.
                GlobalRules = new RuleEngine(NewRuleQueueingMode.QueueNewRules);
                GlobalRules.DeclarePerformRuleBook("at startup", "[] : Considered when the engine is started.");
                GlobalRules.DeclarePerformRuleBook<MudObject>("singleplayer game started", "Considered when a single player game is begun");

                // Integrate modules. The Core assembly is always integrated.
                IntegratedModules.Add(new ModuleAssembly(Assembly.GetExecutingAssembly(), new ModuleInfo { Author = "Blecki", Description = "RMUD Core", BaseNameSpace = "RMUD" }, "Core.dll"));
                IntegratedModules.AddRange(Assemblies);

                if ((Flags & StartupFlags.SearchDirectory) == StartupFlags.SearchDirectory)
                {
                    foreach (var file in System.IO.Directory.EnumerateFiles(System.IO.Directory.GetCurrentDirectory()).Where(p => System.IO.Path.GetExtension(p) == ".dll"))
                    {
                        var assembly = System.Reflection.Assembly.LoadFrom(file);

                        var infoType = assembly.GetTypes().FirstOrDefault(t => t.IsSubclassOf(typeof(ModuleInfo)));
                        if (infoType != null)
                        {
                            IntegratedModules.Add(new ModuleAssembly(assembly, file));
                            if ((Flags & StartupFlags.Silent) == 0)
                                Console.WriteLine("Discovered module: " + file);
                        }
                    }
                }

                foreach (var startupAssembly in IntegratedModules)
                    IntegrateModule(startupAssembly);

                PersistentValueSerializer.AddGlobalSerializer(new BitArraySerializer());

                InitializeCommandProcessor();

                GlobalRules.FinalizeNewRules();

                Core.Database = Database;
                Database.Initialize();

                GlobalRules.ConsiderPerformRule("at startup");

                if ((Flags & StartupFlags.SingleThreaded) == 0)
                    StartThreadedCommandProcesor();
            }
            catch (Exception e)
            {
                LogError("Failed to start mud engine.");
                LogError(e.Message);
                LogError(e.StackTrace);
                throw;
            }
            return true;
        }
Example #5
0
File: Drop.cs Project: SinaC/RMUD
        public static void AtStartup(RuleEngine GlobalRules)
        {
            Core.StandardMessage("you drop", "You drop <the0>.");
            Core.StandardMessage("they drop", "^<the0> drops <a1>.");

            GlobalRules.DeclareCheckRuleBook <MudObject, MudObject>("can drop?", "[Actor, Item] : Determine if the item can be dropped.", "actor", "item");
            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("drop", "[Actor, Item] : Handle an item being dropped.", "actor", "item");

            GlobalRules.Check <MudObject, MudObject>("can drop?")
            .First
            .When((actor, item) => !MudObject.ObjectContainsObject(actor, item))
            .Do((actor, item) =>
            {
                MudObject.SendMessage(actor, "@dont have that");
                return(CheckResult.Disallow);
            })
            .Name("Must be holding it to drop it rule.");

            GlobalRules.Check <MudObject, MudObject>("can drop?")
            .First
            .When((actor, item) => actor is Actor && (actor as Actor).Contains(item, RelativeLocations.Worn))
            .Do((actor, item) =>
            {
                if (GlobalRules.ConsiderCheckRule("can remove?", actor, item) == CheckResult.Allow)
                {
                    GlobalRules.ConsiderPerformRule("remove", actor, item);
                    return(CheckResult.Continue);
                }
                return(CheckResult.Disallow);
            })
            .Name("Dropping worn items follows remove rules rule.");

            GlobalRules.Check <MudObject, MudObject>("can drop?").Do((a, b) => CheckResult.Allow).Name("Default can drop anything rule.");

            GlobalRules.Perform <MudObject, MudObject>("drop").Do((actor, target) =>
            {
                MudObject.SendMessage(actor, "@you drop", target);
                MudObject.SendExternalMessage(actor, "@they drop", actor, target);
                MudObject.Move(target, actor.Location);
                return(PerformResult.Continue);
            }).Name("Default drop handler rule.");
        }
Example #6
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            Core.StandardMessage("you drop", "You drop <the0>.");
            Core.StandardMessage("they drop", "^<the0> drops <a1>.");

            GlobalRules.DeclareCheckRuleBook<MudObject, MudObject>("can drop?", "[Actor, Item] : Determine if the item can be dropped.", "actor", "item");
            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("drop", "[Actor, Item] : Handle an item being dropped.", "actor", "item");

            GlobalRules.Check<MudObject, MudObject>("can drop?")
                .First
                .When((actor, item) => !MudObject.ObjectContainsObject(actor, item))
                .Do((actor, item) =>
                {
                    MudObject.SendMessage(actor, "@dont have that");
                    return CheckResult.Disallow;
                })
                .Name("Must be holding it to drop it rule.");

            GlobalRules.Check<MudObject, MudObject>("can drop?")
                .First
                .When((actor, item) => actor is Actor && (actor as Actor).Contains(item, RelativeLocations.Worn))
                .Do((actor, item) =>
                {
                    if (GlobalRules.ConsiderCheckRule("can remove?", actor, item) == CheckResult.Allow)
                    {
                        GlobalRules.ConsiderPerformRule("remove", actor, item);
                        return CheckResult.Continue;
                    }
                    return CheckResult.Disallow;
                })
                .Name("Dropping worn items follows remove rules rule.");

            GlobalRules.Check<MudObject, MudObject>("can drop?").Do((a, b) => CheckResult.Allow).Name("Default can drop anything rule.");

            GlobalRules.Perform<MudObject, MudObject>("drop").Do((actor, target) =>
            {
                MudObject.SendMessage(actor, "@you drop", target);
                MudObject.SendExternalMessage(actor, "@they drop", actor, target);
                MudObject.Move(target, actor.Location);
                return PerformResult.Continue;
            }).Name("Default drop handler rule.");
        }
Example #7
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("describe in locale", "[Actor, Item] : Generate a locale description for the item.", "actor", "item");

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("describe locale", "[Actor, Room] : Generates a description of the locale.", "actor", "room");

            GlobalRules.Perform <MudObject, MudObject>("describe locale")
            .First
            .When((viewer, room) => room == null)
            .Do((viewer, room) =>
            {
                Core.SendMessage(viewer, "@nowhere");
                return(PerformResult.Stop);
            })
            .Name("Can't describe the locale if there isn't one rule.");

            GlobalRules.Perform <MudObject, MudObject>("describe locale")
            .First
            .Do((viewer, room) =>
            {
                GlobalRules.ConsiderPerformRule("update", room);
                return(PerformResult.Continue);
            })
            .Name("Update room before generating description rule.");

            GlobalRules.Perform <MudObject, MudObject>("describe locale")
            .First
            .Do((viewer, locale) =>
            {
                if (!String.IsNullOrEmpty(locale.GetProperty <String>("short")))
                {
                    Core.SendMessage(viewer, locale.GetProperty <String>("short"));
                }
                return(PerformResult.Continue);
            })
            .Name("Display locale name rule.");

            GlobalRules.Perform <MudObject, MudObject>("describe locale")
            .First
            .When((viewer, room) => room.GetProperty <LightingLevel>("locale light") == LightingLevel.Dark)
            .Do((viewer, room) =>
            {
                Core.SendMessage(viewer, "@dark");
                return(PerformResult.Stop);
            })
            .Name("Can't see in darkness rule.");

            GlobalRules.Perform <MudObject, MudObject>("describe locale")
            .Do((viewer, locale) =>
            {
                GlobalRules.ConsiderPerformRule("describe", viewer, locale);
                return(PerformResult.Continue);
            })
            .Name("Include describe rules in locale description rule.");

            var describingLocale = false;

            GlobalRules.Value <MudObject, MudObject, String, String>("printed name")
            .When((viewer, container, article) => describingLocale && (container.ContentLocationsAllowed & RelativeLocations.ON) == RelativeLocations.ON)
            .Do((viewer, container, article) =>
            {
                var subObjects = new List <MudObject>(container.EnumerateObjects(RelativeLocations.ON)
                                                      .Where(t => GlobalRules.ConsiderCheckRule("should be listed?", viewer, t) == CheckResult.Allow));

                if (subObjects.Count > 0)
                {
                    return(container.GetProperty <String>("short") + " " + Core.FormatMessage(viewer, Core.GetMessage("on which"), subObjects));
                }
                else
                {
                    return(container.GetProperty <String>("short"));
                }
            })
            .Name("List contents of container after name when describing locale rule");

            GlobalRules.DeclareCheckRuleBook <MudObject, MudObject>("should be listed in locale?", "[Viewer, Item] : When describing a room, or the contents of a container, should this item be listed?");

            GlobalRules.Check <MudObject, MudObject>("should be listed in locale?")
            .When((viewer, item) => System.Object.ReferenceEquals(viewer, item))
            .Do((viewer, item) => CheckResult.Disallow)
            .Name("Don't list yourself rule.");

            GlobalRules.Check <MudObject, MudObject>("should be listed in locale?")
            .When((viewer, item) => item.GetProperty <bool>("scenery?"))
            .Do((viewer, item) => CheckResult.Disallow)
            .Name("Don't list scenery objects rule.");

            GlobalRules.Check <MudObject, MudObject>("should be listed in locale?")
            .When((viewer, item) => item.GetProperty <bool>("portal?"))
            .Do((viewer, item) => CheckResult.Disallow)
            .Name("Don't list portals rule.");

            GlobalRules.Check <MudObject, MudObject>("should be listed in locale?")
            .Do((viewer, item) => CheckResult.Allow)
            .Name("List objects by default rule.");

            GlobalRules.Perform <MudObject, MudObject>("describe locale")
            .Do((viewer, room) =>
            {
                var visibleThings = room.EnumerateObjects(RelativeLocations.CONTENTS)
                                    .Where(t => GlobalRules.ConsiderCheckRule("should be listed in locale?", viewer, t) == CheckResult.Allow);

                var normalContents = new List <MudObject>();

                foreach (var thing in visibleThings)
                {
                    Core.BeginOutputQuery();
                    GlobalRules.ConsiderPerformRule("describe in locale", viewer, thing);
                    if (!Core.CheckOutputQuery())
                    {
                        normalContents.Add(thing);
                    }
                }

                if (normalContents.Count > 0)
                {
                    describingLocale = true;
                    Core.SendMessage(viewer, "@also here", normalContents);
                    describingLocale = false;
                }

                return(PerformResult.Continue);
            })
            .Name("List contents of room rule.");

            GlobalRules.Perform <MudObject, MudObject>("describe locale")
            .Last
            .Do((viewer, room) =>
            {
                if (room.EnumerateObjects().Where(l => l.GetProperty <bool>("portal?")).Count() > 0)
                {
                    Core.SendMessage(viewer, "@obvious exits");

                    foreach (var link in room.EnumerateObjects <MudObject>().Where(l => l.GetProperty <bool>("portal?")))
                    {
                        var builder = new StringBuilder();
                        builder.Append("  ^");
                        builder.Append(link.GetProperty <Direction>("link direction").ToString());

                        if (!link.GetProperty <bool>("link anonymous?"))
                        {
                            builder.Append(" " + Core.FormatMessage(viewer, Core.GetMessage("through"), link));
                        }

                        var destinationRoom = Core.GetObject(link.GetProperty <String>("link destination"));
                        if (destinationRoom != null)
                        {
                            builder.Append(" " + Core.FormatMessage(viewer, Core.GetMessage("to"), destinationRoom));
                        }

                        Core.SendMessage(viewer, builder.ToString());
                    }
                }

                return(PerformResult.Continue);
            })
            .Name("List exits in locale description rule.");
        }
Example #8
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            GlobalRules.DeclareCheckRuleBook<MudObject, MudObject>("can introduce?", "[Actor A, Actor B] : Can A introduce B?", "actor", "itroductee");

            GlobalRules.Check<MudObject, MudObject>("can introduce?")
                .When((a, b) => !(b is Actor))
                .Do((a, b) =>
                {
                    MudObject.SendMessage(a, "That just sounds silly.");
                    return CheckResult.Disallow;
                })
                .Name("Can only introduce actors rule.");

            GlobalRules.Check<MudObject, MudObject>("can introduce?")
                .Do((a, b) => MudObject.CheckIsVisibleTo(a, b))
                .Name("Introducee must be visible rule.");

            GlobalRules.Check<MudObject, MudObject>("can introduce?")
                .When((a, b) => !GlobalRules.ConsiderValueRule<bool>("actor knows actor?", a, b))
                .Do((a, b) =>
                {
                    MudObject.SendMessage(a, "How can you introduce <the0> when you don't know them yourself?", b);
                    return CheckResult.Disallow;
                })
                .Name("Can't introduce who you don't know rule.");

            GlobalRules.Perform<MudObject, Actor>("describe")
                .First
                .When((viewer, actor) => GlobalRules.ConsiderValueRule<bool>("actor knows actor?", viewer, actor))
                .Do((viewer, actor) =>
                {
                    MudObject.SendMessage(viewer, "^<the0>, a " + (actor.Gender == Gender.Male ? "man." : "woman."), actor);
                    return PerformResult.Continue;
                })
                .Name("Report gender of known actors rule.");

            #region Perform and report rules

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("introduce", "[Actor A, Actor B] : Handle A introducing B.", "actor", "introductee");

            GlobalRules.Perform<MudObject, MudObject>("introduce")
                .Do((actor, introductee) =>
                {
                    var locale = MudObject.FindLocale(introductee);
                    if (locale != null)
                        foreach (var player in MudObject.EnumerateObjectTree(locale).Where(o => o is Player).Select(o => o as Player))
                            GlobalRules.ConsiderPerformRule("introduce to", introductee, player);

                    MudObject.SendExternalMessage(actor, "^<the0> introduces <the1>.", actor, introductee);
                    MudObject.SendMessage(actor, "You introduce <the0>.", introductee);
                    return PerformResult.Continue;
                })
                .Name("Report introduction rule.");

            GlobalRules.DeclarePerformRuleBook<Actor>("introduce self", "[Introductee] : Introduce the introductee");

            GlobalRules.Perform<Actor>("introduce self")
                .Do((introductee) =>
                {
                    var locale = MudObject.FindLocale(introductee);
                    if (locale != null)
                        foreach (var player in MudObject.EnumerateObjectTree(locale).Where(o => o is Player).Select(o => o as Player))
                            GlobalRules.ConsiderPerformRule("introduce to", introductee, player);

                    MudObject.SendExternalMessage(introductee, "^<the0> introduces themselves.", introductee);
                    MudObject.SendMessage(introductee, "You introduce yourself.");

                    return PerformResult.Continue;
                })
                .Name("Introduce an actor to everyone present rule.");

            #endregion

            #region Printed name rules

            GlobalRules.Value<Player, Actor, String, String>("printed name")
                .When((viewer, thing, article) => GlobalRules.ConsiderValueRule<bool>("actor knows actor?", viewer, thing))
                .Do((viewer, actor, article) => actor.Short)
                .Name("Name of introduced actor.");

            GlobalRules.Value<MudObject, MudObject, String, String>("printed name")
                .When((viewer, thing, article) => thing is Actor && (thing as Actor).Gender == Gender.Male)
                .Do((viewer, actor, article) => article + " man")
                .Name("Default name for unintroduced male actor.");

            GlobalRules.Value<MudObject, MudObject, String, String>("printed name")
                .When((viewer, thing, article) => thing is Actor && (thing as Actor).Gender == Gender.Female)
                .Do((viewer, actor, article) => article + " woman")
                .Name("Default name for unintroduced female actor.");

            #endregion

            #region Knowledge management rules

            GlobalRules.DeclareValueRuleBook<Actor, Actor, bool>("actor knows actor?", "[Player, Whom] : Does the player know the actor?");

            GlobalRules.Value<Player, Actor, bool>("actor knows actor?")
                .Do((player, whom) => player.Recall<bool>(whom, "knows"))
                .Name("Use player memory to recall actors rule.");

            GlobalRules.Value<Actor, Actor, bool>("actor knows actor?")
                .Do((player, whom) => false)
                .Name("Actors that aren't players don't know anybody rule.");

            GlobalRules.DeclarePerformRuleBook<Actor, Actor>("introduce to", "[Introductee, ToWhom] : Introduce the introductee to someone");

            GlobalRules.Perform<Actor, Player>("introduce to")
                .Do((introductee, player) =>
                {
                    player.Remember(introductee, "knows", true);
                    return PerformResult.Continue;
                })
                .Name("Players remember actors rule.");

            #endregion
        }
Example #9
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            GlobalRules.DeclareCheckRuleBook <MudObject, MudObject>("can introduce?", "[Actor A, Actor B] : Can A introduce B?", "actor", "itroductee");

            GlobalRules.Check <MudObject, MudObject>("can introduce?")
            .When((a, b) => !(b is Actor))
            .Do((a, b) =>
            {
                MudObject.SendMessage(a, "That just sounds silly.");
                return(CheckResult.Disallow);
            })
            .Name("Can only introduce actors rule.");

            GlobalRules.Check <MudObject, MudObject>("can introduce?")
            .Do((a, b) => MudObject.CheckIsVisibleTo(a, b))
            .Name("Introducee must be visible rule.");

            GlobalRules.Check <MudObject, MudObject>("can introduce?")
            .When((a, b) => !GlobalRules.ConsiderValueRule <bool>("actor knows actor?", a, b))
            .Do((a, b) =>
            {
                MudObject.SendMessage(a, "How can you introduce <the0> when you don't know them yourself?", b);
                return(CheckResult.Disallow);
            })
            .Name("Can't introduce who you don't know rule.");

            GlobalRules.Perform <MudObject, Actor>("describe")
            .First
            .When((viewer, actor) => GlobalRules.ConsiderValueRule <bool>("actor knows actor?", viewer, actor))
            .Do((viewer, actor) =>
            {
                MudObject.SendMessage(viewer, "^<the0>, a " + (actor.Gender == Gender.Male ? "man." : "woman."), actor);
                return(PerformResult.Continue);
            })
            .Name("Report gender of known actors rule.");

            #region Perform and report rules

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("introduce", "[Actor A, Actor B] : Handle A introducing B.", "actor", "introductee");

            GlobalRules.Perform <MudObject, MudObject>("introduce")
            .Do((actor, introductee) =>
            {
                var locale = MudObject.FindLocale(introductee);
                if (locale != null)
                {
                    foreach (var player in MudObject.EnumerateObjectTree(locale).Where(o => o is Player).Select(o => o as Player))
                    {
                        GlobalRules.ConsiderPerformRule("introduce to", introductee, player);
                    }
                }

                MudObject.SendExternalMessage(actor, "^<the0> introduces <the1>.", actor, introductee);
                MudObject.SendMessage(actor, "You introduce <the0>.", introductee);
                return(PerformResult.Continue);
            })
            .Name("Report introduction rule.");

            GlobalRules.DeclarePerformRuleBook <Actor>("introduce self", "[Introductee] : Introduce the introductee");

            GlobalRules.Perform <Actor>("introduce self")
            .Do((introductee) =>
            {
                var locale = MudObject.FindLocale(introductee);
                if (locale != null)
                {
                    foreach (var player in MudObject.EnumerateObjectTree(locale).Where(o => o is Player).Select(o => o as Player))
                    {
                        GlobalRules.ConsiderPerformRule("introduce to", introductee, player);
                    }
                }

                MudObject.SendExternalMessage(introductee, "^<the0> introduces themselves.", introductee);
                MudObject.SendMessage(introductee, "You introduce yourself.");

                return(PerformResult.Continue);
            })
            .Name("Introduce an actor to everyone present rule.");

            #endregion

            #region Printed name rules

            GlobalRules.Value <Player, Actor, String, String>("printed name")
            .When((viewer, thing, article) => GlobalRules.ConsiderValueRule <bool>("actor knows actor?", viewer, thing))
            .Do((viewer, actor, article) => actor.Short)
            .Name("Name of introduced actor.");

            GlobalRules.Value <MudObject, MudObject, String, String>("printed name")
            .When((viewer, thing, article) => thing is Actor && (thing as Actor).Gender == Gender.Male)
            .Do((viewer, actor, article) => article + " man")
            .Name("Default name for unintroduced male actor.");

            GlobalRules.Value <MudObject, MudObject, String, String>("printed name")
            .When((viewer, thing, article) => thing is Actor && (thing as Actor).Gender == Gender.Female)
            .Do((viewer, actor, article) => article + " woman")
            .Name("Default name for unintroduced female actor.");

            #endregion

            #region Knowledge management rules

            GlobalRules.DeclareValueRuleBook <Actor, Actor, bool>("actor knows actor?", "[Player, Whom] : Does the player know the actor?");

            GlobalRules.Value <Player, Actor, bool>("actor knows actor?")
            .Do((player, whom) => player.Recall <bool>(whom, "knows"))
            .Name("Use player memory to recall actors rule.");

            GlobalRules.Value <Actor, Actor, bool>("actor knows actor?")
            .Do((player, whom) => false)
            .Name("Actors that aren't players don't know anybody rule.");

            GlobalRules.DeclarePerformRuleBook <Actor, Actor>("introduce to", "[Introductee, ToWhom] : Introduce the introductee to someone");

            GlobalRules.Perform <Actor, Player>("introduce to")
            .Do((introductee, player) =>
            {
                player.Remember(introductee, "knows", true);
                return(PerformResult.Continue);
            })
            .Name("Players remember actors rule.");

            #endregion
        }
Example #10
0
File: Rules.cs Project: SinaC/RMUD
        public static void AtStartup(RuleEngine GlobalRules)
        {
            Core.StandardMessage("convo topic prompt", "Suggested topics: <l0>");
            Core.StandardMessage("convo cant converse", "You can't converse with that.");
            Core.StandardMessage("convo greet whom", "Whom did you want to greet?");
            Core.StandardMessage("convo nobody", "You aren't talking to anybody.");
            Core.StandardMessage("convo no response", "There doesn't seem to be a response defined for that topic.");
            Core.StandardMessage("convo no topics", "There is nothing obvious to discuss.");

            GlobalRules.DeclareCheckRuleBook <MudObject, MudObject>("can converse?", "[Actor, Item] : Can the actor converse with the item?", "actor", "item");

            GlobalRules.Check <MudObject, MudObject>("can converse?")
            .When((actor, item) => !(item is NPC))
            .Do((actor, item) =>
            {
                MudObject.SendMessage(actor, "@convo cant converse");
                return(CheckResult.Disallow);
            })
            .Name("Can only converse with NPCs rule.");

            GlobalRules.Check <MudObject, MudObject>("can converse?")
            .Do((actor, item) => MudObject.CheckIsVisibleTo(actor, item))
            .Name("Locutor must be visible rule.");

            GlobalRules.Check <MudObject, MudObject>("can converse?")
            .Last
            .Do((actor, item) => CheckResult.Allow)
            .Name("Let them chat rule.");

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("greet", "[Actor, NPC] : Handle an actor greeting an NPC.", "actor", "npc");

            GlobalRules.DeclarePerformRuleBook <MudObject>("list topics", "[Actor] : List conversation topics available to the actor.", "actor");

            GlobalRules.Perform <MudObject>("list topics")
            .When(actor => !(actor is Player) || actor.GetProperty <NPC>("interlocutor") == null)
            .Do(actor =>
            {
                MudObject.SendMessage(actor, "@convo nobody");
                return(PerformResult.Stop);
            })
            .Name("Need interlocutor to list topics rule.");

            GlobalRules.Perform <MudObject>("list topics")
            .Do(actor =>
            {
                if (!(actor is Player))
                {
                    return(PerformResult.Stop);
                }
                var npc             = actor.GetProperty <NPC>("interlocutor");
                var suggestedTopics = npc.GetPropertyOrDefault <List <MudObject> >("conversation-topics", new List <MudObject>()).AsEnumerable();

                if (!Settings.ListDiscussedTopics)
                {
                    suggestedTopics = suggestedTopics.Where(obj => !obj.GetBooleanProperty("topic-discussed"));
                }

                suggestedTopics = suggestedTopics.Where(topic => GlobalRules.ConsiderCheckRule("topic available?", actor, npc, topic) == CheckResult.Allow);

                var enumeratedSuggestedTopics = new List <MudObject>(suggestedTopics);

                if (enumeratedSuggestedTopics.Count != 0)
                {
                    MudObject.SendMessage(actor, "@convo topic prompt", enumeratedSuggestedTopics);
                }
                else
                {
                    MudObject.SendMessage(actor, "@convo no topics");
                }

                return(PerformResult.Continue);
            })
            .Name("List un-discussed available topics rule.");

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject, MudObject>("discuss topic", "[Actor, NPC, Topic] : Handle the actor discussing the topic with the npc.");

            GlobalRules.Perform <MudObject, MudObject, MudObject>("discuss topic")
            .Do((actor, npc, topic) =>
            {
                GlobalRules.ConsiderPerformRule("topic response", actor, npc, topic);
                if (topic != null)
                {
                    topic.SetProperty("topic-discussed", true);
                }
                return(PerformResult.Continue);
            })
            .Name("Show topic response when discussing topic rule.");

            GlobalRules.Perform <MudObject, MudObject, MudObject>("topic response")
            .Do((actor, npc, topic) =>
            {
                MudObject.SendMessage(actor, "@convo no response");
                return(PerformResult.Stop);
            })
            .Name("No response rule for the topic rule.");

            GlobalRules.DeclareCheckRuleBook <MudObject, MudObject, MudObject>("topic available?", "[Actor, NPC, Topic -> bool] : Is the topic available for discussion with the NPC to the actor?", "actor", "npc", "topic");

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject, MudObject>("topic response", "[Actor, NPC, Topic] : Display the response of the topic.", "actor", "npc", "topic");

            GlobalRules.Check <MudObject, MudObject, MudObject>("topic available?")
            .First
            .When((actor, npc, topic) => (topic != null) && (Settings.AllowRepeats == false) && topic.GetBooleanProperty("topic-discussed"))
            .Do((actor, npc, topic) => CheckResult.Disallow)
            .Name("Already discussed topics unavailable when repeats disabled rule.");

            GlobalRules.Check <MudObject, MudObject, MudObject>("topic available?")
            .Last
            .Do((actor, npc, topic) => CheckResult.Allow)
            .Name("Topics available by default rule.");
        }
Example #11
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            Core.StandardMessage("convo topic prompt", "Suggested topics: <l0>");
            Core.StandardMessage("convo cant converse", "You can't converse with that.");
            Core.StandardMessage("convo greet whom", "Whom did you want to greet?");
            Core.StandardMessage("convo nobody", "You aren't talking to anybody.");
            Core.StandardMessage("convo no response", "There doesn't seem to be a response defined for that topic.");
            Core.StandardMessage("convo no topics", "There is nothing obvious to discuss.");

            GlobalRules.DeclareCheckRuleBook<MudObject, MudObject>("can converse?", "[Actor, Item] : Can the actor converse with the item?", "actor", "item");

            GlobalRules.Check<MudObject, MudObject>("can converse?")
                .When((actor, item) => !(item is NPC))
                .Do((actor, item) =>
                {
                    MudObject.SendMessage(actor, "@convo cant converse");
                    return CheckResult.Disallow;
                })
                .Name("Can only converse with NPCs rule.");

            GlobalRules.Check<MudObject, MudObject>("can converse?")
                .Do((actor, item) => MudObject.CheckIsVisibleTo(actor, item))
                .Name("Locutor must be visible rule.");

            GlobalRules.Check<MudObject, MudObject>("can converse?")
                .Last
                .Do((actor, item) => CheckResult.Allow)
                .Name("Let them chat rule.");

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("greet", "[Actor, NPC] : Handle an actor greeting an NPC.", "actor", "npc");

            GlobalRules.DeclarePerformRuleBook<MudObject>("list topics", "[Actor] : List conversation topics available to the actor.", "actor");

            GlobalRules.Perform<MudObject>("list topics")
                .When(actor => !(actor is Player) || actor.GetProperty<NPC>("interlocutor") == null)
                .Do(actor =>
                {
                    MudObject.SendMessage(actor, "@convo nobody");
                    return PerformResult.Stop;
                })
                .Name("Need interlocutor to list topics rule.");

            GlobalRules.Perform<MudObject>("list topics")
                .Do(actor =>
                {
                    if (!(actor is Player)) return PerformResult.Stop;
                    var npc = actor.GetProperty<NPC>("interlocutor");
                    var suggestedTopics = npc.GetPropertyOrDefault<List<MudObject>>("conversation-topics", new List<MudObject>()).AsEnumerable();

                    if (!Settings.ListDiscussedTopics)
                        suggestedTopics = suggestedTopics.Where(obj => !obj.GetBooleanProperty("topic-discussed"));

                    suggestedTopics = suggestedTopics.Where(topic => GlobalRules.ConsiderCheckRule("topic available?", actor, npc, topic) == CheckResult.Allow);

                    var enumeratedSuggestedTopics = new List<MudObject>(suggestedTopics);

                    if (enumeratedSuggestedTopics.Count != 0)
                        MudObject.SendMessage(actor, "@convo topic prompt", enumeratedSuggestedTopics);
                    else
                        MudObject.SendMessage(actor, "@convo no topics");

                    return PerformResult.Continue;
                })
                .Name("List un-discussed available topics rule.");

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject, MudObject>("discuss topic", "[Actor, NPC, Topic] : Handle the actor discussing the topic with the npc.");

            GlobalRules.Perform<MudObject, MudObject, MudObject>("discuss topic")
                .Do((actor, npc, topic) =>
                {
                    GlobalRules.ConsiderPerformRule("topic response", actor, npc, topic);
                    if (topic != null) topic.SetProperty("topic-discussed", true);
                    return PerformResult.Continue;
                })
                .Name("Show topic response when discussing topic rule.");

            GlobalRules.Perform<MudObject, MudObject, MudObject>("topic response")
                .Do((actor, npc, topic) =>
                {
                    MudObject.SendMessage(actor, "@convo no response");
                    return PerformResult.Stop;
                })
                .Name("No response rule for the topic rule.");

            GlobalRules.DeclareCheckRuleBook<MudObject, MudObject, MudObject>("topic available?", "[Actor, NPC, Topic -> bool] : Is the topic available for discussion with the NPC to the actor?", "actor", "npc", "topic");

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject, MudObject>("topic response", "[Actor, NPC, Topic] : Display the response of the topic.", "actor", "npc", "topic");

            GlobalRules.Check<MudObject, MudObject, MudObject>("topic available?")
                .First
                .When((actor, npc, topic) => (topic != null) && (Settings.AllowRepeats == false) && topic.GetBooleanProperty("topic-discussed"))
                .Do((actor, npc, topic) => CheckResult.Disallow)
                .Name("Already discussed topics unavailable when repeats disabled rule.");

            GlobalRules.Check<MudObject, MudObject, MudObject>("topic available?")
                .Last
                .Do((actor, npc, topic) => CheckResult.Allow)
                .Name("Topics available by default rule.");
        }
Example #12
0
        public static void AtStartup(RuleEngine GlobalRules)
        {
            Core.StandardMessage("nowhere", "You aren't anywhere.");
            Core.StandardMessage("dark", "It's too dark to see.");
            Core.StandardMessage("also here", "Also here: <l0>.");
            Core.StandardMessage("on which", "(on which is <l0>)");
            Core.StandardMessage("obvious exits", "Obvious exits:");
            Core.StandardMessage("through", "through <the0>");
            Core.StandardMessage("to", "to <the0>");

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("describe in locale", "[Actor, Item] : Generate a locale description for the item.", "actor", "item");

            GlobalRules.DeclarePerformRuleBook<MudObject, MudObject>("describe locale", "[Actor, Room] : Generates a description of the locale.", "actor", "room");

            GlobalRules.Perform<MudObject, MudObject>("describe locale")
                .First
                .When((viewer, room) => room == null || !(room is Room))
                .Do((viewer, room) =>
                {
                    MudObject.SendMessage(viewer, "@nowhere");
                    return PerformResult.Stop;
                })
                .Name("Can't describe the locale if there isn't one rule.");

            GlobalRules.Perform<MudObject, MudObject>("describe locale")
                .First
                .Do((viewer, room) =>
                {
                    GlobalRules.ConsiderPerformRule("update", room);
                    return PerformResult.Continue;
                })
                .Name("Update room lighting before generating description rule.");

            GlobalRules.Perform<MudObject, MudObject>("describe locale")
                .First
                .Do((viewer, room) =>
                {
                    if (!String.IsNullOrEmpty(room.Short)) MudObject.SendMessage(viewer, room.Short);
                    return PerformResult.Continue;
                })
                .Name("Display room name rule.");

            GlobalRules.Perform<MudObject, MudObject>("describe locale")
                .First
                .When((viewer, room) => (room as Room).Light == LightingLevel.Dark)
                .Do((viewer, room) =>
                {
                    MudObject.SendMessage(viewer, "@dark");
                    return PerformResult.Stop;
                })
                .Name("Can't see in darkness rule.");

            GlobalRules.Perform<MudObject, MudObject>("describe locale")
                .Do((viewer, room) =>
                {
                    GlobalRules.ConsiderPerformRule("describe", viewer, room);
                    return PerformResult.Continue;
                })
                .Name("Include describe rules in locale description rule.");

            var describingLocale = false;

            GlobalRules.Value<Actor, Container, String, String>("printed name")
                .When((viewer, container, article) =>
                    {
                        return describingLocale && (container.LocationsSupported & RelativeLocations.On) == RelativeLocations.On;
                    })
                .Do((viewer, container, article) =>
                    {
                        var subObjects = new List<MudObject>(container.EnumerateObjects(RelativeLocations.On)
                        .Where(t => GlobalRules.ConsiderCheckRule("should be listed?", viewer, t) == CheckResult.Allow));

                        if (subObjects.Count > 0)
                            return container.Short + " " + Core.FormatMessage(viewer, Core.GetMessage("on which"), subObjects);
                        else
                            return container.Short;
                    })
                    .Name("List contents of container after name when describing locale rule");

            GlobalRules.DeclareCheckRuleBook<MudObject, MudObject>("should be listed in locale?", "[Viewer, Item] : When describing a room, or the contents of a container, should this item be listed?");

            GlobalRules.Check<MudObject, MudObject>("should be listed in locale?")
                .When((viewer, item) => System.Object.ReferenceEquals(viewer, item))
                .Do((viewer, item) => CheckResult.Disallow)
                .Name("Don't list yourself rule.");

            GlobalRules.Check<MudObject, MudObject>("should be listed in locale?")
               .When((viewer, item) => item.GetBooleanProperty("scenery?"))
               .Do((viewer, item) => CheckResult.Disallow)
               .Name("Don't list scenery objects rule.");

            GlobalRules.Check<MudObject, MudObject>("should be listed in locale?")
                .When((viewer, item) => item.GetBooleanProperty("portal?"))
                .Do((viewer, item) => CheckResult.Disallow)
                .Name("Don't list portals rule.");

            GlobalRules.Check<MudObject, MudObject>("should be listed in locale?")
               .Do((viewer, item) => CheckResult.Allow)
               .Name("List objects by default rule.");

            GlobalRules.Perform<MudObject, MudObject>("describe locale")
                .Do((viewer, room) =>
                {
                    var visibleThings = (room as Room).EnumerateObjects(RelativeLocations.Contents)
                        .Where(t => GlobalRules.ConsiderCheckRule("should be listed in locale?", viewer, t) == CheckResult.Allow);

                    var normalContents = new List<MudObject>();

                    foreach (var thing in visibleThings)
                    {
                        Core.BeginOutputQuery();
                        GlobalRules.ConsiderPerformRule("describe in locale", viewer, thing);
                        if (!Core.CheckOutputQuery()) normalContents.Add(thing);
                    }

                    if (normalContents.Count > 0)
                    {
                        describingLocale = true;
                        MudObject.SendMessage(viewer, "@also here", normalContents);
                        describingLocale = false;
                    }

                    return PerformResult.Continue;
                })
                .Name("List contents of room rule.");

            GlobalRules.Perform<Actor, MudObject>("describe locale")
                .Last
                .Do((viewer, room) =>
                {
                    if ((room as Room).EnumerateObjects().Where(l => l.GetBooleanProperty("portal?")).Count() > 0)
                    {
                        MudObject.SendMessage(viewer, "@obvious exits");

                        foreach (var link in (room as Room).EnumerateObjects<MudObject>().Where(l => l.GetBooleanProperty("portal?")))
                        {
                            var builder = new StringBuilder();
                            builder.Append("  ^");
                            builder.Append(link.GetPropertyOrDefault<Direction>("link direction", Direction.NOWHERE).ToString());

                            if (!link.GetPropertyOrDefault<bool>("link anonymous?", false))
                                builder.Append(" " + Core.FormatMessage(viewer, Core.GetMessage("through"), link));

                            var destinationRoom = MudObject.GetObject(link.GetProperty<String>("link destination")) as Room;
                            if (destinationRoom != null)
                                builder.Append(" " + Core.FormatMessage(viewer, Core.GetMessage("to"), destinationRoom));

                            MudObject.SendMessage(viewer, builder.ToString());
                        }
                    }

                    return PerformResult.Continue;
                })
                .Name("List exits in locale description rule.");
        }