Пример #1
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable objectVariable = executor.GetVariableByName(characterName);
            ObjectVariable numberVariable = executor.FindValueByString(numberName);

            if (objectVariable == null)
            {
                throw SchemeExecutor.CreateException($"Character '{characterName}' not found");
            }
            if (numberVariable.Type != VariableTypes.Number)
            {
                throw SchemeExecutor.CreateException($"Number is expected to be of type number");
            }

            int requiredNumber = (int)numberVariable.Value;

            if (!(objectVariable.Value is Character))
            {
                throw SchemeExecutor.CreateException("Items can only be added to characters");
            }

            Character character = (Character)(objectVariable.Value);

            ObjectVariable itemVariable = executor.GetVariableByName(itemName);

            if (!executor.CheckTypeCompatibility(VariableTypes.Object, itemVariable.Type))
            {
                throw SchemeExecutor.CreateException($"Type '{itemVariable.Type}' is not an object type");
            }

            character.AddItem(executor.Game, (Object)itemVariable.Value, requiredNumber);
        }
Пример #2
0
        public SlotSelectorDialog(Object item, Game game)
        {
            InitializeComponent();

            ComboBoxItem bagItem = new ComboBoxItem();

            bagItem.Content = "Bag";
            bagItem.Tag     = SpecialSlots.Bag;
            cb.Items.Add(bagItem);

            foreach (var slot in game.Config.CharacterConfig.InventorySlots)
            {
                if (SchemeExecutor.CheckTypeCompatibility(slot.Type, item.Scheme?.Name, game.Config))
                {
                    ComboBoxItem cbItem = new ComboBoxItem();
                    cbItem.Content = slot.Name;
                    cbItem.Tag     = slot.Name;
                    cb.Items.Add(cbItem);

                    if (game.CurrentPlayer.Character.GetItemBySlot(slot.Name, game.Config) == item)
                    {
                        cbItem.IsSelected = true;
                    }
                }
            }

            if (cb.SelectedItem == null)
            {
                bagItem.IsSelected = true;
            }
        }
Пример #3
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable objectVariable = executor.GetVariableByName(characterName);

            if (objectVariable == null)
            {
                throw SchemeExecutor.CreateException($"Character '{characterName}' not found");
            }

            if (!(objectVariable.Value is Character))
            {
                throw SchemeExecutor.CreateException("Spells can only be added to characters");
            }

            Character character = (Character)(objectVariable.Value);

            ObjectVariable spellVariable = executor.GetVariableByName(spellName);

            if (!executor.CheckTypeCompatibility(VariableTypes.Object, spellVariable.Type))
            {
                throw SchemeExecutor.CreateException($"Type '{spellVariable.Type}' is not an object type");
            }

            character.AddSpell((Object)spellVariable.Value);
        }
Пример #4
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable @object        = executor.GetVariableByName(objectName);
            ObjectVariable numberVariable = executor.FindValueByString(numberName);

            if (@object == null)
            {
                throw SchemeExecutor.CreateException($"Object '{objectName}' not found");
            }
            if (numberVariable.Type != VariableTypes.Number)
            {
                throw SchemeExecutor.CreateException($"Number is expected to be of type number");
            }

            int requiredNumber = (int)numberVariable.Value;

            if (!(@object.Value is Character))
            {
                throw new SchemeExecutionException($"Object '{objectName}' is not a character");
            }

            Character character = (Character)(@object.Value);

            int ownedNumber = character.CountItem(itemName);

            bool owns = ownedNumber >= requiredNumber;

            executor.SetVariable(targetName, new ObjectVariable(VariableTypes.Logical, "", owns));
        }
Пример #5
0
 public void Execute(SchemeExecutor executor)
 {
     if (Evaluate(executor))
     {
         executor.Jump(Line);
     }
 }
Пример #6
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable property = executor.GetPropertyOf(propertyName, objectName);
            ObjectVariable value    = executor.FindValueByString(valueName);

            property.Value = value.Value;
        }
