Esempio n. 1
0
        public static byte[] ConvertMakeWalls(string json)
        {
            var obj = JsonConvert.DeserializeObject <json_dat>(json);
            var dat = ZlibStream.UncompressBuffer(obj.data);

            Dictionary <short, TerrainTile> tileDict = new Dictionary <short, TerrainTile>();

            for (int i = 0; i < obj.dict.Length; i++)
            {
                var o = obj.dict[i];
                tileDict[(short)i] = new TerrainTile()
                {
                    TileId  = o.ground == null ? (short)0xff : XmlDatas.IdToType[o.ground],
                    TileObj = o.objs == null ? null : o.objs[0].id,
                    Name    = o.objs == null ? "" : o.objs[0].name ?? "",
                    Terrain = TerrainType.None,
                    Region  = o.regions == null ? TileRegion.None : (TileRegion)Enum.Parse(typeof(TileRegion), o.regions[0].id.Replace(' ', '_'))
                };
            }

            var tiles = new TerrainTile[obj.width, obj.height];

            using (NReader rdr = new NReader(new MemoryStream(dat)))
                for (int y = 0; y < obj.height; y++)
                {
                    for (int x = 0; x < obj.width; x++)
                    {
                        tiles[x, y]   = tileDict[rdr.ReadInt16()];
                        tiles[x, y].X = x;
                        tiles[x, y].Y = y;
                    }
                }

            foreach (var i in tiles)
            {
                if (i.TileId == 0xff && i.TileObj == null)
                {
                    bool createWall = false;
                    for (int ty = -1; ty <= 1; ty++)
                    {
                        for (int tx = -1; tx <= 1; tx++)
                        {
                            try
                            {
                                if (tiles[i.X + tx, i.Y + ty].TileId != 0xff)
                                {
                                    createWall = true;
                                }
                            }
                            catch { }
                        }
                    }
                    if (createWall)
                    {
                        tiles[i.X, i.Y].TileObj = "Grey Wall";
                    }
                }
            }

            return(WorldMapExporter.Export(tiles));
        }
 private void pic_MouseMove(object sender, MouseEventArgs e)
 {
     if (mode == Mode.Erase && (MouseButtons & MouseButtons.Left) != 0)
     {
         Point center = e.Location;
         for (int y = -10; y <= 10; y++)
         {
             for (int x = -10; x <= 10; x++)
             {
                 if (x * x + y * y <= 10 * 10)
                 {
                     tiles[center.X + x, center.Y + y].Terrain   = TerrainType.None;
                     tiles[center.X + x, center.Y + y].Elevation = 0;
                     tiles[center.X + x, center.Y + y].Biome     = "ocean";
                     tiles[center.X + x, center.Y + y].TileObj   = "";
                     tiles[center.X + x, center.Y + y].TileId    = TileTypes.DeepWater;
                     tiles[center.X + x, center.Y + y].Region    = TileRegion.None;
                     tiles[center.X + x, center.Y + y].Name      = "";
                     bmp.SetPixel(center.X + x, center.Y + y, Color.FromArgb(
                                      (int)GetColor(tiles[center.X + x, center.Y + y])));
                 }
             }
         }
         pic.Invalidate();
         pic2.Invalidate();
     }
     else if (mode == Mode.Average && (MouseButtons & MouseButtons.Left) != 0)
     {
         Point center = e.Location;
         Dictionary <TerrainTile, int> dict = new Dictionary <TerrainTile, int>();
         for (int y = -10; y <= 10; y++)
         {
             for (int x = -10; x <= 10; x++)
             {
                 if (x * x + y * y <= 10 * 10)
                 {
                     TerrainTile t = tiles[center.X + x, center.Y + y];
                     if (dict.ContainsKey(t))
                     {
                         dict[t]++;
                     }
                     else
                     {
                         dict[t] = 0;
                     }
                 }
             }
         }
         int         maxOccurance = dict.Values.Max();
         TerrainTile targetTile   = dict.First(t => t.Value == maxOccurance).Key;
         for (int y = -10; y <= 10; y++)
         {
             for (int x = -10; x <= 10; x++)
             {
                 if (x * x + y * y <= 10 * 10)
                 {
                     tiles[center.X + x, center.Y + y] = targetTile;
                     bmp.SetPixel(center.X + x, center.Y + y, Color.FromArgb(
                                      (int)GetColor(tiles[center.X + x, center.Y + y])));
                 }
             }
         }
         pic.Invalidate();
         pic2.Invalidate();
     }
 }
