Example #1
0
        public static void AtStartup(RMUD.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) => !Core.ObjectContainsObject(actor, item))
            .Do((actor, item) =>
            {
                Core.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.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) =>
            {
                Core.SendMessage(actor, "@you drop", target);
                Core.SendExternalMessage(actor, "@they drop", actor, target);
                if (actor.Location.HasValue(out var loc))
                {
                    Core.Move(target, loc);
                }
                return(PerformResult.Continue);
            }).Name("Default drop handler rule.");
        }
Example #2
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, String DatabasePath, WorldDataService Database, Assembly AdditionalAssembly = null)
        {
            Core.DatabasePath = DatabasePath;

            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");

                DefaultParser = new CommandParser();

                InitializeEngine(Assembly.GetExecutingAssembly());
                if (AdditionalAssembly != null)
                {
                    InitializeEngine(AdditionalAssembly);
                }
                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 #3
0
 public static void AddPlayer(Actor Actor)
 {
     Actor.Rank = 500;
     GlobalRules.ConsiderPerformRule("player joined", Actor);
 }
Example #4
0
        public static void AtStartup(RMUD.RuleEngine GlobalRules)
        {
            PropertyManifest.RegisterProperty("active-quest", typeof(MudObject), null, new DefaultSerializer());
            PropertyManifest.RegisterProperty("offered-quest", typeof(MudObject), null, new DefaultSerializer());

            GlobalRules.Perform <PossibleMatch, MudObject>("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.SetProperty("active-quest", null);
                        GlobalRules.ConsiderPerformRule("quest completed", actor, quest);
                    }
                    else if (GlobalRules.ConsiderValueRule <bool>("quest failed?", actor, quest))
                    {
                        actor.SetProperty("active-quest", null);
                        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 #5
0
 public static void AddPlayer(MudObject Actor)
 {
     Actor.SetProperty("rank", 500);
     GlobalRules.ConsiderPerformRule("player joined", Actor);
 }
Example #6
0
        public static void AtStartup(RMUD.RuleEngine GlobalRules)
        {
            PropertyManifest.RegisterProperty("combat weapon", typeof(MudObject), null, new DefaultSerializer());
            PropertyManifest.RegisterProperty("combat health", typeof(int), 0, new IntSerializer());
            PropertyManifest.RegisterProperty("combat damage die", typeof(String), "", new StringSerializer());
            PropertyManifest.RegisterProperty("combat hit modifier", typeof(int), 0, new IntSerializer());
            PropertyManifest.RegisterProperty("combat armor class", typeof(int), 0, new IntSerializer());

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("attacked", "[Attacker, Target] => Attacker Attacked Target rule.", "Attacker", "Target");

            GlobalRules.Perform <MudObject>("inventory")
            .When(a => a.GetProperty <MudObject>("combat weapon") != null)
            .Do(a =>
            {
                Core.SendMessage(a, "@wielding", a, a.GetProperty <MudObject>("combat weapon"));
                return(PerformResult.Continue);
            })
            .Name("List weapon in inventory rule.");

            GlobalRules.Perform <MudObject, MudObject>("drop")
            .First
            .When((actor, target) => Object.ReferenceEquals(actor.GetProperty <MudObject>("combat weapon"), target))
            .Do((actor, target) =>
            {
                actor.SetProperty("combat weapon", null);
                return(PerformResult.Continue);
            }).Name("Unwield dropped item rule");

            GlobalRules.Perform <MudObject, MudObject>("describe")
            .When((viewer, item) => item.GetProperty <MudObject>("combat weapon") != null)
            .Do((viewer, item) =>
            {
                Core.SendMessage(viewer, "@wielding", item, item.GetProperty <MudObject>("combat weapon"));
                return(PerformResult.Continue);
            })
            .Name("Describe their weapon rule.");

            GlobalRules.Perform <MudObject>("heartbeat")
            .When(o => o.HasProperty("combat target") && o.GetProperty <MudObject>("combat target") != null)
            .Do((o) =>
            {
                var target = o.GetProperty <MudObject>("combat target");
                if (target == null || target.State == ObjectState.Destroyed)
                {
                    o.SetProperty("combat target", null);
                    return(PerformResult.Continue);
                }

                if (!Core.IsVisibleTo(o, target))
                {
                    Core.SendMessage(o, "Your target is no longer here.");
                    o.SetProperty("combat target", null);
                    return(PerformResult.Continue);
                }

                CombatSystem.MeleeAttack(o, target);
                GlobalRules.ConsiderPerformRule("attacked", o, target);
                return(PerformResult.Continue);
            })
            .Name("I'm in a fight, I'd better attack rule.");

            GlobalRules.Perform <MudObject, MudObject>("attacked")
            .Do((attacker, target) =>
            {
                target.SetProperty("combat target", attacker);
                return(PerformResult.Continue);
            })
            .Name("fight back rule");
        }
Example #7
0
        public static void AtStartup(RMUD.RuleEngine GlobalRules)
        {
            PropertyManifest.RegisterProperty("introduction memory", typeof(Dictionary <String, bool>), null, new DefaultSerializer());

            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.GetProperty <bool>("actor?"))
            .Do((a, b) =>
            {
                Core.SendMessage(a, "That just sounds silly.");
                return(CheckResult.Disallow);
            })
            .Name("Can only introduce actors rule.");

            GlobalRules.Check <MudObject, MudObject>("can introduce?")
            .Do((a, b) => Core.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) =>
            {
                Core.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, MudObject>("describe")
            .First
            .When((viewer, actor) => GlobalRules.ConsiderValueRule <bool>("actor knows actor?", viewer, actor))
            .Do((viewer, actor) =>
            {
                Core.SendMessage(viewer, "^<the0>, a " + (actor.GetProperty <Gender>("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 = Core.FindLocale(introductee);
                if (locale != null)
                {
                    foreach (var player in Core.EnumerateObjectTree(locale).Where(o => o.GetProperty <bool>("actor?")))
                    {
                        GlobalRules.ConsiderPerformRule("introduce to", introductee, player);
                    }
                }

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

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

            GlobalRules.Perform <MudObject>("introduce self")
            .Do((introductee) =>
            {
                var locale = Core.FindLocale(introductee);
                if (locale != null)
                {
                    foreach (var player in Core.EnumerateObjectTree(locale).Where(o => o.GetProperty <bool>("actor?")))
                    {
                        GlobalRules.ConsiderPerformRule("introduce to", introductee, player);
                    }
                }

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

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

            #endregion

            #region Printed name rules

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

            GlobalRules.Value <MudObject, MudObject, String, String>("printed name")
            .When((viewer, thing, article) => thing.GetProperty <bool>("actor?"))
            .When((viewer, thing, article) => thing.GetProperty <Gender>("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.GetProperty <bool>("actor?"))
            .When((viewer, thing, article) => thing.GetProperty <Gender>("gender") == Gender.Female)
            .Do((viewer, actor, article) => article + " woman")
            .Name("Default name for unintroduced female actor.");

            #endregion

            #region Knowledge management rules

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

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

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

            GlobalRules.Perform <MudObject, MudObject>("introduce to")
            .Do((introductee, player) =>
            {
                RememberActor(player, introductee);
                return(PerformResult.Continue);
            })
            .Name("Players remember actors rule.");

            #endregion
        }
Example #8
0
        public static void AtStartup(RMUD.RuleEngine GlobalRules)
        {
            PropertyManifest.RegisterProperty("interlocutor", typeof(MudObject), null, new DefaultSerializer());
            PropertyManifest.RegisterProperty("conversation-topics", typeof(List <MudObject>), new List <MudObject>(), new DefaultSerializer());
            PropertyManifest.RegisterProperty("topic-discussed", typeof(bool), false, new BoolSerializer());

            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.GetProperty <bool>("actor?"))
            .Do((actor, item) =>
            {
                Core.SendMessage(actor, "@convo cant converse");
                return(CheckResult.Disallow);
            })
            .Name("Can only converse with NPCs rule.");

            GlobalRules.Check <MudObject, MudObject>("can converse?")
            .Do((actor, item) => Core.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.GetProperty <MudObject>("interlocutor") == null)
            .Do(actor =>
            {
                Core.SendMessage(actor, "@convo nobody");
                return(PerformResult.Stop);
            })
            .Name("Need interlocutor to list topics rule.");

            GlobalRules.Perform <MudObject>("list topics")
            .Do(actor =>
            {
                var npc = actor.GetProperty <MudObject>("interlocutor");
                if (npc != null)
                {
                    var suggestedTopics = npc.GetProperty <List <MudObject> >("conversation-topics").AsEnumerable();

                    if (!ConversationSettings.ListDiscussedTopics)
                    {
                        suggestedTopics = suggestedTopics.Where(obj => !obj.GetProperty <bool>("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)
                    {
                        Core.SendMessage(actor, "@convo topic prompt", enumeratedSuggestedTopics);
                    }
                    else
                    {
                        GlobalRules.ConsiderPerformRule("no topics to discuss", actor, npc);
                    }
                }

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

            GlobalRules.DeclarePerformRuleBook <MudObject, MudObject>("no topics to discuss", "[Actor, NPC] : Handle there being no topics to list.");

            GlobalRules.Perform <MudObject, MudObject>("no topics to discuss")
            .Do((actor, npc) =>
            {
                Core.SendMessage(actor, "@convo no topics");
                return(PerformResult.Continue);
            })
            .Name("Default report no topics to discuss 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) =>
            {
                Core.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) && (ConversationSettings.AllowRepeats == false) && topic.GetProperty <bool>("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 #9
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);
        }