Exemple #1
0
    // Start is called before the first frame update
    void Start()
    {
        model = Model.GetInstance();
        model.AddListener(this);
        List <Cell> cells = model.GetCells(0);

        if (cells == null)
        {
            return;
        }

        for (int i = 0; i < model.GetNumCells(0); i++) //create the E-Coli objects for the cells
        {
            IPointAdapter point = cells[i].GetNextLocation();
            Movement      tmp   = Instantiate(eColi, new Vector3(point.GetX(), 1, point.GetZ()), Quaternion.Euler(0, cells[i].GetAngle(), 0));
            tmp.SetCell(cells[i]);
        }

        AbstractEnvironment environment = model.environment;

        if (environment is MultiLigandEnvironment)
        {
            foreach (Environment env in ((MultiLigandEnvironment)environment).GetEnvironments())
            {
                Instantiate(food, new Vector3(env.GetX(), 1.88f, env.GetZ()), Quaternion.identity);
            }
        }
        else
        {
            Instantiate(food, new Vector3(environment.GetX(), 1.88f, environment.GetX()), Quaternion.identity);
        }
    }
 public AbstractArena(AbstractPlayerCharacter PC, AbstractMonster enemy, AbstractEnvironment envi)
 {
     actionManager   = new ActionManager();
     PlayerCharacter = PC;
     Enemy           = enemy;
     Environment     = envi;
 }
        public CombatModel(AbstractPlayerCharacter PC, AbstractMonster monster, AbstractEnvironment environment)
        {
            PlayerCharacter      = PC;
            MonsterCharacter     = monster;
            EnvironmentCharacter = environment;

            Arena = new DEBUG_Arena(PC, monster, environment);
        }
 private Model()
 {
     //add code as it is needed
     environment        = new Environment(); //super base case just to prevent any scary null pointers
     timeScaleFactor    = 1;
     cells              = new List <Cell> [0];
     cellBirthListeners = new List <ICellBirthListener>();
     allCells           = new List <Cell>();
 }
Exemple #5
0
        void PlayCard(string notification, Object event_target, params object[] event_data)
        {
            AbstractCard        card        = (AbstractCard)event_data[0];
            AbstractPlayer      player      = (AbstractPlayer)event_data[1];
            AbstractPlayer      enemy       = (AbstractPlayer)event_data[2];
            AbstractEnvironment environment = (AbstractEnvironment)event_data[3];

            card.use(player, enemy, environment);
            app.Notify(notifications.CombatEventNotification.ProcessActions, this, null);
        }
Exemple #6
0
        public void DisplayInfoAntFood(RenderTarget target, AbstractEnvironment env)
        {
            var startPos = GetNavBarStartCoordinate();

            _textCount.DisplayedString = env.ListObject.Where(o => o.GetType() == typeof(Food)).ToList().Count.ToString();
            _imageFood.Position        = new Vector2f(startPos.X + 475, startPos.Y + 20);
            _textCount.Position        = new Vector2f(_imageFood.Position.X + _imageFood.Texture.Size.X / 2.0f - _textCount.GetGlobalBounds().Width / 2.0f + 5, _imageFood.Position.Y + _imageFood.GetGlobalBounds().Height - 20);
            target.Draw(_imageFood);
            target.Draw(_textCount);
        }
Exemple #7
0
        public void DisplayInfoAntPicker(RenderTarget target, AbstractEnvironment env)
        {
            var startPos = GetNavBarStartCoordinate();

            _textCount.DisplayedString = env.ListCharacter.Where(c => c.Death == false && c.GetType() == typeof(AntPicker)).ToList().Count.ToString();
            _imageAntPicker.Position   = new Vector2f(startPos.X + 345, startPos.Y + 20);
            _textCount.Position        = new Vector2f(_imageAntPicker.Position.X + _imageAntPicker.Texture.Size.X / 2.0f - _textCount.GetGlobalBounds().Width / 2.0f + 5, _imageAntPicker.Position.Y + _imageAntPicker.GetGlobalBounds().Height - 20);
            target.Draw(_imageAntPicker);
            target.Draw(_textCount);
        }
