Пример #1
0
        public void TestGenerateMapPreviewIcons(string inputFolder)
        {
            var mapInfo        = MapInfo.Parse(File.OpenRead(Path.Combine(inputFolder, MapInfo.FileName)));
            var mapEnvironment = MapEnvironment.Parse(File.OpenRead(Path.Combine(inputFolder, MapEnvironment.FileName)));
            var mapUnits       = MapUnits.Parse(File.OpenRead(Path.Combine(inputFolder, MapUnits.FileName)));

            var expected = MapPreviewIcons.Parse(File.OpenRead(Path.Combine(inputFolder, MapPreviewIcons.FileName)));
            var actual   = new MapPreviewIcons(mapInfo, mapEnvironment, mapUnits);

            var expectedEnumerator = expected.GetEnumerator();
            var actualEnumerator   = actual.GetEnumerator();

            while (true)
            {
                if (expectedEnumerator.MoveNext() & actualEnumerator.MoveNext())
                {
                    var expectedIcon = expectedEnumerator.Current;
                    var actualIcon   = actualEnumerator.Current;

                    const int delta = 1;
                    Assert.AreEqual(expectedIcon.IconType, actualIcon.IconType);
                    Assert.AreEqual(expectedIcon.X, actualIcon.X, delta);
                    Assert.AreEqual(expectedIcon.Y, actualIcon.Y, delta);
                    Assert.AreEqual(expectedIcon.Color.ToArgb(), actualIcon.Color.ToArgb());
                }
                else if (expectedEnumerator.Current != null || actualEnumerator.Current != null)
                {
                    Assert.Fail("Expected and actual icon count are not the same.");
                }
                else
                {
                    break;
                }
            }
        }
Пример #2
0
        public void RefreshMapUnits()
        {
            var mapUnits = objectTileMap.GetComponentsInChildren <GameUnit>();

            if (mapUnits.Any())
            {
                foreach (var unit in mapUnits)
                {
                    // Add the unit to our list of units and objects on this map
                    if (!MapUnits.Contains(unit))
                    {
                        MapUnits.Add(unit);
                    }
                    if (!MapObjects.Contains(unit))
                    {
                        MapObjects.Add(unit);
                    }
                    // And make sure it's Map X/Y coordinates are corred in our grid
                    var unitPosition = objectTileMap.WorldToCell(unit.transform.position);
                    var gridPosition = objectTileMap.CellToWorld(unitPosition);
                    unit.transform.position = gridPosition;
                    TranslateUnityXYToMapXY(unitPosition, out var unitMapX, out var unitMapY);
                    unit.MapX = unitMapX;
                    unit.MapY = unitMapY;
                }
            }
        }
        /// <summary>
        /// Calculates the length in meters between two WebMercator points using spherical distance.
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static double CalculateLengthMeters(MapPoint p1, MapPoint p2, MapUnits mapUnits)
        {
            switch (mapUnits)
            {
            case MapUnits.WebMercatorMeters:
                try
                {
                    MapPoint g1 = p1.WebMercatorToGeographic();
                    MapPoint g2 = p2.WebMercatorToGeographic();
                    return(GetSphericalDistanceMeters(g1, g2));
                }
                catch (ArgumentException)
                {
                    //if the point is outside web mercator coordinates return NAN
                    return(double.NaN);
                }

            case MapUnits.DecimalDegrees:
                return(GetSphericalDistanceMeters(p1, p2));

            case MapUnits.Feet:
                return(Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)) * FeetToMeters);

            default:
                return(Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)));
            }
        }
Пример #4
0
        private static void GetUnitInformation(MapUnits mapUnit, out double factor, out string name,
                                               out string shortName)
        {
            switch (mapUnit)
            {
            case MapUnits.ws_muCustom:
                factor    = 1.0;
                name      = "Unknown";
                shortName = "Unknown";
                break;

            case MapUnits.ws_muMeter:
                factor    = 1.0;
                name      = "Meters";
                shortName = "m";
                break;

            case MapUnits.ws_muFootUS:
                factor    = 0.30480061;
                name      = "Feet";
                shortName = "f";
                break;

            case MapUnits.ws_muYardIndian:
                factor    = 0.914398415;
                name      = "Yards";
                shortName = "yd";
                break;

            case MapUnits.ws_muYardSears:
                factor    = 0.914398531;
                name      = "Yard";
                shortName = "yd";
                break;

            case MapUnits.ws_muMileUS:
                factor    = 1609.347219;
                name      = "Miles";
                shortName = "mi";
                break;

            case MapUnits.ws_muKilometer:
                factor    = 1000.0;
                name      = "Kilometers";
                shortName = "km";
                break;

            case MapUnits.ws_muDegree:
                factor    = 0.0175;
                name      = "Degrees";
                shortName = "d";
                break;

            default:
                factor    = 1.0;
                name      = "Unknown";
                shortName = "Unknown";
                break;
            }
        }
