Example #1
0
        public static JassFunctionDeclarationSyntax CreateRegions(MapRegions mapRegions)
        {
            var statements = new List <IStatementSyntax>();

            foreach (var region in mapRegions.Regions)
            {
                var regionName = region.GetVariableName();

                statements.Add(SyntaxFactory.SetStatement(
                                   regionName,
                                   SyntaxFactory.InvocationExpression(
                                       nameof(Rect),
                                       SyntaxFactory.LiteralExpression(region.Left, precision: 1),
                                       SyntaxFactory.LiteralExpression(region.Bottom, precision: 1),
                                       SyntaxFactory.LiteralExpression(region.Right, precision: 1),
                                       SyntaxFactory.LiteralExpression(region.Top, precision: 1))));

                if (region.WeatherType != WeatherType.None)
                {
                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(EnableWeatherEffect),
                                       SyntaxFactory.InvocationExpression(
                                           nameof(AddWeatherEffect),
                                           SyntaxFactory.VariableReferenceExpression(regionName),
                                           SyntaxFactory.FourCCLiteralExpression((int)region.WeatherType)),
                                       SyntaxFactory.LiteralExpression(true)));
                }

                if (!string.IsNullOrEmpty(region.AmbientSound))
                {
                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(SetSoundPosition),
                                       SyntaxFactory.VariableReferenceExpression(region.AmbientSound),
                                       SyntaxFactory.LiteralExpression(region.CenterX),
                                       SyntaxFactory.LiteralExpression(region.CenterY),
                                       SyntaxFactory.LiteralExpression(0f)));

                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(RegisterStackedSound),
                                       SyntaxFactory.VariableReferenceExpression(region.AmbientSound),
                                       SyntaxFactory.LiteralExpression(true),
                                       SyntaxFactory.LiteralExpression(region.Width),
                                       SyntaxFactory.LiteralExpression(region.Height)));
                }
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(CreateRegions)), statements));
        }
Example #2
0
        protected virtual JassFunctionDeclarationSyntax InitTechTree(Map map)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            var mapInfo = map.Info;

            var statements = new List <IStatementSyntax>();

            foreach (var player in mapInfo.Players)
            {
                var playerNumber = player.Id;
                foreach (var techData in mapInfo.TechData)
                {
                    if (techData.Players[playerNumber])
                    {
                        if (techData.Id.ToRawcode()[0] == 'A')
                        {
                            statements.Add(SyntaxFactory.CallStatement(
                                               nameof(SetPlayerAbilityAvailable),
                                               SyntaxFactory.InvocationExpression(nameof(Player), SyntaxFactory.LiteralExpression(playerNumber)),
                                               SyntaxFactory.FourCCLiteralExpression(techData.Id),
                                               SyntaxFactory.LiteralExpression(false)));
                        }
                        else
                        {
                            statements.Add(SyntaxFactory.CallStatement(
                                               nameof(SetPlayerTechMaxAllowed),
                                               SyntaxFactory.InvocationExpression(nameof(Player), SyntaxFactory.LiteralExpression(playerNumber)),
                                               SyntaxFactory.FourCCLiteralExpression(techData.Id),
                                               SyntaxFactory.LiteralExpression(0)));
                        }
                    }
                }
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(InitTechTree)), statements));
        }