Exemple #8
0
    private Vector3Adapter CalculateNextPoint(float x, float z, AbstractEnvironment environment)
    {
        //calculates the angle that the cell should move towards based on the ligand gradient
        float alfa   = MathFloat.Atan2(environment.GradZ(x, z), environment.GradX(x, z));
        float factor = 1 - smartnessFactor;

        //calculates the x and z delta based on the angle and the sampled value
        float dx = MathFloat.Cos(alfa) * dT * v + (float)Normal.Sample(0.0, v * dT * factor);
        float dz = MathFloat.Sin(alfa) * dT * v + (float)Normal.Sample(0.0, v * dT * factor);

        //adds the delta to the x and z cords while making sure that it does not move outside the dish
        x = MathFloat.Clamp(x + dx, -14, 14);
        z = MathFloat.Clamp(z + dz, -14, 14);

        //create the new point
        return(new Vector3Adapter(x, z));
    }
        public static void Run()
        {
            AbstractEnvironment env1 = new AbstractEnvironment();
            env1.SetValue("A", new int[] {0,0,0});
            env1.SetValue("B", false);
            env1.SetValue("I", 32);
            AbstractEnvironment env2 = new AbstractEnvironment();
            env2.SetValue("A", new int[] {0,0,0});
            env2.SetValue("B", false);
            env2.SetValue("I", 32);

            SDDebug.Assert(env1.Equals(env2));

            env2.SetValue("A", new int[] {0,0,1});

            SDDebug.Assert(env1.Equals(env2) == false);
        }
