OSDN Git Service

W.I.P. Experimental use of Heightmap to replace Region map data
[automap/automap.git] / Automap / Renderers / AlternateRenderer.cs
1 using System;
2 using System.Linq;
3
4 using Hjg.Pngcs;
5
6 using Vintagestory.API.Client;
7 using Vintagestory.API.Common;
8 using Vintagestory.API.MathTools;
9
10 namespace Automap
11 {
12         public class AlternateRenderer : AChunkRenderer
13         {
14                 
15                 /// <summary>
16                 /// V.G.D:'s Alternative renderer
17                 /// </summary>
18                 /// <param name="clientAPI">Client API.</param>
19                 /// <param name="logger">Logger.</param>
20                 public AlternateRenderer(ICoreClientAPI clientAPI, ILogger logger) : base(clientAPI, logger)
21                 {
22                         
23                 }
24
25                 public override void GenerateChunkPngShard(Vec2i chunkPos, IMapChunk mc, ColumnMeta metaData, ref ColumnsMetadata allCols, out uint pixelCount)
26                 {
27                         pixelCount = 0;
28                         BlockPos tmpPos = new BlockPos();
29                         Vec2i localpos = new Vec2i();
30                         var chunksColumn = new IWorldChunk[ClientAPI.World.BlockAccessor.MapSizeY / chunkSize];
31
32                         int topChunkY = mc.YMax / chunkSize;//Heywaitaminute -- this isn't a highest FEATURE, if Rainmap isn't accurate!
33                                                                                                 //Metadata of DateTime chunk was edited, chunk coords.,world-seed? Y-Max feature height
34                                                                                                 //Grab a chunk COLUMN... Topmost Y down...
35                         for (int chunkY = 0; chunkY <= topChunkY; chunkY++)
36                         {
37                                 chunksColumn[chunkY] = ClientAPI.World.BlockAccessor.GetChunk(chunkPos.X, chunkY, chunkPos.Y);
38                                 //What to do if chunk is a void? invalid?
39                         }
40
41                         //pre-create PNG line slices...
42                         ImageLine[] lines = Enumerable.Repeat(new object(), chunkSize).Select(l => new ImageLine(this.PngWriter.ImgInfo)).ToArray();
43                         ushort[] allMapYs = mc.RainHeightMap;
44                         for (int posIndex = 0; posIndex < (chunkSize * chunkSize); posIndex++)
45                         {
46                                 int currY = allMapYs[posIndex];
47                                 int localChunkY = currY / chunkSize;
48                                 if (localChunkY >= (chunksColumn.Length)) continue; //Out of range!
49                                 if (chunksColumn[localChunkY] == null) continue;
50
51                                 MapUtil.PosInt2d(posIndex, chunkSize, localpos);
52                                 int localX = localpos.X;
53                                 int localZ = localpos.Y;
54
55                                 chunksColumn[localChunkY].Unpack();
56                                 int blockId = chunksColumn[localChunkY].Blocks[MapUtil.Index3d(localX, currY % chunkSize, localZ, chunkSize, chunkSize)];
57
58                                 Block block = ClientAPI.World.Blocks[blockId];
59
60                                 tmpPos.Set(chunkSize * chunkPos.X + localX, currY, chunkSize * chunkPos.Y + localZ);
61
62                                 int red;
63                                 int green;
64                                 int blue;
65
66                                 //============ POI Population =================
67                                 if (BlockID_Designators.ContainsKey(blockId))
68                                 {
69                                         var desig = BlockID_Designators[blockId];
70                                         red = desig.OverwriteColor.R;
71                                         green = desig.OverwriteColor.G;
72                                         blue = desig.OverwriteColor.B;
73
74
75                                         ImageLineHelper.SetPixel(lines[localZ], localX, red, green, blue);
76                                         continue;
77                                 }
78
79                                 float b = GetSlope(localX, localZ, allMapYs);
80
81                                 int col = block.GetColor(ClientAPI, tmpPos);
82                                 int packedFormat = ColorUtil.ColorMultiply3Clamped(col, b);
83
84                                 red = ColorUtil.ColorB(packedFormat);
85                                 green = ColorUtil.ColorG(packedFormat);
86                                 blue = ColorUtil.ColorR(packedFormat);
87                                 ImageLineHelper.SetPixel(lines[localZ], localX, red, green, blue);
88
89                                 //chunkImage.SetPixel(localX, localZ, pixelColor);
90                                 pixelCount++;
91                         }
92
93                         for (int row = 0; row < this.PngWriter.ImgInfo.Rows; row++)
94                         {
95                                 this.PngWriter.WriteRow(lines[row], row);
96                         }
97
98                         this.PngWriter.End();
99                 }
100
101                 private float GetSlope(int x, int y, ushort[] heightMap)
102                 {
103                         int baseY = heightMap[MapUtil.Index2d(x, y, chunkSize)];
104                         float runningY = 0;
105                         // check bounds. i hate this.
106                         int locIndex;
107                         if (x > 0)
108                         {
109                                 locIndex = MapUtil.Index2d(x - 1, y, chunkSize);
110                                 runningY += (baseY - heightMap[locIndex]);
111                         }
112                         if (x < chunkSize - 1)
113                         {
114                                 locIndex = MapUtil.Index2d(x + 1, y, chunkSize);
115                                 runningY += (baseY - heightMap[locIndex]);
116                         }
117                         if (y > 0)
118                         {
119                                 locIndex = MapUtil.Index2d(x, y - 1, chunkSize);
120                                 runningY += (baseY - heightMap[locIndex]);
121                         }
122                         if (y < chunkSize - 1)
123                         {
124                                 locIndex = MapUtil.Index2d(x, y + 1, chunkSize);
125                                 runningY += (baseY - heightMap[locIndex]);
126                         }
127                         runningY /= 4; // average now
128                         runningY /= 5; // idk
129                         return 1 + runningY;
130                 }
131         }
132 }
133