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; }
51 internal Dictionary<int, string> AiryIdCodes { get; private set; }
53 internal CommandType CurrentState { get; set; }
54 //Run status, Chunks processed, stats, center of map....
55 private uint nullChunkCount, nullMapCount, updatedChunksTotal;
56 private Vec2i startChunkColumn;
58 private readonly int chunkSize;
60 private IAsset staticMap;
61 private PersistedConfiguration configuration;
64 public static string AutomapStatusEventKey = @"AutomapStatus";
65 public static string AutomapCommandEventKey = @"AutomapCommand";
67 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
69 this.ClientAPI = clientAPI;
71 chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
72 ClientAPI.Event.LevelFinalize += EngageAutomap;
73 configuration = config;
75 //TODO:Choose which one from GUI
76 this.ChunkRenderer = new StandardRenderer(clientAPI, logger, this.configuration.SeasonalColors);
78 //Listen on bus for commands
79 ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
82 if (configuration.Autostart)
84 CurrentState = CommandType.Run;
85 Logger.Debug("Autostart is Enabled.");
92 private void EngageAutomap()
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 ClientAPI.GetOrCreateDataPath(Path.Combine(path, _chunkPath));
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.Pos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.Pos.AsBlockPos.Z / chunkSize));
109 chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
110 Logger.Notification("AUTOMAP Start {0}", startChunkColumn);
113 ClientAPI.Event.ChunkDirty += ChunkAChanging;
115 cartographer_thread = new Thread(Cartographer)
117 Name = "Cartographer",
118 Priority = ThreadPriority.Lowest,
122 snapshot = new Snapshotter(path, chunkTopMetadata, chunkSize,ClientAPI.World.Seed );
123 snapshotThread = new Thread(Snap)
126 Priority = ThreadPriority.Lowest,
130 ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
133 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
135 Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
137 columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
140 private void AwakenCartographer(float delayed)
143 if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
146 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
149 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted))
151 cartographer_thread.Start();
153 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
155 //Time to (re)write chunk shards
156 cartographer_thread.Interrupt();
159 //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})");
162 else if (CurrentState == CommandType.Snapshot)
164 if (snapshotThread.ThreadState.HasFlag(ThreadState.Unstarted))
166 snapshotThread.Start();
167 } else if (snapshotThread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
169 snapshotThread.Interrupt();
176 private void Cartographer()
179 Logger.VerboseDebug("Cartographer thread awoken");
183 uint ejectedItem = 0;
184 uint updatedChunks = 0;
185 uint updatedPixels = 0;
187 //-- Should dodge enumerator changing underfoot....at a cost.
188 if (!columnCounter.IsEmpty)
190 var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
191 UpdateEntityMetadata();
193 foreach (var mostActiveCol in tempSet)
195 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
197 if (mapChunk == null)
199 //TODO: REVISIT THIS CHUNK!
200 Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
202 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
206 ColumnMeta chunkMeta;
207 if (chunkTopMetadata.Contains(mostActiveCol.Key))
209 chunkMeta = chunkTopMetadata[mostActiveCol.Key];
211 Logger.VerboseDebug("Loaded meta-chunk {0}", mostActiveCol.Key);
216 chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
218 Logger.VerboseDebug("Created meta-chunk {0}", mostActiveCol.Key);
221 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
223 ChunkRenderer.SetupPngImage(mostActiveCol.Key, path, _chunkPath, ref chunkMeta);
224 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, out updatedPixels);
226 if (updatedPixels > 0)
229 Logger.VerboseDebug("Wrote top-chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
232 chunkTopMetadata.Update(chunkMeta);
233 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
237 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
239 Logger.VerboseDebug("Un-painted chunk shard: ({0}) ", mostActiveCol.Key);
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);
254 //Cleanup in-memory Metadata...
255 chunkTopMetadata.ClearMetadata( );
258 //Then sleep until interupted again, and repeat
260 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
262 Thread.Sleep(Timeout.Infinite);
265 catch (ThreadInterruptedException)
269 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
274 catch (ThreadAbortException)
277 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
283 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
293 Logger.VerboseDebug("Snapshot started");
299 Logger.VerboseDebug("Snapshot sleeping");
301 CurrentState = CommandType.Run;
302 Thread.Sleep(Timeout.Infinite);
304 catch (ThreadInterruptedException)
307 Logger.VerboseDebug("Snapshot intertupted");
313 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
315 StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
317 this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
320 private void Prefill_POI_Designators()
323 this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
324 this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
325 this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
326 this.AiryIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "tallgrass-"), EnumBlockMaterial.Plant);
328 //Add special marker types for BlockID's of "Interest", overwrite colour, and method
330 Reload_POI_Designators();
333 private void Reload_POI_Designators()
335 Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
336 foreach (var designator in configuration.BlockDesignators)
338 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
339 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
340 foreach (var entry in blockIDs)
342 BlockID_Designators.Add(entry.Key, designator);
345 this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
348 Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
349 foreach (var designator in configuration.EntityDesignators)
351 //Get Variants first, from EntityTypes...better be populated!
352 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
354 foreach (var match in matched)
356 Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
357 this.Entity_Designators.Add(match.Code, designator);
360 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
369 /// Store Points/Entity of Interest
371 private void PersistPointsData()
373 //POI and EOI raw dump files ~ WRITE em!
374 //var poiRawFile = File.
375 string poiPath = Path.Combine(path, poiFileName);
376 string eoiPath = Path.Combine(path, eoiFileName);
378 if (this.POIs.Count > 0)
380 using (var poiFile = File.OpenWrite(poiPath))
382 Serializer.Serialize<PointsOfInterest>(poiFile, this.POIs);
386 if (this.EOIs.Count > 0)
388 using (var eoiFile = File.OpenWrite(eoiPath))
390 Serializer.Serialize<EntitiesOfInterest>(eoiFile, this.EOIs);
394 //Create Easy to Parse TSV file for tool/human use....
395 string pointsTsvPath = Path.Combine(path, pointsTsvFileName);
397 using (var tsvWriter = new StreamWriter(pointsTsvPath, false, Encoding.UTF8))
399 tsvWriter.WriteLine("Name\tDescription\tLocation\tTime\tDestination");
400 foreach (var point in this.POIs)
402 tsvWriter.Write(point.Name + "\t");
403 var notes = point.Notes
404 .Replace("\n", "\\n")
405 .Replace("\t", "\\t")
406 .Replace("\\", "\\\\");
407 tsvWriter.Write(notes + "\t");
408 tsvWriter.Write(point.Location.PrettyCoords(ClientAPI) + "\t");
409 tsvWriter.Write(point.Timestamp.ToString("u") + "\t");
410 tsvWriter.Write((point.Destination != null ? point.Destination.PrettyCoords(ClientAPI) : "---") +"\t");
411 tsvWriter.WriteLine();
413 foreach (var entity in this.EOIs)
415 tsvWriter.Write(entity.Name + "\t");
416 var notes = entity.Notes
417 .Replace("\n", "\\n")
418 .Replace("\t", "\\t")
419 .Replace("\\", "\\\\");
420 tsvWriter.Write(notes + "\t");
421 tsvWriter.Write(entity.Location.PrettyCoords(ClientAPI) + "\t");
422 tsvWriter.Write(entity.Timestamp.ToString("u") + "\t");
423 tsvWriter.Write("n/a\t");
424 tsvWriter.WriteLine();
426 tsvWriter.WriteLine();
432 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
434 ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize, (ClientAPI.World.BlockAccessor.MapSizeY / chunkSize));
435 BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
437 mostActiveCol.Key.Y * chunkSize);
439 var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
440 data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
446 /// Reload chunk bounds from chunk shards
448 /// <returns>The metadata.</returns>
449 private void Reload_Metadata()
451 var shardsDir = new DirectoryInfo( Path.Combine(path, _chunkPath) );
453 if (!shardsDir.Exists)
456 Logger.VerboseDebug("Could not open world map (shards) directory");
460 var shardFiles = shardsDir.GetFiles(chunkFile_filter);
462 if (shardFiles.Length > 0)
465 Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length);
468 foreach (var shardFile in shardFiles)
471 if (shardFile.Length < 1024) continue;
472 var result = chunkShardRegex.Match(shardFile.Name);
473 if (!result.Success) continue;
475 int X_chunk_pos = int.Parse(result.Groups["X"].Value);
476 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
480 using (var fileStream = shardFile.OpenRead())
483 PngReader pngRead = new PngReader(fileStream);
484 pngRead.ReadSkippingAllRows();
486 //Parse PNG chunks for METADATA in shard
487 PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
488 var column = metadataFromPng.ChunkMetadata;
489 if (column.PrettyLocation == null)
490 column = column.Reload(ClientAPI);
491 chunkTopMetadata.Add(column);
495 catch (PngjException someEx)
497 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
500 catch (ProtoException protoEx)
502 Logger.Error("ProtoBuf invalid! file:'{0}' - Reason: {1}", shardFile.Name, protoEx);
508 //POI and EOI raw dump files ~ reload em!
509 //var poiRawFile = File.
510 string poiPath = Path.Combine(path, poiFileName);
511 string eoiPath = Path.Combine(path, eoiFileName);
513 if (File.Exists(poiPath))
515 using (var poiFile = File.OpenRead(poiPath))
517 this.POIs = Serializer.Deserialize<PointsOfInterest>(poiFile);
518 Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count);
522 if (File.Exists(eoiPath))
524 using (var eoiFile = File.OpenRead(eoiPath))
526 this.EOIs = Serializer.Deserialize<EntitiesOfInterest>(eoiFile);
527 Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count);
536 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
538 /// <param name="key">Chunk Coordinate</param>
539 /// <param name="mapChunk">Map chunk.</param>
540 /// <param name="chunkMeta">Chunk metadata</param>
541 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
543 int targetChunkY = mapChunk.YMax / chunkSize;//Surface ish...
547 Logger.VerboseDebug("Start col @ X{0} Y{1} Z{2} !", key.X, targetChunkY, key.Y);
550 chunkMeta.ResetMetadata(ClientAPI.World.BlockAccessor.MapSizeY);
552 for (; targetChunkY > 0; targetChunkY--)
554 WorldChunk worldChunk = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
556 if (worldChunk == null || worldChunk.BlockEntities == null)
559 Logger.VerboseDebug("WORLD chunk: null or empty X{0} Y{1} Z{2} !", key.X, targetChunkY, key.Y);
565 if (worldChunk.IsPacked())
567 Logger.VerboseDebug("WORLD chunk: Compressed: X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
568 worldChunk.Unpack( );//RESEARCH: Thread Unsafe?
571 /*************** Chunk Entities Scanning *********************/
572 if (worldChunk.BlockEntities != null && worldChunk.BlockEntities.Count > 0)
575 Logger.VerboseDebug("Scan pos.({0}) for BlockEntities# {1}", key, worldChunk.BlockEntities.Count);
578 foreach (var blockEnt in worldChunk.BlockEntities)
580 if (blockEnt.Value != null && blockEnt.Value.Block != null && BlockID_Designators.ContainsKey(blockEnt.Value.Block.BlockId))
582 var designator = BlockID_Designators[blockEnt.Value.Block.BlockId];
583 designator.SpecialAction(ClientAPI, POIs, blockEnt.Value.Pos.Copy(), blockEnt.Value.Block);
588 /********************* Chunk/Column BLOCKs scanning ****************/
589 //Heightmap, Stats, block tally
591 int X_index, Y_index, Z_index;
593 //First Chance fail-safe;
594 if (worldChunk.Blocks == null || worldChunk.Blocks.Length <= 0) {
595 Logger.VerboseDebug("WORLD chunk; Missing block DATA⁈ X{0} Y{1} Z{2} ⁈", key.X, targetChunkY, key.Y);
600 chunkMeta.ColumnPresense[targetChunkY] = true;
602 for (Y_index = 0; Y_index < chunkSize; Y_index++)
604 for (Z_index = 0; Z_index < chunkSize; Z_index++)
606 for (X_index = 0; X_index < chunkSize; X_index++)
608 var indicie = MapUtil.Index3d(X_index, Y_index, Z_index, chunkSize, chunkSize);
610 //'Last' Chance fail-safe;
611 if (worldChunk.Blocks == null || worldChunk.Blocks.Length <= 0) {
612 Logger.VerboseDebug("Processing Block: Missing block DATA⁈ X{0} Y{1} Z{2} ⁈", X_index, Y_index, Z_index);
617 int aBlockId = worldChunk.Blocks[indicie];
619 if (aBlockId == 0 || AiryIdCodes.ContainsKey(aBlockId)) {//Airy blocks,,,
620 chunkMeta.AirBlocks++;
624 if (RockIdCodes.ContainsKey(aBlockId)) {
625 if (chunkMeta.RockRatio.ContainsKey(aBlockId))
626 chunkMeta.RockRatio[aBlockId]++;
628 chunkMeta.RockRatio.Add(aBlockId, 1);
631 chunkMeta.NonAirBlocks++;
633 ushort localHeight = ( ushort )(Y_index + (targetChunkY * chunkSize));
634 //Heightmap - Need to ignore Grass & Snow
635 if (localHeight > chunkMeta.HeightMap[X_index, Z_index])
637 chunkMeta.HeightMap[X_index, Z_index] = localHeight;
638 if (localHeight > chunkMeta.YMax) chunkMeta.YMax = localHeight;
645 Logger.VerboseDebug("COLUMN X{0} Z{1}: {2}, processed.", key.X , key.Y, chunkTally + 1);
648 private void UpdateEntityMetadata()
650 Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
651 //Mabey scan only for 'new' entities by tracking ID in set?
652 foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
656 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<");
659 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
660 if (dMatch.Value != null)
662 dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.Pos.AsBlockPos.Copy(), loadedEntity.Value);
670 private void AddNote(string notation)
672 var playerNodePoi = new PointOfInterest()
675 Location = ClientAPI.World.Player.Entity.Pos.AsBlockPos.Copy(),
677 Timestamp = DateTime.UtcNow,
680 this.POIs.AddReplace(playerNodePoi);
685 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
687 //Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
689 CommandData cmdData = data as CommandData;
691 switch (cmdData.State)
693 case CommandType.Run:
694 case CommandType.Stop:
695 case CommandType.Snapshot:
696 if (CurrentState != cmdData.State)
698 CurrentState = cmdData.State;
699 AwakenCartographer(0.0f);
703 case CommandType.Notation:
704 //Add to POI list where player location
705 AddNote(cmdData.Notation);
709 ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");