OSDN Git Service

Events should attched after config data is loaded...
[automap/automap.git] / Automap / Renderers / FlatRenderer.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4
5 using Hjg.Pngcs;
6
7 using Vintagestory.API.Client;
8 using Vintagestory.API.Common;
9 using Vintagestory.API.MathTools;
10 using Vintagestory.Common;
11
12 namespace Automap
13 {
14         public class FlatRenderer : AChunkRenderer
15         {
16                 public const string Name = @"Flat";
17
18                 /// <summary>
19                 /// Renders shards similar to the OLD VS (1.8) version, plus P.O.I. markings.
20                 /// </summary>
21                 /// <param name="clientAPI">Client API.</param>
22                 /// <param name="logger">Logger.</param>
23                 public FlatRenderer(ICoreClientAPI clientAPI, ILogger logger, bool seasonalColor) : base(clientAPI, logger, seasonalColor)
24                 {
25
26                 }
27
28                 public override void GenerateChunkPngShard(Vec2i chunkPos, IMapChunk mc, ColumnMeta targetColMeta, ref ColumnsMetadata allCols, out uint pixelCount)
29                 {
30                 pixelCount = 0;
31                 BlockPos tmpPos = new BlockPos( );
32                 Vec2i localpos = new Vec2i( );
33
34                 var chunksColumn = new IWorldChunk[ClientAPI.World.BlockAccessor.MapSizeY / chunkSize];
35
36                 //pre-create PNG line slices...
37                 ImageLine[ ] lines = Enumerable.Repeat(new object( ), chunkSize).Select(l => new ImageLine(this.PngWriter.ImgInfo)).ToArray( );
38
39                 int topChunkY = targetColMeta.YMax / chunkSize;
40
41                 for (int chunkY = 0; chunkY <= topChunkY; chunkY++) {
42                 chunksColumn[chunkY] = ClientAPI.World.BlockAccessor.GetChunk(chunkPos.X, chunkY, chunkPos.Y);
43                 WorldChunk ownChunk = chunksColumn[chunkY] as WorldChunk;
44                 if (ownChunk != null) {
45                 if (ownChunk.IsPacked( )) ownChunk.Unpack( );//Gah - probably done already by chunk processor
46                 }
47                 else {
48                 Logger.Warning("CHUNK A.W.O.L. : X{0} Y{1} Z{2} - Missing slice FOR COLUMN", chunkPos.X, chunkY, chunkPos.Y);
49                 //return;
50                 }
51                 }
52
53                 for (int pixelIndex = 0; pixelIndex < (chunkSize * chunkSize); pixelIndex++) {
54                 MapUtil.PosInt2d(pixelIndex, chunkSize, localpos);
55                 int localX = localpos.X;
56                 int localZ = localpos.Y;
57                 ushort localY = targetColMeta.HeightMap[localX, localZ];
58
59                 int localChunkY = localY / chunkSize;
60                 if (localChunkY >= (chunksColumn.Length)) continue;//Out of range!
61                 if (chunksColumn[localChunkY] == null) continue;//BIG Gaps!
62
63                 int blockId = chunksColumn[localChunkY].MaybeBlocks[MapUtil.Index3d(localX, (localY % chunkSize), localZ, chunkSize, chunkSize)];
64
65                 Block block = ClientAPI.World.Blocks[blockId];
66
67                 tmpPos.Set(chunkSize * chunkPos.X + localpos.X, localY, chunkSize * chunkPos.Y + localpos.Y);
68
69                 int red = 0, green = 0, blue = 0;
70
71                 ExtractBlockColor(tmpPos, block, 1.0f, out red, out green, out blue);
72
73                 //============ POI Population =================
74                 if (BlockID_Designators.ContainsKey(blockId)) {
75                 var desig = BlockID_Designators[blockId];
76
77                 if (desig.Enabled) {
78                 red = desig.OverwriteColor.R;
79                 green = desig.OverwriteColor.G;
80                 blue = desig.OverwriteColor.B;
81                 }
82                 }
83
84                 ImageLineHelper.SetPixel(lines[localZ], localX, red, green, blue);
85                 pixelCount++;
86                 }
87
88                 for (int row = 0; row < this.PngWriter.ImgInfo.Rows; row++) {
89                 this.PngWriter.WriteRow(lines[row], row);
90                 }
91
92                 this.PngWriter.End( );
93                 }
94         }
95 }