Пример #5
0
        public void TestParseMapUnits(string mapUnitsFilePath)
        {
            using var original  = FileProvider.GetFile(mapUnitsFilePath);
            using var recreated = new MemoryStream();

            MapUnits.Parse(original, true).SerializeTo(recreated, true);
            StreamAssert.AreEqual(original, recreated, true);
        }
Пример #6
0
 public FunctionBuilderData(MapInfo mapInfo, MapDoodads mapDoodads, MapUnits mapUnits, MapRegions mapRegions, MapSounds mapSounds, string?lobbyMusic, bool csharp)
 {
     _mapInfo    = mapInfo;
     _mapDoodads = mapDoodads;
     _mapUnits   = mapUnits;
     _mapRegions = mapRegions;
     _mapSounds  = mapSounds;
     _lobbyMusic = lobbyMusic;
     _csharp     = csharp;
 }
Пример #7
0
 public static void DowngradeOnce(this MapUnits mapUnits)
 {
     if (mapUnits.UseNewFormat)
     {
         mapUnits.UseNewFormat = false;
     }
     else
     {
         mapUnits.FormatVersion = MapWidgetsFormatVersion.RoC;
         mapUnits.SubVersion    = MapWidgetsSubVersion.V9;
     }
 }
Пример #8
0
        public static GamePatch GetMinimumPatch(this MapUnits mapUnits)
        {
            if (mapUnits.UseNewFormat)
            {
                return(GamePatch.v1_32_0);
            }

            return(mapUnits.FormatVersion switch
            {
                MapWidgetsFormatVersion.RoC => GamePatch.v1_00,
                MapWidgetsFormatVersion.TFT => GamePatch.v1_07,
            });
Пример #9
0
        public void TestParseMapUnits(string mapUnitsFilePath)
        {
            using var recreated = new MemoryStream();
            using var original  = File.OpenRead(mapUnitsFilePath);

            var data = MapUnits.Parse(original, true);

            Assert.AreEqual(original.Length, original.Position);

            data.SerializeTo(recreated, true);

            StreamAssert.AreEqual(original, recreated, true);
        }
Пример #10
0
        public static bool TryDowngrade(this MapUnits mapUnits, GamePatch targetPatch)
        {
            try
            {
                while (mapUnits.GetMinimumPatch() > targetPatch)
                {
                    mapUnits.DowngradeOnce();
                }

                return(true);
            }
            catch (NotSupportedException)
            {
                return(false);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// Calculates the length of the polyline.
        /// </summary>
        public static double CalculateLengthMeters(Polyline polyline, MapUnits mapUnits)
        {
            double length = 0;

            foreach (ESRI.ArcGIS.Client.Geometry.PointCollection points in polyline.Paths)
            {
                for (int i = 1; i < points.Count; i++)
                {
                    double value = CalculateLengthMeters(points[i - 1], points[i], mapUnits);
                    if (value >= 0)
                    {
                        length += value;
                    }
                    else
                    {
                        return(value); //failed to calculate length, return
                    }
                }
            }

            return(length);
        }
Пример #12
0
        public void TestParseMapUnits(string mapUnitsFilePath)
        {
            using var recreated = new MemoryStream();
            using var original  = File.OpenRead(mapUnitsFilePath);

            var data = MapUnits.Parse(original, true);

            Assert.AreEqual(original.Length, original.Position);

            data.SerializeTo(recreated, true);

            original.Position = 4;
            using (var reader = new BinaryReader(original, new UTF8Encoding(false, true), true))
            {
                var version = (MapWidgetsVersion)reader.ReadUInt32();
                if (version != MapWidgetsVersion.TFT)
                {
                    // Can't compare streams, because serialize always saves as TFT, which sets header version to 8 and has additional data per unit.
                    return;
                }
            }

            StreamAssert.AreEqual(original, recreated, true);
        }
Пример #13
0
        public static MapPreviewIcons GenerateIcons(MapInfo info, MapEnvironment environment, MapUnits units)
        {
            var padding = info.CameraBoundsComplements;

            var minX  = environment.Left + (128 * padding.Left);
            var width = environment.Right - (128 * padding.Right) - minX;

            var minY   = environment.Bottom + (128 * padding.Bottom);
            var height = environment.Top - (128 * padding.Top) - minY;

            var ratio = width / height;
            var sizeX = 255 * (ratio > 1 ? 1 : ratio);
            var sizeY = 255 * (ratio > 1 ? 1 / ratio : 1);

            var lowerX = 1 + (0.5f * (255 - sizeX));
            var upperY = 1 + (0.5f * (255 + sizeY));

            sizeX /= width;
            sizeY /= height;

            var icons = new List <PreviewIcon>();

            if (units != null)
            {
                foreach (var unit in units.Units)
                {
                    if (MapPreviewIconProvider.TryGetIcon(unit.TypeId, unit.OwnerId, out var iconType, out var iconColor))
                    {
                        icons.Add(new PreviewIcon()
                        {
                            IconType = iconType,
                            // X = (byte)(lowerX + (sizeX * (unit.Position.X - minX))),
                            // Y = (byte)(upperY - (sizeY * (unit.Position.Y - minY))),
                            X     = (byte)(0.0f + lowerX + (sizeX * (unit.Position.X - minX))),
                            Y     = (byte)(0.5f + upperY - (sizeY * (unit.Position.Y - minY))),
                            Color = iconColor,
                        });
                    }
                }
            }

            return(new MapPreviewIcons(MapPreviewIconsFormatVersion.Normal)
            {
                Icons = icons,
            });
        }
Пример #14
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));
        }