Exemple #10
0
        public void CreateEgg(AbstractEnvironment env)
        {
            List <AbstractObject> foods = env.ListObject.Where(o => o.GetType() == typeof(Food)).ToList();

            if (foods.Count > 500 && foods.Count - 500 > 200 && Position.X == env.Position.X && Position.Y == env.Position.Y)
            {
                for (var i = 0; i < 500; i++)
                {
                    AbstractObject food = foods[i];
                    ListObject.Remove(food);
                }

                for (int i = 0; i < 5; i++)
                {
                    env.AddObject(_factoryEgg.CreateObject());
                }
            }
        }
        private static bool SpecializeExpression(ILFunction f, ILExpression expr, AbstractEnvironment env, StringBuilder source, List<FunctionCallContainer> calls)
        {
            if (expr.Type == ILExpressionType.Assign) {

                var left = expr.LeftNode;
                var right = expr.RightNode;
                if (left.Type == ILExpressionType.VariableAccess) {
                    var varName = left.Const.ToString();
                    try {
                        var rightRed = right.AbstactReduce(varName, env, f.LocalTypes);
                        if (rightRed is ILExpression) {
                                var fcall = rightRed as ILExpression;

                                SpecializeFunctionCall(f, fcall, varName, source, env, calls);
                                //source.Append("<<FUNCTION_CALL>>");
                        }
                        else if (env.IsDynamic(varName)) {

                            //source.Append(varName); source.Append(" := ");

                            if (rightRed is string) {
                                source.Append("\t"); source.Append(varName); source.Append(" := ");
                                source.Append((string)rightRed);
                                source.Append(";"); source.Append(System.Environment.NewLine);
                            }
                            else {
                                source.Append("\t"); source.Append(varName); source.Append(" := ");
                                source.Append(RenderConst(rightRed));
                                source.Append(";"); source.Append(System.Environment.NewLine);
                            }
                        }
                        else {
                            object val = rightRed;

                            System.Diagnostics.Debug.Assert(val is string == false);
                            System.Diagnostics.Debug.Assert(val is ILExpression == false);

                            env.SetValue(varName, val);
                        }
                    }
                    catch (NullReferenceException) {
                        source.Append("\t__spec_raise_null_ref_exception();"); source.Append(System.Environment.NewLine);
                    }
                    catch (IndexOutOfRangeException) {
                        source.Append("\t__spec_raise_index_out_of_range_exception();"); source.Append(System.Environment.NewLine);
                    }
                }
                else {
                    System.Diagnostics.Debug.Assert(left.Type == ILExpressionType.ArrayAccess);

                    try {
                        var arrayName = left.LeftNode.Const.ToString();
                        object index = null;
                        if (env.IsDynamic(left.RightNode.Const.ToString())) {
                            index = left.RightNode.Const.ToString();
                        } else {
                            index = env.GetValue(left.RightNode.Const.ToString());
                        }

                        object rightVar = null;
                        if (env.IsDynamic(right.Const.ToString())) {
                            rightVar = right.Const.ToString();
                        } else {
                            rightVar = env.GetValue(right.Const.ToString());
                        }

                        if (env.IsDynamic(arrayName)) {
                            source.Append("\t"); source.Append(arrayName); source.Append("["); source.Append(index); source.Append("] := ");
                            source.Append(rightVar); source.Append(";"); source.Append(System.Environment.NewLine);
                        }
                        else {
                            object[] a = (object[])env.GetValue(arrayName);
                            a[Convert.ToInt32(index)] = rightVar;
                        }
                    }
                    catch (NullReferenceException) {
                        source.Append("\t__spec_raise_null_ref_exception();"); source.Append(System.Environment.NewLine);
                    }
                    catch (IndexOutOfRangeException) {
                        source.Append("\t__spec_raise_index_out_of_range_exception();"); source.Append(System.Environment.NewLine);
                    }
                }

            }
            else if (expr.Type == ILExpressionType.FunctionCall) {
                source.Append("\t"); source.Append(expr.Const.ToString()); source.Append("(");
                for (int i = 0; i < expr.VAList.Count; ++i) {
                    string p = null;
                    if (env.IsDynamic(expr.VAList[i].Const.ToString())) {
                        p = expr.VAList[i].Const.ToString();
                    } else {
                        p = RenderConst(env.GetValue(expr.VAList[i].Const.ToString()));
                    }
                    source.Append(p);
                    if (i != expr.VAList.Count - 1)
                        source.Append(", ");
                }
                source.Append(");"); source.Append(System.Environment.NewLine);
            }

            return false;
        }
        private static FunctionSpecializationResult SpecializeFunction(
		                                                               ILFunction function, 
		                                                               Dictionary<string, object> pars, 
		                                                               List<string> forceDynamic)
        {
            var source = new StringBuilder(1024 * 1024);
            int LabelId = 1;

            var visited = new SpecPointContainer();
            var q = new Queue<SpecPoint>();
            var initEnv = new AbstractEnvironment();
            foreach (var kvp in pars) {
                initEnv.SetValue(kvp.Key, kvp.Value);
            }
            foreach (var kvp in function.LocalTypes) {
                if (pars.ContainsKey(kvp.Key))
                    continue;

                object val = null;
                if (kvp.Value.TypeEnum == L1Runtime.SyntaxTree.VariableTypeEnum.Integer)
                    val = 0;
                if (kvp.Value.TypeEnum == L1Runtime.SyntaxTree.VariableTypeEnum.Bool)
                    val = false;
                if (kvp.Value.TypeEnum == L1Runtime.SyntaxTree.VariableTypeEnum.Char)
                    val = (char)0;
                initEnv.SetValue(kvp.Key, val);
            }
            foreach (var d in forceDynamic) {
                initEnv.SetValue(d, Dynamic.Value);
            }

            initEnv = _bindTimeAnalyze(function, initEnv);
            initEnv.Trace();

            Dictionary<VariableType, List<string>> localVarsInfo = new Dictionary<VariableType, List<string>>();
            foreach (var kvp in function.LocalTypes) {
                if (initEnv.IsDynamic(kvp.Key) && !function.Parameters.Contains(kvp.Key)) {
                    if (localVarsInfo.ContainsKey(kvp.Value) == false) {
                        localVarsInfo.Add(kvp.Value, new List<string>());
                    }
                    localVarsInfo[kvp.Value].Add(kvp.Key);
                }
            }
            foreach (var kvp in localVarsInfo) {
                source.Append("\t"); source.Append(kvp.Key.ToCompileableString()); source.Append(" ");
                for (int i = 0; i < kvp.Value.Count; ++i) {
                    source.Append(kvp.Value[i]);
                    if (i != kvp.Value.Count - 1)
                        source.Append(", ");
                }
                source.Append(";"); source.Append(System.Environment.NewLine);
            }

            foreach (var kvp in pars) {
                if (kvp.Value != Dynamic.Value && initEnv.IsDynamic(kvp.Key)) {
                    source.Append("\t");
                    source.Append(function.LocalTypes[kvp.Key].ToCompileableString());
                    source.Append(" ");
                    source.Append(kvp.Key); source.Append(" := "); source.Append(RenderConst(kvp.Value));
                    source.Append(";"); source.AppendLine();
                }
            }

            source.Append("* Begin of function specialized body"); source.Append(System.Environment.NewLine);

            var initialSp = new SpecPoint { Env = initEnv, P = 1, L = LabelId++ } ;
            q.Enqueue(initialSp);
            initialSp.Env.Freeze();
            visited.AddSpecPoint(initialSp);

            int operations = 0;
            var calls = new List<FunctionCallContainer>();
            while (q.Count != 0) {
                operations++;

                var sp = q.Dequeue();
                var env = sp.Env.Clone();

                source.Append("L_"); source.Append(sp.L); source.Append(":"); source.Append(System.Environment.NewLine);

                bool stopped = false;
                int p = sp.P;

                while (!stopped) {
                    var instr = function.Body[p - 1];

                    if (instr is ILExpression) {
                        var expr = instr as ILExpression;

                        stopped = SpecializeExpression(function, expr, env, source, calls);

                        p++;
                    }
                    else if (instr is ILBranch) {
                        var br = (instr as ILBranch);

                        var condVar = br.Condition.Const.ToString();
                        if (env.IsDynamic(condVar)) {

                            env.Freeze();
                            var succSp = visited.GetSpecPoint(br.SuccessJump, env);
                            var failSp = visited.GetSpecPoint(br.FailJump, env);

                            if (succSp == null) {
                                succSp = new SpecPoint { Env = env, P = br.SuccessJump, L = LabelId++ };
                                q.Enqueue(succSp);
                                visited.AddSpecPoint(succSp);
                            }
                            if (failSp == null) {
                                failSp = new SpecPoint { Env = env, P = br.FailJump, L = LabelId++ };
                                q.Enqueue(failSp);
                                visited.AddSpecPoint(failSp);
                            }

                            source.Append("\tif "); source.Append(condVar); source.Append(" then ");
                            source.Append("goto "); source.Append("L_"); source.Append(succSp.L);
                            source.Append(" else ");
                            source.Append("goto "); source.Append("L_"); source.Append(failSp.L);
                            source.Append(" end;"); source.Append(System.Environment.NewLine);

                            stopped = true;
                        }
                        else {
                            var cond = (bool)env.GetValue(condVar);
                            if (cond)
                                p = br.SuccessJump;
                            else
                                p = br.FailJump;
                        }
                    }
                    else if (instr is ILGoto) {
                        p = (instr as ILGoto).GoTo;
                    }
                    else if (instr is ILReturn) {
                        var ret = (instr as ILReturn);

                        if (ret.Return == null) {
                            source.Append("\treturn;"); source.Append(System.Environment.NewLine);
                        }
                        else {
                            source.Append("\treturn ");
                            var retVar = ret.Return.Const.ToString();
                            if (env.IsDynamic(retVar)) {
                                source.Append(retVar);
                            }
                            else {
                                m_renderConst(source, env.GetValue(retVar));
                            }
                            source.Append(";"); source.Append(System.Environment.NewLine);
                        }

                        stopped = true;
                    }
                }

                //TODO: Stop if too big query and try with more dynamic variables

                if (operations >= MaximimOperations) {
                    var ch = visited.GetMostChangeableVariables();
                    forceDynamic = forceDynamic.Union(ch).ToList();

                    var res1 = SpecializeFunction(function, pars, forceDynamic);
                    return res1;
                }
            }
            source.Append("0").AppendLine();

            //System.Console.WriteLine(source.ToString());

            var r = new FunctionSpecializationResult { FunctionCallsNeedToResolve = calls, Source = source, Function = function, SpecializationRules = pars };
            return r;
        }
        internal static AbstractEnvironment _bindTimeAnalyze(ILFunction func, AbstractEnvironment initialEnvironment)
        {
            var env = initialEnvironment;
            int dynamicAdded = 0;

            do {
                dynamicAdded = 0;
                foreach (var instr in func.Body) {
                    if (instr is ILExpression) {
                        var expr = instr as ILExpression;
                        if (expr.Type == ILExpressionType.Assign) {

                            //Case 1 - assign to local variable
                            if (expr.LeftNode.Type == ILExpressionType.VariableAccess) {
                                if (m_simpleExprContainsDynamicVariables(expr.RightNode, env)) {
                                    //If reading from static array by dynamic index
                                    if (expr.RightNode.Type == ILExpressionType.ArrayAccess) {
                                        if (env.IsDynamic(expr.RightNode.LeftNode.Const.ToString()) == false) {
                                            env.SetValue(expr.RightNode.LeftNode.Const.ToString(), Dynamic.Value);
                                            dynamicAdded++;
                                        }
                                    }
                                    if (env.IsDynamic(expr.LeftNode.Const.ToString()) == false) {
                                        env.SetValue(expr.LeftNode.Const.ToString(), Dynamic.Value);
                                        dynamicAdded++;
                                    }
                                }
                            }
                            //Case 2 - assign to array
                            else if (expr.LeftNode.Type == ILExpressionType.ArrayAccess) {

                                // A(s,d)[D] = S, D -> A:=D
                                if (m_simpleExprContainsDynamicVariables(expr.LeftNode, env)) {
                                    if (env.IsDynamic(expr.LeftNode.LeftNode.Const.ToString()) == false) {
                                        env.SetValue(expr.LeftNode.LeftNode.Const.ToString(), Dynamic.Value);
                                        dynamicAdded++;
                                    }
                                }
                                // A(s)[S] = D -> A:=D
                                else if (m_simpleExprContainsDynamicVariables(expr.RightNode, env)) {
                                    if (env.IsDynamic(expr.LeftNode.LeftNode.Const.ToString()) == false) {
                                        env.SetValue(expr.LeftNode.LeftNode.Const.ToString(), Dynamic.Value);
                                        dynamicAdded++;
                                    }
                                }
                                // A(s)[S] = S - do nothing, array is static (in this scope

                            }

                        }

                    }
                }
            } while (dynamicAdded != 0);

            return env;
        }
 //Simple expr contains dynamic variables?
 private static bool m_simpleExprContainsDynamicVariables(ILExpression expr, AbstractEnvironment env)
 {
     if (expr.Type == ILExpressionType.FunctionCall) {
         if (expr.Function.EmbeddedBody != null && !expr.Function.CanBeCalculatedWithoutRun)
             return true;
         foreach (var arg in expr.VAList) {
             if (env.IsDynamic(arg.Const.ToString()))
                 return true;
         }
         if (expr.Function.CanBeCalculatedWithoutRun == false) {
             return true;
         }
     } else if (expr.Type == ILExpressionType.VariableAccess) {
         if (env.IsDynamic(expr.Const.ToString()))
             return true;
     } else {
         if (expr.LeftNode != null) {
             if (env.IsDynamic(expr.LeftNode.Const.ToString()))
                 return true;
         }
         if (expr.RightNode != null) {
             if (env.IsDynamic(expr.RightNode.Const.ToString()))
                 return true;
         }
     }
     return false;
 }
        //Reduce RIGHT part of il_expression
        public object AbstactReduce(string leftVariable, AbstractEnvironment state, Dictionary<string, VariableType> localTypeScope)
        {
            if (Type == ILExpressionType.Const) {
                if (Const is string) {
                    var s = (string)Const;
                    var a = new object[s.Length];
                    for (int i = 0; i < s.Length; ++i) {
                        a[i] = (int)s[i];
                    }
                    return a;
                }
                return Const;
            }
            //Only variable
            else if (Type == ILExpressionType.VariableAccess) {

                if (state.IsDynamic(Const.ToString()))
                    return Const.ToString();
                else
                    return state.GetValue(Const.ToString());
            }
            else if (Type == ILExpressionType.Alloc) {
                bool isDynamicLeftPart = state.IsDynamic(leftVariable);
                if (isDynamicLeftPart || state.IsDynamic(LeftNode.Const.ToString())) {
                    return "new " + localTypeScope[leftVariable].NestedType.ToCompileableString() + "[" + v(state, LeftNode.Const.ToString()) + "]";
                }
                else
                {
                    int arraySize = (int)state.GetValue(LeftNode.Const.ToString());
                    var a = new object[arraySize];
                    object defElem = null;
                    if (localTypeScope[leftVariable].NestedType.TypeEnum == VariableTypeEnum.Integer) {
                        defElem = 0;
                    }
                    if (localTypeScope[leftVariable].NestedType.TypeEnum == VariableTypeEnum.Bool) {
                        defElem = false;
                    }
                    for (int i = 0; i < arraySize; ++i) {
                        a[i] = defElem;
                    }
                    return a;
                }
            }
            else if (Type == ILExpressionType.FunctionCall) {
                return this;
            }
            //Binary expression & unary
            else {
                object r = Eval(state);
                if (r == Dynamic.Value)
                    return GetOp(LeftNode.Const.ToString(), (RightNode != null) ? RightNode.Const.ToString() : "", Type, state);
                else
                    return r;
            }
        }
 public void InitializeCombat(AbstractPlayerCharacter PC, AbstractMonster Monster, AbstractEnvironment Environment)
 {
     SetModelState(ModelState.Combat);
     combat_model = new CombatModel(PC, Monster, Environment);
 }
 public abstract void use(AbstractPlayer Self, AbstractPlayer Enemy, AbstractEnvironment Environment);
 //REDNER variable or it's value
 private object v(AbstractEnvironment env, string vname)
 {
     if (env.IsDynamic(vname))
         return vname;
     else
         return rc(env.GetValue(vname));
 }
