2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
7 using System.Text.RegularExpressions;
8 using System.Threading;
11 using Hjg.Pngcs.Chunks;
12 using Newtonsoft.Json;
13 using Vintagestory.API.Client;
14 using Vintagestory.API.Common;
15 using Vintagestory.API.Config;
16 using Vintagestory.API.Datastructures;
17 using Vintagestory.API.MathTools;
18 using Vintagestory.Common;
22 public class AutomapSystem
24 private Thread cartographer_thread;
25 private ICoreClientAPI ClientAPI { get; set; }
26 private ILogger Logger { get; set; }
27 private IChunkRenderer ChunkRenderer { get; set; }
29 private const string _mapPath = @"Maps";
30 private const string _chunkPath = @"Chunks";
31 private const string _domain = @"automap";
32 private const string chunkFile_filter = @"*_*.png";
33 private static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+).png", RegexOptions.Singleline);
35 private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150);
36 private ColumnsMetadata chunkTopMetadata;
37 private PointsOfInterest POIs = new PointsOfInterest();
38 private EntitiesOfInterest EOIs = new EntitiesOfInterest();
40 internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set; }
41 internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
42 internal Dictionary<int, string> RockIdCodes { get; private set; }
44 internal RunState CurrentState { get; set; }
45 //Run status, Chunks processed, stats, center of map....
46 private uint nullChunkCount, updatedChunksTotal;
47 private Vec2i startChunkColumn;
49 private readonly int chunkSize;
51 private IAsset staticMap;
53 public static string AutomapStatusEventKey = @"AutomapStatus";
54 public static string AutomapCommandEventKey = @"AutomapCommand";
57 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger)
59 this.ClientAPI = clientAPI;
61 chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
62 ClientAPI.Event.LevelFinalize += EngageAutomap;
64 //TODO:Choose which one from GUI
65 this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
67 //Listen on bus for commands
68 ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
74 private void EngageAutomap()
76 path = ClientAPI.GetOrCreateDataPath(_mapPath);
77 path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName'
80 string mapFilename = Path.Combine(path, "automap.html");
81 StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
83 staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html"));
84 outputText.Write(staticMap.ToText());
87 Prefill_POI_Designators();
88 startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Z / chunkSize));
89 chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
91 Logger.Notification("AUTOMAP Start {0}", startChunkColumn);
94 ClientAPI.Event.ChunkDirty += ChunkAChanging;
96 cartographer_thread = new Thread(Cartographer)
98 Name = "Cartographer",
99 Priority = ThreadPriority.Lowest,
103 ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
106 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
108 Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
110 columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
113 private void AwakenCartographer(float delayed)
116 if (CurrentState == RunState.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true))
119 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
122 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted))
124 cartographer_thread.Start();
126 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin))
128 //Time to (re)write chunk shards
129 cartographer_thread.Interrupt();
132 //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})");
135 else if (CurrentState == RunState.Snapshot)
137 //TODO: Snapshot generator second thread...
143 private void Cartographer()
146 Logger.VerboseDebug("Cartographer thread awoken");
150 uint ejectedItem = 0;
151 uint updatedChunks = 0;
152 uint updatedPixels = 0;
154 //-- Should dodge enumerator changing underfoot....at a cost.
155 if (!columnCounter.IsEmpty)
157 var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
158 foreach (var mostActiveCol in tempSet)
161 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
163 if (mapChunk == null)
165 Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
167 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
171 ColumnMeta chunkMeta;
172 if (chunkTopMetadata.Contains(mostActiveCol.Key))
174 chunkMeta = chunkTopMetadata[mostActiveCol.Key];
178 chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk);
181 UpdateEntityMetadata();
182 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, chunkMeta);
184 PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, chunkMeta);
185 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter, out updatedPixels);
187 if (updatedPixels > 0)
191 Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
194 chunkTopMetadata.Update(chunkMeta);
195 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
199 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
200 Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key);
206 UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks);
208 if (updatedChunks > 0)
210 //What about chunk updates themselves; a update bitmap isn't kept...
211 updatedChunksTotal += updatedChunks;
212 GenerateJSONMetadata();
216 //Then sleep until interupted again, and repeat
218 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
220 Thread.Sleep(Timeout.Infinite);
223 catch (ThreadInterruptedException)
226 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
230 catch (ThreadAbortException)
232 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
237 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
241 private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta)
243 StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, RunState.Run);
245 this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData);
248 private void Prefill_POI_Designators()
251 this.BlockID_Designators = new Dictionary<int, BlockDesignator>();
252 this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>();
253 this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock"), EnumBlockMaterial.Stone);
255 //Add special marker types for BlockID's of "Interest", overwrite colour, and method
257 Install_POI_Designators(DefaultDesignators.DefaultBlockDesignators(), DefaultDesignators.DefaultEntityDesignators());
260 private void Install_POI_Designators(ICollection<BlockDesignator> blockDesig, List<EntityDesignator> entDesig)
262 Logger.VerboseDebug("Connecting {0} standard Block-Designators", blockDesig.Count);
263 foreach (var designator in blockDesig)
265 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
266 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); }
267 foreach (var entry in blockIDs)
269 BlockID_Designators.Add(entry.Key, designator);
272 this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
275 Logger.VerboseDebug("Connecting {0} standard Entity-Designators", entDesig.Count);
276 foreach (var designator in entDesig)
278 //Get Variants first, from EntityTypes...better be populated!
279 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
281 foreach (var match in matched)
283 Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
284 this.Entity_Designators.Add(match.Code, designator);
289 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
296 /// Generates the JSON Metadata. (in Map object format )
298 private void GenerateJSONMetadata()
300 string jsonFilename = Path.Combine(path, "Metadata.js");
302 StreamWriter jsonWriter = new StreamWriter(jsonFilename, false, Encoding.UTF8);
305 jsonWriter.Write("ViewFrame.chunks={};");
306 jsonWriter.Write("ViewFrame.chunks.worldSeedNum={0};", ClientAPI.World.Seed);
307 jsonWriter.Write("ViewFrame.chunks.genTime=new Date('{0}');", DateTimeOffset.UtcNow.ToString("O"));
308 jsonWriter.Write("ViewFrame.chunks.startCoords=[{0},{1}];", startChunkColumn.X, startChunkColumn.Y);
309 jsonWriter.Write("ViewFrame.chunks.chunkSize={0};", chunkSize);
310 jsonWriter.Write("ViewFrame.chunks.northMostChunk={0};", chunkTopMetadata.North_mostChunk);
311 jsonWriter.Write("ViewFrame.chunks.southMostChunk={0};", chunkTopMetadata.South_mostChunk);
312 jsonWriter.Write("ViewFrame.chunks.eastMostChunk={0};", chunkTopMetadata.East_mostChunk);
313 jsonWriter.Write("ViewFrame.chunks.westMostChunk={0};", chunkTopMetadata.West_mostChunk);
314 // this is so that the tool tip doesnt need to be hard coded in the map
315 jsonWriter.Write("ViewFrame.chunks.chunkMetaNames=[");
316 // there are 10 (TEN) (ten) things
317 jsonWriter.Write("'Loc.','Age','Temp.','Y Max','Fert.','Forest','Rain','Shrub','Air','Non-Air'");
318 jsonWriter.Write("];");
319 //MAP object format - [key, value]: key is "x_y"
320 jsonWriter.Write("ViewFrame.chunks.chunkMetadata=new Map([");
321 foreach (var shard in chunkTopMetadata)
323 jsonWriter.Write("['{0}_{1}',", shard.Location.X, shard.Location.Y);
324 jsonWriter.Write("[");
325 // 10 things but 0 indexed so NINE (9)
326 jsonWriter.Write("'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}'",
327 shard.Location.PrettyCoords(ClientAPI),
328 shard.ChunkAge.ToString("g"),//World age - relative? or last edit ??
329 shard.Temperature.ToString("F1"),
331 shard.Fertility.ToString("F1"),
332 shard.ForestDensity.ToString("F1"),
333 shard.Rainfall.ToString("F1"),
334 shard.ShrubDensity.ToString("F1"),
338 //TODO: Rock-ratio, also requires a BlockID => Name lookup table....elsewhere
339 jsonWriter.Write("]],");
341 jsonWriter.Write("]);");
344 jsonWriter.Write("ViewFrame.chunks.pointsOfInterest=new Map([");
345 foreach (var poi in POIs)
347 jsonWriter.Write("['{0}_{1}',", (float) poi.Location.X / chunkSize, (float) poi.Location.Z / chunkSize);
348 jsonWriter.Write("{");
349 jsonWriter.Write("prettyCoord:'{0}',", poi.Location.PrettyCoords(ClientAPI));
350 jsonWriter.Write("notes:{0},", JsonConvert.ToString(poi.Notes, '\'', StringEscapeHandling.EscapeHtml));
351 jsonWriter.Write("time:new Date('{0}'),", poi.Timestamp.ToString("O"));
352 jsonWriter.Write("chunkPos:'{0}_{1}',", (poi.Location.X / chunkSize), (poi.Location.Z / chunkSize));
353 jsonWriter.Write("}],");
355 jsonWriter.Write("]);");
357 jsonWriter.Write("ViewFrame.chunks.entitiesOfInterest=new Map([");
358 foreach (var eoi in EOIs)
360 jsonWriter.Write("['{0}_{1}',", (float) eoi.Location.X / chunkSize, (float) eoi.Location.Z / chunkSize);
361 jsonWriter.Write("{");
362 jsonWriter.Write("prettyCoord:'{0}',", eoi.Location.PrettyCoords(ClientAPI));
363 jsonWriter.Write("notes:{0},", JsonConvert.ToString(eoi.Notes, '\'', StringEscapeHandling.EscapeHtml));
364 jsonWriter.Write("time:new Date('{0}'),", eoi.Timestamp.ToString("O"));
365 jsonWriter.Write("chunkPos:'{0}_{1}',", (eoi.Location.X / chunkSize), (eoi.Location.Z / chunkSize));
366 jsonWriter.Write("entityId:'{0}'", eoi.EntityId);
367 jsonWriter.Write("}],");
369 jsonWriter.Write("]);");
377 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
379 ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), (byte) chunkSize);
380 BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
382 mostActiveCol.Key.Y * chunkSize);
384 var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
385 data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
391 /// Reload chunk bounds from chunk shards
393 /// <returns>The metadata.</returns>
394 private void Reload_Metadata()
396 var worldmapDir = new DirectoryInfo(path);
398 if (worldmapDir.Exists)
401 var files = worldmapDir.GetFiles(chunkFile_filter);
403 if (files.Length > 0)
406 Logger.VerboseDebug("{0} Existing world chunk shards", files.Length);
409 foreach (var shardFile in files)
412 if (shardFile.Length < 1024) continue;
413 var result = chunkShardRegex.Match(shardFile.Name);
416 int X_chunk_pos = int.Parse(result.Groups["X"].Value);
417 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value);
421 using (var fileStream = shardFile.OpenRead())
424 PngReader pngRead = new PngReader(fileStream);
425 pngRead.ReadSkippingAllRows();
427 //Parse PNG chunks for METADATA in shard
428 PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
430 chunkTopMetadata.Add(metadataFromPng.ChunkMetadata);
434 catch (PngjException someEx)
436 Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx);
449 Logger.VerboseDebug("Could not open world map directory");
457 private PngWriter SetupPngImage(Vec2i coord, ColumnMeta metadata)
459 ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
461 string filename = $"{coord.X}_{coord.Y}.png";
462 filename = Path.Combine(path, filename);
464 PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true);
465 PngMetadata meta = pngWriter.GetMetadata();
467 meta.SetText("Chunk_X", coord.X.ToString("D"));
468 meta.SetText("Chunk_Y", coord.Y.ToString("D"));
469 //Setup specialized meta-data PNG chunks here...
470 PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo)
472 ChunkMetadata = metadata
474 pngWriter.GetChunksList().Queue(pngChunkMeta);
475 pngWriter.CompLevel = 9;// 9 is the maximum compression
476 pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman;
482 /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats...
484 /// <param name="key">Chunk Coordinate</param>
485 /// <param name="mapChunk">Map chunk.</param>
486 /// <param name="chunkMeta">Chunk metadata</param>
487 private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ColumnMeta chunkMeta)
490 int targetChunkY = mapChunk.YMax / chunkSize;//Surface ...
491 for (; targetChunkY > 0; targetChunkY--)
493 WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk;
494 if (chunkData == null || chunkData.BlockEntities == null)
497 Logger.VerboseDebug("Chunk null or empty X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y);
502 /*************** Chunk Entities Scanning *********************/
503 if (chunkData.BlockEntities != null && chunkData.BlockEntities.Length > 0)
506 Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Length);
509 foreach (var blockEnt in chunkData.BlockEntities)
512 if (blockEnt != null && blockEnt.Block != null && BlockID_Designators.ContainsKey(blockEnt.Block.BlockId))
514 var designator = BlockID_Designators[blockEnt.Block.BlockId];
515 designator.SpecialAction(ClientAPI, POIs, blockEnt.Pos.Copy(), blockEnt.Block);
520 /********************* Chunk/Column BLOCKs scanning ****************/
521 //Heightmap, Stats, block tally
524 int X_index, Y_index, Z_index;
525 X_index = Y_index = Z_index = 0;
533 /* Encode packed indicie
534 (y * chunksize + z) * chunksize + x
536 var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index);
537 int aBlockId = chunkData.Blocks[indicie];
541 chunkMeta.AirBlocks++;
545 if (RockIdCodes.ContainsKey(aBlockId))
547 if (chunkMeta.RockRatio.ContainsKey(aBlockId)) { chunkMeta.RockRatio[aBlockId]++; } else { chunkMeta.RockRatio.Add(aBlockId, 1); }
550 chunkMeta.NonAirBlocks++;
553 //if (chunkMeta.HeightMap[X_index, Z_index] == 0)
555 // chunkMeta.HeightMap[X_index, Z_index]
556 // = (ushort) (Y_index + (targetChunkY * chunkSize));
559 while (X_index++ < (chunkSize - 1));
562 while (Z_index++ < (chunkSize - 1));
565 while (Y_index++ < (chunkSize - 1));
570 private void UpdateEntityMetadata()
572 Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
573 //Mabey scan only for 'new' entities by tracking ID in set?
574 foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToList())
578 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<");
581 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
582 if (dMatch.Value != null)
584 Logger.Chat("Entity designator hit!");
585 dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.LocalPos.AsBlockPos.Copy(), loadedEntity.Value);
593 private void AddNote(string notation)
595 var playerNodePoi = new PointOfInterest()
597 Location = ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Copy(),
599 Timestamp = DateTimeOffset.UtcNow,
602 this.POIs.AddReplace(playerNodePoi);
607 private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data)
609 Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken());
611 CommandData cmdData = data as CommandData;
614 if (CurrentState != RunState.Snapshot)
616 switch (cmdData.State)
619 CurrentState = cmdData.State;
620 AwakenCartographer(0.0f);
624 CurrentState = cmdData.State;
627 case RunState.Snapshot:
628 CurrentState = RunState.Stop;
629 //Snapshot starts a second thread/process...
633 case RunState.Notation:
634 //Add to POI list where player location
635 AddNote(cmdData.Notation);
641 if (CurrentState != cmdData.State)
643 CurrentState = cmdData.State;
644 AwakenCartographer(0.0f);
648 Logger.VerboseDebug($"Automap commanded to: {cmdData.State} ");