Пример #7
0
        protected override bool GetResult(ObjectVariable variable1, ObjectVariable variable2)
        {
            //In case of numbers and logicals we compare values
            if (variable1.Type == VariableTypes.Number)
            {
                if (variable2.Type != VariableTypes.Number)
                {
                    throw SchemeExecutor.CreateException("Cannot compare number and non-number");
                }
                int value1_int = (int)variable1.Value;
                int value2_int = (int)variable2.Value;
                return(value1_int == value2_int);
            }
            if (variable1.Type == VariableTypes.Logical)
            {
                if (variable2.Type != VariableTypes.Logical)
                {
                    throw SchemeExecutor.CreateException("Cannot compare logical and non-logical");
                }
                bool value1_bool = (bool)variable1.Value;
                bool value2_bool = (bool)variable2.Value;
                return(value1_bool == value2_bool);
            }

            //In case of other types, we compare if they are the same object
            return(variable1.Value == variable2.Value);
        }
Пример #8
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable variable1 = executor.FindValueByString(value1);
            ObjectVariable variable2 = executor.FindValueByString(value2);

            bool result = GetResult(variable1, variable2);

            executor.SetVariable(target, new ObjectVariable(VariableTypes.Logical, "", result));
        }
Пример #9
0
        //Executes function and returns the action points to be removed from current player
        public int Execute(Object @object, Object actor, Game game)
        {
            SchemeExecutor executor = new SchemeExecutor(@object, actor, Commands, game);

            if (executor.Execute())
            {
                return(ActionPoints);
            }
            return(0);
        }
Пример #10
0
        public void Execute(SchemeExecutor executor)
        {
            var objVar = executor.GetVariableByName(objectName);

            if (objVar != null)
            {
                var obj = (Object)objVar.Value;
                obj.IsVisible = !obj.IsVisible;
            }
        }
Пример #11
0
        protected override bool GetResult(ObjectVariable variable1, ObjectVariable variable2)
        {
            if (variable1.Type != VariableTypes.Number || variable2.Type != VariableTypes.Number)
            {
                throw SchemeExecutor.CreateException("Cannot compare non-numbers");
            }

            int value1_int = (int)variable1.Value;
            int value2_int = (int)variable2.Value;

            return(value1_int < value2_int);
        }
Пример #12
0
        public bool MoveItemToSlot(Item item, string slotName, Config config)
        {
            var variable = GetVariableByName(slotName, config);

            if (item.Scheme == null || !SchemeExecutor.CheckTypeCompatibility(variable.Type, item.Scheme.Name, config))
            {
                return(false);
            }

            variable.Value = item;
            return(true);
        }
Пример #13
0
        protected override bool Evaluate(SchemeExecutor executor)
        {
            ObjectVariable variable = executor.FindValueByString(value);

            if (variable.Type != VariableTypes.Logical)
            {
                throw SchemeExecutor.CreateException("Cannot use non-logical value in JF");
            }

            bool value_bool = (bool)variable.Value;

            return(!value_bool);
        }
Пример #14
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable variable = executor.FindValueByString(value);

            if (variable.Type != VariableTypes.Logical)
            {
                throw SchemeExecutor.CreateException("Cannot invert non-logical");
            }

            bool value_bool = (bool)variable.Value;

            executor.SetVariable(target, new ObjectVariable(VariableTypes.Logical, "", (!value_bool)));
        }
Пример #15
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable objectVariable = executor.GetVariableByName(objectName);

            if (!executor.CheckTypeCompatibility(VariableTypes.Object, objectVariable.Type))
            {
                throw SchemeExecutor.CreateException($"Object has to be compatible with 'object' (actual type: '{objectVariable.Type}')");
            }

            Object @object = (Object)objectVariable.Value;

            @object.ForbidAttribute(attributeName);
        }
Пример #16
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable objectVariable = executor.GetVariableByName(objectName);

            // ObjectVariable property = executor

            //Uses of 'is':
            //  object has attribute                actor is forest_wanderer
            //  class var                           race of actor is dwarf          // -> OF(race, actor, _0) EQUALS(_0, dwarf, _0)
            //  item/spell is the same as...        some_item is sword_1            //some_item.Name == "sword_1"

            /*
             * 1. Check if object's name equals to propertyName
             * 2. Check if object has attribute named propertyName
             */

            bool value = false;

            //If we check class variable
            if (executor.Game.Config.IsClassType(objectVariable.Type))
            {
                //we search for the required class
                var c = executor.FindValueByString(propertyName);
                //then we compare it to the classvar
                value = objectVariable.Value == c.Value;
            }
            else
            {
                if (!executor.CheckTypeCompatibility(VariableTypes.Object, objectVariable.Type))
                {
                    throw SchemeExecutor.CreateException("Object has to be compatible with 'object'");
                }

                Object @object = (Object)objectVariable.Value;

                if (@object.Name == propertyName)
                {
                    value = true;
                }
                else if (@object.HasAttribute(propertyName))
                {
                    value = true;
                }
            }

            executor.SetVariable(target, new ObjectVariable(VariableTypes.Logical, "", value));
        }
