Exemplo n.º 1
0
        public override bool?Execute()
        {
            Game            g    = MainViewModel.GetMainViewModelStatic().CurrentGame;
            VariableWrapper left = null;

            if (parent.GetVarById(line.SelectedVariable.LinkedVarId) != null)
            {
                left = parent.GetVarById(line.SelectedVariable.LinkedVarId);
            }
            else
            {
                MainViewModel.WriteText("ERROR: Can not find variable with ID " + line.SelectedVariable.LinkedVarId + ". Terminating script.", this.parent);
                return(false);
            }
            VariableWrapper rightVar = null;

            if (line.IsVariable)
            {
                if (parent.GetVarById(line.TargetVar.LinkedVarId) != null)
                {
                    rightVar = parent.GetVarById(line.TargetVar.LinkedVarId);
                }
                else
                {
                    MainViewModel.WriteText("ERROR: Can not find variable with ID " + line.SelectedVariable.LinkedVarId + ". Terminating script.", this.parent);
                    return(false);
                }
            }

            if (line.IsDateTime)
            {
                DateTime right = DateTime.MinValue;
                if (rightVar != null)
                {
                    right = parent.GetVarById(line.TargetVar.LinkedVarId).CurrentDateTimeValue;
                }
                else
                {
                    right = new DateTime(new TimeSpan(line.Days, line.Hours, line.Minutes, line.Seconds).Ticks);
                }
                if (line.IsSet)
                {
                    left.CurrentDateTimeValue = right;
                }
                if (line.IsIncrement)
                {
                    left.CurrentDateTimeValue = left.CurrentDateTimeValue.AddTicks(right.Ticks);
                }
                if (line.IsDecrement)
                {
                    left.CurrentDateTimeValue = left.CurrentDateTimeValue.AddTicks(right.Ticks);
                }
                if (line.IsRandomized)
                {
                    left.CurrentDateTimeValue = new DateTime(RandNums.GetRandomTimespan(right.Ticks).Ticks);
                }
            }
            else if (line.IsNumber)
            {
                int right = 0;
                if (rightVar != null)
                {
                    right = parent.GetVarById(line.TargetVar.LinkedVarId).CurrentNumberValue;
                }
                else
                {
                    right = line.NumberValue;
                }
                if (line.IsSet)
                {
                    left.CurrentNumberValue = right;
                }
                else if (line.IsIncrement)
                {
                    left.CurrentNumberValue += right;
                }
                else if (line.IsDecrement)
                {
                    left.CurrentNumberValue -= right;
                }
                else if (line.IsMultiply)
                {
                    left.CurrentNumberValue *= right;
                }
                else if (line.IsDivide)
                {
                    left.CurrentNumberValue /= right;
                }
                else if (line.IsRemainder)
                {
                    left.CurrentNumberValue %= right;
                }
                else if (line.IsRandomized)
                {
                    left.CurrentNumberValue = RandNums.GetNextRandomInt(right);
                }
            }
            else if (line.IsString)
            {
                if (line.IsSet)
                {
                    string right = "";
                    if (rightVar != null)
                    {
                        right = parent.GetVarById(line.TargetVar.LinkedVarId).CurrentStringValue;
                    }
                    else
                    {
                        right = line.StringValue;
                    }
                    left.CurrentStringValue = right;
                }
                else if (line.IsAppendToEnd)
                {
                    string right = "";
                    if (rightVar != null)
                    {
                        right = parent.GetVarById(line.TargetVar.LinkedVarId).CurrentStringValue;
                    }
                    else
                    {
                        right = line.StringValue;
                    }
                    left.CurrentStringValue = left.CurrentStringValue + right;
                }
                else if (line.IsAppendToStart)
                {
                    string right = "";
                    if (rightVar != null)
                    {
                        right = parent.GetVarById(line.TargetVar.LinkedVarId).CurrentStringValue;
                    }
                    else
                    {
                        right = line.StringValue;
                    }
                    left.CurrentStringValue = right + left.CurrentStringValue;
                }
            }
            else if (line.IsItem)
            {
                ItemInstance right = null;
                if (rightVar != null)
                {
                    right = parent.GetVarById(line.TargetVar.LinkedVarId).CurrentItemValue;
                }
                else
                {
                    if (line.IsItemInInventory)
                    {
                        var itemInInventory = MainViewModel.GetMainViewModelStatic().CurrentGame.PlayerInventory.Where(a => a.item.ItemID == line.ItemValue.LinkedItemId).FirstOrDefault();

                        right = itemInInventory;
                        left.CurrentItemValue = right;
                    }
                    else
                    {
                        if (line.ItemValue != null && line.ItemValue.LinkedItem != null)
                        {
                            right = new ItemInstance(line.ItemValue.LinkedItem);
                            left.CurrentItemValue = right;
                        }
                        else
                        {
                            MainViewModel.WriteText("ERROR: Item " + line.ItemValue.LinkedItemId + " Unknown!", this.parent);
                            return(false);
                        }
                    }
                }

                //if (rightVar == null)
                //{
                //    if (line.ItemValue != null && line.ItemValue.LinkedItem != null)
                //    {
                //        ItemInstance right = new ItemInstance(line.ItemValue.LinkedItem);
                //        left.CurrentItemValue = right;
                //    }
                //    else
                //    {
                //        MainViewModel.WriteText("ERROR: Item " + line.ItemValue.LinkedItemId + " Unknown!");
                //        return false;
                //    }
                //}
                //else
                //{
                //    left.CurrentItemValue = rightVar.CurrentItemValue;
                //}
            }
            else if (line.IsCommonEventRef)
            {
                CommonEventRef right = null;
                if (rightVar != null)
                {
                    right = parent.GetVarById(line.TargetVar.LinkedVarId).CurrentCommonEventValue;
                }
                else
                {
                    right = line.CommonEventValue;
                }
                left.CurrentCommonEventValue = right;
            }
            return(null);
        }
        public override bool?Execute()
        {
            if ((line.SelectedEvent != null && line.SelectedEvent.LinkedCommonEvent != null) || (line.RunFromVariable && line.VarScript != null))
            {
                try
                {
                    var            game = MainViewModel.GetMainViewModelStatic().CurrentGame;
                    ScriptWrapper  wrapper;
                    string         scriptType;
                    CommonEventRef cRef = null;
                    if (line.RunFromVariable)
                    {
                        if (parent.GetVarById(line.VarScript.LinkedVarId) != null)
                        {
                            CommonEventRef cer = parent.GetVarById(line.VarScript.LinkedVarId).CurrentCommonEventValue;
                            if (cer != null && cer.LinkedCommonEvent != null)
                            {
                                wrapper    = new ScriptWrapper(cer.LinkedCommonEvent.AssociatedScript);
                                scriptType = cer.LinkedCommonEvent.EventType.Item1;
                                cRef       = cer;
                            }
                            else
                            {
                                MainViewModel.WriteText("Error: RunCommonEvent associated variable is null.", this.parent);
                                return(false);
                            }
                        }
                        else
                        {
                            MainViewModel.WriteText("Error: RunCommonEvent missing associated variable for script.", this.parent);
                            return(false);
                        }
                    }
                    else
                    {
                        wrapper    = new ScriptWrapper(line.SelectedEvent.LinkedCommonEvent.AssociatedScript);
                        scriptType = line.SelectedEvent.LinkedCommonEvent.EventType.Item1;
                        cRef       = line.SelectedEvent;
                    }
                    if (scriptType != CommonEvent.ScriptTypeOverwriteLocals)
                    {
                        wrapper.parent = this.parent;
                        wrapper.DupeVars(this.parent);
                        wrapper.IsRootScript = true;
                    }
                    else
                    {
                        wrapper.isSubscript = true;
                        wrapper.parent      = this.parent;
                    }
                    var result = wrapper.Execute();

                    if (wrapper.VariableResult != null && line.VarRef != null && parent.GetVarById(wrapper.VariableResult.LinkedVarId) != null && parent.GetVarById(line.VarRef.LinkedVarId) != null)
                    {
                        var source = wrapper.GetVarById(wrapper.VariableResult.LinkedVarId);
                        var dest   = parent.GetVarById(line.VarRef.LinkedVarId);
                        dest.CurrentDateTimeValue    = source.CurrentDateTimeValue;
                        dest.CurrentItemValue        = source.CurrentItemValue;
                        dest.CurrentNumberValue      = source.CurrentNumberValue;
                        dest.CurrentStringValue      = source.CurrentStringValue;
                        dest.CurrentCommonEventValue = source.CurrentCommonEventValue;
                    }

                    if (cRef.LinkedCommonEvent.EventType.Item1 == CommonEvent.ScriptTypeTrueFalse)
                    {
                        return(result);
                    }
                    else
                    {
                        return(null);
                    }
                }

                catch (Exception e)
                {
                    MainViewModel.WriteText("Unhandled Exception in " + this.line.SelectedEvent.LinkedCommonEvent.Name + ":\n\n" + e.Message, null);
                }
            }
            MainViewModel.WriteText("Error: RunCommonEvent missing associated script.", this.parent);
            return(false);
        }