public TeamGroupType(ITypeSupplier typeSupplier, ElementEnum enumData) : base(enumData, typeSupplier, false)
        {
            Opposite = new InternalVar("Opposite", this, CompletionItemKind.Property)
            {
                Documentation = new MarkupBuilder()
                                .Add("The opposite team of the value. If the team value is ").Code("Team 1").Add(", Opposite will be ").Code("Team 2").Add(" and vice versa. If the value is ")
                                .Code("All").Add(", Opposite will still be ").Code("All").Add(".")
            };
            Score = new InternalVar("Score", typeSupplier.Number(), CompletionItemKind.Property)
            {
                Documentation = new MarkupBuilder()
                                .Add("The current score for the team. Results in 0 in free-for-all modes.")
            };
            OnDefense = new InternalVar("OnDefense", typeSupplier.Boolean(), CompletionItemKind.Property)
            {
                Documentation = new MarkupBuilder()
                                .Add("Whether the specified team is currently on defense. Results in False if the game mode is not Assault, Escort, or Assault/Escort.")
            };
            OnOffense = new InternalVar("OnOffense", typeSupplier.Boolean(), CompletionItemKind.Property)
            {
                Documentation = new MarkupBuilder()
                                .Add("Whether the specified team is currently on offense. Results in False if the game mode is not Assault, Escort, or Assault/Escort.")
            };

            _objectScope.AddNativeVariable(Opposite);
            _objectScope.AddNativeVariable(Score);
            _objectScope.AddNativeVariable(OnDefense);
            _objectScope.AddNativeVariable(OnOffense);
        }