Пример #15
0
 private static void GetUnitInformation(MapUnits mapUnit, out double factor, out string name,
                                        out string shortName)
 {
     switch (mapUnit)
     {
         case MapUnits.ws_muCustom:
             factor = 1.0;
             name = "Unknown";
             shortName = "Unknown";
             break;
         case MapUnits.ws_muMeter:
             factor = 1.0;
             name = "Meters";
             shortName = "m";
             break;
         case MapUnits.ws_muFootUS:
             factor = 0.30480061;
             name = "Feet";
             shortName = "f";
             break;
         case MapUnits.ws_muYardIndian:
             factor = 0.914398415;
             name = "Yards";
             shortName = "yd";
             break;
         case MapUnits.ws_muYardSears:
             factor = 0.914398531;
             name = "Yard";
             shortName = "yd";
             break;
         case MapUnits.ws_muMileUS:
             factor = 1609.347219;
             name = "Miles";
             shortName = "mi";
             break;
         case MapUnits.ws_muKilometer:
             factor = 1000.0;
             name = "Kilometers";
             shortName = "km";
             break;
         case MapUnits.ws_muDegree:
             factor = 0.0175;
             name = "Degrees";
             shortName = "d";
             break;
         default:
             factor = 1.0;
             name = "Unknown";
             shortName = "Unknown";
             break;
     }
 }
Пример #16
0
 private static bool WantGenerateUnitsHelperFunction(MapUnits mapUnits)
 {
     return(mapUnits?.Where(mapUnit => mapUnit.IsUnit).FirstOrDefault() != null);
 }
Пример #17
0
 private static bool WantGenerateItemsHelperFunction(MapUnits mapUnits)
 {
     return(mapUnits?.Where(mapUnit => mapUnit.IsItem && mapUnit.TypeId != "sloc").FirstOrDefault() != null);
 }
Пример #18
0
        static public double DistanceTo(BasicGeoposition pos1, BasicGeoposition pos2, MapUnits unit = MapUnits.Killometers)
        {
            double rlat1  = Math.PI * pos1.Latitude / 180;
            double rlat2  = Math.PI * pos2.Latitude / 180;
            double theta  = pos1.Longitude - pos2.Longitude;
            double rtheta = Math.PI * theta / 180;
            double dist   =
                Math.Sin(rlat1) * Math.Sin(rlat2) + Math.Cos(rlat1) *
                Math.Cos(rlat2) * Math.Cos(rtheta);

            dist = Math.Acos(dist);
            dist = dist * 180 / Math.PI;
            dist = dist * 60 * 1.1515;

            switch (unit)
            {
            case MapUnits.Killometers:
                return(dist * 1.609344);

            case MapUnits.NauticalMiles:
                return(dist * 0.8684);

            case MapUnits.Miles:
                return(dist);
            }

            return(dist);
        }
Пример #19
0
 public static void Write(this BinaryWriter writer, MapUnits mapUnits) => mapUnits.WriteTo(writer);