Пример #17
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable variable1 = executor.FindValueByString(value1);
            ObjectVariable variable2 = executor.FindValueByString(value2);

            if (variable1.Type != VariableTypes.Logical)
            {
                throw new Exception("Cannot do logical operation with non-logical variables");
            }

            bool value1_bool = (bool)variable1.Value;
            bool value2_bool = (bool)variable2.Value;

            bool result = GetResult(value1_bool, value2_bool);

            executor.SetVariable(target, new ObjectVariable(VariableTypes.Logical, "", result));
        }
Пример #18
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable variable1 = executor.FindValueByString(value1);
            ObjectVariable variable2 = executor.FindValueByString(value2);

            if (variable1.Type != VariableTypes.Number)
            {
                throw new Exception("Cannot do operation with non-number variables");
            }

            int value1_int = (int)variable1.Value;
            int value2_int = (int)variable2.Value;

            int result = GetResult(value1_int, value2_int);

            executor.SetVariable(target, new ObjectVariable(VariableTypes.Number, "", result));
        }
Пример #19
0
        public void Execute(SchemeExecutor executor)
        {
            ObjectVariable @object = executor.GetVariableByName(objectName);

            if (@object == null)
            {
                throw SchemeExecutor.CreateException($"Object '{objectName}' not found");
            }

            if (!(@object.Value is Character))
            {
                throw new SchemeExecutionException($"Object '{objectName}' is not a character");
            }

            Character character = (Character)(@object.Value);

            bool owns = character.KnowsSpell(spellName);

            executor.SetVariable(targetName, new ObjectVariable(VariableTypes.Logical, "", owns));
        }
Пример #20
0
        public static void SolveSimpleValueRegs(SchemeFunction function)
        {
            //We iterate commands
            //If we find a SET(<reg>, <simple value>) command, we
            //    //remove this SET command
            //    //Iterate over the subsequent commands in the base block
            //    //If we find any command with input <reg> we change it to <simple value>
            //    //If we find any command with output <reg> we stop and goto 1. (continuing from the SET command we found)

            var           commands         = function.Commands;
            HashSet <int> removableIndexes = new HashSet <int>();

            for (int i = 0; i < commands.Count; ++i)
            {
                ISchemeCommand cmd = commands[i];

                if (cmd is CommandSetVariable)
                {
                    CommandSetVariable cmdSet    = (CommandSetVariable)cmd;
                    string             targetVar = cmdSet.GetVariableName();
                    string             value     = cmdSet.GetValue();

                    if (SchemeExecutor.IsRegister(targetVar))
                    {
                        removableIndexes.Add(i);
                        for (int i2 = i + 1; i2 < commands.Count; ++i2)
                        {
                            ISchemeCommand cmd2 = commands[i2];
                            cmd2.ChangeInputs(targetVar, value);
                            if (cmd2 is CommandJumpBase || cmd2.HasOutput(targetVar))
                            {
                                break;
                            }
                        }
                    }
                }
            }

            RemoveCommandsAt(function, removableIndexes);
        }
Пример #21
0
        protected override bool GetResult(ObjectVariable variable1, ObjectVariable variable2)
        {
            if (variable1.Type != VariableTypes.Number || variable2.Type != VariableTypes.Number)
            {
                throw SchemeExecutor.CreateException("Cannot compare non-numbers");
            }

            int value1_int = (int)variable1.Value;
            int value2_int = (int)variable2.Value;

            if (op == ">")
            {
                return(value1_int > value2_int);
            }
            if (op == "<")
            {
                return(value1_int < value2_int);
            }
            if (op == ">=")
            {
                return(value1_int >= value2_int);
            }
            if (op == "<=")
            {
                return(value1_int <= value2_int);
            }
            if (op == "!=")
            {
                return(value1_int != value2_int);
            }
            if (op == "=")
            {
                return(value1_int == value2_int);
            }

            return(false);
        }
Пример #22
0
 protected abstract bool Evaluate(SchemeExecutor executor);
Пример #23
0
 public void Execute(SchemeExecutor executor)
 {
     Console.WriteLine(text);
 }