Example #2
0
        public ArrayType(ITypeSupplier supplier, CodeType arrayOfType) : base(arrayOfType.GetNameOrAny() + "[]")
        {
            ArrayOfType   = arrayOfType;
            ArrayHandler  = arrayOfType.ArrayHandler;
            Attributes    = arrayOfType.Attributes;
            TypeSemantics = arrayOfType.TypeSemantics;
            AsReferenceResetSettability = arrayOfType.AsReferenceResetSettability;
            DebugVariableResolver       = new Debugger.ArrayResolver(ArrayOfType?.DebugVariableResolver, ArrayOfType?.GetName(), ArrayOfType is ClassType);

            Generics = new[] { arrayOfType };

            _length = new InternalVar("Length", supplier.Number(), CompletionItemKind.Property)
            {
                Ambiguous = false
            };
            _last = new InternalVar("Last", ArrayOfType, CompletionItemKind.Property)
            {
                Ambiguous = false
            };
            _first = new InternalVar("First", ArrayOfType, CompletionItemKind.Property)
            {
                Ambiguous = false
            };
            _supplier = supplier;
        }
        private VectorType() : base("Vector")
        {
            CanBeDeleted  = false;
            CanBeExtended = false;
            Kind          = "struct";

            X = CreateInternalVar("X", "The X component of the vector.");
            Y = CreateInternalVar("Y", "The Y component of the vector.");
            Z = CreateInternalVar("Z", "The Z component of the vector.");
            HorizontalAngle = CreateInternalVar("HorizontalAngle", "The horizontal angle of the vector.");
            VerticalAngle   = CreateInternalVar("VerticalAngle", "The vertical angle of the vector.");
            Zero            = CreateInternalVar("Zero", "Equal to `Vector(0, 0, 0)`.", true);

            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <DistanceTo>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <CrossProduct>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <DotProduct>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <Normalize>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <DirectionTowards>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <FarthestPlayer>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <ClosestPlayer>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <IsInLineOfSight>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <Towards>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <AsLocalVector>());
            objectScope.AddNativeMethod(CustomMethodData.GetCustomMethod <AsWorldVector>());
        }
        public void GetMeta()
        {
            _length = new InternalVar("Length", _typeSupplier.Number(), CompletionItemKind.Property);

            Operations.AddTypeOperation(new ITypeOperation[] {
                new StringAddOperation(_typeSupplier)
            });

            _scope.AddNativeMethod(FormatFunction(_typeSupplier));
            _scope.AddNativeMethod(ContainsFunction(_typeSupplier));
            _scope.AddNativeMethod(SliceFunction(_typeSupplier));
            _scope.AddNativeMethod(CharInStringFunction(_typeSupplier));
            _scope.AddNativeMethod(IndexOfFunction(_typeSupplier));
            _scope.AddNativeMethod(SplitFunction(_typeSupplier));
            _scope.AddNativeMethod(ReplaceFunction(_typeSupplier));
            _scope.AddNativeVariable(_length);
        }
        public void GetMeta()
        {
            X = CreateInternalVar("X", "The X component of the vector.", _typeSupplier.Number());
            Y = CreateInternalVar("Y", "The Y component of the vector.", _typeSupplier.Number());
            Z = CreateInternalVar("Z", "The Z component of the vector.", _typeSupplier.Number());
            HorizontalAngle = CreateInternalVar("HorizontalAngle", "The horizontal angle of the vector.", _typeSupplier.Number());
            VerticalAngle   = CreateInternalVar("VerticalAngle", "The vertical angle of the vector.", _typeSupplier.Number());
            Magnitude       = CreateInternalVar("Magnitude", "The magnitude of the vector.", _typeSupplier.Number());
            Zero            = CreateInternalVar("Zero", "Equal to `Vector(0, 0, 0)`.", _typeSupplier.Vector(), Element.Vector(0, 0, 0), true);

            _deltinScript.GetComponent <StaticVariableCollection>().AddVariable(Zero.GetDefaultInstance(this));

            objectScope.AddNativeMethod(DistanceTo);
            objectScope.AddNativeMethod(CrossProduct);
            objectScope.AddNativeMethod(DotProduct);
            objectScope.AddNativeMethod(Normalize);
            objectScope.AddNativeMethod(DirectionTowards);
            objectScope.AddNativeMethod(FarthestPlayer);
            objectScope.AddNativeMethod(ClosestPlayer);
            objectScope.AddNativeMethod(IsInLineOfSight);
            objectScope.AddNativeMethod(Towards);
            objectScope.AddNativeMethod(AsLocalVector);
            objectScope.AddNativeMethod(AsWorldVector);

            Operations.AddTypeOperation(new TypeOperation[] {
                new TypeOperation(TypeOperator.Add, this, this),                        // Vector + vector
                new TypeOperation(TypeOperator.Subtract, this, this),                   // Vector - vector
                new TypeOperation(TypeOperator.Multiply, this, this),                   // Vector * vector
                new TypeOperation(TypeOperator.Divide, this, this),                     // Vector / vector
                new TypeOperation(TypeOperator.Multiply, _typeSupplier.Number(), this), // Vector * number
                new TypeOperation(TypeOperator.Divide, _typeSupplier.Number(), this),   // Vector / number
            });
            Operations.AddTypeOperation(new[] {
                new AssignmentOperation(AssignmentOperator.AddEqual, this),                        // += vector
                new AssignmentOperation(AssignmentOperator.SubtractEqual, this),                   // -= vector
                new AssignmentOperation(AssignmentOperator.MultiplyEqual, this),                   // *= vector
                new AssignmentOperation(AssignmentOperator.DivideEqual, this),                     // /= vector
                new AssignmentOperation(AssignmentOperator.MultiplyEqual, _typeSupplier.Number()), // *= number
                new AssignmentOperation(AssignmentOperator.DivideEqual, _typeSupplier.Number())    // /= number
            });
        }
        public void ResolveElements()
        {
            X = CreateInternalVar("X", "The X component of the vector.");
            Y = CreateInternalVar("Y", "The Y component of the vector.");
            Z = CreateInternalVar("Z", "The Z component of the vector.");
            HorizontalAngle = CreateInternalVar("HorizontalAngle", "The horizontal angle of the vector.");
            VerticalAngle   = CreateInternalVar("VerticalAngle", "The vertical angle of the vector.");
            Zero            = CreateInternalVar("Zero", "Equal to `Vector(0, 0, 0)`.", true);

            objectScope.AddNativeMethod(DistanceTo);
            objectScope.AddNativeMethod(CrossProduct);
            objectScope.AddNativeMethod(DotProduct);
            objectScope.AddNativeMethod(Normalize);
            objectScope.AddNativeMethod(DirectionTowards);
            objectScope.AddNativeMethod(FarthestPlayer);
            objectScope.AddNativeMethod(ClosestPlayer);
            objectScope.AddNativeMethod(IsInLineOfSight);
            objectScope.AddNativeMethod(Towards);
            objectScope.AddNativeMethod(AsLocalVector);
            objectScope.AddNativeMethod(AsWorldVector);
        }
        private InternalVar CreateInternalVar(string name, string documentation, CodeType type, bool isStatic = false)
        {
            // Create the variable.
            InternalVar newInternalVar = new InternalVar(name, type, CompletionItemKind.Property)
            {
                // IsSettable = false, // Make the variable unsettable.
                Documentation = documentation // Set the documentation.
            };

            // Add the variable to the object scope.
            if (!isStatic)
            {
                objectScope.AddNativeVariable(newInternalVar);
            }
            // Add the variable to the static scope.
            else
            {
                staticScope.AddNativeVariable(newInternalVar);
            }

            return(newInternalVar);
        }
        string GetImportedFile(DeltinScript deltinScript, ScriptFile script, FileImporter importer, Import importFileContext)
        {
            // If the file being imported is being imported as an object, get the variable name.
            string variableName = importFileContext.Identifier?.Text;

            DocRange stringRange = importFileContext.File.Range;

            ImportResult importResult = importer.Import(
                stringRange,
                importFileContext.File.Text.RemoveQuotes(),
                script.Uri
                );

            if (!importResult.SuccessfulReference)
            {
                return(importResult.Directory);
            }

            // Add hover and definition info.
            script.AddDefinitionLink(stringRange, new Location(importResult.Uri, DocRange.Zero));
            script.AddHover(stringRange, importResult.FilePath);

            if (importResult.ShouldImport)
            {
                // Import the file if it should be imported.
                if (variableName == null)
                {
                    switch (importResult.FileType)
                    {
                    // Get script file.
                    case ".del":
                    case ".ostw":
                    case ".workshop":
                        ScriptFile importedScript = new ScriptFile(_diagnostics, _fileGetter.GetScript(importResult.Uri));
                        CollectScriptFiles(deltinScript, importedScript);
                        break;

                    // Get lobby settings.
                    case ".json":
                    case ".lobby":
                        JObject lobbySettings = null;

                        // Make sure the json is in the correct format.
                        try
                        {
                            ImportedScript file = _fileGetter.GetImportedFile(importResult.Uri);
                            file.Update();

                            // Convert the json to a jobject.
                            lobbySettings = JObject.Parse(file.Content);

                            // An exception will be thrown if the jobject cannot be converted to a Ruleset.
                            lobbySettings.ToObject(typeof(Ruleset));

                            if (!Ruleset.Validate(lobbySettings, script.Diagnostics, stringRange))
                            {
                                break;
                            }
                        }
                        catch
                        {
                            // Error if the json failed to parse.
                            script.Diagnostics.Error("Failed to parse the settings file.", stringRange);
                            break;
                        }

                        // If no lobby settings were imported yet, set MergedLobbySettings to the jobject.
                        if (MergedLobbySettings == null)
                        {
                            MergedLobbySettings = lobbySettings;
                        }
                        else
                        {
                            // Otherwise, merge current lobby settings.
                            lobbySettings.Merge(MergedLobbySettings, new JsonMergeSettings
                            {
                                MergeArrayHandling     = MergeArrayHandling.Union,
                                MergeNullValueHandling = MergeNullValueHandling.Ignore
                            });
                            MergedLobbySettings = lobbySettings;
                        }
                        break;
                    }
                }
                else
                {
                    switch (importResult.FileType)
                    {
                    case ".json":
                        ImportedScript file = _fileGetter.GetImportedFile(importResult.Uri);
                        file.Update();

                        JObject     jsonData = JObject.Parse(file.Content);
                        InternalVar jsonVar  = new InternalVar(variableName, new JsonType(jsonData));

                        if (((JsonType)jsonVar.CodeType).ContainsDeepArrays())
                        {
                            script.Diagnostics.Error("JSON Arrays cannot include objects or arrays.", stringRange);
                        }

                        _deltinScript.RulesetScope.AddVariable(jsonVar, script.Diagnostics, importFileContext.Identifier.Range);
                        _deltinScript.DefaultIndexAssigner.Add(jsonVar, Element.Null());
                        break;
                    }
                }
            }
            return(importResult.Directory);
        }