Exemple #19
0
 public override void use(AbstractPlayer Self, AbstractPlayer Enemy, AbstractEnvironment Environment)
 {
     throw new NotImplementedException();
 }
        private static void SpecializeFunctionCall(ILFunction context, 
		                                           ILExpression fcall, 
		                                           string resultVar, 
		                                           StringBuilder source, 
		                                           AbstractEnvironment env, 
		                                           List<FunctionCallContainer> calls)
        {
            var f = fcall.Function;
            //Stdlib function
            if (f.EmbeddedBody != null) {
                if (f.CanBeCalculatedWithoutRun && !fcall.VAList.Any(pexpr => env.IsDynamic(pexpr.Const.ToString()))) {

                    var args = fcall.VAList.Select(ilexpr => ilexpr.Eval(env))
                        .Select(o => (o is Int32) ? o : ( (o is Boolean) ? o : L1Runtime.L1Runtime.GetArrayFromObjectArray((object[])o))).ToArray();
                    var result = f.EmbeddedBody.Invoke(null, args);

                    if (env.IsDynamic(resultVar)) {
                        source.Append("\t"); source.Append(resultVar); source.Append(" := "); RenderConst(result); source.Append(");"); source.Append(System.Environment.NewLine);
                    } else {
                        if (result is L1Runtime.L1Array<int>) {
                            var l1arr = result as L1Runtime.L1Array<int>;
                            var a = new object[l1arr.GetLength()];
                            for (int i = 0; i < a.Length; ++i) {
                                a[i] = l1arr.GetValue(i);
                            }
                            result = a;
                        }
                        env.SetValue(resultVar, result);
                    }
                } else {
                    source.Append("\t"); source.Append(resultVar); source.Append(" := "); source.Append(f.Name); source.Append("(");
                    for (int i = 0; i < fcall.VAList.Count; ++i) {
                        var p = fcall.VAList[i].Const.ToString();
                        if (env.IsDynamic(p)) {
                            source.Append(p);
                        } else {
                            source.Append(RenderConst(env.GetValue(p)));
                        }
                        if (i != fcall.VAList.Count - 1)
                            source.Append(", ");
                    }
                    source.Append(");"); source.Append(System.Environment.NewLine);
                }
            } else {
                if (f.CanBeCalculatedWithoutRun && !fcall.VAList.Any(pexpr => env.IsDynamic(pexpr.Const.ToString()))) {
                    var args = fcall.VAList.Select(ilexpr => ilexpr.Eval(env)).ToArray();
                    var res = f.Call(args);

                    if (env.IsDynamic(resultVar)) {
                        source.Append("\t").Append(resultVar).Append(" := ").Append(RenderConst(res)).Append(";").AppendLine();
                    } else {
                        env.SetValue(resultVar, res);
                    }
                }
                else {
                    var functionCallRef = new FunctionCallContainer {
                        Function = fcall.Function,
                        SourceFunction = context,
                        ResultVariable = resultVar,
                        Parameters = fcall.VAList.Select(ilexpr => env.IsDynamic(ilexpr.Const.ToString()) ? ilexpr.Const.ToString() : env.GetValue(ilexpr.Const.ToString())).ToList()
                    };
                    calls.Add(functionCallRef);
                    source.Append("\t").Append(functionCallRef.ToString()).AppendLine();
                }
            }
        }
