]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Script.cs
Fixed path finder thrown exception when finding a path to the cell you are already at.
[chaz/carfire] / CarFire / CarFire / CarFire / Script.cs
index 5ae6908b11475262ffdeaaa97fce6a29886d6051..146bb22685249cf7db7449c521a1cbe644aef819 100644 (file)
 using System.Collections.Generic;\r
 using System.Linq;\r
 using System.Text;\r
+using System.Reflection;\r
+using System.Diagnostics;\r
 \r
 namespace CarFire\r
 {\r
     public class Script\r
     {\r
-        #region Public Types\r
+        #region Public Properties\r
 \r
-        public enum Status\r
+        public bool IsRunning { get { return mIsRunning; } }\r
+\r
+        #endregion\r
+\r
+\r
+        #region Public Methods\r
+\r
+        public Script(string code, Game game)\r
         {\r
-            Waiting,\r
-            Done,\r
+            mGame = game;\r
+\r
+            string[] functions = Parse.List(code);\r
+            if (functions != null)\r
+            {\r
+                foreach (string function in functions)\r
+                {\r
+                    string[] parts = Parse.Function(function);\r
+                    if (parts != null)\r
+                    {\r
+                        string[] args = Parse.List(parts[1]);\r
+                        if (args != null)\r
+                        {\r
+                            Function func = new Function(parts[0], args);\r
+                            mFunctions.Add(func);\r
+                        }\r
+                        else throw new Exception("Arguments could not be parsed: " + parts[1]);\r
+                    }\r
+                    else throw new Exception("Function could not be parsed: " + function);\r
+                }\r
+            }\r
+            else throw new Exception("Script could not be parsed: " + code);\r
         }\r
 \r
-        public enum Function\r
+        public bool Run(Player player)\r
         {\r
-            Create,\r
-            Has,\r
-            Play,\r
-            Remove,\r
-            UseUp,\r
-            Wait,\r
+            bool result = false;\r
+\r
+            if (!mIsRunning)\r
+            {\r
+                mIsRunning = true;\r
+                mRunningIndex = 0;\r
+            }\r
+\r
+            for (; mRunningIndex < mFunctions.Count; mRunningIndex++)\r
+            {\r
+                result = Call(mRunningIndex, player);\r
+                if (!result) break;\r
+            }\r
+\r
+            if (mRunningIndex >= mFunctions.Count - 1) mIsRunning = false;\r
+            return result;\r
         }\r
 \r
         #endregion\r
 \r
 \r
-        #region Public Methods\r
+        #region Private Methods\r
 \r
-        public Script(string code)\r
+        bool Call(int index, Player player)\r
         {\r
+            Debug.Assert(0 <= index && index < mFunctions.Count);\r
+            try\r
+            {\r
+                object[] args = new object[2];\r
+                args[0] = player;\r
+                args[1] = mFunctions[index].Arguments;\r
+                return (bool)typeof(Impl).InvokeMember(mFunctions[index].Name, BindingFlags.InvokeMethod, null, null, args);\r
+            }\r
+#pragma warning disable 0168\r
+            catch (System.MissingMethodException ex)\r
+#pragma warning restore 0168\r
+            {\r
+                throw new Exception("Function could not be found: " + mFunctions[index].Name);\r
+            }\r
         }\r
 \r
-        public Status Run()\r
+        #endregion\r
+\r
+\r
+        #region Private Types\r
+\r
+        class Impl\r
         {\r
-            return Status.Done;\r
+            public static bool True(Player player, string[] args)\r
+            {\r
+                return true;\r
+            }\r
+\r
+            public static bool False(Player player, string[] args)\r
+            {\r
+                return false;\r
+            }\r
+\r
+            public static bool Has(Player player, string[] args)\r
+            {\r
+                return false;\r
+            }\r
+\r
+            public static bool Print(Player player, string[] args)\r
+            {\r
+                foreach (string arg in args)\r
+                {\r
+                    string line = Parse.String(arg);\r
+                    if (line != null) Console.WriteLine(line);\r
+                }\r
+                return true;\r
+            }\r
         }\r
 \r
-        public Status Evaluate(out bool value)\r
+        class Function\r
         {\r
-            value = true;\r
-            return Status.Done;\r
+            public string Name { get { return mName; } }\r
+            public string[] Arguments { get { return mArgs; } }\r
+\r
+            public Function(string name, string[] args)\r
+            {\r
+                mName = name;\r
+                mArgs = args;\r
+            }\r
+\r
+            string mName;\r
+            string[] mArgs;\r
         }\r
 \r
         #endregion\r
+\r
+\r
+        #region Private Variables\r
+\r
+        Game mGame;\r
+        List<Function> mFunctions = new List<Function>();\r
+        bool mIsRunning;\r
+        int mRunningIndex;\r
+        Impl mImpl = new Impl();\r
+\r
+        #endregion\r
     }\r
 }\r
This page took 0.023768 seconds and 4 git commands to generate.