2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
6 using System.Reflection;
8 using System.Text.RegularExpressions;
9 using System.Threading;
12 using Hjg.Pngcs.Chunks;
14 using Newtonsoft.Json;
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;
27 public class AutomapSystem
29 private Thread cartographer_thread;
30 private ICoreClientAPI ClientAPI { get; set; }
31 private ILogger Logger { get; set; }
32 private IChunkRenderer ChunkRenderer { get; set; }
33 private JsonGenerator JsonGenerator { get; set; }
35 private const string _mapPath = @"Maps";
36 private const string _chunkPath = @"Chunks";
37 private const string _domain = @"automap";
38 private const string chunkFile_filter = @"*_*.png";
39 private const string poiFileName = @"poi_binary";
40 private const string eoiFileName = @"eoi_binary";
41 private const string pointsTsvFileName = @"points_of_interest.tsv";
42 private static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+).png", RegexOptions.Singleline);
44 private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150);
45 private ColumnsMetadata chunkTopMetadata;
46 private PointsOfInterest POIs = new PointsOfInterest();
47 private EntitiesOfInterest EOIs = new EntitiesOfInterest();
48 private string jsonPreBuilt;
50 internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set; }
51 internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
52 internal Dictionary<int, string> RockIdCodes { get; private set; }
54 internal CommandType CurrentState { get; set; }
55 //Run status, Chunks processed, stats, center of map....
56 private uint nullChunkCount, updatedChunksTotal;
57 private Vec2i startChunkColumn;
59 private readonly int chunkSize;
61 private IAsset staticMap;
62 private PersistedConfiguration configuration;
65 public static string AutomapStatusEventKey = @"AutomapStatus";
66 public static string AutomapCommandEventKey = @"AutomapCommand";
67 PersistedConfiguration cachedConfiguration;
69 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
71 this.ClientAPI = clientAPI;
73 chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
74 ClientAPI.Event.LevelFinalize += EngageAutomap;
75 configuration = config;
78 //TODO:Choose which one from GUI
79 this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
81 //Listen on bus for commands
82 ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
84 if (configuration.Autostart)
86 CurrentState = CommandType.Run;
87 Logger.Debug("Autostart is Enabled.");
94 private void EngageAutomap()
96 path = ClientAPI.GetOrCreateDataPath(_mapPath);
97 path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName'
98 JsonGenerator = new JsonGenerator(ClientAPI, Logger, path);
100 string mapFilename = Path.Combine(path, "automap.html");
101 StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
103 staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html"));
104 outputText.Write(staticMap.ToText());
107 Prefill_POI_Designators();
108 startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Z / chunkSize));
109 chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
111 Logger.Notification("AUTOMAP Start {0}", startChunkColumn);
114 ClientAPI.Event.ChunkDirty += ChunkAChanging;
116 cartographer_thread = new Thread(Cartographer)
118 Name = "Cartographer",
119 Priority = ThreadPriority.Lowest,
123 ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
126 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
128 Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
130 columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
133 private void AwakenCartographer(float delayed)
136 if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
139 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
142 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted))
144 cartographer_thread.Start();
146 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
148 //Time to (re)write chunk shards
149 cartographer_thread.Interrupt();
152 //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})");
155 else if (CurrentState == CommandType.Snapshot)
157 //TODO: Snapshot generator second thread...
163 private void Cartographer()
166 Logger.VerboseDebug("Cartographer thread awoken");
170 uint ejectedItem = 0;
171 uint updatedChunks = 0;
172 uint updatedPixels = 0;
174 //-- Should dodge enumerator changing underfoot....at a cost.
175 if (!columnCounter.IsEmpty)
177 var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
178 UpdateEntityMetadata();
180 foreach (var mostActiveCol in tempSet)
182 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
184 if (mapChunk == null)
186 Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
188 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
192 ColumnMeta chunkMeta;
193 if (chunkTopMetadata.Contains(mostActiveCol.Key))
195 chunkMeta = chunkTopMetadata[mostActiveCol.Key];
199 chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
202 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
204 PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, chunkMeta);
205 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter, out updatedPixels);
207 if (updatedPixels > 0)
211 Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
214 chunkTopMetadata.Update(chunkMeta);
215 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
219 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
220 Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key);
226 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
228 if (updatedChunks > 0)
230 //What about chunk updates themselves; a update bitmap isn't kept...
231 updatedChunksTotal += updatedChunks;
232 JsonGenerator.GenerateJSONMetadata(chunkTopMetadata, startChunkColumn, POIs, EOIs, RockIdCodes);
236 //Then sleep until interupted again, and repeat
238 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
240 Thread.Sleep(Timeout.Infinite);
243 catch (ThreadInterruptedException)
246 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
250 catch (ThreadAbortException)
252 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
257 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
258 PersistPointsData( );
262 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
264 StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
266 this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
269 private void Prefill_POI_Designators()
272 this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
273 this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
274 this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
276 //Add special marker types for BlockID's of "Interest", overwrite colour, and method
278 Reload_POI_Designators();
281 private void Reload_POI_Designators()
283 Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
284 foreach (var designator in configuration.BlockDesignators)
286 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
287 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
288 foreach (var entry in blockIDs)
290 BlockID_Designators.Add(entry.Key, designator);
293 this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
296 Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
297 foreach (var designator in configuration.EntityDesignators)
299 //Get Variants first, from EntityTypes...better be populated!
300 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
302 foreach (var match in matched)
304 Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
305 this.Entity_Designators.Add(match.Code, designator);
310 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
319 /// Store Points/Entity of Interest
321 private void PersistPointsData( )
323 //POI and EOI raw dump files ~ WRITE em!
324 //var poiRawFile = File.
325 string poiPath = Path.Combine(path, poiFileName);
326 string eoiPath = Path.Combine(path, eoiFileName);
328 if (this.POIs.Count > 0) {
329 using (var poiFile = File.OpenWrite(poiPath)) {
330 Serializer.Serialize<PointsOfInterest>(poiFile, this.POIs);
334 if (this.EOIs.Count > 0) {
335 using (var eoiFile = File.OpenWrite(eoiPath)) {
336 Serializer.Serialize<EntitiesOfInterest>(eoiFile, this.EOIs);
340 //Create Easy to Parse TSV file for tool/human use....
341 string pointsTsvPath = Path.Combine(path, pointsTsvFileName);
343 using (var tsvWriter = new StreamWriter(pointsTsvPath, false, Encoding.UTF8))
345 tsvWriter.WriteLine("Name\tDescription\tLocation\tTime\t");
346 foreach (var point in this.POIs) {
347 tsvWriter.Write(point.Name + "\t");
348 tsvWriter.Write(point.Notes + "\t");
349 tsvWriter.Write(point.Location.PrettyCoords(ClientAPI) + "\t");
350 tsvWriter.Write(point.Timestamp.ToString("u")+"\t");
351 tsvWriter.WriteLine();
353 foreach (var entity in this.EOIs) {
354 tsvWriter.Write(entity.Name + "\t");
355 tsvWriter.Write(entity.Notes + "\t");
356 tsvWriter.Write(entity.Location.PrettyCoords(ClientAPI) + "\t");
357 tsvWriter.Write(entity.Timestamp.ToString("u") + "\t");
358 tsvWriter.WriteLine( );
360 tsvWriter.WriteLine();
366 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
368 ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), (byte) chunkSize);
369 BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
371 mostActiveCol.Key.Y * chunkSize);
373 var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
374 data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
380 /// Reload chunk bounds from chunk shards
382 /// <returns>The metadata.</returns>
383 private void Reload_Metadata()
385 var worldmapDir = new DirectoryInfo(path);
387 if (worldmapDir.Exists)
390 var shardFiles = worldmapDir.GetFiles(chunkFile_filter);
392 if (shardFiles.Length > 0)
395 Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
398 foreach (var shardFile in shardFiles) {
400 if (shardFile.Length < 1024) continue;
401 var result = chunkShardRegex.Match(shardFile.Name);
402 if (result.Success) {
403 int X_chunk_pos = int.Parse(result.Groups["X"].Value);
404 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
408 using (var fileStream = shardFile.OpenRead( )) {
410 PngReader pngRead = new PngReader(fileStream);
411 pngRead.ReadSkippingAllRows( );
413 //Parse PNG chunks for METADATA in shard
414 PngMetadataChunk metadataFromPng = pngRead.GetChunksList( ).GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
416 chunkTopMetadata.Add(metadataFromPng.ChunkMetadata);
420 catch (PngjException someEx)
422 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
430 //POI and EOI raw dump files ~ reload em!
431 //var poiRawFile = File.
432 string poiPath = Path.Combine(path, poiFileName);
433 string eoiPath = Path.Combine(path, eoiFileName);
435 if (File.Exists(poiPath)) {
436 using (var poiFile = File.OpenRead(poiPath)) {
437 this.POIs = Serializer.Deserialize<PointsOfInterest>(poiFile);
438 Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count);
442 if (File.Exists(eoiPath)) {
443 using (var eoiFile = File.OpenRead(eoiPath)) {
444 this.EOIs = Serializer.Deserialize<EntitiesOfInterest>(eoiFile);
445 Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count);
453 Logger.VerboseDebug("Could not open world map directory");
461 private PngWriter SetupPngImage(Vec2i coord, ColumnMeta metadata)
463 ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
465 string filename = $"{coord.X}_{coord.Y}.png";
466 filename = Path.Combine(path, filename);
468 PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true);
469 PngMetadata meta = pngWriter.GetMetadata();
471 meta.SetText("Chunk_X", coord.X.ToString("D"));
472 meta.SetText("Chunk_Y", coord.Y.ToString("D"));
473 //Setup specialized meta-data PNG chunks here...
474 PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo)
476 ChunkMetadata = metadata
478 pngWriter.GetChunksList().Queue(pngChunkMeta);
479 pngWriter.CompLevel = 9;// 9 is the maximum compression
480 pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman;
486 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
488 /// <param name="key">Chunk Coordinate</param>
489 /// <param name="mapChunk">Map chunk.</param>
490 /// <param name="chunkMeta">Chunk metadata</param>
491 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
494 int targetChunkY = mapChunk.YMax / chunkSize;//Surface ...
495 for (; targetChunkY > 0; targetChunkY--)
497 WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
499 if (chunkData == null || chunkData.BlockEntities == null) {
501 Logger.VerboseDebug("Chunk null or empty X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
506 /*************** Chunk Entities Scanning *********************/
507 if (chunkData.BlockEntities != null && chunkData.BlockEntities.Length > 0)
510 Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Length);
513 foreach (var blockEnt in chunkData.BlockEntities)
516 if (blockEnt != null && blockEnt.Block != null && BlockID_Designators.ContainsKey(blockEnt.Block.BlockId))
518 var designator = BlockID_Designators[blockEnt.Block.BlockId];
519 designator.SpecialAction(ClientAPI, POIs, blockEnt.Pos.Copy(), blockEnt.Block);
524 /********************* Chunk/Column BLOCKs scanning ****************/
525 //Heightmap, Stats, block tally
528 int X_index, Y_index, Z_index;
529 X_index = Y_index = Z_index = 0;
537 /* Encode packed indicie
538 (y * chunksize + z) * chunksize + x
540 var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index);
541 int aBlockId = chunkData.Blocks[indicie];
545 chunkMeta.AirBlocks++;
549 if (RockIdCodes.ContainsKey(aBlockId))
551 if (chunkMeta.RockRatio.ContainsKey(aBlockId)) { chunkMeta.RockRatio[aBlockId]++; } else { chunkMeta.RockRatio.Add(aBlockId, 1); }
554 chunkMeta.NonAirBlocks++;
557 if (chunkMeta.HeightMap[X_index, Z_index] == 0)
558 { chunkMeta.HeightMap[X_index, Z_index] = (ushort) (Y_index + (targetChunkY * chunkSize)); }
561 while (X_index++ < (chunkSize - 1));
564 while (Z_index++ < (chunkSize - 1));
567 while (Y_index++ < (chunkSize - 1));
572 private void UpdateEntityMetadata()
574 Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
575 //Mabey scan only for 'new' entities by tracking ID in set?
576 foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
580 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<");
583 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
584 if (dMatch.Value != null)
586 dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.LocalPos.AsBlockPos.Copy(), loadedEntity.Value);
594 private void AddNote(string notation)
596 var playerNodePoi = new PointOfInterest()
599 Location = ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Copy(),
601 Timestamp = DateTime.UtcNow,
604 this.POIs.AddReplace(playerNodePoi);
609 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
611 Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
613 CommandData cmdData = data as CommandData;
616 if (CurrentState != CommandType.Snapshot)
618 switch (cmdData.State)
620 case CommandType.Run:
621 case CommandType.Stop:
622 if (CurrentState != cmdData.State) {
623 CurrentState = cmdData.State;
624 AwakenCartographer(0.0f);
628 case CommandType.Snapshot:
629 CurrentState = CommandType.Stop;
630 //Snapshot starts a second thread/process...
634 case CommandType.Notation:
635 //Add to POI list where player location
636 AddNote(cmdData.Notation);
644 ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");