Exemple #21
0
 public DEBUG_Arena(AbstractPlayerCharacter PC, AbstractMonster enemy, AbstractEnvironment envi) :
     base(PC, enemy, envi)
 {
 }
Exemple #22
0
        public virtual object Call(object[] arguments)
        {
            if (arguments.Length != Parameters.Count)
            {
                throw new InvalidOperationException("Interpreter bad error: function call with bad number of arguments =(");
            }

            AbstractEnvironment env = new AbstractEnvironment();
            for (int i = 0; i < Parameters.Count; ++i)
            {
                env.SetValue(Parameters[i], arguments[i]);
            }

            int PC = 1;
            var curr = Body[0];
            while (curr is ILReturn == false)
            {
                curr = Body[PC - 1];
                if (curr is ILExpression)
                {
                    object r = (curr as ILExpression).Eval(env);
                    if (r == Dynamic.Value)
                        throw new InvalidOperationException("Interpreter bad error: try to calculate dynamic function");
                    PC++;
                }
                else if (curr is ILBranch)
                {
                    object r = (curr as ILBranch).Condition.Eval(env);
                    if (r == Dynamic.Value)
                        throw new InvalidOperationException("Interpreter bad error: try to calculate dynamic function");
                    bool br = (bool)r;
                    if (br)
                        PC = (curr as ILBranch).SuccessJump;
                    else
                        PC = (curr as ILBranch).FailJump;
                }
                else if (curr is ILGoto)
                {
                    PC = (curr as ILGoto).GoTo;
                }
            }
            object result = (curr as ILReturn).Return.Eval(env);
            if (result == Dynamic.Value)
                throw new InvalidOperationException("Interpreter bad error: try to calculate dynamic function");

            return result;
        }
 public override void use(AbstractPlayer Self, AbstractPlayer Enemy, AbstractEnvironment Environment)
 {
     AbstractArena.actionManager.Add(new DamageAction(Self, CardTargettingUtility.GetPCTargets(this, Self, Enemy, Environment), BASEDAMAGE));
 }
 ///env must be freezed!!!
 public SpecPoint GetSpecPoint(int P, AbstractEnvironment env)
 {
     if (f_container.ContainsKey(P)) {
         var list = f_container[P];
         var hash = env.GetHashCode();
         foreach (var sp in list) {
             if (sp.Env.GetHashCode() == hash && sp.Env.Equals(env))
                 return sp;
         }
     }
     return null;
 }
        public object Eval(AbstractEnvironment state)
        {
            #region +, -, *, /, mod, pow

            if (Type == ILExpressionType.Plus)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left + (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Minus)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left - (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Mul)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left * (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Div)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left / (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Mod)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left % (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Pow)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return L1Runtime.L1Runtime.Deg((int)left, (int)right);
                else
                    return Dynamic.Value;
            }

            #endregion

            #region []

            if (Type == ILExpressionType.ArrayAccess)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return ((Array)left).GetValue((int)right);
                else
                    return Dynamic.Value;
            }

            #endregion

            #region >, >=, <, <=, =, <>

            if (Type == ILExpressionType.Gr)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left > (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Greq)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left >= (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Le)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left < (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Leeq)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left <= (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Eq)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left == (int)right;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.NotEq)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (int)left != (int)right;
                else
                    return Dynamic.Value;
            }

            #endregion

            #region Uminus, Unot

            if (Type == ILExpressionType.Uminus)
            {
                object left = LeftNode.Eval(state);
                if (left != Dynamic.Value)
                    return - (int)left;
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.Unot)
            {
                object left = LeftNode.Eval(state);
                if (left != Dynamic.Value)
                    return !(bool)left;
                else
                    return Dynamic.Value;
            }

            #endregion

            #region And, Or, Xor

            if (Type == ILExpressionType.And)
            {
                throw new InvalidOperationException("And operator is not allowed in IL!");
            }
            if (Type == ILExpressionType.Or)
            {
                throw new InvalidOperationException("Or operator is not allowed in IL!");
            }
            if (Type == ILExpressionType.Xor)
            {
                object left = LeftNode.Eval(state);
                object right = RightNode.Eval(state);
                if (left != Dynamic.Value && right != Dynamic.Value)
                    return (bool)left ^ (bool)right;
                else
                    return Dynamic.Value;
            }

            #endregion

            #region Alloc, ArrayLength, VariableAccess, Const

            if (Type == ILExpressionType.Alloc)
            {
                object left = LeftNode.Eval(state);
                if (left != Dynamic.Value) {
                    int size = (int)left;
                    object[] r = new object[size];
                    if (OriginalType.NestedType == VariableType.IntType) {
                        for (int i = 0; i < size; ++i) {
                            r[i] = 0;
                        }
                    }
                    if (OriginalType.NestedType == VariableType.BoolType) {
                        for (int i = 0; i < size; ++i) {
                            r[i] = false;
                        }
                    }
                    return r;
                }
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.ArrayLength)
            {
                object left = LeftNode.Eval(state);
                if (left != Dynamic.Value)
                    return ((Array)left).GetLength(0);
                else
                    return Dynamic.Value;
            }
            if (Type == ILExpressionType.VariableAccess)
            {
                return state.GetValue((string)Const);
            }
            if (Type == ILExpressionType.Const)
            {
                if (Const is string) {
                    var s = (string)Const;
                    var a = new object[s.Length];
                    for (int i = 0; i < s.Length; ++i) {
                        a[i] = (int)s[i];
                    }
                    return a;
                }
                return Const;
            }

            #endregion

            #region Assign

            if (Type == ILExpressionType.Assign)
            {
                object right = RightNode.Eval(state);
                if (LeftNode.Type == ILExpressionType.VariableAccess)
                {
                    state.SetValue((string)LeftNode.Const, right);
                    return right;
                }
                else if (LeftNode.Type == ILExpressionType.ArrayAccess)
                {
                    object array = LeftNode.LeftNode.Eval(state);
                    object index = LeftNode.RightNode.Eval(state);
                    if (array == Dynamic.Value)
                    {
                        return right;
                    }
                    else
                    {
                        if (index == Dynamic.Value)
                        {
                            //Set whole array as Dynamic
                            state.SetArrayAsDynamic(array);
                        }
                        else
                        {
                            ((Array)array).SetValue(right, (int)index);
                        }
                        return right;
                    }
                }
                else
                {
                    throw new InvalidOperationException("Bad assign construction!");
                }
            }

            #endregion

            #region FunctionCall

            if (Type == ILExpressionType.FunctionCall)
            {
                string functionName = (string)Const;

                var args = new List<object>();
                bool isDynamic = false;
                foreach (ILExpression expr in VAList)
                {
                    object arg = expr.Eval(state);
                    args.Add(arg);
                    if (arg == Dynamic.Value)
                        isDynamic = true;
                }
                if (isDynamic)
                    return Dynamic.Value;
                else
                    return Function.Call(args.ToArray());;
            }

            #endregion

            throw new InvalidOperationException("Bad interpreter error! =(");
        }
        public static List <AbstractPlayer> GetPCTargets(AbstractCard Card, AbstractPlayer Self, AbstractPlayer Enemy, AbstractEnvironment Environment)
        {
            List <AbstractPlayer> Targets = new List <AbstractPlayer>();

            switch (Card.GetCardTarget())
            {
            case AbstractCard.CardTarget.All:
                Targets.Add(Self);
                Targets.Add(Enemy);
                Targets.Add(Environment);
                break;

            case AbstractCard.CardTarget.Enemy:
                Targets.Add(Enemy);
                break;

            case AbstractCard.CardTarget.Environment:
                Targets.Add(Environment);
                break;

            case AbstractCard.CardTarget.Self:
                Targets.Add(Self);
                break;
            }
            return(Targets);
        }
        private string GetOp(string arg1, string arg2, ILExpressionType type, AbstractEnvironment env)
        {
            if (type == ILExpressionType.ArrayLength) {
                return "ArrayLength(" + arg1 + ")";
            }
            if (type == ILExpressionType.Unot) {
                return "not " + arg1;
            }
            if (type == ILExpressionType.Uminus) {
                return "-" + arg1;
            }
            if (type == ILExpressionType.ArrayAccess) {
                return v(env, arg1) + "[" + v(env, arg2) + "]";
            }

            string op = "<<unknown_binary_op>>";
            if (type == ILExpressionType.Plus) op = " + ";
            if (type == ILExpressionType.Minus) op = " - ";
            if (type == ILExpressionType.Mul) op = " * ";
            if (type == ILExpressionType.Div) op = " / ";
            if (type == ILExpressionType.Pow) op = " ** ";
            if (type == ILExpressionType.Mod) op = " mod ";
            if (type == ILExpressionType.Gr) op = " > ";
            if (type == ILExpressionType.Greq) op = " >= ";
            if (type == ILExpressionType.Le) op = " < ";
            if (type == ILExpressionType.Leeq) op = " <= ";
            if (type == ILExpressionType.Eq) op = " = ";
            if (type == ILExpressionType.NotEq) op = " <> ";
            if (type == ILExpressionType.Xor) op = " xor ";

            return v(env, arg1) + op + v(env, arg2);
        }