Example #3
0
        public static JassFunctionDeclarationSyntax CreateAllItems(MapUnits mapUnits)
        {
            const string LocalItemIdVariableName = "itemID";

            var statements = new List <IStatementSyntax>();

            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, LocalItemIdVariableName));

            foreach (var item in mapUnits.Units.Where(item => item.IsItem() && !item.IsPlayerStartLocation()))
            {
                if (item.IsRandomItem())
                {
                    var randomData = item.RandomData;
                    switch (randomData)
                    {
                    case RandomUnitAny randomUnitAny:
                        statements.Add(SyntaxFactory.SetStatement(
                                           LocalItemIdVariableName,
                                           SyntaxFactory.InvocationExpression(
                                               nameof(ChooseRandomItemEx),
                                               SyntaxFactory.InvocationExpression(nameof(ConvertItemType), SyntaxFactory.LiteralExpression((int)randomUnitAny.Class)),
                                               SyntaxFactory.LiteralExpression(randomUnitAny.Level))));

                        break;

                    case RandomUnitGlobalTable randomUnitGlobalTable:
                        break;

                    case RandomUnitCustomTable randomUnitCustomTable:
                        statements.Add(SyntaxFactory.CallStatement(nameof(War3Api.Blizzard.RandomDistReset)));

                        var summedChance = 0;
                        foreach (var randomItem in randomUnitCustomTable.RandomUnits)
                        {
                            IExpressionSyntax id = RandomItemProvider.IsRandomItem(randomItem.UnitId, out var itemClass, out var level)
                                    ? SyntaxFactory.InvocationExpression(
                                nameof(ChooseRandomItemEx),
                                SyntaxFactory.InvocationExpression(nameof(ConvertItemType), SyntaxFactory.LiteralExpression((int)itemClass)),
                                SyntaxFactory.LiteralExpression(level))
                                    : SyntaxFactory.FourCCLiteralExpression(randomItem.UnitId);

                            statements.Add(SyntaxFactory.CallStatement(
                                               nameof(War3Api.Blizzard.RandomDistAddItem),
                                               id,
                                               SyntaxFactory.LiteralExpression(randomItem.Chance)));

                            summedChance += randomItem.Chance;
                        }

                        if (summedChance < 100)
                        {
                            statements.Add(SyntaxFactory.CallStatement(
                                               nameof(War3Api.Blizzard.RandomDistAddItem),
                                               SyntaxFactory.LiteralExpression(-1),
                                               SyntaxFactory.LiteralExpression(100 - summedChance)));
                        }

                        statements.Add(SyntaxFactory.SetStatement(
                                           LocalItemIdVariableName,
                                           SyntaxFactory.InvocationExpression(nameof(War3Api.Blizzard.RandomDistChoose))));

                        break;

                    default:
                        break;
                    }

                    statements.Add(SyntaxFactory.IfStatement(
                                       SyntaxFactory.BinaryNotEqualsExpression(SyntaxFactory.VariableReferenceExpression(LocalItemIdVariableName), SyntaxFactory.LiteralExpression(-1)),
                                       SyntaxFactory.CallStatement(
                                           nameof(CreateItem),
                                           SyntaxFactory.VariableReferenceExpression(LocalItemIdVariableName),
                                           SyntaxFactory.LiteralExpression(item.Position.X),
                                           SyntaxFactory.LiteralExpression(item.Position.Y))));
                }
                else
                {
                    var args = new List <IExpressionSyntax>()
                    {
                        SyntaxFactory.FourCCLiteralExpression(item.TypeId),
                        SyntaxFactory.LiteralExpression(item.Position.X),
                        SyntaxFactory.LiteralExpression(item.Position.Y),
                    };

                    var hasSkin = item.SkinId != 0 && item.SkinId != item.TypeId;
                    if (hasSkin)
                    {
                        args.Add(SyntaxFactory.FourCCLiteralExpression(item.SkinId));
                    }

                    statements.Add(SyntaxFactory.CallStatement(hasSkin ? nameof(BlzCreateItemWithSkin) : nameof(CreateItem), args.ToArray()));
                }
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(CreateAllItems)), statements));
        }
Example #4
0
        public static JassFunctionDeclarationSyntax InitUpgrades(MapInfo mapInfo)
        {
            var statements = new List <IStatementSyntax>();

            foreach (var player in mapInfo.Players)
            {
                var playerNumber = player.Id;
                var maxLevel     = new Dictionary <int, int>();
                var researched   = new Dictionary <int, int>();
                for (var i = 0; i < mapInfo.UpgradeData.Count; i++)
                {
                    var upgradeData = mapInfo.UpgradeData[i];
                    if (upgradeData.Availability != UpgradeAvailability.Available && upgradeData.Players[playerNumber])
                    {
                        if (upgradeData.Availability == UpgradeAvailability.Unavailable)
                        {
                            if (maxLevel.TryGetValue(upgradeData.Id, out var level))
                            {
                                if (upgradeData.Level < level)
                                {
                                    maxLevel[upgradeData.Id] = upgradeData.Level;
                                }
                            }
                            else
                            {
                                maxLevel.Add(upgradeData.Id, upgradeData.Level);
                            }
                        }
                        else if (upgradeData.Availability == UpgradeAvailability.Researched)
                        {
                            if (researched.TryGetValue(upgradeData.Id, out var level))
                            {
                                if (upgradeData.Level > level)
                                {
                                    researched[upgradeData.Id] = upgradeData.Level;
                                }
                            }
                            else
                            {
                                researched.Add(upgradeData.Id, upgradeData.Level);
                            }
                        }
                    }
                }

                foreach (var tech in maxLevel)
                {
                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(SetPlayerTechMaxAllowed),
                                       SyntaxFactory.InvocationExpression(nameof(Player), SyntaxFactory.LiteralExpression(playerNumber)),
                                       SyntaxFactory.FourCCLiteralExpression(tech.Key),
                                       SyntaxFactory.LiteralExpression(tech.Value)));
                }

                foreach (var tech in researched)
                {
                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(SetPlayerTechResearched),
                                       SyntaxFactory.InvocationExpression(nameof(Player), SyntaxFactory.LiteralExpression(playerNumber)),
                                       SyntaxFactory.FourCCLiteralExpression(tech.Key),
                                       SyntaxFactory.LiteralExpression(tech.Value + 1)));
                }
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(InitUpgrades)), statements));
        }