Esempio n. 3
0
        TerrainType GetBiomeTerrain(TerrainTile tile)
        {
            if (tile.PolygonId == -1)
            {
                return(TerrainType.None);
            }
            MapPolygon poly = map.Polygons[tile.PolygonId];

            if (!poly.IsWater && beaches.Contains(poly))
            {
                return(TerrainType.ShoreSand);
            }
            else if (poly.IsWater)
            {
                return(TerrainType.None);
            }
            else
            {
                if (tile.Elevation >= elevationThreshold[3])
                {
                    return(TerrainType.Mountains);
                }
                else if (tile.Elevation > elevationThreshold[2])
                {
                    if (tile.Moisture > moistureThreshold[4])
                    {
                        return(TerrainType.HighPlains);
                    }
                    else if (tile.Moisture > moistureThreshold[2])
                    {
                        return(TerrainType.HighForest);
                    }
                    else
                    {
                        return(TerrainType.HighSand);
                    }
                }
                else if (tile.Elevation > elevationThreshold[1])
                {
                    if (tile.Moisture > moistureThreshold[4])
                    {
                        return(TerrainType.MidForest);
                    }
                    else if (tile.Moisture > moistureThreshold[2])
                    {
                        return(TerrainType.MidPlains);
                    }
                    else
                    {
                        return(TerrainType.MidSand);
                    }
                }
                else
                {
                    if (poly.Neighbour.Any(_ => beaches.Contains(_)))
                    {
                        if (tile.Moisture > moistureThreshold[2])
                        {
                            return(TerrainType.ShorePlains);
                        }
                    }

                    if (tile.Moisture > moistureThreshold[3])
                    {
                        return(TerrainType.LowForest);
                    }
                    else if (tile.Moisture > moistureThreshold[2])
                    {
                        return(TerrainType.LowPlains);
                    }
                    else
                    {
                        return(TerrainType.LowSand);
                    }
                }
            }
            return(TerrainType.None);
        }
Esempio n. 4
0
        string GetBiome(TerrainTile tile)
        {
            if (tile.PolygonId == -1)
            {
                return("unknown");
            }
            MapPolygon poly = map.Polygons[tile.PolygonId];

            if (beaches.Contains(poly))
            {
                return("beach");
            }
            else if (poly.IsWater)
            {
                if (poly.IsCoast)
                {
                    return("coast");
                }
                else if (poly.IsOcean)
                {
                    return("ocean");
                }
                else
                {
                    return("water");
                }
            }
            else
            {
                if (tile.Elevation >= elevationThreshold[3])
                {
                    if (tile.Moisture > moistureThreshold[4])
                    {
                        return("snowy");
                    }
                    else
                    {
                        return("mountain");
                    }
                }
                else if (tile.Elevation > elevationThreshold[2])
                {
                    if (tile.Moisture > moistureThreshold[4])
                    {
                        return("dryland");
                    }
                    else if (tile.Moisture > moistureThreshold[2])
                    {
                        return("taiga");
                    }
                    else
                    {
                        return("desert");
                    }
                }
                else if (tile.Elevation > elevationThreshold[1])
                {
                    if (tile.Moisture > moistureThreshold[4])
                    {
                        return("forest");
                    }
                    else if (tile.Moisture > moistureThreshold[2])
                    {
                        return("shrub");
                    }
                    else
                    {
                        return("desert");
                    }
                }
                else
                {
                    if (tile.Moisture > moistureThreshold[4])
                    {
                        return("rainforest");
                    }
                    else if (tile.Moisture > moistureThreshold[3])
                    {
                        return("forest");
                    }
                    else if (tile.Moisture > moistureThreshold[2])
                    {
                        return("grassland");
                    }
                    else
                    {
                        return("desert");
                    }
                }
            }
            return("unknown");
        }
