OSDN Git Service

28b40cb79efea05496adc28e15a798c7e590093e
[automap/automap.git] / Automap / Subsystems / AutomapSystem.cs
1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 using System.Text.RegularExpressions;
8 using System.Threading;
9
10 using Hjg.Pngcs;
11 using Hjg.Pngcs.Chunks;
12
13 using Newtonsoft.Json;
14
15 using Vintagestory.API.Client;
16 using Vintagestory.API.Common;
17 using Vintagestory.API.Config;
18 using Vintagestory.API.Datastructures;
19 using Vintagestory.API.MathTools;
20 using Vintagestory.Common;
21
22 namespace Automap
23 {
24         public class AutomapSystem
25         {
26                 private Thread cartographer_thread;
27                 private ICoreClientAPI ClientAPI { get; set; }
28                 private ILogger Logger { get; set; }
29                 private IChunkRenderer ChunkRenderer { get; set; }
30
31                 private const string _mapPath = @"Maps";
32                 private const string _chunkPath = @"Chunks";
33                 private const string _domain = @"automap";
34                 private const string chunkFile_filter = @"*_*.png";
35                 private static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+).png", RegexOptions.Singleline);
36
37                 private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150);
38                 private ColumnsMetadata chunkTopMetadata;
39                 private PointsOfInterest POIs = new PointsOfInterest();
40                 private EntitiesOfInterest EOIs = new EntitiesOfInterest();
41
42                 internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set; }
43                 internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
44                 internal Dictionary<int, string> RockIdCodes { get; private set; }
45
46                 internal CommandType CurrentState { get; set; }
47                 //Run status, Chunks processed, stats, center of map....
48                 private uint nullChunkCount, updatedChunksTotal;
49                 private Vec2i startChunkColumn;
50
51                 private readonly int chunkSize;
52                 private string path;
53                 private IAsset staticMap;
54                 private PersistedConfiguration configuration;
55
56
57                 public static string AutomapStatusEventKey = @"AutomapStatus";
58                 public static string AutomapCommandEventKey = @"AutomapCommand";
59                 PersistedConfiguration cachedConfiguration;
60
61                 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
62                 {
63                         this.ClientAPI = clientAPI;
64                         this.Logger = logger;
65                         chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
66                         ClientAPI.Event.LevelFinalize += EngageAutomap;
67                         configuration = config;
68
69
70
71                         //TODO:Choose which one from GUI 
72                         this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
73
74                         //Listen on bus for commands
75                         ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
76
77                         if (configuration.Autostart) 
78                         {
79                                 CurrentState = CommandType.Run;
80                                 Logger.Debug("Autostart is Enabled.");
81                         }
82
83                 }
84
85
86                 #region Internals
87                 private void EngageAutomap()
88                 {
89                         path = ClientAPI.GetOrCreateDataPath(_mapPath);
90                         path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName'
91
92
93                         string mapFilename = Path.Combine(path, "automap.html");
94                         StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
95
96                         staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html"));
97                         outputText.Write(staticMap.ToText());
98                         outputText.Flush();
99
100                         Prefill_POI_Designators();
101                         startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Z / chunkSize));
102                         chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
103
104                         Logger.Notification("AUTOMAP Start {0}", startChunkColumn);
105                         Reload_Metadata();
106
107                         ClientAPI.Event.ChunkDirty += ChunkAChanging;
108
109                         cartographer_thread = new Thread(Cartographer)
110                         {
111                                 Name = "Cartographer",
112                                 Priority = ThreadPriority.Lowest,
113                                 IsBackground = true
114                         };
115
116                         ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
117                 }
118
119                 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
120                 {
121                         Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
122
123                         columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
124                 }
125
126                 private void AwakenCartographer(float delayed)
127                 {
128
129                         if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
130                         {
131 #if DEBUG
132                                 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
133 #endif
134
135                                 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted))
136                                 {
137                                         cartographer_thread.Start();
138                                 }
139                                 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
140                                 {
141                                         //Time to (re)write chunk shards
142                                         cartographer_thread.Interrupt();
143                                 }
144                                 //#if DEBUG
145                                 //ClientAPI.TriggerChatMessage($"Automap {updatedChunksTotal} Updates - MAX (N:{chunkTopMetadata.North_mostChunk},S:{chunkTopMetadata.South_mostChunk},E:{chunkTopMetadata.East_mostChunk}, W:{chunkTopMetadata.West_mostChunk} - TOTAL: {chunkTopMetadata.Count})");
146                                 //#endif
147                         }
148                         else if (CurrentState == CommandType.Snapshot)
149                         {
150                                 //TODO: Snapshot generator second thread...
151                         }
152
153                 }
154
155
156                 private void Cartographer()
157                 {
158                         wake:
159                         Logger.VerboseDebug("Cartographer thread awoken");
160
161                         try
162                         {
163                                 uint ejectedItem = 0;
164                                 uint updatedChunks = 0;
165                                 uint updatedPixels = 0;
166
167                                 //-- Should dodge enumerator changing underfoot....at a cost.
168                                 if (!columnCounter.IsEmpty)
169                                 {
170                                         var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
171                                         UpdateEntityMetadata( );
172
173                                         foreach (var mostActiveCol in tempSet)
174                                         {
175                                                 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
176
177                                                 if (mapChunk == null)
178                                                 {
179                                                         Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
180                                                         nullChunkCount++;
181                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
182                                                         continue;
183                                                 }
184
185                                                 ColumnMeta chunkMeta;
186                                                 if (chunkTopMetadata.Contains(mostActiveCol.Key)) {
187                                                 chunkMeta = chunkTopMetadata[mostActiveCol.Key];
188                                                 }
189                                                 else {
190                                                 chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
191                                                 }
192
193                                                 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
194
195                                                 PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, chunkMeta);
196                                                 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter, out updatedPixels);
197
198                                                 if (updatedPixels > 0)
199                                                 {
200
201                                                         #if DEBUG
202                                                         Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
203                                                         #endif
204                                                         updatedChunks++;
205                                                         chunkTopMetadata.Update(chunkMeta);
206                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
207                                                 }
208                                                 else
209                                                 {
210                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
211                                                         Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key);
212                                                 }
213
214                                         }
215                                 }
216
217                                 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
218
219                                 if (updatedChunks > 0)
220                                 {
221                                         //What about chunk updates themselves; a update bitmap isn't kept...
222                                         updatedChunksTotal += updatedChunks;
223                                         GenerateJSONMetadata();
224                                         updatedChunks = 0;
225                                 }
226
227                                 //Then sleep until interupted again, and repeat
228
229                                 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
230
231                                 Thread.Sleep(Timeout.Infinite);
232
233                         }
234                         catch (ThreadInterruptedException)
235                         {
236
237                                 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
238                                 goto wake;
239
240                         }
241                         catch (ThreadAbortException)
242                         {
243                                 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
244
245                         }
246                         finally
247                         {
248                                 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
249                         }
250                 }
251
252                 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
253                 {
254                         StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
255
256                         this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
257                 }
258
259                 private void Prefill_POI_Designators()
260                 {
261
262                         this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
263                         this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
264                         this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
265
266                         //Add special marker types for BlockID's of "Interest", overwrite colour, and method
267
268                         Reload_POI_Designators();
269                 }
270
271                 private void Reload_POI_Designators()
272                 {                       
273                         Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
274                         foreach (var designator in configuration.BlockDesignators)
275                         {
276                                 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
277                                 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
278                                 foreach (var entry in blockIDs)
279                                 {
280                                         BlockID_Designators.Add(entry.Key, designator);
281                                 }
282                         }
283                         this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
284
285
286                         Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
287                         foreach (var designator in configuration.EntityDesignators)
288                         {
289                                 //Get Variants first, from EntityTypes...better be populated!
290                                 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
291
292                                 foreach (var match in matched)
293                                 {
294                                         Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
295                                         this.Entity_Designators.Add(match.Code, designator);
296                                 }
297
298
299
300                                 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
301                         }
302
303
304                 }
305
306                 //TODO: Rewrite as Newtonsoft  JsonTextWriter !!!
307                 /// <summary>
308                 /// Generates the JSON Metadata. (in Map object format )
309                 /// </summary>
310                 private void GenerateJSONMetadata()
311                 {
312                         string jsonFilename = Path.Combine(path, "Metadata.js");
313
314                         StreamWriter stream = new StreamWriter(jsonFilename, false, Encoding.UTF8);
315
316                         using (stream) {
317                         JsonTextWriter jsonWriter = new JsonTextWriter(stream);
318
319                         jsonWriter.Formatting = Formatting.None;
320                         jsonWriter.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
321                         jsonWriter.Indentation = 0;
322                         //jsonWriter.AutoCompleteOnClose = true;
323                         jsonWriter.QuoteChar = '\'';
324                         jsonWriter.DateFormatHandling = DateFormatHandling.IsoDateFormat;
325                         jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
326
327                         using (jsonWriter)
328                         {
329                                 jsonWriter.WriteRaw("ViewFrame.chunks={};\n");
330                                 jsonWriter.WriteRaw("ViewFrame.chunks.worldSeedNum=" );
331                                 jsonWriter.WriteValue(ClientAPI.World.Seed);
332                                 jsonWriter.WriteRaw(";\n");
333
334                                 jsonWriter.WriteRaw("ViewFrame.chunks.genTime=");
335                                 jsonWriter.WriteValue(DateTimeOffset.UtcNow);
336                                 jsonWriter.WriteRaw(";\n");
337
338                                 jsonWriter.WriteRaw("ViewFrame.chunks.startCoords=");
339                                 jsonWriter.WriteStartArray( );
340                                 jsonWriter.WriteValue(startChunkColumn.X);
341                                 jsonWriter.WriteValue(startChunkColumn.Y);
342                                 jsonWriter.WriteEndArray( );
343                                 jsonWriter.WriteRaw(";\n");
344
345                                 jsonWriter.WriteRaw("ViewFrame.chunks.chunkSize=");
346                                 jsonWriter.WriteValue(chunkSize);
347                                 jsonWriter.WriteRaw(";\n");
348
349                                 jsonWriter.WriteRaw("ViewFrame.chunks.northMostChunk=");
350                                 jsonWriter.WriteValue(chunkTopMetadata.North_mostChunk);
351                                 jsonWriter.WriteRaw(";\n");
352
353                                 jsonWriter.WriteRaw("ViewFrame.chunks.southMostChunk=");
354                                 jsonWriter.WriteValue(chunkTopMetadata.South_mostChunk);
355                                 jsonWriter.WriteRaw(";\n");
356
357                                 jsonWriter.WriteRaw("ViewFrame.chunks.westMostChunk=");
358                                 jsonWriter.WriteValue(chunkTopMetadata.West_mostChunk);
359                                 jsonWriter.WriteRaw(";\n");
360
361                                 jsonWriter.WriteRaw("ViewFrame.chunks.eastMostChunk=");
362                                 jsonWriter.WriteValue(chunkTopMetadata.East_mostChunk);
363                                 jsonWriter.WriteRaw(";\n");
364
365
366                                 //MAP object format - [key, value]: key is "x_y"
367                                 jsonWriter.WriteRaw("ViewFrame.chunks.chunkMetadata=");
368                                 jsonWriter.WriteStartConstructor("Map");
369                                 jsonWriter.WriteStartArray( );//An array of... 2-component arrays
370
371
372                                 foreach (var shard in chunkTopMetadata)
373                                 {
374                                         jsonWriter.WriteStartArray( );//Start tuple
375                                         jsonWriter.WriteValue($"{shard.Location.X}_{shard.Location.Y}");//Key of Tuple
376
377                                         jsonWriter.WriteStartObject( );
378                                         jsonWriter.WritePropertyName("prettyCoord");
379                                         jsonWriter.WriteValue( shard.Location.PrettyCoords(ClientAPI));
380
381                                         jsonWriter.WritePropertyName("chunkAge");
382                                         jsonWriter.WriteValue(shard.ChunkAge);
383
384                                         jsonWriter.WritePropertyName("temp");
385                                         jsonWriter.WriteValue(shard.Temperature);
386
387                                         jsonWriter.WritePropertyName("YMax");
388                                         jsonWriter.WriteValue(shard.YMax);
389
390                                         jsonWriter.WritePropertyName("fert");
391                                         jsonWriter.WriteValue(shard.Fertility);
392                                         
393                                         jsonWriter.WritePropertyName("forestDens");
394                         jsonWriter.WriteValue( shard.ForestDensity);
395
396                                         jsonWriter.WritePropertyName("rain"); 
397                         jsonWriter.WriteValue( shard.Rainfall);
398
399                                         jsonWriter.WritePropertyName("shrubDens");
400                         jsonWriter.WriteValue(  shard.ShrubDensity);
401
402                                         jsonWriter.WritePropertyName("airBlocks");
403                         jsonWriter.WriteValue( shard.AirBlocks);
404
405                                         jsonWriter.WritePropertyName("nonAirBlocks");
406                         jsonWriter.WriteValue(  shard.NonAirBlocks);
407
408                                         //TODO: Heightmap ?
409                                         //Start rockMap ; FOR a Ratio....on tooltip GUI
410                                         jsonWriter.WritePropertyName("rockRatio");
411                                         jsonWriter.WriteStartConstructor("Map");
412                                         jsonWriter.WriteStartArray( );
413                                         foreach (var rockEntry in shard.RockRatio) {
414                                                 var rockBlock = ClientAPI.World.GetBlock(rockEntry.Key);
415                                                 jsonWriter.WriteStartArray( );
416                                                 jsonWriter.WriteValue(rockBlock.Code.Path);
417                                                 jsonWriter.WriteValue(rockEntry.Value);//Total per chunk-column
418                                                 jsonWriter.WriteEndArray( );
419                                         }
420                                         jsonWriter.WriteEndArray( );
421                                         jsonWriter.WriteEndConstructor( );//end rock-map
422
423                                         jsonWriter.WriteEndObject( );//end Map value: {Object}
424                                         jsonWriter.WriteEndArray( );//end Tuple
425                                 }
426
427                                 jsonWriter.WriteEndArray( );//Enclose tuples of chunkMetadata
428                                 jsonWriter.WriteEndConstructor( );//Close constructor of Map (chunkMetadata)
429                                 jsonWriter.WriteRaw(";\n");
430
431                                 jsonWriter.WriteRaw("ViewFrame.chunks.pointsOfInterest=");
432                                 jsonWriter.WriteStartConstructor("Map");
433                                 jsonWriter.WriteStartArray( );//An array of... 2-component arrays
434
435                                 foreach (var poi in POIs)
436                                 {
437                                         jsonWriter.WriteStartArray( );
438                                         jsonWriter.WriteValue($"{poi.Location.X}_{poi.Location.Z}");
439
440                                         jsonWriter.WriteStartObject();
441                                         jsonWriter.WritePropertyName("prettyCoord");
442                                         jsonWriter.WriteValue(poi.Location.PrettyCoords(ClientAPI) );
443
444                                         jsonWriter.WritePropertyName("notes");
445                                         jsonWriter.WriteValue(poi.Notes);//Encoded to HTML Entities
446
447                                         jsonWriter.WritePropertyName("time");
448                                         jsonWriter.WriteValue(poi.Timestamp);
449                                         
450                                         jsonWriter.WritePropertyName("chunkPos");
451                                         jsonWriter.WriteValue($"{(poi.Location.X / chunkSize)}_{(poi.Location.Z / chunkSize)}");
452                                         
453                                         jsonWriter.WriteEndObject( );
454                                         jsonWriter.WriteEndArray( );
455                                 }
456
457                                 foreach (var poi in EOIs)
458                                 {
459                                         jsonWriter.WriteStartArray( );
460                                         jsonWriter.WriteValue($"{poi.Location.X}_{poi.Location.Z}");
461
462                                         jsonWriter.WriteStartObject( );
463                                         jsonWriter.WritePropertyName("prettyCoord");
464                                         jsonWriter.WriteValue(poi.Location.PrettyCoords(ClientAPI));
465
466                                         jsonWriter.WritePropertyName("notes");
467                                         jsonWriter.WriteValue(poi.Notes);//Encoded to HTML Entities
468
469                                         jsonWriter.WritePropertyName("time");
470                                         jsonWriter.WriteValue(poi.Timestamp);
471
472                                         jsonWriter.WritePropertyName("chunkPos");
473                                         jsonWriter.WriteValue($"{(poi.Location.X / chunkSize)}_{(poi.Location.Z / chunkSize)}");
474
475                                         jsonWriter.WriteEndObject( );
476                                         jsonWriter.WriteEndArray( );
477                                 }
478
479                                 jsonWriter.WriteEndArray( );
480                                 jsonWriter.WriteEndConstructor( );
481                                 jsonWriter.WriteRaw(";\n");
482
483                                 jsonWriter.WriteWhitespace("\n");
484                                 jsonWriter.WriteComment("============= BlockID's for Rockmap / Rock-ratios ===============");
485                                 jsonWriter.WriteWhitespace("\n");
486
487                                 jsonWriter.WriteRaw("ViewFrame.chunks.rock_Lookup =");
488                                 jsonWriter.WriteStartConstructor("Map");
489                                 jsonWriter.WriteStartArray( );//An array of... 2-component arrays
490
491                                 foreach (var entry in RockIdCodes) {
492                                 var block = ClientAPI.World.GetBlock(entry.Key);
493                                 
494                                 jsonWriter.WriteStartArray( );
495                                 jsonWriter.WriteValue(block.Code.Path);
496                                                                         
497                                 jsonWriter.WriteStartObject( );
498                                 jsonWriter.WritePropertyName("assetCode");
499                                 jsonWriter.WriteValue(entry.Value);
500
501                                 jsonWriter.WritePropertyName("name");
502                                 jsonWriter.WriteValue(Lang.GetUnformatted(block.Code.Path));
503                                 //Color?
504
505                                 jsonWriter.WriteEndObject( );
506                                 jsonWriter.WriteEndArray( );
507                                 }
508                                 jsonWriter.WriteEndArray( );
509                                 jsonWriter.WriteEndConstructor();
510                                 
511                                 jsonWriter.WriteRaw(";\n");
512
513                                 jsonWriter.Flush();
514                         }
515                         }
516
517                 }
518
519
520                 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
521                 {
522                         ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ( byte )chunkSize);
523                         BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
524                                                                                         mapChunk.YMax,
525                                                                                         mostActiveCol.Key.Y * chunkSize);
526
527                         var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
528                         data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
529
530                         return data;
531                 }
532
533                 /// <summary>
534                 /// Reload chunk bounds from chunk shards
535                 /// </summary>
536                 /// <returns>The metadata.</returns>
537                 private void Reload_Metadata()
538                 {
539                         var worldmapDir = new DirectoryInfo(path);
540
541                         if (worldmapDir.Exists)
542                         {
543
544                                 var files = worldmapDir.GetFiles(chunkFile_filter);
545
546                                 if (files.Length > 0)
547                                 {
548                                         #if DEBUG
549                                         Logger.VerboseDebug("{0} Existing world chunk shards", files.Length);
550                                         #endif
551                                          
552                                         foreach (var shardFile in files) {
553
554                                         if (shardFile.Length < 1024) continue;
555                                         var result = chunkShardRegex.Match(shardFile.Name);
556                                         if (result.Success) {
557                                         int X_chunk_pos = int.Parse(result.Groups["X"].Value);
558                                         int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
559
560                                         try 
561                                                 {
562                                                 using (var fileStream = shardFile.OpenRead( )) {
563
564                                                 PngReader pngRead = new PngReader(fileStream);
565                                                 pngRead.ReadSkippingAllRows( );
566                                                 pngRead.End( );
567                                                 //Parse PNG chunks for METADATA in shard
568                                                 PngMetadataChunk metadataFromPng = pngRead.GetChunksList( ).GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
569
570                                                 chunkTopMetadata.Add(metadataFromPng.ChunkMetadata);
571                                                 }
572
573                                                 }
574                                                 catch (PngjException someEx) 
575                                                 {
576                                                         Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
577                                                         continue;
578                                                 }
579                                         }
580
581                                         }
582                                 }
583
584
585                         }
586                         else
587                         {
588 #if DEBUG
589                                 Logger.VerboseDebug("Could not open world map directory");
590 #endif
591                         }
592
593
594
595                 }
596
597                 private PngWriter SetupPngImage(Vec2i coord, ColumnMeta metadata)
598                 {
599                         ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
600
601                         string filename = $"{coord.X}_{coord.Y}.png";
602                         filename = Path.Combine(path, filename);
603
604                         PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true);
605                         PngMetadata meta = pngWriter.GetMetadata();
606                         meta.SetTimeNow();
607                         meta.SetText("Chunk_X", coord.X.ToString("D"));
608                         meta.SetText("Chunk_Y", coord.Y.ToString("D"));
609                         //Setup specialized meta-data PNG chunks here...
610                         PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo)
611                         {
612                                 ChunkMetadata = metadata
613                         };
614                         pngWriter.GetChunksList().Queue(pngChunkMeta);
615                         pngWriter.CompLevel = 9;// 9 is the maximum compression
616                         pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman;
617
618                         return pngWriter;
619                 }
620
621                 /// <summary>
622                 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
623                 /// </summary>
624                 /// <param name="key">Chunk Coordinate</param>
625                 /// <param name="mapChunk">Map chunk.</param>
626                 /// <param name="chunkMeta">Chunk metadata</param>
627                 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
628                 {
629
630                         int targetChunkY = mapChunk.YMax / chunkSize;//Surface ... 
631                         for (; targetChunkY > 0; targetChunkY--)
632                         {
633                         WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
634
635                         if (chunkData == null || chunkData.BlockEntities == null) {
636                         #if DEBUG
637                         Logger.VerboseDebug("Chunk null or empty X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
638                         #endif
639                         continue;
640                         }
641
642                                 /*************** Chunk Entities Scanning *********************/
643                                 if (chunkData.BlockEntities != null && chunkData.BlockEntities.Length > 0)
644                                 {
645                                 #if DEBUG
646                                         Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Length);
647                                 #endif
648
649                                         foreach (var blockEnt in chunkData.BlockEntities)
650                                         {
651
652                                                 if (blockEnt != null && blockEnt.Block != null && BlockID_Designators.ContainsKey(blockEnt.Block.BlockId))
653                                                 {
654                                                         var designator = BlockID_Designators[blockEnt.Block.BlockId];
655                                                         designator.SpecialAction(ClientAPI, POIs, blockEnt.Pos.Copy(), blockEnt.Block);
656                                                 }
657                                         }
658
659                                 }
660                                 /********************* Chunk/Column BLOCKs scanning ****************/
661                                 //Heightmap, Stats, block tally
662                                 chunkData.Unpack();
663
664                                 int X_index, Y_index, Z_index;
665                                 X_index = Y_index = Z_index = 0;
666
667                                 do
668                                 {
669                                         do
670                                         {
671                                                 do
672                                                 {
673                                                         /* Encode packed indicie
674                                                         (y * chunksize + z) * chunksize + x
675                                                         */
676                                                         var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index);
677                                                         int aBlockId = chunkData.Blocks[indicie];
678
679                                                         if (aBlockId == 0)
680                                                         {//Air
681                                                                 chunkMeta.AirBlocks++;
682                                                                 continue;
683                                                         }
684
685                                                         if (RockIdCodes.ContainsKey(aBlockId))
686                                                         {
687                                                                 if (chunkMeta.RockRatio.ContainsKey(aBlockId)) { chunkMeta.RockRatio[aBlockId]++; } else { chunkMeta.RockRatio.Add(aBlockId, 1); }
688                                                         }
689
690                                                         chunkMeta.NonAirBlocks++;
691
692                                                         //Heightmap 
693                                                         if (chunkMeta.HeightMap[X_index, Z_index] == 0)
694                                                         { chunkMeta.HeightMap[X_index, Z_index] = (ushort) (Y_index + (targetChunkY * chunkSize)); }
695
696                                                 }
697                                                 while (X_index++ < (chunkSize - 1));
698                                                 X_index = 0;
699                                         }
700                                         while (Z_index++ < (chunkSize - 1));
701                                         Z_index = 0;
702                                 }
703                                 while (Y_index++ < (chunkSize - 1));
704
705                         }
706                 }
707
708                 private void UpdateEntityMetadata()
709                 {
710                         Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
711                         //Mabey scan only for 'new' entities by tracking ID in set?
712                         foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
713                         {
714
715                                 #if DEBUG
716                                 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos}    <<<<<<<<<<<<");
717                                 #endif
718
719                                 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
720                                 if (dMatch.Value != null)
721                                 {
722                                         dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.LocalPos.AsBlockPos.Copy(), loadedEntity.Value);
723                                 }
724
725                         }
726
727
728                 }
729
730                 private void AddNote(string notation)
731                 {
732                         var playerNodePoi = new PointOfInterest()
733                         {
734                                 Location = ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Copy(),
735                                 Notes = notation,
736                                 Timestamp = DateTimeOffset.UtcNow,
737                         };
738
739                         this.POIs.AddReplace(playerNodePoi);
740                 }
741
742
743
744                 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
745                 {
746                         Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
747
748                         CommandData cmdData = data as CommandData;
749
750
751                         if (CurrentState != CommandType.Snapshot)
752                         {
753                                 switch (cmdData.State)
754                                 {
755                                         case CommandType.Run:
756                                                 CurrentState = cmdData.State;
757                                                 AwakenCartographer(0.0f);
758                                                 break;
759
760                                         case CommandType.Stop:
761                                                 CurrentState = cmdData.State;
762                                                 break;
763
764                                         case CommandType.Snapshot:
765                                                 CurrentState = CommandType.Stop;
766                                                 //Snapshot starts a second thread/process...
767
768                                                 break;
769
770                                         case CommandType.Notation:
771                                                 //Add to POI list where player location
772                                                 AddNote(cmdData.Notation);
773                                                 break;
774                                 }
775
776                         }
777
778                         if (CurrentState != cmdData.State)
779                         {
780                                 CurrentState = cmdData.State;
781                                 AwakenCartographer(0.0f);
782                         }
783
784 #if DEBUG
785                         ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");
786 #endif
787
788                 }
789
790
791                 #endregion
792
793         }
794
795 }