Example #5
0
        protected virtual JassFunctionDeclarationSyntax main(Map map)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            var mapEnvironment = map.Environment;
            var mapInfo        = map.Info;

            var statements = new List <IStatementSyntax>();

            if (UseWeatherEffectVariable && EnableGlobalWeatherEffectCondition(map))
            {
                statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(SyntaxFactory.ParseTypeName(TypeName.WeatherEffect), VariableName.WeatherEffect));
            }

            if (mapInfo.CameraBoundsComplements is null)
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetCameraBounds,
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.BottomLeft.X, precision: 1),
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.BottomLeft.Y, precision: 1),
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.TopRight.X, precision: 1),
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.TopRight.Y, precision: 1),
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.TopLeft.X, precision: 1),
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.TopLeft.Y, precision: 1),
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.BottomRight.X, precision: 1),
                                   SyntaxFactory.LiteralExpression(mapInfo.CameraBounds.BottomRight.Y, precision: 1)));
            }
            else
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetCameraBounds,
                                   SyntaxFactory.BinaryAdditionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Left + (128 * mapInfo.CameraBoundsComplements.Left), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Left))),
                                   SyntaxFactory.BinaryAdditionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Bottom + (128 * mapInfo.CameraBoundsComplements.Bottom), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Bottom))),
                                   SyntaxFactory.BinarySubtractionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Right - (128 * mapInfo.CameraBoundsComplements.Right), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Right))),
                                   SyntaxFactory.BinarySubtractionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Top - (128 * mapInfo.CameraBoundsComplements.Top), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Top))),
                                   SyntaxFactory.BinaryAdditionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Left + (128 * mapInfo.CameraBoundsComplements.Left), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Left))),
                                   SyntaxFactory.BinarySubtractionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Top - (128 * mapInfo.CameraBoundsComplements.Top), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Top))),
                                   SyntaxFactory.BinarySubtractionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Right - (128 * mapInfo.CameraBoundsComplements.Right), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Right))),
                                   SyntaxFactory.BinaryAdditionExpression(
                                       SyntaxFactory.LiteralExpression(mapEnvironment.Bottom + (128 * mapInfo.CameraBoundsComplements.Bottom), precision: 1),
                                       SyntaxFactory.InvocationExpression(NativeName.GetCameraMargin, SyntaxFactory.VariableReferenceExpression(CameraMarginName.Bottom)))));
            }

            if (SetDayNightModelsCondition(map))
            {
                var lightEnvironment = mapInfo.LightEnvironment == Tileset.Unspecified ? mapInfo.Tileset : mapInfo.LightEnvironment;
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetDayNightModels,
                                   SyntaxFactory.LiteralExpression(EscapedStringProvider.GetEscapedString(LightEnvironmentProvider.GetTerrainLightEnvironmentModel(lightEnvironment))),
                                   SyntaxFactory.LiteralExpression(EscapedStringProvider.GetEscapedString(LightEnvironmentProvider.GetUnitLightEnvironmentModel(lightEnvironment)))));
            }

            if (SetTerrainFogExCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetTerrainFogEx,
                                   SyntaxFactory.LiteralExpression((int)mapInfo.FogStyle),
                                   SyntaxFactory.LiteralExpression(mapInfo.FogStartZ),
                                   SyntaxFactory.LiteralExpression(mapInfo.FogEndZ),
                                   SyntaxFactory.LiteralExpression(mapInfo.FogDensity),
                                   SyntaxFactory.LiteralExpression(mapInfo.FogColor.R / 255f),
                                   SyntaxFactory.LiteralExpression(mapInfo.FogColor.G / 255f),
                                   SyntaxFactory.LiteralExpression(mapInfo.FogColor.B / 255f)));
            }

            if (SetWaterBaseColorCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetWaterBaseColor,
                                   SyntaxFactory.LiteralExpression(mapInfo.WaterTintingColor.R),
                                   SyntaxFactory.LiteralExpression(mapInfo.WaterTintingColor.G),
                                   SyntaxFactory.LiteralExpression(mapInfo.WaterTintingColor.B),
                                   SyntaxFactory.LiteralExpression(mapInfo.WaterTintingColor.A)));
            }

            if (EnableGlobalWeatherEffectCondition(map))
            {
                var createWeather = SyntaxFactory.InvocationExpression(
                    NativeName.AddWeatherEffect,
                    SyntaxFactory.InvocationExpression(
                        NativeName.Rect,
                        SyntaxFactory.LiteralExpression(mapEnvironment.Left, precision: 1),
                        SyntaxFactory.LiteralExpression(mapEnvironment.Bottom, precision: 1),
                        SyntaxFactory.LiteralExpression(mapEnvironment.Right, precision: 1),
                        SyntaxFactory.LiteralExpression(mapEnvironment.Top, precision: 1)),
                    SyntaxFactory.FourCCLiteralExpression((int)mapInfo.GlobalWeather));

                if (UseWeatherEffectVariable)
                {
                    statements.Add(SyntaxFactory.SetStatement(VariableName.WeatherEffect, createWeather));
                    statements.Add(SyntaxFactory.CallStatement(NativeName.EnableWeatherEffect, SyntaxFactory.VariableReferenceExpression(VariableName.WeatherEffect), SyntaxFactory.LiteralExpression(true)));
                }
                else
                {
                    statements.Add(SyntaxFactory.CallStatement(NativeName.EnableWeatherEffect, createWeather, SyntaxFactory.LiteralExpression(true)));
                }
            }

            if (NewSoundEnvironmentCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.NewSoundEnvironment,
                                   SyntaxFactory.LiteralExpression(EscapedStringProvider.GetEscapedString(string.IsNullOrEmpty(mapInfo.SoundEnvironment) ? "Default" : mapInfo.SoundEnvironment))));
            }

            if (SetAmbientSoundCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   FunctionName.SetAmbientDaySound,
                                   SyntaxFactory.LiteralExpression(EscapedStringProvider.GetEscapedString(SoundEnvironmentProvider.GetAmbientDaySound(mapInfo.Tileset)))));

                statements.Add(SyntaxFactory.CallStatement(
                                   FunctionName.SetAmbientNightSound,
                                   SyntaxFactory.LiteralExpression(EscapedStringProvider.GetEscapedString(SoundEnvironmentProvider.GetAmbientNightSound(mapInfo.Tileset)))));
            }

            if (SetMapMusicCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetMapMusic,
                                   SyntaxFactory.LiteralExpression("Music"),
                                   SyntaxFactory.LiteralExpression(true),
                                   SyntaxFactory.LiteralExpression(0)));
            }

            if (InitSoundsCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(InitSounds)));
            }

            if (CreateRegionsCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(CreateRegions)));
            }

            if (CreateCamerasCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(CreateCameras)));
            }

            if (InitUpgradesCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(InitUpgrades)));
            }

            if (InitTechTreeCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(InitTechTree)));
            }

            if (CreateAllDestructablesCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(CreateAllDestructables)));
            }

            if (CreateAllItemsCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(CreateAllItems)));
            }

            if (InitRandomGroupsCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(InitRandomGroups)));
            }

            if (CreateAllUnitsCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(nameof(CreateAllUnits)));
            }

            if (InitBlizzardCondition(map))
            {
                statements.Add(SyntaxFactory.CallStatement(FunctionName.InitBlizzard));
            }

            if (UseCSharpLua)
            {
                statements.Add(SyntaxFactory.CallStatement(CSharpLua.LuaSyntaxGenerator.kManifestFuncName));
            }

            statements.Add(JassEmptyStatementSyntax.Value);

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(main)), statements));
        }