Esempio n. 5
0
        static TerrainTile[,] CreateTerrain(int seed, PolygonMap map)
        {
            Rasterizer <TerrainTile> rasterizer = new Rasterizer <TerrainTile>(Size, Size);

            //Set all to ocean
            rasterizer.Clear(new TerrainTile()
            {
                PolygonId = -1,
                Elevation = 0,
                Moisture  = 1,
                TileId    = TileTypes.DeepWater,
                TileObj   = null
            });
            //Render lands poly
            foreach (var poly in map.Polygons.Where(_ => !_.IsWater))
            {
                uint color = 0x00ffffff;
                color |= (uint)(poly.DistanceToCoast * 255) << 24;
                rasterizer.FillPolygon(
                    poly.Nodes.SelectMany(_ =>
                {
                    return(new[] { (_.X + 1) / 2 * Size,
                                   (_.Y + 1) / 2 * Size });
                }).Concat(new[] { (poly.Nodes[0].X + 1) / 2 * Size,
                                  (poly.Nodes[0].Y + 1) / 2 * Size }).ToArray(),
                    new TerrainTile()
                {
                    PolygonId = poly.Id,
                    Elevation = (float)poly.DistanceToCoast,
                    Moisture  = -1,
                    TileId    = TileTypes.Grass,
                    TileObj   = null
                });
            }
            MapFeatures fea = new MapFeatures(map, seed);
            //Render roads
            var roads = fea.GenerateRoads();

            foreach (var i in roads)
            {
                rasterizer.DrawClosedCurve(i.SelectMany(_ => new[] {
                    (_.X + 1) / 2 * Size, (_.Y + 1) / 2 * Size
                }).ToArray(),
                                           1, t =>
                {
                    t.TileId = TileTypes.Road;
                    return(t);
                }, 3);
            }
            //Render waters poly
            foreach (var poly in map.Polygons.Where(_ => _.IsWater))
            {
                var tile = new TerrainTile()
                {
                    PolygonId = poly.Id,
                    Elevation = (float)poly.DistanceToCoast,
                    TileObj   = null
                };
                if (poly.IsCoast)
                {
                    tile.TileId   = TileTypes.MovingWater;
                    tile.Moisture = 0;
                }
                else
                {
                    tile.TileId   = TileTypes.DeepWater;
                    tile.Moisture = 1;
                }
                rasterizer.FillPolygon(
                    poly.Nodes.SelectMany(_ =>
                {
                    return(new[] { (_.X + 1) / 2 * Size,
                                   (_.Y + 1) / 2 * Size });
                }).Concat(new[] { (poly.Nodes[0].X + 1) / 2 * Size,
                                  (poly.Nodes[0].Y + 1) / 2 * Size }).ToArray(), tile);
            }
            //Render rivers
            var rivers = fea.GenerateRivers();
            Dictionary <Tuple <MapNode, MapNode>, int> edges = new Dictionary <Tuple <MapNode, MapNode>, int>();

            foreach (var i in rivers)
            {
                for (int j = 1; j < i.Length; j++)
                {
                    Tuple <MapNode, MapNode> edge = new Tuple <MapNode, MapNode>(i[j - 1], i[j]);
                    int count;
                    if (edges.TryGetValue(edge, out count))
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    edges[edge] = count;
                }
            }
            foreach (var i in edges)
            {
                i.Key.Item1.IsWater    = true;
                i.Key.Item1.RiverValue = i.Value + 1;
                i.Key.Item2.IsWater    = true;
                i.Key.Item2.RiverValue = i.Value + 1;
                rasterizer.DrawLineBresenham(
                    (i.Key.Item1.X + 1) / 2 * Size, (i.Key.Item1.Y + 1) / 2 * Size,
                    (i.Key.Item2.X + 1) / 2 * Size, (i.Key.Item2.Y + 1) / 2 * Size,
                    t =>
                {
                    t.TileId    = TileTypes.Water;
                    t.Elevation = (float)(i.Key.Item1.DistanceToCoast + i.Key.Item2.DistanceToCoast) / 2;
                    t.Moisture  = 1;
                    return(t);
                }, 3 * Math.Min(2, i.Value));
            }

            return(rasterizer.Buffer);
        }
