2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
7 using System.Text.RegularExpressions;
8 using System.Threading;
11 using Hjg.Pngcs.Chunks;
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;
24 public class AutomapSystem
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 IChunkRenderer ChunkRenderer { get; set; }
32 private JsonGenerator JsonGenerator { get; set; }
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);
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();
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; }
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;
57 private readonly int chunkSize;
59 private IAsset staticMap;
60 private PersistedConfiguration configuration;
63 public static string AutomapStatusEventKey = @"AutomapStatus";
64 public static string AutomapCommandEventKey = @"AutomapCommand";
66 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
68 this.ClientAPI = clientAPI;
70 chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
71 ClientAPI.Event.LevelFinalize += EngageAutomap;
72 configuration = config;
74 //TODO:Choose which one from GUI
75 this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
77 //Listen on bus for commands
78 ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
81 if (configuration.Autostart)
83 CurrentState = CommandType.Run;
84 Logger.Debug("Autostart is Enabled.");
91 private void EngageAutomap()
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));
97 JsonGenerator = new JsonGenerator(ClientAPI, Logger, path);
99 string mapFilename = Path.Combine(path, "automap.html");
100 StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
102 staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html"));
103 outputText.Write(staticMap.ToText());
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);
112 ClientAPI.Event.ChunkDirty += ChunkAChanging;
114 cartographer_thread = new Thread(Cartographer)
116 Name = "Cartographer",
117 Priority = ThreadPriority.Lowest,
121 snapshot = new Snapshotter(path, chunkTopMetadata, chunkSize,ClientAPI.World.Seed );
122 snapshotThread = new Thread(Snap)
125 Priority = ThreadPriority.Lowest,
129 ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
132 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
134 Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
136 columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
139 private void AwakenCartographer(float delayed)
142 if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
145 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
148 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted))
150 cartographer_thread.Start();
152 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
154 //Time to (re)write chunk shards
155 cartographer_thread.Interrupt();
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})");
161 else if (CurrentState == CommandType.Snapshot)
163 if (snapshotThread.ThreadState.HasFlag(ThreadState.Unstarted))
165 snapshotThread.Start();
166 } else if (snapshotThread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
168 snapshotThread.Interrupt();
175 private void Cartographer()
178 Logger.VerboseDebug("Cartographer thread awoken");
182 uint ejectedItem = 0;
183 uint updatedChunks = 0;
184 uint updatedPixels = 0;
186 //-- Should dodge enumerator changing underfoot....at a cost.
187 if (!columnCounter.IsEmpty)
189 var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
190 UpdateEntityMetadata();
192 foreach (var mostActiveCol in tempSet)
194 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
196 if (mapChunk == null)
198 Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
200 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
204 ColumnMeta chunkMeta;
205 if (chunkTopMetadata.Contains(mostActiveCol.Key))
207 chunkMeta = chunkTopMetadata[mostActiveCol.Key];
209 Logger.VerboseDebug("Loaded chunk {0}", mostActiveCol.Key);
214 chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
216 Logger.VerboseDebug("Created chunk {0}", mostActiveCol.Key);
219 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
221 PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, ref chunkMeta);
222 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter, out updatedPixels);
224 if (updatedPixels > 0)
227 Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
230 chunkTopMetadata.Update(chunkMeta);
231 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
235 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
237 Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key);
241 //Cleanup persisted Metadata...
242 chunkTopMetadata.ClearMetadata();
245 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
247 if (updatedChunks > 0)
249 //What about chunk updates themselves; a update bitmap isn't kept...
250 updatedChunksTotal += updatedChunks;
251 JsonGenerator.GenerateJSONMetadata(chunkTopMetadata, startChunkColumn, POIs, EOIs, RockIdCodes);
255 //Then sleep until interupted again, and repeat
257 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
259 Thread.Sleep(Timeout.Infinite);
262 catch (ThreadInterruptedException)
266 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
271 catch (ThreadAbortException)
274 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
280 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
290 Logger.VerboseDebug("Snapshot started");
296 Logger.VerboseDebug("Snapshot sleeping");
298 CurrentState = CommandType.Run;
299 Thread.Sleep(Timeout.Infinite);
301 catch (ThreadInterruptedException)
304 Logger.VerboseDebug("Snapshot intertupted");
310 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
312 StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
314 this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
317 private void Prefill_POI_Designators()
320 this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
321 this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
322 this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
324 //Add special marker types for BlockID's of "Interest", overwrite colour, and method
326 Reload_POI_Designators();
329 private void Reload_POI_Designators()
331 Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
332 foreach (var designator in configuration.BlockDesignators)
334 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
335 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
336 foreach (var entry in blockIDs)
338 BlockID_Designators.Add(entry.Key, designator);
341 this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
344 Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
345 foreach (var designator in configuration.EntityDesignators)
347 //Get Variants first, from EntityTypes...better be populated!
348 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
350 foreach (var match in matched)
352 Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
353 this.Entity_Designators.Add(match.Code, designator);
356 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
365 /// Store Points/Entity of Interest
367 private void PersistPointsData()
369 //POI and EOI raw dump files ~ WRITE em!
370 //var poiRawFile = File.
371 string poiPath = Path.Combine(path, poiFileName);
372 string eoiPath = Path.Combine(path, eoiFileName);
374 if (this.POIs.Count > 0)
376 using (var poiFile = File.OpenWrite(poiPath))
378 Serializer.Serialize<PointsOfInterest>(poiFile, this.POIs);
382 if (this.EOIs.Count > 0)
384 using (var eoiFile = File.OpenWrite(eoiPath))
386 Serializer.Serialize<EntitiesOfInterest>(eoiFile, this.EOIs);
390 //Create Easy to Parse TSV file for tool/human use....
391 string pointsTsvPath = Path.Combine(path, pointsTsvFileName);
393 using (var tsvWriter = new StreamWriter(pointsTsvPath, false, Encoding.UTF8))
395 tsvWriter.WriteLine("Name\tDescription\tLocation\tTime\tDestination");
396 foreach (var point in this.POIs)
398 tsvWriter.Write(point.Name + "\t");
399 var notes = point.Notes
400 .Replace("\n", "\\n")
401 .Replace("\t", "\\t")
402 .Replace("\\", "\\\\");
403 tsvWriter.Write(notes + "\t");
404 tsvWriter.Write(point.Location.PrettyCoords(ClientAPI) + "\t");
405 tsvWriter.Write(point.Timestamp.ToString("u") + "\t");
406 tsvWriter.Write((point.Destination != null ? point.Destination.PrettyCoords(ClientAPI) : "---") +"\t");
407 tsvWriter.WriteLine();
409 foreach (var entity in this.EOIs)
411 tsvWriter.Write(entity.Name + "\t");
412 var notes = entity.Notes
413 .Replace("\n", "\\n")
414 .Replace("\t", "\\t")
415 .Replace("\\", "\\\\");
416 tsvWriter.Write(notes + "\t");
417 tsvWriter.Write(entity.Location.PrettyCoords(ClientAPI) + "\t");
418 tsvWriter.Write(entity.Timestamp.ToString("u") + "\t");
419 tsvWriter.Write("n/a\t");
420 tsvWriter.WriteLine();
422 tsvWriter.WriteLine();
428 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
430 ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize);
431 BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
433 mostActiveCol.Key.Y * chunkSize);
435 var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
436 data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
442 /// Reload chunk bounds from chunk shards
444 /// <returns>The metadata.</returns>
445 private void Reload_Metadata()
447 var shardsDir = new DirectoryInfo( Path.Combine(path, _chunkPath) );
449 if (!shardsDir.Exists)
452 Logger.VerboseDebug("Could not open world map (shards) directory");
456 var shardFiles = shardsDir.GetFiles(chunkFile_filter);
458 if (shardFiles.Length > 0)
461 Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
464 foreach (var shardFile in shardFiles)
467 if (shardFile.Length < 1024) continue;
468 var result = chunkShardRegex.Match(shardFile.Name);
469 if (!result.Success) continue;
471 int X_chunk_pos = int.Parse(result.Groups["X"].Value);
472 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
476 using (var fileStream = shardFile.OpenRead())
479 PngReader pngRead = new PngReader(fileStream);
480 pngRead.ReadSkippingAllRows();
482 //Parse PNG chunks for METADATA in shard
483 PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
484 var column = metadataFromPng.ChunkMetadata;
485 if (column.PrettyLocation == null)
486 column = column.Reload(ClientAPI);
487 chunkTopMetadata.Add(column);
491 catch (PngjException someEx)
493 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
496 catch (ProtoException protoEx)
498 Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
504 //POI and EOI raw dump files ~ reload em!
505 //var poiRawFile = File.
506 string poiPath = Path.Combine(path, poiFileName);
507 string eoiPath = Path.Combine(path, eoiFileName);
509 if (File.Exists(poiPath))
511 using (var poiFile = File.OpenRead(poiPath))
513 this.POIs = Serializer.Deserialize<PointsOfInterest>(poiFile);
514 Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count);
518 if (File.Exists(eoiPath))
520 using (var eoiFile = File.OpenRead(eoiPath))
522 this.EOIs = Serializer.Deserialize<EntitiesOfInterest>(eoiFile);
523 Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count);
529 private PngWriter SetupPngImage(Vec2i coord, ref ColumnMeta metadata)
531 ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
533 string filename = $"{coord.X}_{coord.Y}.png";
534 filename = Path.Combine(path, _chunkPath ,filename);
536 PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true);
537 PngMetadata meta = pngWriter.GetMetadata();
539 meta.SetText("Chunk_X", coord.X.ToString("D"));
540 meta.SetText("Chunk_Y", coord.Y.ToString("D"));
541 //Setup specialized meta-data PNG chunks here...
542 PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo)
544 ChunkMetadata = metadata
546 pngWriter.GetChunksList().Queue(pngChunkMeta);
547 pngWriter.CompLevel = 5;// 9 is the maximum compression but thats too high for the small benefit it gives
548 pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman;
554 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
556 /// <param name="key">Chunk Coordinate</param>
557 /// <param name="mapChunk">Map chunk.</param>
558 /// <param name="chunkMeta">Chunk metadata</param>
559 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
562 int targetChunkY = mapChunk.YMax / chunkSize;//Surface ...
563 for (; targetChunkY > 0; targetChunkY--)
565 WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
567 if (chunkData == null || chunkData.BlockEntities == null)
570 Logger.VerboseDebug("Chunk null or empty X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
576 /*************** Chunk Entities Scanning *********************/
577 if (chunkData.BlockEntities != null && chunkData.BlockEntities.Count > 0)
580 Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Count);
583 foreach (var blockEnt in chunkData.BlockEntities)
585 if (blockEnt.Value != null && blockEnt.Value.Block != null && BlockID_Designators.ContainsKey(blockEnt.Value.Block.BlockId))
587 var designator = BlockID_Designators[blockEnt.Value.Block.BlockId];
588 designator.SpecialAction(ClientAPI, POIs, blockEnt.Value.Pos.Copy(), blockEnt.Value.Block);
592 /********************* Chunk/Column BLOCKs scanning ****************/
593 //Heightmap, Stats, block tally
596 //int X_index, Y_index, Z_index;
598 //Ensure ChunkData Metadata fields arn't null...due to being tossed out
599 //if (chunkMeta.HeightMap == null) { chunkMeta.HeightMap = new ushort[chunkSize, chunkSize]; }
600 //if (chunkMeta.RockRatio == null) { chunkMeta.RockRatio = new Dictionary<int, uint>(10); }
602 //for (Y_index = 0; Y_index < chunkSize - 1; Y_index++)
604 // for (Z_index = 0; Z_index < chunkSize - 1; Z_index++)
606 // for (X_index = 0; X_index < chunkSize - 1; X_index++)
608 // /* Encode packed indicie
609 // (y * chunksize + z) * chunksize + x
611 // var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index);
612 // int aBlockId = chunkData.Blocks[indicie];
614 // if (aBlockId == 0)
616 // chunkMeta.AirBlocks++;
620 // if (RockIdCodes.ContainsKey(aBlockId))
622 // if (chunkMeta.RockRatio.ContainsKey(aBlockId))
623 // chunkMeta.RockRatio[aBlockId]++;
625 // chunkMeta.RockRatio.Add(aBlockId, 1);
628 // chunkMeta.NonAirBlocks++;
631 // //if (chunkMeta.HeightMap[X_index, Z_index] == 0)
634 // // chunkMeta.HeightMap[X_index, Z_index] = (ushort) (Y_index + (targetChunkY * chunkSize));
644 private void UpdateEntityMetadata()
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())
652 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<");
655 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
656 if (dMatch.Value != null)
658 dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.Pos.AsBlockPos.Copy(), loadedEntity.Value);
666 private void AddNote(string notation)
668 var playerNodePoi = new PointOfInterest()
671 Location = ClientAPI.World.Player.Entity.Pos.AsBlockPos.Copy(),
673 Timestamp = DateTime.UtcNow,
676 this.POIs.AddReplace(playerNodePoi);
681 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
683 //Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
685 CommandData cmdData = data as CommandData;
687 switch (cmdData.State)
689 case CommandType.Run:
690 case CommandType.Stop:
691 case CommandType.Snapshot:
692 if (CurrentState != cmdData.State)
694 CurrentState = cmdData.State;
695 AwakenCartographer(0.0f);
699 case CommandType.Notation:
700 //Add to POI list where player location
701 AddNote(cmdData.Notation);
705 ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");