2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
9 using System.Text.RegularExpressions;
10 using System.Threading;
14 using Hjg.Pngcs.Chunks;
16 using Vintagestory.API.Client;
17 using Vintagestory.API.Common;
18 using Vintagestory.API.Common.Entities;
19 using Vintagestory.API.MathTools;
20 using Vintagestory.Common;
24 public class AutomapSystem
26 private Thread cartographer_thread;
27 private ICoreClientAPI ClientAPI { get; set; }
28 private ILogger Logger { get; set; }
29 private IChunkRenderer ChunkRenderer { get; set; }
31 private const string _mapPath = @"Maps";
32 private const string _chunkPath = @"Chunks";
33 private const string _domain = @"automap";
34 private const string chunkFile_filter = @"*_*.png";
35 private static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+).png", RegexOptions.Singleline);
37 private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150 );
38 private ColumnsMetadata chunkTopMetadata;
39 private PointsOfInterest POIs;
40 private EntitiesOfInterest EOIs;
42 internal Dictionary<int, BlockDesignator> BlockID_Designators { get; private set;}
43 internal Dictionary<AssetLocation, EntityDesignator> Entity_Designators { get; private set; }
45 internal bool Enabled { get; set; }
46 //Run status, Chunks processed, stats, center of map....
47 internal uint nullChunkCount, updatedChunksTotal;
48 internal Vec2i startChunkColumn;
50 private readonly int chunkSize;
52 private IAsset stylesFile;
55 public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger)
57 this.ClientAPI = clientAPI;
59 chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
60 ClientAPI.Event.LevelFinalize += EngageAutomap;
62 //TODO:Choose which one from GUI
63 this.ChunkRenderer = new StandardRenderer(clientAPI, logger);
68 private void EngageAutomap( )
70 path = ClientAPI.GetOrCreateDataPath(_mapPath);
71 path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName'
73 stylesFile = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap_format.css"));
74 Logger.VerboseDebug("CSS loaded: {0} size: {1}",stylesFile.IsLoaded() ,stylesFile.ToText( ).Length);
76 Prefill_POI_Designators( );
77 startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Z / chunkSize));
78 chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
80 Logger.Notification("AUTOMAP Start {0}", startChunkColumn);
83 ClientAPI.Event.ChunkDirty += ChunkAChanging;
85 cartographer_thread = new Thread(Cartographer);
86 cartographer_thread.Name = "Cartographer";
87 cartographer_thread.Priority = ThreadPriority.Lowest;
88 cartographer_thread.IsBackground = true;
90 ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000);
93 private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
95 Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
97 columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
100 private void AwakenCartographer(float delayed)
103 if (Enabled && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true)) {
105 Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState);
108 if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted)) {
109 cartographer_thread.Start( );
111 else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin)) {
112 //Time to (re)write chunk shards
113 cartographer_thread.Interrupt( );
116 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})");
123 private void Cartographer( )
126 Logger.VerboseDebug("Cartographer thread awoken");
129 uint ejectedItem = 0;
130 uint updatedChunks = 0;
132 //-- Should dodge enumerator changing underfoot....at a cost.
133 if (!columnCounter.IsEmpty) {
134 var tempSet = columnCounter.ToArray( ).OrderByDescending(kvp => kvp.Value);
135 foreach (var mostActiveCol in tempSet) {
137 var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key);
139 if (mapChunk == null) {
140 Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
142 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem );
146 ColumnMeta chunkMeta = CreateColumnMetadata(mostActiveCol,mapChunk);
147 PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, chunkMeta);
148 UpdateEntityMetadata( );
149 ProcessChunkBlocks(mostActiveCol.Key, mapChunk, chunkMeta);
151 uint updatedPixels = 0;
153 ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter , out updatedPixels);
155 if (updatedPixels > 0) {
158 Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
161 chunkTopMetadata.Update(chunkMeta);
162 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
165 columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
166 Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key);
172 if (updatedChunks > 0) {
173 //What about chunk updates themselves; a update bitmap isn't kept...
174 updatedChunksTotal += updatedChunks;
179 //Then sleep until interupted again, and repeat
181 Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
183 Thread.Sleep(Timeout.Infinite);
185 } catch (ThreadInterruptedException) {
187 Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name);
190 } catch (ThreadAbortException) {
191 Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name);
194 Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name);
198 private void Prefill_POI_Designators( )
200 this.POIs = new PointsOfInterest( );
201 this.EOIs = new EntitiesOfInterest( );
202 this.BlockID_Designators = new Dictionary<int, BlockDesignator>( );
203 this.Entity_Designators = new Dictionary<AssetLocation, EntityDesignator>( );
205 //Add special marker types for BlockID's of "Interest", overwrite colour, and method
207 Install_POI_Designators(DefaultDesignators.DefaultBlockDesignators(), DefaultDesignators.DefaultEntityDesignators());
210 private void Install_POI_Designators(ICollection<BlockDesignator> blockDesig, List<EntityDesignator> entDesig)
212 Logger.VerboseDebug("Connecting {0} standard Block-Designators", blockDesig.Count);
213 foreach (var designator in blockDesig) {
214 var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material);
215 if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString( ), blockIDs.Count); }
216 foreach (var entry in blockIDs) {
217 BlockID_Designators.Add(entry.Key, designator);
220 this.ChunkRenderer.BlockID_Designators = BlockID_Designators;
223 Logger.VerboseDebug("Connecting {0} standard Entity-Designators", entDesig.Count);
224 foreach (var designator in entDesig) {
225 //Get Variants first, from EntityTypes...better be populated!
226 var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path));
228 foreach (var match in matched) {
229 Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator);
230 this.Entity_Designators.Add(match.Code, designator);
235 //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern);
241 //TODO: Convert to RAZOR model
242 private void GenerateMapHTML( )
244 string mapFilename = Path.Combine(path, "Automap.html");
246 int TopNorth = chunkTopMetadata.North_mostChunk;
247 int TopSouth = chunkTopMetadata.South_mostChunk;
248 int TopEast = chunkTopMetadata.East_mostChunk;
249 int TopWest = chunkTopMetadata.West_mostChunk;
251 using (StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))) {
252 using (HtmlTextWriter tableWriter = new HtmlTextWriter(outputText)) {
253 tableWriter.BeginRender( );
254 tableWriter.RenderBeginTag(HtmlTextWriterTag.Html);
256 tableWriter.RenderBeginTag(HtmlTextWriterTag.Head);
257 tableWriter.RenderBeginTag(HtmlTextWriterTag.Title);
258 tableWriter.WriteEncodedText("Generated Automap");
259 tableWriter.RenderEndTag( );
261 tableWriter.RenderBeginTag(HtmlTextWriterTag.Style);
262 tableWriter.Write(stylesFile.ToText( ));
263 tableWriter.RenderEndTag( );//</style>
265 //## JSON map-state data ######################
266 tableWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
267 tableWriter.RenderBeginTag(HtmlTextWriterTag.Script);
269 tableWriter.Write("var available_images = [");
271 foreach (var shard in this.chunkTopMetadata) {
272 tableWriter.Write("{{X:{0},Y:{1} }}, ", shard.Location.X, shard.Location.Y);
275 tableWriter.Write(" ];\n");
277 tableWriter.RenderEndTag( );
279 tableWriter.RenderEndTag( );
281 tableWriter.RenderBeginTag(HtmlTextWriterTag.Body);
282 tableWriter.RenderBeginTag(HtmlTextWriterTag.P);
283 tableWriter.WriteEncodedText($"Created {DateTimeOffset.UtcNow.ToString("u")}");
284 tableWriter.RenderEndTag( );
285 tableWriter.RenderBeginTag(HtmlTextWriterTag.P);
286 tableWriter.WriteEncodedText($"W:{TopWest}, E: {TopEast}, N:{TopNorth}, S:{TopSouth} ");
287 tableWriter.RenderEndTag( );
288 tableWriter.WriteLine( );
289 tableWriter.RenderBeginTag(HtmlTextWriterTag.Table);
290 tableWriter.RenderBeginTag(HtmlTextWriterTag.Caption);
291 tableWriter.WriteEncodedText($"Start: {startChunkColumn}, Seed: {ClientAPI.World.Seed}\n");
292 tableWriter.RenderEndTag( );
294 //################ X-Axis <thead> #######################
295 tableWriter.RenderBeginTag(HtmlTextWriterTag.Thead);
296 tableWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
298 tableWriter.RenderBeginTag(HtmlTextWriterTag.Th);
299 tableWriter.Write("N, W");
300 tableWriter.RenderEndTag( );
302 for (int xAxisT = TopWest; xAxisT <= TopEast; xAxisT++) {
303 tableWriter.RenderBeginTag(HtmlTextWriterTag.Th);
304 tableWriter.Write(xAxisT);
305 tableWriter.RenderEndTag( );
308 tableWriter.RenderBeginTag(HtmlTextWriterTag.Th);
309 tableWriter.Write("N, E");
310 tableWriter.RenderEndTag( );
312 tableWriter.RenderEndTag( );
313 tableWriter.RenderEndTag( );
314 //###### </thead> ################################
316 //###### <tbody> - Chunk rows & Y-axis cols
317 tableWriter.RenderBeginTag(HtmlTextWriterTag.Tbody);
319 //######## <tr> for every vertical row
320 for (int yAxis = TopNorth; yAxis <= TopSouth; yAxis++) {
321 tableWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
322 tableWriter.RenderBeginTag(HtmlTextWriterTag.Td);
323 tableWriter.Write(yAxis);//legend: Y-axis
324 tableWriter.RenderEndTag( );
326 for (int xAxis = TopWest; xAxis <= TopEast; xAxis++) {
327 //###### <td> #### for chunk shard
328 tableWriter.RenderBeginTag(HtmlTextWriterTag.Td);
329 var colLoc = new Vec2i(xAxis, yAxis);
330 if (chunkTopMetadata.Contains( colLoc)){
331 ColumnMeta meta = chunkTopMetadata[colLoc];
333 tableWriter.AddAttribute(HtmlTextWriterAttribute.Class, "tooltip");
334 tableWriter.RenderBeginTag(HtmlTextWriterTag.Div);
336 tableWriter.AddAttribute(HtmlTextWriterAttribute.Src, $"{xAxis}_{yAxis}.png");
337 tableWriter.RenderBeginTag(HtmlTextWriterTag.Img);
338 tableWriter.RenderEndTag( );
339 // <span class="tooltiptext">Tooltip text
340 tableWriter.AddAttribute(HtmlTextWriterAttribute.Class, "tooltiptext");
341 tableWriter.RenderBeginTag(HtmlTextWriterTag.Span);
343 StringBuilder tooltipText = new StringBuilder( );
344 tooltipText.Append($"{meta.Location.PrettyCoords(ClientAPI)} ");
345 tooltipText.Append($" Max-Height: {meta.YMax}, Temp: {meta.Temperature.ToString("F1")} " );
346 tooltipText.Append($" Rainfall: {meta.Rainfall.ToString("F1")}, ");
347 tooltipText.Append($" Shrubs: {meta.ShrubDensity.ToString("F1")}, ");
348 tooltipText.Append($" Forest: {meta.ForestDensity.ToString("F1")}, ");
349 tooltipText.Append($" Fertility: {meta.Fertility.ToString("F1")}, ");
351 if (meta.RockRatio != null) {
352 foreach (KeyValuePair<int, uint> blockID in meta.RockRatio) {
353 var block = ClientAPI.World.GetBlock(blockID.Key);
354 tooltipText.AppendFormat(" {0} × {1},\t", block.Code.GetName( ), meta.RockRatio[blockID.Key]);
358 tableWriter.WriteEncodedText(tooltipText.ToString() );
360 tableWriter.RenderEndTag( );//</span>
363 tableWriter.RenderEndTag( );//</div> --tooltip enclosure
366 tableWriter.Write("?");
369 tableWriter.RenderEndTag( );
370 }//############ </td> ###########
372 tableWriter.RenderBeginTag(HtmlTextWriterTag.Td);
373 tableWriter.Write(yAxis);//legend: Y-axis
374 tableWriter.RenderEndTag( );
376 tableWriter.RenderEndTag( );
379 tableWriter.RenderEndTag( );
381 //################ X-Axis <tfoot> #######################
382 tableWriter.RenderBeginTag(HtmlTextWriterTag.Tfoot);
383 tableWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
385 tableWriter.RenderBeginTag(HtmlTextWriterTag.Td);
386 tableWriter.Write("S, W");
387 tableWriter.RenderEndTag( );
389 for (int xAxisB = TopWest; xAxisB <= TopEast; xAxisB++) {
390 tableWriter.RenderBeginTag(HtmlTextWriterTag.Td);
391 tableWriter.Write(xAxisB);
392 tableWriter.RenderEndTag( );
395 tableWriter.RenderBeginTag(HtmlTextWriterTag.Td);
396 tableWriter.Write("S, E");
397 tableWriter.RenderEndTag( );
399 tableWriter.RenderEndTag( );
400 tableWriter.RenderEndTag( );
401 //###### </tfoot> ################################
404 tableWriter.RenderEndTag( );//</table>
406 //############## POI list #####################
407 tableWriter.RenderBeginTag(HtmlTextWriterTag.P);
408 tableWriter.WriteLine("Points of Interest");
409 tableWriter.RenderEndTag( );
410 tableWriter.RenderBeginTag(HtmlTextWriterTag.Ul);
411 foreach (var poi in this.POIs) {
412 tableWriter.RenderBeginTag(HtmlTextWriterTag.Li);
413 tableWriter.WriteEncodedText(poi.Location.PrettyCoords(this.ClientAPI)+"\t");
414 tableWriter.WriteEncodedText(poi.Notes+ "\t");
415 tableWriter.WriteEncodedText(poi.Timestamp.ToString("u"));
416 tableWriter.RenderEndTag( );
419 foreach (var eoi in this.EOIs.PointsList) {
420 tableWriter.RenderBeginTag(HtmlTextWriterTag.Li);
421 tableWriter.WriteEncodedText(eoi.Location.PrettyCoords(this.ClientAPI)+ "\t");
422 tableWriter.WriteEncodedText(eoi.Notes+ "\t");
423 tableWriter.WriteEncodedText(eoi.Timestamp.ToString("u") );
424 tableWriter.RenderEndTag( );
427 tableWriter.RenderEndTag( );
432 tableWriter.RenderEndTag( );//### </BODY> ###
434 tableWriter.EndRender( );
435 tableWriter.Flush( );
440 Logger.VerboseDebug("Generated HTML map");
445 private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
447 ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), chunkSize);
448 BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
450 mostActiveCol.Key.Y * chunkSize);
452 var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP);
453 data.UpdateFieldsFrom(climate, mapChunk,TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours));
459 /// Reload chunk bounds from chunk shards
461 /// <returns>The metadata.</returns>
462 private void Reload_Metadata( )
464 var worldmapDir = new DirectoryInfo(path);
466 if (worldmapDir.Exists) {
468 var files = worldmapDir.GetFiles(chunkFile_filter);
470 if (files.Length > 0) {
472 Logger.VerboseDebug("{0} Existing world chunk shards", files.Length);
477 foreach (var shardFile in files) {
479 if (shardFile.Length < 1000) continue;
480 var result = chunkShardRegex.Match(shardFile.Name);
481 if (result.Success) {
482 int X_chunk_pos = int.Parse(result.Groups["X"].Value );
483 int Z_chunk_pos = int.Parse(result.Groups["Z"].Value );
485 //Parse PNG chunks for METADATA in shard
486 using (var fileStream = shardFile.OpenRead( ))
488 PngReader pngRead = new PngReader(fileStream );
489 pngRead.ReadSkippingAllRows( );
492 PngMetadataChunk metadataFromPng = pngRead.GetChunksList( ).GetById1(PngMetadataChunk.ID) as PngMetadataChunk;
494 chunkTopMetadata.Add(metadataFromPng.ChunkMetadata);
504 Logger.VerboseDebug("Could not open world map directory");
512 private PngWriter SetupPngImage(Vec2i coord, ColumnMeta metadata)
514 ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false);
516 string filename = $"{coord.X}_{coord.Y}.png";
517 filename = Path.Combine(path, filename);
519 PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true);
520 PngMetadata meta = pngWriter.GetMetadata( );
522 meta.SetText("Chunk_X", coord.X.ToString("D"));
523 meta.SetText("Chunk_Y", coord.Y.ToString("D"));
524 //Setup specialized meta-data PNG chunks here...
525 PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo);
526 pngChunkMeta.ChunkMetadata = metadata;
527 pngWriter.GetChunksList( ).Queue(pngChunkMeta);
533 /// Does the heavy lifting of Scanning columns of chunks - creates Heightmap and Processes POIs, Entities, 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, ColumnMeta chunkMeta)
540 //TODO: build stack of chunk(s) - surface down to bedrock
541 int topChunkY = mapChunk.YMax / chunkSize;
542 WorldChunk chunkData = ( Vintagestory.Common.WorldChunk )ClientAPI.World.BlockAccessor.GetChunk(key.X, topChunkY, key.Y);
544 if (chunkData.BlockEntities != null && chunkData.BlockEntities.Length > 0) {
546 Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Length);
548 foreach (var blockEnt in chunkData.BlockEntities) {
549 if (BlockID_Designators.ContainsKey(blockEnt.Block.BlockId))
551 var designator = BlockID_Designators[blockEnt.Block.BlockId];
552 designator.SpecialAction(ClientAPI, POIs, blockEnt.Pos.Copy(), blockEnt.Block);
560 private void UpdateEntityMetadata( )
562 Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count);
563 //Mabey scan only for 'new' entities by tracking ID in set?
564 foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToList()) {
567 //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<");
570 var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code));
571 if (dMatch.Value != null) {
572 if (dMatch.Value.Enabled) {
573 dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.LocalPos.AsBlockPos.Copy( ), loadedEntity.Value);