Esempio n. 6
0
        private void Randomize(TerrainTile[,] buff)
        {
            int w = buff.GetLength(0);
            int h = buff.GetLength(1);

            TerrainTile[,] tmp = (TerrainTile[, ])buff.Clone();
            for (int y = 10; y < h - 10; y++)
            {
                for (int x = 10; x < w - 10; x++)
                {
                    TerrainTile tile = buff[x, y];

                    if (tile.TileId == TileTypes.Water && tile.Elevation >= _elevationThreshold[3])
                    {
                        tile.TileId = TileTypes.SnowRock;
                    }
                    else if (tile.TileId != TileTypes.Water && tile.TileId != TileTypes.Road &&
                             tile.TileId != TileTypes.Beach && tile.TileId != TileTypes.MovingWater &&
                             tile.TileId != TileTypes.DeepWater)
                    {
                        ushort id = tmp[x + _rand.Next(-3, 4), y + _rand.Next(-3, 4)].TileId;
                        while (id == TileTypes.Water || id == TileTypes.Road ||
                               id == TileTypes.Beach || id == TileTypes.MovingWater ||
                               id == TileTypes.DeepWater)
                        {
                            id = tmp[x + _rand.Next(-3, 4), y + _rand.Next(-3, 4)].TileId;
                        }
                        tile.TileId = id;
                    }

                    //if (tile.TileId == TileTypes.Beach)
                    //    tile.Region = TileRegion.Spawn;

                    string biome = tile.Biome;
                    if (tile.TileId == TileTypes.Beach)
                    {
                        biome = "beach";
                    }
                    else if (tile.TileId == TileTypes.MovingWater)
                    {
                        biome = "coast";
                    }

                    string biomeObj = Decoration.GetDecor(biome, _rand);
                    if (biomeObj != null)
                    {
                        tile.TileObj = biomeObj;
                        int?size = Decoration.GetSize(biomeObj, _rand);
                        if (size != null)
                        {
                            tile.Name = "size:" + size;
                        }
                    }

                    float elevation = 0;
                    int   c         = 0;
                    for (int dy = -1; dy <= 1; dy++)
                    {
                        for (int dx = -1; dx <= 1; dx++)
                        {
                            if (x + dx < 0 || x + dx >= w || y + dy < 0 || y + dy >= h)
                            {
                                continue;
                            }
                            elevation += tmp[x + dx, y + dy].Elevation;
                            c++;
                        }
                    }
                    tile.Elevation = elevation / c;

                    buff[x, y] = tile;
                }
            }
        }
Esempio n. 7
0
        private string GetBiome(TerrainTile tile)
        {
            if (tile.PolygonId == -1)
            {
                return("unknown");
            }
            if (tile.TileId == TileTypes.Road)
            {
                return("road");
            }
            if (tile.TileId == TileTypes.Water)
            {
                return("river");
            }
            MapPolygon poly = _map.Polygons[tile.PolygonId];

            if (tile.TileId == 0xb4)
            {
                return("towel");
            }

            if (_beaches.Contains(poly))
            {
                return("beach");
            }
            if (poly.IsWater)
            {
                if (poly.IsCoast)
                {
                    return("coast");
                }
                if (poly.IsOcean)
                {
                    return("ocean");
                }
                return("water");
            }
            if (tile.Elevation >= _elevationThreshold[3])
            {
                if (tile.Moisture > _moistureThreshold[4])
                {
                    return("snowy");
                }
                return("mountain");
            }
            if (tile.Elevation > _elevationThreshold[2])
            {
                if (tile.Moisture > _moistureThreshold[4])
                {
                    return("dryland");
                }
                if (tile.Moisture > _moistureThreshold[2])
                {
                    return("taiga");
                }
                return("desert");
            }
            if (tile.Elevation > _elevationThreshold[1])
            {
                if (tile.Moisture > _moistureThreshold[4])
                {
                    return("forest");
                }
                if (tile.Moisture > _moistureThreshold[2])
                {
                    return("shrub");
                }
                return("desert");
            }
            if (tile.Moisture > _moistureThreshold[4])
            {
                return("rainforest");
            }
            if (tile.Moisture > _moistureThreshold[3])
            {
                return("forest");
            }
            if (tile.Moisture > _moistureThreshold[2])
            {
                return("grassland");
            }
            return("desert");
            //return "unknown";
        }
