OSDN Git Service

W.I.P. Inelegant fixes for a thorny problem...apparently working
[automap/automap.git] / Automap / Subsystems / AutomapSystem.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Concurrent;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Linq;
7 using System.Text;
8 using System.Text.RegularExpressions;
9 using System.Threading;
10
11 using Hjg.Pngcs;
12
13 using ProtoBuf;
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 Thread snapshotThread;
28                 private Snapshotter snapshot;
29                 private ICoreClientAPI ClientAPI { get; set; }
30                 private ILogger Logger { get; set; }
31                 private AChunkRenderer ChunkRenderer { get; set; }
32                 private JsonGenerator JsonGenerator { get; set; }
33
34                 internal const string _mapPath = @"Maps";
35                 internal const string _chunkPath = @"Chunks";
36                 private const string _domain = @"automap";
37                 private const string chunkFile_filter = @"*_*.png";
38                 private const string poiFileName = @"poi_binary";
39                 private const string eoiFileName = @"eoi_binary";
40                 private const string pointsTsvFileName = @"points_of_interest.tsv";
41                 private static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+)\.png", RegexOptions.Singleline);
42
43                 private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150);
44                 private ColumnsMetadata chunkTopMetadata;
45                 private PointsOfInterest POIs = new PointsOfInterest();
46                 private EntitiesOfInterest EOIs = new EntitiesOfInterest();
47
48                 internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set; }
49                 internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
50                 internal Dictionary<int, string> RockIdCodes { get; private set; }
51
52                 internal CommandType CurrentState { get; set; }
53                 //Run status, Chunks processed, stats, center of map....
54                 private uint nullChunkCount, nullMapCount, updatedChunksTotal;
55                 private Vec2i startChunkColumn;
56
57                 private readonly int chunkSize;
58                 private string path;
59                 private IAsset staticMap;
60                 private PersistedConfiguration configuration;
61
62
63                 public static string AutomapStatusEventKey = @"AutomapStatus";
64                 public static string AutomapCommandEventKey = @"AutomapCommand";
65
66                 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
67                 {
68                         this.ClientAPI = clientAPI;
69                         this.Logger = logger;
70                         chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
71                         ClientAPI.Event.LevelFinalize += EngageAutomap;
72                         configuration = config;
73
74                         //TODO:Choose which one from GUI 
75                         this.ChunkRenderer = new StandardRenderer(clientAPI, logger, this.configuration.SeasonalColors);
76
77                         //Listen on bus for commands
78                         ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
79
80
81                         if (configuration.Autostart)
82                         {
83                                 CurrentState = CommandType.Run;
84                                 Logger.Debug("Autostart is Enabled.");
85                         }
86
87                 }
88
89
90                 #region Internals
91                 private void EngageAutomap()
92                 {
93                         path = ClientAPI.GetOrCreateDataPath(_mapPath);
94                         path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName'
95                         ClientAPI.GetOrCreateDataPath(Path.Combine(path, _chunkPath));
96                                                   
97                         JsonGenerator = new JsonGenerator(ClientAPI, Logger, path);
98
99                         string mapFilename = Path.Combine(path, "automap.html");
100                         StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
101
102                         staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html"));
103                         outputText.Write(staticMap.ToText());
104                         outputText.Flush();
105
106                         Prefill_POI_Designators();
107                         startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.Pos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.Pos.AsBlockPos.Z / chunkSize));
108                         chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
109                         Logger.Notification("AUTOMAP Start {0}", startChunkColumn);
110                         Reload_Metadata();
111
112                         ClientAPI.Event.ChunkDirty += ChunkAChanging;
113
114                         cartographer_thread = new Thread(Cartographer)
115                         {
116                                 Name = "Cartographer",
117                                 Priority = ThreadPriority.Lowest,
118                                 IsBackground = true
119                         };
120
121                         snapshot = new Snapshotter(path, chunkTopMetadata, chunkSize,ClientAPI.World.Seed );
122                         snapshotThread = new Thread(Snap)
123                         {
124                                 Name = "Snapshot",
125                                 Priority = ThreadPriority.Lowest,
126                                 IsBackground = true
127                         };
128
129                         ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
130                 }
131
132                 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
133                 {
134                         Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
135
136                         columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
137                 }
138
139                 private void AwakenCartographer(float delayed)
140                 {
141
142                         if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
143                         {
144 #if DEBUG
145                                 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
146 #endif
147
148                                 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted))
149                                 {
150                                         cartographer_thread.Start();
151                                 }
152                                 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
153                                 {
154                                         //Time to (re)write chunk shards
155                                         cartographer_thread.Interrupt();
156                                 }
157                                 //#if DEBUG
158                                 //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})");
159                                 //#endif
160                         }
161                         else if (CurrentState == CommandType.Snapshot)
162                         {
163                                 if (snapshotThread.ThreadState.HasFlag(ThreadState.Unstarted))
164                                 {
165                                         snapshotThread.Start();
166                                 } else if (snapshotThread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
167                                 {
168                                         snapshotThread.Interrupt();
169                                 }
170                         }
171
172                 }
173
174
175                 private void Cartographer()
176                 {
177                         wake:
178                         Logger.VerboseDebug("Cartographer thread awoken");
179
180                         try
181                         {
182                                 uint ejectedItem = 0;
183                                 uint updatedChunks = 0;
184                                 uint updatedPixels = 0;
185
186                                 //-- Should dodge enumerator changing underfoot....at a cost.
187                                 if (!columnCounter.IsEmpty)
188                                 {
189                                         var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
190                                         UpdateEntityMetadata();
191
192                                         foreach (var mostActiveCol in tempSet)
193                                         {
194                                                 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
195
196                                                 if (mapChunk == null)
197                                                 {
198                                                         //TODO: REVISIT THIS CHUNK!
199                                                         Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
200                                                         nullMapCount++;
201                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
202                                                         continue;
203                                                 }
204
205                                                 ColumnMeta chunkMeta;
206                                                 if (chunkTopMetadata.Contains(mostActiveCol.Key))
207                                                 {
208                                                         chunkMeta = chunkTopMetadata[mostActiveCol.Key];
209                                                         #if DEBUG
210                                                         Logger.VerboseDebug("Loaded meta-chunk {0}", mostActiveCol.Key);
211                                                         #endif
212                                                 }
213                                                 else
214                                                 {
215                                                         chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
216                                                         #if DEBUG
217                                                         Logger.VerboseDebug("Created meta-chunk {0}", mostActiveCol.Key);
218                                                         #endif
219                                                 }
220                                                 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
221
222                                                 ChunkRenderer.SetupPngImage(mostActiveCol.Key, path, _chunkPath, ref chunkMeta);
223                                                 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, out updatedPixels);
224
225                                                 if (updatedPixels > 0)
226                                                 {
227                                                         #if DEBUG
228                                                         Logger.VerboseDebug("Wrote top-chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
229                                                         #endif
230                                                         updatedChunks++;
231                                                         chunkTopMetadata.Update(chunkMeta);
232                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
233                                                 }
234                                                 else
235                                                 {
236                                                         columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
237                                                         #if DEBUG
238                                                         Logger.VerboseDebug("Un-painted chun shard: ({0}) ", mostActiveCol.Key);
239                                                         #endif
240                                                 }
241                                         }
242                                         //Cleanup persisted Metadata...
243                                         chunkTopMetadata.ClearMetadata();
244                                 }
245
246                                 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
247
248                                 if (updatedChunks > 0)
249                                 {
250                                         //What about chunk updates themselves; a update bitmap isn't kept...
251                                         updatedChunksTotal += updatedChunks;
252                                         JsonGenerator.GenerateJSONMetadata(chunkTopMetadata, startChunkColumn, POIs, EOIs, RockIdCodes);
253                                         updatedChunks = 0;
254                                 }
255
256                                 //Then sleep until interupted again, and repeat
257 #if DEBUG
258                                 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
259 #endif
260                                 Thread.Sleep(Timeout.Infinite);
261
262                         }
263                         catch (ThreadInterruptedException)
264                         {
265
266 #if DEBUG
267                                 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
268 #endif
269                                 goto wake;
270
271                         }
272                         catch (ThreadAbortException)
273                         {
274 #if DEBUG
275                                 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
276 #endif
277                         }
278                         finally
279                         {
280 #if DEBUG
281                                 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
282 #endif
283                                 PersistPointsData();
284                         }
285                 }
286
287                 private void Snap()
288                 {
289                         snapshotTake:
290 #if DEBUG
291                         Logger.VerboseDebug("Snapshot started");
292 #endif
293                         try
294                         {
295                                 snapshot.Take();
296 #if DEBUG
297                                 Logger.VerboseDebug("Snapshot sleeping");
298 #endif
299                                 CurrentState = CommandType.Run;
300                                 Thread.Sleep(Timeout.Infinite);
301                         }
302                         catch (ThreadInterruptedException)
303                         {
304 #if DEBUG
305                                 Logger.VerboseDebug("Snapshot intertupted");
306 #endif
307                                 goto snapshotTake;
308                         }
309                 }
310
311                 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
312                 {
313                         StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
314
315                         this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
316                 }
317
318                 private void Prefill_POI_Designators()
319                 {
320
321                         this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
322                         this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
323                         this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
324
325                         //Add special marker types for BlockID's of "Interest", overwrite colour, and method
326
327                         Reload_POI_Designators();
328                 }
329
330                 private void Reload_POI_Designators()
331                 {
332                         Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
333                         foreach (var designator in configuration.BlockDesignators)
334                         {
335                                 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
336                                 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
337                                 foreach (var entry in blockIDs)
338                                 {
339                                         BlockID_Designators.Add(entry.Key, designator);
340                                 }
341                         }
342                         this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
343
344
345                         Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
346                         foreach (var designator in configuration.EntityDesignators)
347                         {
348                                 //Get Variants first, from EntityTypes...better be populated!
349                                 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
350
351                                 foreach (var match in matched)
352                                 {
353                                         Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
354                                         this.Entity_Designators.Add(match.Code, designator);
355                                 }
356
357                                 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
358                         }
359
360
361                 }
362
363
364
365                 /// <summary>
366                 /// Store Points/Entity of Interest
367                 /// </summary>
368                 private void PersistPointsData()
369                 {
370                         //POI and EOI raw dump files ~ WRITE em!
371                         //var poiRawFile = File.
372                         string poiPath = Path.Combine(path, poiFileName);
373                         string eoiPath = Path.Combine(path, eoiFileName);
374
375                         if (this.POIs.Count > 0)
376                         {
377                                 using (var poiFile = File.OpenWrite(poiPath))
378                                 {
379                                         Serializer.Serialize<PointsOfInterest>(poiFile, this.POIs);
380                                 }
381                         }
382
383                         if (this.EOIs.Count > 0)
384                         {
385                                 using (var eoiFile = File.OpenWrite(eoiPath))
386                                 {
387                                         Serializer.Serialize<EntitiesOfInterest>(eoiFile, this.EOIs);
388                                 }
389                         }
390
391                         //Create Easy to Parse TSV file for tool/human use....
392                         string pointsTsvPath = Path.Combine(path, pointsTsvFileName);
393
394                         using (var tsvWriter = new StreamWriter(pointsTsvPath, false, Encoding.UTF8))
395                         {
396                                 tsvWriter.WriteLine("Name\tDescription\tLocation\tTime\tDestination");
397                                 foreach (var point in this.POIs)
398                                 {
399                                         tsvWriter.Write(point.Name + "\t");
400                                         var notes = point.Notes
401                                                 .Replace("\n", "\\n")
402                                                 .Replace("\t", "\\t")
403                                                 .Replace("\\", "\\\\");
404                                         tsvWriter.Write(notes + "\t");
405                                         tsvWriter.Write(point.Location.PrettyCoords(ClientAPI) + "\t");
406                                         tsvWriter.Write(point.Timestamp.ToString("u") + "\t");
407                                         tsvWriter.Write((point.Destination != null ? point.Destination.PrettyCoords(ClientAPI) : "---") +"\t");
408                                         tsvWriter.WriteLine();
409                                 }
410                                 foreach (var entity in this.EOIs)
411                                 {
412                                         tsvWriter.Write(entity.Name + "\t");
413                                         var notes = entity.Notes
414                                                 .Replace("\n", "\\n")
415                                                 .Replace("\t", "\\t")
416                                                 .Replace("\\", "\\\\");
417                                         tsvWriter.Write(notes + "\t");
418                                         tsvWriter.Write(entity.Location.PrettyCoords(ClientAPI) + "\t");
419                                         tsvWriter.Write(entity.Timestamp.ToString("u") + "\t");
420                                         tsvWriter.Write("n/a\t");
421                                         tsvWriter.WriteLine();
422                                 }
423                                 tsvWriter.WriteLine();
424                                 tsvWriter.Flush();
425                         }
426
427                 }
428
429                 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
430                 {
431                         ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize, (ClientAPI.World.BlockAccessor.MapSizeY / chunkSize));
432                         BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
433                                                                                         mapChunk.YMax,
434                                                                                         mostActiveCol.Key.Y * chunkSize);
435
436                         var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
437                         data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
438
439                         return data;
440                 }
441
442                 /// <summary>
443                 /// Reload chunk bounds from chunk shards
444                 /// </summary>
445                 /// <returns>The metadata.</returns>
446                 private void Reload_Metadata()
447                 {
448                         var shardsDir = new DirectoryInfo( Path.Combine(path, _chunkPath) );
449
450                         if (!shardsDir.Exists)
451                         {
452                                 #if DEBUG
453                                 Logger.VerboseDebug("Could not open world map (shards) directory");
454                                 #endif
455                                 return;
456                         }
457                         var shardFiles = shardsDir.GetFiles(chunkFile_filter);
458
459                         if (shardFiles.Length > 0)
460                         {
461                                 #if DEBUG
462                                 Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
463                                 #endif
464
465                                 foreach (var shardFile in shardFiles)
466                                 {
467
468                                         if (shardFile.Length < 1024) continue;
469                                         var result = chunkShardRegex.Match(shardFile.Name);
470                                         if (!result.Success) continue;
471
472                                         int X_chunk_pos = int.Parse(result.Groups["X"].Value);
473                                         int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
474
475                                         try
476                                         {
477                                                 using (var fileStream = shardFile.OpenRead())
478                                                 {
479
480                                                         PngReader pngRead = new PngReader(fileStream);
481                                                         pngRead.ReadSkippingAllRows();
482                                                         pngRead.End();
483                                                         //Parse PNG chunks for METADATA in shard
484                                                         PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
485                                                         var column = metadataFromPng.ChunkMetadata;
486                                                         if (column.PrettyLocation == null)
487                                                                 column = column.Reload(ClientAPI);
488                                                         chunkTopMetadata.Add(column);
489                                                 }
490
491                                         }
492                                         catch (PngjException someEx)
493                                         {
494                                                 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
495                                                 continue;
496                                         }
497                                         catch (ProtoException protoEx) 
498                                         {
499                                                 Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
500                                                 continue;
501                                         }
502                                 }
503                         }
504
505                         //POI and EOI raw dump files ~ reload em!
506                         //var poiRawFile = File.
507                         string poiPath = Path.Combine(path, poiFileName);
508                         string eoiPath = Path.Combine(path, eoiFileName);
509
510                         if (File.Exists(poiPath))
511                         {
512                                 using (var poiFile = File.OpenRead(poiPath))
513                                 {
514                                         this.POIs = Serializer.Deserialize<PointsOfInterest>(poiFile);
515                                         Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count);
516                                 }
517                         }
518
519                         if (File.Exists(eoiPath))
520                         {
521                                 using (var eoiFile = File.OpenRead(eoiPath))
522                                 {
523                                         this.EOIs = Serializer.Deserialize<EntitiesOfInterest>(eoiFile);
524                                         Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count);
525                                 }
526                         }
527
528                 }
529
530
531
532                 /// <summary>
533                 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
534                 /// </summary>
535                 /// <param name="key">Chunk Coordinate</param>
536                 /// <param name="mapChunk">Map chunk.</param>
537                 /// <param name="chunkMeta">Chunk metadata</param>
538                 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
539                 {
540
541                         int targetChunkY = mapChunk.YMax / chunkSize;//Surface ... 
542                         for (; targetChunkY > 0; targetChunkY--)
543                         {
544                                 WorldChunk worldChunk = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
545
546                                 if (worldChunk == null || worldChunk.BlockEntities == null)
547                                 {
548                                         #if DEBUG
549                                         Logger.VerboseDebug("WORLD chunk: null or empty X{0} Y{1} Z{2} !", key.X, targetChunkY, key.Y);
550                                         #endif
551                                         nullChunkCount++;
552                                         continue;
553                                 }
554
555                                 if (worldChunk.IsPacked()) 
556                                 {
557                                 Logger.VerboseDebug("WORLD chunk: Compressed: X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
558                                 worldChunk.Unpack( );//RESEARCH: Thread Unsafe? 
559                                 }
560
561                                 /*************** Chunk Entities Scanning *********************/
562                                 if (worldChunk.BlockEntities != null && worldChunk.BlockEntities.Count > 0)
563                                 {
564                                         #if DEBUG
565                                         Logger.VerboseDebug("Scan pos.({0}) for BlockEntities# {1}", key, worldChunk.BlockEntities.Count);
566                                         #endif
567
568                                         foreach (var blockEnt in worldChunk.BlockEntities)
569                                         {
570                                                 if (blockEnt.Value != null && blockEnt.Value.Block != null && BlockID_Designators.ContainsKey(blockEnt.Value.Block.BlockId))
571                                                 {
572                                                         var designator = BlockID_Designators[blockEnt.Value.Block.BlockId];
573                                                         designator.SpecialAction(ClientAPI, POIs, blockEnt.Value.Pos.Copy(), blockEnt.Value.Block);
574                                                 }
575                                         }
576                                 }
577
578                                 //NOTE: Temporary; remove - replace later
579                                 if (chunkMeta.ColumnPresense == null) { chunkMeta.ColumnPresense = new BitArray((ClientAPI.World.BlockAccessor.MapSizeY / chunkSize), false); }
580                                 chunkMeta.ColumnPresense[targetChunkY] = true;
581
582                                 /********************* Chunk/Column BLOCKs scanning ****************/
583                                 //Heightmap, Stats, block tally
584
585                                 int X_index, Y_index, Z_index;
586
587                                 //Ensure ChunkData Metadata fields arn't null...due to being tossed out
588                                 if (chunkMeta.HeightMap == null) { chunkMeta.HeightMap = new ushort[chunkSize, chunkSize]; }
589                                 if (chunkMeta.RockRatio == null) { chunkMeta.RockRatio = new Dictionary<int, uint>(10); }
590
591                                 //First Chance fail-safe;
592                                 if (worldChunk.Blocks == null || worldChunk.Blocks.Length <= 0) {
593                                 Logger.VerboseDebug("WORLD chunk; Missing block DATA⁈ X{0} Y{1} Z{2} ⁈", key.X, targetChunkY, key.Y);
594                                 continue;
595                                 }               
596
597                                 for (Y_index = 0; Y_index < chunkSize - 1; Y_index++)
598                                 {
599                                         for (Z_index = 0; Z_index < chunkSize - 1; Z_index++)
600                                         {
601                                                 for (X_index = 0; X_index < chunkSize - 1; X_index++)
602                                                 {
603                                                 /* Encode packed indicie
604                                                 (y * chunksize + z) * chunksize + x
605                                                 */
606                                                 var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index);
607
608                                                 //'Last' Chance fail-safe;
609                                                 if (worldChunk.Blocks == null || worldChunk.Blocks.Length <= 0) {
610                                                 Logger.VerboseDebug("Processing Block: Missing block DATA⁈ X{0} Y{1} Z{2} ⁈", X_index, Y_index, Z_index);
611                                                 goto loop_bustout;;
612                                                 }
613                                                 
614                                                 int aBlockId = worldChunk.Blocks[indicie];
615
616                                                 if (aBlockId == 0)
617                                                 {//Air
618                                                         chunkMeta.AirBlocks++;
619                                                         continue;
620                                                 }
621
622                                                 if (RockIdCodes.ContainsKey(aBlockId))
623                                                 {
624                                                         if (chunkMeta.RockRatio.ContainsKey(aBlockId))
625                                                                 chunkMeta.RockRatio[aBlockId]++;
626                                                         else
627                                                                 chunkMeta.RockRatio.Add(aBlockId, 1);
628                                                 }
629
630                                                 chunkMeta.NonAirBlocks++;
631
632                                                 //Heightmap 
633                                                 if (chunkMeta.HeightMap[X_index, Z_index] == 0)
634                                                 {
635                                                 chunkMeta.HeightMap[X_index, Z_index] = (ushort) (Y_index + (targetChunkY * chunkSize));
636                                                 }
637                                                 }
638                                         }
639                                 }
640                                 loop_bustout:;
641                         }
642                 }
643
644                 private void UpdateEntityMetadata()
645                 {
646                         Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
647                         //Mabey scan only for 'new' entities by tracking ID in set?
648                         foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
649                         {
650
651                                 #if DEBUG
652                                 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos}    <<<<<<<<<<<<");
653                                 #endif
654
655                                 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
656                                 if (dMatch.Value != null)
657                                 {
658                                         dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.Pos.AsBlockPos.Copy(), loadedEntity.Value);
659                                 }
660
661                         }
662
663
664                 }
665
666                 private void AddNote(string notation)
667                 {
668                         var playerNodePoi = new PointOfInterest()
669                         {
670                                 Name = "Note",
671                                 Location = ClientAPI.World.Player.Entity.Pos.AsBlockPos.Copy(),
672                                 Notes = notation,
673                                 Timestamp = DateTime.UtcNow,
674                         };
675
676                         this.POIs.AddReplace(playerNodePoi);
677                 }
678
679
680
681                 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
682                 {
683                         //Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
684
685                         CommandData cmdData = data as CommandData;
686
687                         switch (cmdData.State)
688                         {
689                                 case CommandType.Run:
690                                 case CommandType.Stop:
691                                 case CommandType.Snapshot:
692                                         if (CurrentState != cmdData.State)
693                                         {
694                                                 CurrentState = cmdData.State;
695                                                 AwakenCartographer(0.0f);
696                                         }
697                                         break;
698
699                                 case CommandType.Notation:
700                                         //Add to POI list where player location
701                                         AddNote(cmdData.Notation);
702                                         break;
703                         }
704 #if DEBUG
705                         ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");
706 #endif
707                 }
708                 #endregion
709
710         }
711
712 }