Example #6
0
        public static JassFunctionDeclarationSyntax InitRandomGroups(IEnumerable <RandomUnitTable> randomUnitTables)
        {
            const string LocalCurrentSetVariableName = "curset";

            var statements = new List <IStatementSyntax>();

            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, LocalCurrentSetVariableName));
            statements.Add(JassEmptyStatementSyntax.Value);

            foreach (var unitTable in randomUnitTables)
            {
                statements.Add(new JassCommentStatementSyntax($" Group {unitTable.Index} - {unitTable.Name}"));
                statements.Add(SyntaxFactory.CallStatement(nameof(War3Api.Blizzard.RandomDistReset)));

                for (var i = 0; i < unitTable.UnitSets.Count; i++)
                {
                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(War3Api.Blizzard.RandomDistAddItem),
                                       SyntaxFactory.LiteralExpression(i),
                                       SyntaxFactory.LiteralExpression(unitTable.UnitSets[i].Chance)));
                }

                statements.Add(SyntaxFactory.SetStatement(LocalCurrentSetVariableName, SyntaxFactory.InvocationExpression(nameof(War3Api.Blizzard.RandomDistChoose))));
                statements.Add(JassEmptyStatementSyntax.Value);

                var groupVarName   = unitTable.GetVariableName();
                var ifElseifBlocks = new List <(IExpressionSyntax Condition, IStatementSyntax[] Body)>();
                for (var setIndex = 0; setIndex < unitTable.UnitSets.Count; setIndex++)
                {
                    var set = unitTable.UnitSets[setIndex];

                    var condition      = SyntaxFactory.BinaryEqualsExpression(SyntaxFactory.VariableReferenceExpression(LocalCurrentSetVariableName), SyntaxFactory.LiteralExpression(setIndex));
                    var bodyStatements = new List <IStatementSyntax>();

                    for (var position = 0; position < unitTable.Types.Count; position++)
                    {
                        var id = set?.UnitIds[position] ?? 0;
                        var unitTypeExpression = RandomUnitProvider.IsRandomUnit(id, out var level)
                            ? SyntaxFactory.InvocationExpression(nameof(ChooseRandomCreep), SyntaxFactory.LiteralExpression(level))
                            : id == 0 ? SyntaxFactory.LiteralExpression(-1) : SyntaxFactory.FourCCLiteralExpression(id);

                        bodyStatements.Add(SyntaxFactory.SetStatement(
                                               groupVarName,
                                               SyntaxFactory.LiteralExpression(position),
                                               unitTypeExpression));
                    }

                    ifElseifBlocks.Add((new JassParenthesizedExpressionSyntax(condition), bodyStatements.ToArray()));
                }

                var elseClauseStatements = new List <IStatementSyntax>();
                for (var position = 0; position < unitTable.Types.Count; position++)
                {
                    elseClauseStatements.Add(SyntaxFactory.SetStatement(
                                                 groupVarName,
                                                 SyntaxFactory.LiteralExpression(position),
                                                 SyntaxFactory.LiteralExpression(-1)));
                }

                statements.Add(SyntaxFactory.IfStatement(
                                   ifElseifBlocks.First().Condition,
                                   SyntaxFactory.StatementList(ifElseifBlocks.First().Body),
                                   ifElseifBlocks.Skip(1).Select(elseIf => new JassElseIfClauseSyntax(elseIf.Condition, SyntaxFactory.StatementList(elseIf.Body))),
                                   new JassElseClauseSyntax(SyntaxFactory.StatementList(elseClauseStatements))));

                statements.Add(JassEmptyStatementSyntax.Value);
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(InitRandomGroups)), statements));
        }