Пример #24
0
 public void Execute(SchemeExecutor executor)
 {
     executor.Object.AddAction(actionName);
 }
Пример #25
0
        public void Execute(SchemeExecutor executor)
        {
            var variable = executor.FindValueByString(valueName);

            Console.WriteLine(variable.Name + " = " + variable.Value);
        }
Пример #26
0
 public void Execute(SchemeExecutor executor)
 {
     executor.CreateLocalVariable(type, varName, null);
 }
Пример #27
0
 protected override bool Evaluate(SchemeExecutor executor)
 {
     return(true);
 }
Пример #28
0
 public void Execute(SchemeExecutor executor)
 {
     executor.Object.ClearActions();
 }
Пример #29
0
        public static void SolveRegisterBuffers(SchemeFunction function)
        {
            // if an operation stores its result in a register <reg>, and it is followed by a SET(<var>, <reg>) command
            //      we delete the SET command and set the opperation's output to <var>
            //      we iterate over the following commands and change any <reg> to <var>
            //      we stop if <reg> is set again or we leave the block

            var           commands         = function.Commands;
            HashSet <int> removableIndexes = new HashSet <int>();

            for (int i = 0; i < commands.Count; ++i)
            {
                ISchemeCommand cmd = commands[i];

                var outputs = cmd.GetOutputs();

                if (outputs == null)
                {
                    continue;
                }

                // check if any output is a register
                foreach (var reg in outputs)
                {
                    if (SchemeExecutor.IsRegister(reg))
                    {
                        string variable = null;
                        for (int j = i + 1; j < commands.Count; ++j)
                        {
                            ISchemeCommand cmd2 = commands[j];

                            if (cmd2 is CommandJumpBase)
                            {
                                break;                          // break if we leave the block
                            }
                            if (cmd2.HasOutput(reg))
                            {
                                break;                       // break if the register is set again
                            }
                            if (cmd2 is CommandSetVariable && cmd2.HasInput(reg))
                            {
                                CommandSetVariable setCmd = (CommandSetVariable)cmd2;
                                variable = setCmd.GetVariableName();
                                if (SchemeExecutor.IsRegister(variable))
                                {
                                    variable = null;
                                }
                                else
                                {
                                    cmd.ChangeOutput(reg, variable);
                                    removableIndexes.Add(j);
                                }
                            }

                            if (variable != null && cmd2.HasInput(reg))
                            {
                                cmd2.ChangeInputs(reg, variable);
                            }
                        }
                    }
                }
            }

            RemoveCommandsAt(function, removableIndexes);
        }
Пример #30
0
        public static void SolveDeadCode(SchemeFunction function)
        {
            //For each command C:
            //. If C uses local variable X that is in V, as input, we remove X from V and the corresponding line from L
            //. If C has local variable output X that is in V, then we remove the line of code that is connected to X (found in L)
            //. If C has local variable output, we store the output variable's name in array V, and the code line in L
            //. If we reach new basic block, we clear V and L

            bool changed = true;

            while (changed)
            {
                changed = false;

                HashSet <int> blocks = GetBlockPositions(function);

                var           commands         = function.Commands;
                HashSet <int> removableIndexes = new HashSet <int>();

                Dictionary <string, int> lastSetLines = new Dictionary <string, int>();

                for (int i = 0; i < commands.Count; ++i)
                {
                    if (blocks.Contains(i))
                    {
                        lastSetLines.Clear();
                    }

                    ISchemeCommand cmd = commands[i];

                    if (cmd is CommandCreateVariable)
                    {
                        continue;                               //We don't want to remove CREATE commands
                    }
                    List <string> inputs  = cmd.GetInputs();
                    List <string> outputs = cmd.GetOutputs();

                    if (inputs != null)
                    {
                        foreach (string input in inputs)
                        {
                            if (SchemeExecutor.IsVariable(input))
                            {
                                lastSetLines.Remove(input);
                            }
                        }
                    }

                    if (outputs != null)
                    {
                        foreach (string output in outputs)
                        {
                            if (SchemeExecutor.IsVariable(output))
                            {
                                if (lastSetLines.ContainsKey(output))
                                {
                                    removableIndexes.Add(lastSetLines[output]);
                                }
                                lastSetLines[output] = i;
                            }
                        }
                    }
                }

                if (removableIndexes.Count > 0)
                {
                    changed = true;
                    RemoveCommandsAt(function, removableIndexes);
                }
            }
        }