OSDN Git Service

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