Example #7
0
        protected virtual JassFunctionDeclarationSyntax CreateAllDestructables(Map map)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            var mapDoodads = map.Doodads;

            if (mapDoodads is null)
            {
                throw new ArgumentException($"Function '{nameof(CreateAllDestructables)}' cannot be generated without {nameof(MapDoodads)}.", nameof(map));
            }

            var statements = new List <IStatementSyntax>();

            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(SyntaxFactory.ParseTypeName(nameof(destructable)), VariableName.Destructable));
            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(SyntaxFactory.ParseTypeName(nameof(trigger)), VariableName.Trigger));

            if (UseLifeVariable)
            {
                statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Real, VariableName.Life));
            }

            var createFunctions = new[]
            {
                nameof(CreateDestructable),
                nameof(CreateDeadDestructable),
                nameof(CreateDestructableZ),
                nameof(CreateDeadDestructableZ),
                nameof(BlzCreateDestructableWithSkin),
                nameof(BlzCreateDeadDestructableWithSkin),
                nameof(BlzCreateDestructableZWithSkin),
                nameof(BlzCreateDeadDestructableZWithSkin),
            };

            foreach (var(destructable, id) in mapDoodads.Doodads.IncludeId().Where(pair => CreateAllDestructablesConditionSingleDoodad(map, pair.Obj)))
            {
                var isDead              = destructable.Life == 0;
                var hasZ                = destructable.State.HasFlag(DoodadState.WithZ);
                var hasSkin             = destructable.SkinId != 0 && destructable.SkinId != destructable.TypeId;
                var createFunctionIndex = isDead ? 1 : 0;

                var arguments = new List <IExpressionSyntax>();
                arguments.Add(SyntaxFactory.FourCCLiteralExpression(destructable.TypeId));
                arguments.Add(SyntaxFactory.LiteralExpression(destructable.Position.X));
                arguments.Add(SyntaxFactory.LiteralExpression(destructable.Position.Y));
                if (hasZ)
                {
                    arguments.Add(SyntaxFactory.LiteralExpression(destructable.Position.Z));
                    createFunctionIndex += 2;
                }

                arguments.Add(SyntaxFactory.LiteralExpression(destructable.Rotation * (180f / MathF.PI), precision: 3));
                arguments.Add(SyntaxFactory.LiteralExpression(destructable.Scale.X, precision: 3));
                arguments.Add(SyntaxFactory.LiteralExpression(destructable.Variation));
                if (hasSkin)
                {
                    arguments.Add(SyntaxFactory.FourCCLiteralExpression(destructable.SkinId));
                    createFunctionIndex += 4;
                }

                statements.Add(SyntaxFactory.SetStatement(
                                   VariableName.Destructable,
                                   SyntaxFactory.InvocationExpression(createFunctions[createFunctionIndex], arguments.ToArray())));

                if (!isDead && destructable.Life != 100)
                {
                    if (UseLifeVariable)
                    {
                        statements.Add(SyntaxFactory.SetStatement(
                                           VariableName.Life,
                                           SyntaxFactory.InvocationExpression(
                                               nameof(GetDestructableLife),
                                               SyntaxFactory.VariableReferenceExpression(VariableName.Destructable))));

                        statements.Add(SyntaxFactory.CallStatement(
                                           nameof(SetDestructableLife),
                                           SyntaxFactory.VariableReferenceExpression(VariableName.Destructable),
                                           SyntaxFactory.BinaryMultiplicationExpression(
                                               SyntaxFactory.LiteralExpression(destructable.Life * 0.01f, precision: 2),
                                               SyntaxFactory.VariableReferenceExpression(VariableName.Life))));
                    }
                    else
                    {
                        statements.Add(SyntaxFactory.CallStatement(
                                           nameof(SetDestructableLife),
                                           SyntaxFactory.VariableReferenceExpression(VariableName.Destructable),
                                           SyntaxFactory.BinaryMultiplicationExpression(
                                               SyntaxFactory.LiteralExpression(destructable.Life * 0.01f, precision: 2),
                                               SyntaxFactory.InvocationExpression(nameof(GetDestructableLife), SyntaxFactory.VariableReferenceExpression(VariableName.Destructable)))));
                    }
                }

                statements.Add(SyntaxFactory.SetStatement(
                                   VariableName.Trigger,
                                   SyntaxFactory.InvocationExpression(nameof(CreateTrigger))));

                statements.Add(SyntaxFactory.CallStatement(
                                   nameof(TriggerRegisterDeathEvent),
                                   SyntaxFactory.VariableReferenceExpression(VariableName.Trigger),
                                   SyntaxFactory.VariableReferenceExpression(VariableName.Destructable)));

                statements.Add(SyntaxFactory.CallStatement(
                                   nameof(TriggerAddAction),
                                   SyntaxFactory.VariableReferenceExpression(VariableName.Trigger),
                                   SyntaxFactory.FunctionReferenceExpression(nameof(War3Api.Blizzard.SaveDyingWidget))));

                statements.Add(SyntaxFactory.CallStatement(
                                   nameof(TriggerAddAction),
                                   SyntaxFactory.VariableReferenceExpression(VariableName.Trigger),
                                   SyntaxFactory.FunctionReferenceExpression(destructable.GetDropItemsFunctionName(id))));
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(CreateAllDestructables)), statements));
        }
