OSDN Git Service

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