2 using System.Collections;
3 using System.Collections.Concurrent;
4 using System.Collections.Generic;
8 using System.Text.RegularExpressions;
9 using System.Threading;
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 AChunkRenderer 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, this.configuration.SeasonalColors);
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 //TODO: REVISIT THIS CHUNK!
199 Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
201 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
205 ColumnMeta chunkMeta;
206 if (chunkTopMetadata.Contains(mostActiveCol.Key))
208 chunkMeta = chunkTopMetadata[mostActiveCol.Key];
210 Logger.VerboseDebug("Loaded meta-chunk {0}", mostActiveCol.Key);
215 chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
217 Logger.VerboseDebug("Created meta-chunk {0}", mostActiveCol.Key);
220 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
222 ChunkRenderer.SetupPngImage(mostActiveCol.Key, path, _chunkPath, ref chunkMeta);
223 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, out updatedPixels);
225 if (updatedPixels > 0)
228 Logger.VerboseDebug("Wrote top-chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
231 chunkTopMetadata.Update(chunkMeta);
232 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
236 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
238 Logger.VerboseDebug("Un-painted chunk shard: ({0}) ", mostActiveCol.Key);
242 //Cleanup in-memory Metadata...
243 chunkTopMetadata.ClearMetadata();
246 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
248 if (updatedChunks > 0)
250 //What about chunk updates themselves; a update bitmap isn't kept...
251 updatedChunksTotal += updatedChunks;
252 JsonGenerator.GenerateJSONMetadata(chunkTopMetadata, startChunkColumn, POIs, EOIs, RockIdCodes);
256 //Then sleep until interupted again, and repeat
258 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
260 Thread.Sleep(Timeout.Infinite);
263 catch (ThreadInterruptedException)
267 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
272 catch (ThreadAbortException)
275 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
281 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
291 Logger.VerboseDebug("Snapshot started");
297 Logger.VerboseDebug("Snapshot sleeping");
299 CurrentState = CommandType.Run;
300 Thread.Sleep(Timeout.Infinite);
302 catch (ThreadInterruptedException)
305 Logger.VerboseDebug("Snapshot intertupted");
311 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
313 StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
315 this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
318 private void Prefill_POI_Designators()
321 this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
322 this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
323 this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
325 //Add special marker types for BlockID's of "Interest", overwrite colour, and method
327 Reload_POI_Designators();
330 private void Reload_POI_Designators()
332 Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
333 foreach (var designator in configuration.BlockDesignators)
335 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
336 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
337 foreach (var entry in blockIDs)
339 BlockID_Designators.Add(entry.Key, designator);
342 this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
345 Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
346 foreach (var designator in configuration.EntityDesignators)
348 //Get Variants first, from EntityTypes...better be populated!
349 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
351 foreach (var match in matched)
353 Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
354 this.Entity_Designators.Add(match.Code, designator);
357 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
366 /// Store Points/Entity of Interest
368 private void PersistPointsData()
370 //POI and EOI raw dump files ~ WRITE em!
371 //var poiRawFile = File.
372 string poiPath = Path.Combine(path, poiFileName);
373 string eoiPath = Path.Combine(path, eoiFileName);
375 if (this.POIs.Count > 0)
377 using (var poiFile = File.OpenWrite(poiPath))
379 Serializer.Serialize<PointsOfInterest>(poiFile, this.POIs);
383 if (this.EOIs.Count > 0)
385 using (var eoiFile = File.OpenWrite(eoiPath))
387 Serializer.Serialize<EntitiesOfInterest>(eoiFile, this.EOIs);
391 //Create Easy to Parse TSV file for tool/human use....
392 string pointsTsvPath = Path.Combine(path, pointsTsvFileName);
394 using (var tsvWriter = new StreamWriter(pointsTsvPath, false, Encoding.UTF8))
396 tsvWriter.WriteLine("Name\tDescription\tLocation\tTime\tDestination");
397 foreach (var point in this.POIs)
399 tsvWriter.Write(point.Name + "\t");
400 var notes = point.Notes
401 .Replace("\n", "\\n")
402 .Replace("\t", "\\t")
403 .Replace("\\", "\\\\");
404 tsvWriter.Write(notes + "\t");
405 tsvWriter.Write(point.Location.PrettyCoords(ClientAPI) + "\t");
406 tsvWriter.Write(point.Timestamp.ToString("u") + "\t");
407 tsvWriter.Write((point.Destination != null ? point.Destination.PrettyCoords(ClientAPI) : "---") +"\t");
408 tsvWriter.WriteLine();
410 foreach (var entity in this.EOIs)
412 tsvWriter.Write(entity.Name + "\t");
413 var notes = entity.Notes
414 .Replace("\n", "\\n")
415 .Replace("\t", "\\t")
416 .Replace("\\", "\\\\");
417 tsvWriter.Write(notes + "\t");
418 tsvWriter.Write(entity.Location.PrettyCoords(ClientAPI) + "\t");
419 tsvWriter.Write(entity.Timestamp.ToString("u") + "\t");
420 tsvWriter.Write("n/a\t");
421 tsvWriter.WriteLine();
423 tsvWriter.WriteLine();
429 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
431 ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize, (ClientAPI.World.BlockAccessor.MapSizeY / chunkSize));
432 BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
434 mostActiveCol.Key.Y * chunkSize);
436 var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
437 data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
443 /// Reload chunk bounds from chunk shards
445 /// <returns>The metadata.</returns>
446 private void Reload_Metadata()
448 var shardsDir = new DirectoryInfo( Path.Combine(path, _chunkPath) );
450 if (!shardsDir.Exists)
453 Logger.VerboseDebug("Could not open world map (shards) directory");
457 var shardFiles = shardsDir.GetFiles(chunkFile_filter);
459 if (shardFiles.Length > 0)
462 Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
465 foreach (var shardFile in shardFiles)
468 if (shardFile.Length < 1024) continue;
469 var result = chunkShardRegex.Match(shardFile.Name);
470 if (!result.Success) continue;
472 int X_chunk_pos = int.Parse(result.Groups["X"].Value);
473 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
477 using (var fileStream = shardFile.OpenRead())
480 PngReader pngRead = new PngReader(fileStream);
481 pngRead.ReadSkippingAllRows();
483 //Parse PNG chunks for METADATA in shard
484 PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
485 var column = metadataFromPng.ChunkMetadata;
486 if (column.PrettyLocation == null)
487 column = column.Reload(ClientAPI);
488 chunkTopMetadata.Add(column);
492 catch (PngjException someEx)
494 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
497 catch (ProtoException protoEx)
499 Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
505 //POI and EOI raw dump files ~ reload em!
506 //var poiRawFile = File.
507 string poiPath = Path.Combine(path, poiFileName);
508 string eoiPath = Path.Combine(path, eoiFileName);
510 if (File.Exists(poiPath))
512 using (var poiFile = File.OpenRead(poiPath))
514 this.POIs = Serializer.Deserialize<PointsOfInterest>(poiFile);
515 Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count);
519 if (File.Exists(eoiPath))
521 using (var eoiFile = File.OpenRead(eoiPath))
523 this.EOIs = Serializer.Deserialize<EntitiesOfInterest>(eoiFile);
524 Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count);
533 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
535 /// <param name="key">Chunk Coordinate</param>
536 /// <param name="mapChunk">Map chunk.</param>
537 /// <param name="chunkMeta">Chunk metadata</param>
538 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
540 int targetChunkY = mapChunk.YMax / chunkSize;//Surface ish...
544 Logger.VerboseDebug("Start col @ X{0} Y{1} Z{2} !", key.X, targetChunkY, key.Y);
547 chunkMeta.ResetMetadata(ClientAPI.World.BlockAccessor.MapSizeY);
549 for (; targetChunkY > 0; targetChunkY--)
551 WorldChunk worldChunk = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
553 if (worldChunk == null || worldChunk.BlockEntities == null)
556 Logger.VerboseDebug("WORLD chunk: null or empty X{0} Y{1} Z{2} !", key.X, targetChunkY, key.Y);
562 if (worldChunk.IsPacked())
564 Logger.VerboseDebug("WORLD chunk: Compressed: X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
565 worldChunk.Unpack( );//RESEARCH: Thread Unsafe?
568 /*************** Chunk Entities Scanning *********************/
569 if (worldChunk.BlockEntities != null && worldChunk.BlockEntities.Count > 0)
572 Logger.VerboseDebug("Scan pos.({0}) for BlockEntities# {1}", key, worldChunk.BlockEntities.Count);
575 foreach (var blockEnt in worldChunk.BlockEntities)
577 if (blockEnt.Value != null && blockEnt.Value.Block != null && BlockID_Designators.ContainsKey(blockEnt.Value.Block.BlockId))
579 var designator = BlockID_Designators[blockEnt.Value.Block.BlockId];
580 designator.SpecialAction(ClientAPI, POIs, blockEnt.Value.Pos.Copy(), blockEnt.Value.Block);
585 /********************* Chunk/Column BLOCKs scanning ****************/
586 //Heightmap, Stats, block tally
588 int X_index, Y_index, Z_index;
590 //First Chance fail-safe;
591 if (worldChunk.Blocks == null || worldChunk.Blocks.Length <= 0) {
592 Logger.VerboseDebug("WORLD chunk; Missing block DATA⁈ X{0} Y{1} Z{2} ⁈", key.X, targetChunkY, key.Y);
597 chunkMeta.ColumnPresense[targetChunkY] = true;
599 for (Y_index = 0; Y_index < chunkSize; Y_index++)
601 for (Z_index = 0; Z_index < chunkSize; Z_index++)
603 for (X_index = 0; X_index < chunkSize; X_index++)
605 var indicie = MapUtil.Index3d(X_index, Y_index, Z_index, chunkSize, chunkSize);
607 //'Last' Chance fail-safe;
608 if (worldChunk.Blocks == null || worldChunk.Blocks.Length <= 0) {
609 Logger.VerboseDebug("Processing Block: Missing block DATA⁈ X{0} Y{1} Z{2} ⁈", X_index, Y_index, Z_index);
614 int aBlockId = worldChunk.Blocks[indicie];
616 if (aBlockId == 0) {//Air
617 chunkMeta.AirBlocks++;
621 if (RockIdCodes.ContainsKey(aBlockId)) {
622 if (chunkMeta.RockRatio.ContainsKey(aBlockId))
623 chunkMeta.RockRatio[aBlockId]++;
625 chunkMeta.RockRatio.Add(aBlockId, 1);
628 chunkMeta.NonAirBlocks++;
630 ushort localHeight = ( ushort )(Y_index + (targetChunkY * chunkSize));
631 //Heightmap - Need to ignore Grass & Snow
632 if (localHeight > chunkMeta.HeightMap[X_index, Z_index])
634 chunkMeta.HeightMap[X_index, Z_index] = localHeight;
635 if (localHeight > chunkMeta.YMax) chunkMeta.YMax = localHeight;
642 Logger.VerboseDebug("COLUMN X{0} Z{1}: {2}, processed.", key.X , key.Y, chunkTally + 1);
645 private void UpdateEntityMetadata()
647 Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
648 //Mabey scan only for 'new' entities by tracking ID in set?
649 foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
653 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<");
656 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
657 if (dMatch.Value != null)
659 dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.Pos.AsBlockPos.Copy(), loadedEntity.Value);
667 private void AddNote(string notation)
669 var playerNodePoi = new PointOfInterest()
672 Location = ClientAPI.World.Player.Entity.Pos.AsBlockPos.Copy(),
674 Timestamp = DateTime.UtcNow,
677 this.POIs.AddReplace(playerNodePoi);
682 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
684 //Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
686 CommandData cmdData = data as CommandData;
688 switch (cmdData.State)
690 case CommandType.Run:
691 case CommandType.Stop:
692 case CommandType.Snapshot:
693 if (CurrentState != cmdData.State)
695 CurrentState = cmdData.State;
696 AwakenCartographer(0.0f);
700 case CommandType.Notation:
701 //Add to POI list where player location
702 AddNote(cmdData.Notation);
706 ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");