Example #8
0
        protected virtual JassFunctionDeclarationSyntax CreateRegions(Map map)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            var mapRegions = map.Regions;

            if (mapRegions is null)
            {
                throw new ArgumentException($"Function '{nameof(CreateRegions)}' cannot be generated without {nameof(MapRegions)}.", nameof(map));
            }

            var statements = new List <IStatementSyntax>();

            if (UseWeatherEffectVariable)
            {
                statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(SyntaxFactory.ParseTypeName(nameof(weathereffect)), VariableName.WeatherEffect));
                statements.Add(JassEmptyStatementSyntax.Value);
            }

            foreach (var region in mapRegions.Regions)
            {
                var regionName = region.GetVariableName();

                statements.Add(SyntaxFactory.SetStatement(
                                   regionName,
                                   SyntaxFactory.InvocationExpression(
                                       nameof(Rect),
                                       SyntaxFactory.LiteralExpression(region.Left, precision: 1),
                                       SyntaxFactory.LiteralExpression(region.Bottom, precision: 1),
                                       SyntaxFactory.LiteralExpression(region.Right, precision: 1),
                                       SyntaxFactory.LiteralExpression(region.Top, precision: 1))));

                if (region.WeatherType != WeatherType.None)
                {
                    if (UseWeatherEffectVariable)
                    {
                        statements.Add(SyntaxFactory.SetStatement(
                                           VariableName.WeatherEffect,
                                           SyntaxFactory.InvocationExpression(
                                               nameof(AddWeatherEffect),
                                               SyntaxFactory.VariableReferenceExpression(regionName),
                                               SyntaxFactory.FourCCLiteralExpression((int)region.WeatherType))));

                        statements.Add(SyntaxFactory.CallStatement(
                                           nameof(EnableWeatherEffect),
                                           SyntaxFactory.VariableReferenceExpression(VariableName.WeatherEffect),
                                           JassBooleanLiteralExpressionSyntax.True));
                    }
                    else
                    {
                        statements.Add(SyntaxFactory.CallStatement(
                                           nameof(EnableWeatherEffect),
                                           SyntaxFactory.InvocationExpression(
                                               nameof(AddWeatherEffect),
                                               SyntaxFactory.VariableReferenceExpression(regionName),
                                               SyntaxFactory.FourCCLiteralExpression((int)region.WeatherType)),
                                           JassBooleanLiteralExpressionSyntax.True));
                    }
                }

                if (!string.IsNullOrEmpty(region.AmbientSound))
                {
                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(SetSoundPosition),
                                       SyntaxFactory.VariableReferenceExpression(region.AmbientSound),
                                       SyntaxFactory.LiteralExpression(region.CenterX),
                                       SyntaxFactory.LiteralExpression(region.CenterY),
                                       SyntaxFactory.LiteralExpression(0f)));

                    statements.Add(SyntaxFactory.CallStatement(
                                       nameof(RegisterStackedSound),
                                       SyntaxFactory.VariableReferenceExpression(region.AmbientSound),
                                       SyntaxFactory.LiteralExpression(true),
                                       SyntaxFactory.LiteralExpression(region.Width),
                                       SyntaxFactory.LiteralExpression(region.Height)));
                }
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(CreateRegions)), statements));
        }
