2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics;
7 using System.Reflection;
9 using System.Text.RegularExpressions;
10 using System.Threading;
13 using Hjg.Pngcs.Chunks;
15 using Newtonsoft.Json;
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;
26 public class AutomapSystem
28 private Thread cartographer_thread;
29 private ICoreClientAPI ClientAPI { get; set; }
30 private ILogger Logger { get; set; }
31 private IChunkRenderer ChunkRenderer { get; set; }
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 static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+).png", RegexOptions.Singleline);
39 private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150);
40 private ColumnsMetadata chunkTopMetadata;
41 private PointsOfInterest POIs = new PointsOfInterest();
42 private EntitiesOfInterest EOIs = new EntitiesOfInterest();
43 private string jsonPreBuilt;
45 internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set; }
46 internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
47 internal Dictionary<int, string> RockIdCodes { get; private set; }
49 internal CommandType CurrentState { get; set; }
50 //Run status, Chunks processed, stats, center of map....
51 private uint nullChunkCount, updatedChunksTotal;
52 private Vec2i startChunkColumn;
54 private readonly int chunkSize;
56 private IAsset staticMap;
57 private PersistedConfiguration configuration;
60 public static string AutomapStatusEventKey = @"AutomapStatus";
61 public static string AutomapCommandEventKey = @"AutomapCommand";
62 //PersistedConfiguration cachedConfiguration;
64 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config)
66 this.ClientAPI = clientAPI;
68 chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
69 ClientAPI.Event.LevelFinalize += EngageAutomap;
70 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);
80 if (configuration.Autostart)
82 CurrentState = CommandType.Run;
83 Logger.Debug("Autostart is Enabled.");
90 private void EngageAutomap()
92 path = ClientAPI.GetOrCreateDataPath(_mapPath);
93 path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName'
96 string mapFilename = Path.Combine(path, "automap.html");
97 StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
99 staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html"));
100 outputText.Write(staticMap.ToText());
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);
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 ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
124 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
126 Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
128 columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
131 private void AwakenCartographer(float delayed)
134 if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
137 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
140 if (cartographer_thread.ThreadState.HasFlag(System.Threading.ThreadState.Unstarted))
142 cartographer_thread.Start();
144 else if (cartographer_thread.ThreadState.HasFlag(System.Threading.ThreadState.WaitSleepJoin))
146 //Time to (re)write chunk shards
147 cartographer_thread.Interrupt();
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})");
153 else if (CurrentState == CommandType.Snapshot)
155 //TODO: Snapshot generator second thread...
161 private void Cartographer()
164 Logger.VerboseDebug("Cartographer thread awoken");
168 uint ejectedItem = 0;
169 uint updatedChunks = 0;
170 uint updatedPixels = 0;
172 //-- Should dodge enumerator changing underfoot....at a cost.
173 if (!columnCounter.IsEmpty)
175 var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
176 UpdateEntityMetadata();
178 foreach (var mostActiveCol in tempSet)
180 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
182 if (mapChunk == null)
184 Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
186 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
190 ColumnMeta chunkMeta;
191 if (chunkTopMetadata.Contains(mostActiveCol.Key))
193 chunkMeta = chunkTopMetadata[mostActiveCol.Key];
197 chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
200 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta);
202 PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, chunkMeta);
203 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter, out updatedPixels);
205 if (updatedPixels > 0)
209 Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
212 chunkTopMetadata.Update(chunkMeta);
213 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
217 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
218 Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key);
224 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
226 if (updatedChunks > 0)
228 //What about chunk updates themselves; a update bitmap isn't kept...
229 updatedChunksTotal += updatedChunks;
230 GenerateJSONMetadata();
234 //Then sleep until interupted again, and repeat
236 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
238 Thread.Sleep(Timeout.Infinite);
241 catch (ThreadInterruptedException)
244 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
248 catch (ThreadAbortException)
250 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
255 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
259 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
261 StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run);
263 this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
266 private void Prefill_POI_Designators()
269 this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
270 this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
271 this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone);
273 //Add special marker types for BlockID's of "Interest", overwrite colour, and method
275 Reload_POI_Designators();
278 private void Reload_POI_Designators()
280 Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count);
281 foreach (var designator in configuration.BlockDesignators)
283 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
284 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
285 foreach (var entry in blockIDs)
287 BlockID_Designators.Add(entry.Key, designator);
290 this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
293 Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count);
294 foreach (var designator in configuration.EntityDesignators)
296 //Get Variants first, from EntityTypes...better be populated!
297 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
299 foreach (var match in matched)
301 Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
302 this.Entity_Designators.Add(match.Code, designator);
307 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
314 /// Generates the JSON Metadata. (in Map object format )
316 private void GenerateJSONMetadata()
318 string jsonFilename = Path.Combine(path, "Metadata.js");
320 StreamWriter stream = new StreamWriter(jsonFilename, false, Encoding.UTF8);
324 Stopwatch stopWatch = new Stopwatch();
327 stream.Write(jsonPreBuilt);
329 stream.Write("ViewFrame.chunks.genTime='{0}';",
330 DateTimeOffset.UtcNow
333 stream.Write("ViewFrame.chunks.startCoords=[{0},{1}];",
334 startChunkColumn.X, startChunkColumn.Y
337 stream.Write("ViewFrame.chunks.bounds=[{0},{1},{2},{3}];",
338 chunkTopMetadata.North_mostChunk,
339 chunkTopMetadata.South_mostChunk,
340 chunkTopMetadata.West_mostChunk,
341 chunkTopMetadata.East_mostChunk
345 //MAP object format - [key, value]: key is "x_y"
346 stream.Write("ViewFrame.chunks.chunkMetadata=new Map([");
348 foreach (var shard in chunkTopMetadata)
350 shard.Write(stream, ClientAPI);
353 stream.Write("]);");// Close constructor of Map (chunkMetadata)
356 stream.Write("ViewFrame.chunks.pointsOfInterest=new Map([");
358 foreach (var poi in POIs)
360 poi.Write(stream, ClientAPI);
366 stream.Write("ViewFrame.chunks.entitiesOfInterest=new Map([");
367 foreach (var eoi in EOIs)
369 eoi.Write(stream, ClientAPI);
372 stream.Write("]);\n");
374 //stream.Write("//============= BlockID's for Rockmap / Rock-ratios ===============");
376 //stream.Write("ViewFrame.chunks.rockLookup=new Map([");
378 //foreach (var entry in RockIdCodes)
380 // var block = ClientAPI.World.GetBlock(entry.Key);
382 // stream.Write("[");
383 // stream.Write("'{0},", block.Code.Path);
385 // stream.Write("[");
386 // stream.Write("'{0}',", entry.Value);
388 // stream.Write("{0},", Lang.GetUnformatted(block.Code.Path));
391 // stream.Write("]],");
394 //stream.Write("]);\n");
399 Logger.Debug("Run time of chunk write {0}", stopWatch.Elapsed);
405 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
407 ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), (byte) chunkSize);
408 BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
410 mostActiveCol.Key.Y * chunkSize);
412 var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
413 data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
419 /// Reload chunk bounds from chunk shards
421 /// <returns>The metadata.</returns>
422 private void Reload_Metadata()
424 var worldmapDir = new DirectoryInfo(path);
426 if (worldmapDir.Exists)
429 var files = worldmapDir.GetFiles(chunkFile_filter);
431 if (files.Length > 0)
434 Logger.VerboseDebug("{0} Existing world chunk shards", files.Length);
437 foreach (var shardFile in files)
440 if (shardFile.Length < 1024) continue;
441 var result = chunkShardRegex.Match(shardFile.Name);
444 int X_chunk_pos = int.Parse(result.Groups["X"].Value);
445 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
449 using (var fileStream = shardFile.OpenRead())
452 PngReader pngRead = new PngReader(fileStream);
453 pngRead.ReadSkippingAllRows();
455 //Parse PNG chunks for METADATA in shard
456 PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
458 chunkTopMetadata.Add(metadataFromPng.ChunkMetadata);
462 catch (PngjException someEx)
464 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
477 Logger.VerboseDebug("Could not open world map directory");
485 private PngWriter SetupPngImage(Vec2i coord, ColumnMeta metadata)
487 ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
489 string filename = $"{coord.X}_{coord.Y}.png";
490 filename = Path.Combine(path, filename);
492 PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true);
493 PngMetadata meta = pngWriter.GetMetadata();
495 meta.SetText("Chunk_X", coord.X.ToString("D"));
496 meta.SetText("Chunk_Y", coord.Y.ToString("D"));
497 //Setup specialized meta-data PNG chunks here...
498 PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo)
500 ChunkMetadata = metadata
502 pngWriter.GetChunksList().Queue(pngChunkMeta);
503 pngWriter.CompLevel = 9;// 9 is the maximum compression
504 pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman;
510 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
512 /// <param name="key">Chunk Coordinate</param>
513 /// <param name="mapChunk">Map chunk.</param>
514 /// <param name="chunkMeta">Chunk metadata</param>
515 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta)
518 int targetChunkY = mapChunk.YMax / chunkSize;//Surface ...
519 for (; targetChunkY > 0; targetChunkY--)
521 WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
522 if (chunkData == null || chunkData.BlockEntities == null)
525 Logger.VerboseDebug("Chunk null or empty X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
530 /*************** Chunk Entities Scanning *********************/
531 if (chunkData.BlockEntities != null && chunkData.BlockEntities.Length > 0)
534 Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Length);
537 foreach (var blockEnt in chunkData.BlockEntities)
540 if (blockEnt != null && blockEnt.Block != null && BlockID_Designators.ContainsKey(blockEnt.Block.BlockId))
542 var designator = BlockID_Designators[blockEnt.Block.BlockId];
543 designator.SpecialAction(ClientAPI, POIs, blockEnt.Pos.Copy(), blockEnt.Block);
548 /********************* Chunk/Column BLOCKs scanning ****************/
549 //Heightmap, Stats, block tally
552 int X_index, Y_index, Z_index;
553 X_index = Y_index = Z_index = 0;
561 /* Encode packed indicie
562 (y * chunksize + z) * chunksize + x
564 var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index);
565 int aBlockId = chunkData.Blocks[indicie];
569 chunkMeta.AirBlocks++;
573 if (RockIdCodes.ContainsKey(aBlockId))
575 if (chunkMeta.RockRatio.ContainsKey(aBlockId)) { chunkMeta.RockRatio[aBlockId]++; } else { chunkMeta.RockRatio.Add(aBlockId, 1); }
578 chunkMeta.NonAirBlocks++;
580 while (X_index++ < (chunkSize - 1));
583 while (Z_index++ < (chunkSize - 1));
586 while (Y_index++ < (chunkSize - 1));
591 private void UpdateEntityMetadata()
593 Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
594 //Mabey scan only for 'new' entities by tracking ID in set?
595 foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray())
599 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<");
602 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
603 if (dMatch.Value != null)
605 Logger.Chat("Entity designator hit!");
606 dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.LocalPos.AsBlockPos.Copy(), loadedEntity.Value);
614 private void AddNote(string notation)
616 var playerNodePoi = new PointOfInterest()
618 Location = ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Copy(),
620 Timestamp = DateTimeOffset.UtcNow,
623 this.POIs.AddReplace(playerNodePoi);
628 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
630 Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
632 CommandData cmdData = data as CommandData;
635 if (CurrentState != CommandType.Snapshot)
637 switch (cmdData.State)
639 case CommandType.Run:
640 CurrentState = cmdData.State;
641 AwakenCartographer(0.0f);
644 case CommandType.Stop:
645 CurrentState = cmdData.State;
648 case CommandType.Snapshot:
649 CurrentState = CommandType.Stop;
650 //Snapshot starts a second thread/process...
654 case CommandType.Notation:
655 //Add to POI list where player location
656 AddNote(cmdData.Notation);
662 if (CurrentState != cmdData.State)
664 CurrentState = cmdData.State;
665 AwakenCartographer(0.0f);
669 Logger.VerboseDebug($"Automap commanded to: {cmdData.State} ");
675 private void MakePreBuiltJSON()
677 var builder = new StringBuilder(512);
678 builder.Append("ViewFrame.chunks={};\n");
679 builder.AppendFormat("ViewFrame.chunks.worldSeedNum='{0}';",
682 builder.AppendFormat("ViewFrame.chunks.chunkSize={0};",
686 builder.Append("ViewFrame.chunks.chunkMetadataNames=[");
687 var fields = typeof(ColumnMeta).GetFields();
688 var attsToSort = new List<DisplayNameAttribute>();
690 foreach (var f in fields)
692 var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
695 attsToSort.Add((DisplayNameAttribute) att);
699 foreach (var att in attsToSort.OrderBy(a => a.order))
700 builder.AppendFormat("'{0}',", att.name);
701 builder.Append("];\n");
703 builder.Append("ViewFrame.chunks.pointsOfInterestNames=[");
704 fields = typeof(PointOfInterest).GetFields();
705 attsToSort = new List<DisplayNameAttribute>();
707 foreach (var f in fields)
709 var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
711 attsToSort.Add((DisplayNameAttribute) att);
713 foreach (var att in attsToSort.OrderBy(a => a.order))
714 builder.AppendFormat("'{0}',", att.name);
715 builder.Append("];\n");
717 builder.Append("ViewFrame.chunks.entityOfInterestNames=[");
718 fields = typeof(EntityOfInterest).GetFields();
719 attsToSort = new List<DisplayNameAttribute>();
721 foreach (var f in fields)
723 var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
725 attsToSort.Add((DisplayNameAttribute) att);
727 foreach (var att in attsToSort.OrderBy(a => a.order))
728 builder.AppendFormat("'{0}',", att.name);
729 builder.Append("];\n");
731 jsonPreBuilt = builder.ToString();