Esempio n. 8
0
        private TerrainType GetBiomeTerrain(TerrainTile tile)
        {
            if (tile.PolygonId == -1 ||
                tile.TileId == TileTypes.Road ||
                tile.TileId == TileTypes.Water)
            {
                return(TerrainType.None);
            }
            MapPolygon poly = _map.Polygons[tile.PolygonId];

            if (!poly.IsWater && _beaches.Contains(poly))
            {
                return(TerrainType.ShoreSand);
            }
            if (poly.IsWater)
            {
                return(TerrainType.None);
            }
            if (tile.Elevation >= _elevationThreshold[3])
            {
                return(TerrainType.Mountains);
            }
            if (tile.Elevation > _elevationThreshold[2])
            {
                if (tile.Moisture > _moistureThreshold[4])
                {
                    return(TerrainType.HighPlains);
                }
                if (tile.Moisture > _moistureThreshold[2])
                {
                    return(TerrainType.HighForest);
                }
                return(TerrainType.HighSand);
            }
            if (tile.Elevation > _elevationThreshold[1])
            {
                if (tile.Moisture > _moistureThreshold[4])
                {
                    return(TerrainType.MidForest);
                }
                if (tile.Moisture > _moistureThreshold[2])
                {
                    return(TerrainType.MidPlains);
                }
                return(TerrainType.MidSand);
            }
            if (poly.Neighbour.Any(_ => _beaches.Contains(_)))
            {
                if (tile.Moisture > _moistureThreshold[2])
                {
                    return(TerrainType.ShorePlains);
                }
            }

            if (tile.Moisture > _moistureThreshold[3])
            {
                return(TerrainType.LowForest);
            }
            if (tile.Moisture > _moistureThreshold[2])
            {
                return(TerrainType.LowPlains);
            }
            return(TerrainType.LowSand);
            //return TerrainType.None;
        }
Esempio n. 9
0
        public static byte[] Convert(XmlData data, string json)
        {
            var obj = JsonConvert.DeserializeObject <JsonDat>(json);
            var dat = ZlibStream.UncompressBuffer(obj.data);

            Dictionary <short, TerrainTile> tileDict = new Dictionary <short, TerrainTile>();

            for (int i = 0; i < obj.dict.Length; i++)
            {
                var o = obj.dict[i];
                tileDict[(short)i] = new TerrainTile
                {
                    TileId  = o.ground == null ? (ushort)0xff : data.IdToTileType[o.ground],
                    TileObj = o.objs == null ? null : o.objs[0].id,
                    Name    = o.objs == null ? "" : o.objs[0].name ?? "",
                    Terrain = TerrainType.None,
                    Region  = o.regions == null ? TileRegion.None : (TileRegion)Enum.Parse(typeof(TileRegion), o.regions[0].id.Replace(' ', '_'))
                };
            }

            var    tiles = new TerrainTile[obj.width, obj.height];
            ushort objType;
            //creates a new case insensitive dictionary based on the XmlDatas
            Dictionary <string, ushort> icdatas = new Dictionary <string, ushort>(
                data.IdToObjectType,
                StringComparer.OrdinalIgnoreCase);

            using (NReader rdr = new NReader(new MemoryStream(dat)))
                for (int y = 0; y < obj.height; y++)
                {
                    for (int x = 0; x < obj.width; x++)
                    {
                        tiles[x, y] = tileDict[rdr.ReadInt16()];
                        if (tiles[x, y].TileId.ToString().Length == 2)
                        {
                            File.AppendAllText("Tiles.txt", tiles[x, y].TileId + "  ");
                        }
                        else if (String.IsNullOrEmpty(tiles[x, y].TileId.ToString()))
                        {
                            File.AppendAllText("Tiles.txt", "   ");
                        }
                        else
                        {
                            File.AppendAllText("Tiles.txt", tiles[x, y].TileId + " ");
                        }
                        if (tiles[x, y].TileObj == null)
                        {
                            File.AppendAllText("Objects.txt", "     ");
                        }
                        else
                        {
                            if (!icdatas.TryGetValue(tiles[x, y].TileObj, out objType) ||
                                !data.ObjectDescs.ContainsKey(objType))
                            {
                            }
                            if (objType.ToString().Length == 3)
                            {
                                File.AppendAllText("Objects.txt", objType + "  ");
                            }
                            else
                            {
                                File.AppendAllText("Objects.txt", objType + " ");
                            }
                        }
                    }
                    File.AppendAllText("Objects.txt", Environment.NewLine);
                    File.AppendAllText("Tiles.txt", Environment.NewLine);
                }
            return(WorldMapExporter.Export(tiles));
        }