Example #9
0
        protected virtual JassFunctionDeclarationSyntax InitUpgrades_Player(Map map, int playerId)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            var mapInfo = map.Info;

            var statements = new List <IStatementSyntax>();

            var maxLevel   = new Dictionary <int, int>();
            var researched = new Dictionary <int, int>();

            for (var i = 0; i < mapInfo.UpgradeData.Count; i++)
            {
                var upgradeData = mapInfo.UpgradeData[i];
                if (upgradeData.Availability != UpgradeAvailability.Available && upgradeData.Players[playerId])
                {
                    if (upgradeData.Availability == UpgradeAvailability.Unavailable)
                    {
                        if (maxLevel.TryGetValue(upgradeData.Id, out var level))
                        {
                            if (upgradeData.Level < level)
                            {
                                maxLevel[upgradeData.Id] = upgradeData.Level;
                            }
                        }
                        else
                        {
                            maxLevel.Add(upgradeData.Id, upgradeData.Level);
                        }
                    }
                    else if (upgradeData.Availability == UpgradeAvailability.Researched)
                    {
                        if (researched.TryGetValue(upgradeData.Id, out var level))
                        {
                            if (upgradeData.Level > level)
                            {
                                researched[upgradeData.Id] = upgradeData.Level;
                            }
                        }
                        else
                        {
                            researched.Add(upgradeData.Id, upgradeData.Level);
                        }
                    }
                }
            }

            foreach (var tech in maxLevel)
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetPlayerTechMaxAllowed,
                                   SyntaxFactory.InvocationExpression(NativeName.Player, SyntaxFactory.LiteralExpression(playerId)),
                                   SyntaxFactory.FourCCLiteralExpression(tech.Key),
                                   SyntaxFactory.LiteralExpression(tech.Value)));
            }

            foreach (var tech in researched)
            {
                statements.Add(SyntaxFactory.CallStatement(
                                   NativeName.SetPlayerTechResearched,
                                   SyntaxFactory.InvocationExpression(NativeName.Player, SyntaxFactory.LiteralExpression(playerId)),
                                   SyntaxFactory.FourCCLiteralExpression(tech.Key),
                                   SyntaxFactory.LiteralExpression(tech.Value + 1)));
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(InitUpgrades_Player) + playerId), statements));
        }
Example #10
0
        protected virtual JassFunctionDeclarationSyntax CreateAllItems(Map map)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            var mapUnits = map.Units;

            if (mapUnits is null)
            {
                throw new ArgumentException($"Function '{nameof(CreateAllItems)}' cannot be generated without {nameof(MapUnits)}.", nameof(map));
            }

            var statements = new List <IStatementSyntax>();

            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, VariableName.ItemId));

            foreach (var item in mapUnits.Units.Where(item => CreateAllItemsConditionSingleItem(map, item)))
            {
                if (item.IsRandomItem())
                {
                    var randomData = item.RandomData;
                    switch (randomData)
                    {
                    case RandomUnitAny randomUnitAny:
                        statements.Add(SyntaxFactory.SetStatement(
                                           VariableName.ItemId,
                                           SyntaxFactory.InvocationExpression(
                                               NativeName.ChooseRandomItemEx,
                                               SyntaxFactory.InvocationExpression(NativeName.ConvertItemType, SyntaxFactory.LiteralExpression((int)randomUnitAny.Class)),
                                               SyntaxFactory.LiteralExpression(randomUnitAny.Level))));

                        break;

                    case RandomUnitGlobalTable randomUnitGlobalTable:
                        break;

                    case RandomUnitCustomTable randomUnitCustomTable:
                        statements.Add(SyntaxFactory.CallStatement(FunctionName.RandomDistReset));

                        var summedChance = 0;
                        foreach (var randomItem in randomUnitCustomTable.RandomUnits)
                        {
                            IExpressionSyntax id = RandomItemProvider.IsRandomItem(randomItem.UnitId, out var itemClass, out var level)
                                    ? SyntaxFactory.InvocationExpression(
                                NativeName.ChooseRandomItemEx,
                                SyntaxFactory.InvocationExpression(NativeName.ConvertItemType, SyntaxFactory.LiteralExpression((int)itemClass)),
                                SyntaxFactory.LiteralExpression(level))
                                    : SyntaxFactory.FourCCLiteralExpression(randomItem.UnitId);

                            statements.Add(SyntaxFactory.CallStatement(
                                               FunctionName.RandomDistAddItem,
                                               id,
                                               SyntaxFactory.LiteralExpression(randomItem.Chance)));

                            summedChance += randomItem.Chance;
                        }

                        if (summedChance < 100)
                        {
                            statements.Add(SyntaxFactory.CallStatement(
                                               FunctionName.RandomDistAddItem,
                                               SyntaxFactory.LiteralExpression(-1),
                                               SyntaxFactory.LiteralExpression(100 - summedChance)));
                        }

                        statements.Add(SyntaxFactory.SetStatement(
                                           VariableName.ItemId,
                                           SyntaxFactory.InvocationExpression(FunctionName.RandomDistChoose)));

                        break;

                    default:
                        break;
                    }

                    statements.Add(SyntaxFactory.IfStatement(
                                       SyntaxFactory.BinaryNotEqualsExpression(SyntaxFactory.VariableReferenceExpression(VariableName.ItemId), SyntaxFactory.LiteralExpression(-1)),
                                       SyntaxFactory.CallStatement(
                                           NativeName.CreateItem,
                                           SyntaxFactory.VariableReferenceExpression(VariableName.ItemId),
                                           SyntaxFactory.LiteralExpression(item.Position.X),
                                           SyntaxFactory.LiteralExpression(item.Position.Y))));
                }
                else
                {
                    var args = new List <IExpressionSyntax>()
                    {
                        SyntaxFactory.FourCCLiteralExpression(item.TypeId),
                        SyntaxFactory.LiteralExpression(item.Position.X),
                        SyntaxFactory.LiteralExpression(item.Position.Y),
                    };

                    var hasSkin = item.SkinId != 0 && item.SkinId != item.TypeId;
                    if (hasSkin)
                    {
                        args.Add(SyntaxFactory.FourCCLiteralExpression(item.SkinId));
                    }

                    statements.Add(SyntaxFactory.CallStatement(hasSkin ? NativeName.BlzCreateItemWithSkin : NativeName.CreateItem, args.ToArray()));
                }
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(CreateAllItems)), statements));
        }