Example #9
0
        public ArrayType(CodeType arrayOfType) : base((arrayOfType?.Name ?? "define") + "[]")
        {
            ArrayOfType = arrayOfType;

            _length = new InternalVar("Length", CompletionItemKind.Property);
            _last   = new InternalVar("Last", ArrayOfType, CompletionItemKind.Property);
            _first  = new InternalVar("First", ArrayOfType, CompletionItemKind.Property);

            _scope.AddNativeVariable(_length);
            _scope.AddNativeVariable(_last);
            _scope.AddNativeVariable(_first);

            // Filtered Array
            Func(new FuncMethodBuilder()
            {
                Name            = "FilteredArray",
                Documentation   = "A copy of the specified array with any values that do not match the specified condition removed.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("conditionLambda", "The condition that is evaluated for each element of the copied array. If the condition is true, the element is kept in the copied array.", new MacroLambda(null, ArrayOfType))
                },
                Action = (actionSet, methodCall) => GenericSort <V_FilteredArray>(actionSet, methodCall)
            });
            // Sorted Array
            Func(new FuncMethodBuilder()
            {
                Name            = "SortedArray",
                Documentation   = "A copy of the specified array with the values sorted according to the value rank that is evaluated for each element.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("conditionLambda", "The value that is evaluated for each element of the copied array. The array is sorted by this rank in ascending order.", new MacroLambda(null, ArrayOfType))
                },
                Action = (actionSet, methodCall) => GenericSort <V_SortedArray>(actionSet, methodCall)
            });
            // Is True For Any
            Func(new FuncMethodBuilder()
            {
                Name            = "IsTrueForAny",
                Documentation   = "Whether the specified condition evaluates to true for any value in the specified array.",
                DoesReturnValue = true,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("conditionLambda", "The condition that is evaluated for each element of the specified array.", new MacroLambda(null, ArrayOfType))
                },
                Action = (actionSet, methodCall) => GenericSort <V_IsTrueForAny>(actionSet, methodCall)
            });
            // Is True For All
            Func(new FuncMethodBuilder()
            {
                Name            = "IsTrueForAll",
                Documentation   = "Whether the specified condition evaluates to true for every value in the specified array.",
                DoesReturnValue = true,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("conditionLambda", "The condition that is evaluated for each element of the specified array.", new MacroLambda(null, ArrayOfType))
                },
                Action = (actionSet, methodCall) => GenericSort <V_IsTrueForAll>(actionSet, methodCall)
            });
            // Contains
            Func(new FuncMethodBuilder()
            {
                Name            = "Contains",
                Documentation   = "Wether the array contains the specified value.",
                DoesReturnValue = true,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value that is being looked for in the array.", ArrayOfType)
                },
                Action = (actionSet, methodCall) => Element.Part <V_ArrayContains>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
            // Random
            Func(new FuncMethodBuilder()
            {
                Name            = "Random",
                Documentation   = "Gets a random value from the array.",
                DoesReturnValue = true,
                ReturnType      = ArrayOfType,
                Action          = (actionSet, methodCall) => Element.Part <V_RandomValueInArray>(actionSet.CurrentObject)
            });
            // Randomize
            Func(new FuncMethodBuilder()
            {
                Name            = "Randomize",
                Documentation   = "Returns a copy of the array that is randomized.",
                DoesReturnValue = true,
                ReturnType      = this,
                Action          = (actionSet, methodCall) => Element.Part <V_RandomizedArray>(actionSet.CurrentObject)
            });
            // Append
            Func(new FuncMethodBuilder()
            {
                Name            = "Append",
                Documentation   = "A copy of the array with the specified value appended to it.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value that is appended to the array. If the value is an array, it will be flattened.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_Append>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
            // Remove
            Func(new FuncMethodBuilder()
            {
                Name            = "Remove",
                Documentation   = "A copy of the array with the specified value removed from it.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value that is removed from the array.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_RemoveFromArray>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
            // Slice
            Func(new FuncMethodBuilder()
            {
                Name            = "Slice",
                Documentation   = "A copy of the array containing only values from a specified index range.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("startIndex", "The first index of the range."),
                    new CodeParameter("count", "The number of elements in the resulting array. The resulting array will contain fewer elements if the specified range exceeds the bounds of the array.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_ArraySlice>(actionSet.CurrentObject, methodCall.ParameterValues[0], methodCall.ParameterValues[1])
            });
            // Index Of
            Func(new FuncMethodBuilder()
            {
                Name            = "IndexOf",
                Documentation   = "The index of a value within an array or -1 if no such value can be found.",
                DoesReturnValue = true,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value for which to search.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_IndexOfArrayValue>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
        }