Example #11
0
        protected virtual JassFunctionDeclarationSyntax InitRandomGroups(Map map)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            var randomUnitTables = map.Info.RandomUnitTables;

            if (randomUnitTables is null)
            {
                throw new ArgumentException($"Function '{nameof(InitRandomGroups)}' cannot be generated without {nameof(MapInfo.RandomUnitTables)}.", nameof(map));
            }

            var statements = new List <IStatementSyntax>();

            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, VariableName.CurrentSet));
            statements.Add(JassEmptyStatementSyntax.Value);

            foreach (var unitTable in randomUnitTables)
            {
                statements.Add(new JassCommentStatementSyntax($" Group {unitTable.Index} - {unitTable.Name}"));
                statements.Add(SyntaxFactory.CallStatement(FunctionName.RandomDistReset));

                for (var i = 0; i < unitTable.UnitSets.Count; i++)
                {
                    statements.Add(SyntaxFactory.CallStatement(
                                       FunctionName.RandomDistAddItem,
                                       SyntaxFactory.LiteralExpression(i),
                                       SyntaxFactory.LiteralExpression(unitTable.UnitSets[i].Chance)));
                }

                statements.Add(SyntaxFactory.SetStatement(VariableName.CurrentSet, SyntaxFactory.InvocationExpression(FunctionName.RandomDistChoose)));
                statements.Add(JassEmptyStatementSyntax.Value);

                var groupVarName   = unitTable.GetVariableName();
                var ifElseifBlocks = new List <(IExpressionSyntax Condition, IStatementSyntax[] Body)>();
                for (var setIndex = 0; setIndex < unitTable.UnitSets.Count; setIndex++)
                {
                    var set = unitTable.UnitSets[setIndex];

                    var condition      = SyntaxFactory.BinaryEqualsExpression(SyntaxFactory.VariableReferenceExpression(VariableName.CurrentSet), SyntaxFactory.LiteralExpression(setIndex));
                    var bodyStatements = new List <IStatementSyntax>();

                    for (var position = 0; position < unitTable.Types.Count; position++)
                    {
                        var id = set?.UnitIds[position] ?? 0;
                        var unitTypeExpression = RandomUnitProvider.IsRandomUnit(id, out var level)
                            ? SyntaxFactory.InvocationExpression(NativeName.ChooseRandomCreep, SyntaxFactory.LiteralExpression(level))
                            : id == 0 ? SyntaxFactory.LiteralExpression(-1) : SyntaxFactory.FourCCLiteralExpression(id);

                        bodyStatements.Add(SyntaxFactory.SetStatement(
                                               groupVarName,
                                               SyntaxFactory.LiteralExpression(position),
                                               unitTypeExpression));
                    }

                    ifElseifBlocks.Add((new JassParenthesizedExpressionSyntax(condition), bodyStatements.ToArray()));
                }

                var elseClauseStatements = new List <IStatementSyntax>();
                for (var position = 0; position < unitTable.Types.Count; position++)
                {
                    elseClauseStatements.Add(SyntaxFactory.SetStatement(
                                                 groupVarName,
                                                 SyntaxFactory.LiteralExpression(position),
                                                 SyntaxFactory.LiteralExpression(-1)));
                }

                statements.Add(SyntaxFactory.IfStatement(
                                   ifElseifBlocks.First().Condition,
                                   SyntaxFactory.StatementList(ifElseifBlocks.First().Body),
                                   ifElseifBlocks.Skip(1).Select(elseIf => new JassElseIfClauseSyntax(elseIf.Condition, SyntaxFactory.StatementList(elseIf.Body))),
                                   new JassElseClauseSyntax(SyntaxFactory.StatementList(elseClauseStatements))));

                statements.Add(JassEmptyStatementSyntax.Value);
            }

            return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(InitRandomGroups)), statements));
        }