OSDN Git Service

Events should attched after config data is loaded...
[automap/automap.git] / Automap / Subsystems / AutomapSystem.cs
index f61fef5..04db4c2 100644 (file)
@@ -33,6 +33,7 @@ namespace Automap
 
                internal const string _mapPath = @"Maps";
                internal const string _chunkPath = @"Chunks";
+               internal const uint editThreshold = 1;
                private const string _domain = @"automap";
                private const string chunkFile_filter = @"*_*.png";
                private const string poiFileName = @"poi_binary";
@@ -40,7 +41,7 @@ namespace Automap
                private const string pointsTsvFileName = @"points_of_interest.tsv";
                private static Regex chunkShardRegex = new Regex(@"(?<X>[\d]+)_(?<Z>[\d]+)\.png", RegexOptions.Singleline);
 
-               private ConcurrentDictionary<Vec2i, uint> columnCounter = new ConcurrentDictionary<Vec2i, uint>(3, 150);
+               private ConcurrentDictionary<Vec2i, ColumnCounter> columnCounters = new ConcurrentDictionary<Vec2i, ColumnCounter>(3, 150);
                private ColumnsMetadata chunkTopMetadata;
                private PointsOfInterest POIs = new PointsOfInterest();
                private EntitiesOfInterest EOIs = new EntitiesOfInterest();
@@ -69,11 +70,11 @@ namespace Automap
                        this.ClientAPI = clientAPI;
                        this.Logger = logger;
                        chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
-                       ClientAPI.Event.LevelFinalize += EngageAutomap;
+
                        configuration = config;
+                       ClientAPI.Event.LevelFinalize += EngageAutomap;
 
-                       //TODO:Choose which one from GUI 
-                       this.ChunkRenderer = new StandardRenderer(clientAPI, logger, this.configuration.SeasonalColors);
+                       this.ChunkRenderer = InstantiateChosenRenderer(config.RendererName);
 
                        //Listen on bus for commands
                        ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey);
@@ -132,14 +133,14 @@ namespace Automap
 
                private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason)
                {
-               Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);
-
-               //TODO: Track Y Chunk - Column, surface chunks being more important
-               //Only NEW/LOADED chunks unless edits > N 
-               //if (reason == EnumChunkDirtyReason.NewlyCreated || reason == EnumChunkDirtyReason.NewlyLoaded)
-               //{
-               columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1);
-               //}
+               Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z);              
+               bool newOrEdit = (reason == EnumChunkDirtyReason.NewlyCreated || reason == EnumChunkDirtyReason.NewlyLoaded);
+               
+               columnCounters.AddOrUpdate(topPosition, 
+                                             new ColumnCounter(chunkSize, newOrEdit, chunkCoord), 
+                                             (chkPos, chkChng) => chkChng.Update(chunkCoord, chunkSize, newOrEdit)
+                                            );
+               
                }
 
                private void AwakenCartographer(float delayed)
@@ -185,14 +186,14 @@ namespace Automap
 
                        try
                        {
-                               uint ejectedItem = 0;
+                               ColumnCounter ejectedItem ;
                                uint updatedChunks = 0;
                                uint updatedPixels = 0;
 
                                //-- Should dodge enumerator changing underfoot....at a cost.
-                               if (!columnCounter.IsEmpty)
+                               if (!columnCounters.IsEmpty)
                                {
-                                       var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value);
+                                       var tempSet = columnCounters.ToArray().Where(cks => cks.Value.WeightedSum > editThreshold) .OrderByDescending(kvp => kvp.Value.WeightedSum);
                                        UpdateEntityMetadata();
 
                                        foreach (var mostActiveCol in tempSet)
@@ -204,7 +205,7 @@ namespace Automap
                                                        //TODO: REVISIT THIS CHUNK!
                                                        Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key);
                                                        nullMapCount++;
-                                                       columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
+                                                       columnCounters.TryRemove(mostActiveCol.Key, out ejectedItem);
                                                        continue;
                                                }
 
@@ -231,15 +232,15 @@ namespace Automap
                                                if (updatedPixels > 0)
                                                {
                                                        #if DEBUG
-                                                       Logger.VerboseDebug("Wrote top-chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
+                                                       Logger.VerboseDebug("Wrote top-chunk shard: ({0}) - Weight:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels);
                                                        #endif
                                                        updatedChunks++;
                                                        chunkTopMetadata.Update(chunkMeta);
-                                                       columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
+                                                       columnCounters.TryRemove(mostActiveCol.Key, out ejectedItem);
                                                }
                                                else
                                                {
-                                                       columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem);
+                                                       columnCounters.TryRemove(mostActiveCol.Key, out ejectedItem);
                                                        #if DEBUG
                                                        Logger.VerboseDebug("Un-painted chunk shard: ({0}) ", mostActiveCol.Key);
                                                        #endif
@@ -260,6 +261,12 @@ namespace Automap
                                        chunkTopMetadata.ClearMetadata( );
                                }
 
+                               #if DEBUG
+                               Logger.VerboseDebug("Clearing Column Counters of: {0} non-written shards", columnCounters.Count);
+                               #endif
+
+                               columnCounters.Clear( );
+
                                //Then sleep until interupted again, and repeat
 #if DEBUG
                                Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name);
@@ -440,7 +447,7 @@ namespace Automap
 
                }
 
-               private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, uint> mostActiveCol, IMapChunk mapChunk)
+               private ColumnMeta CreateColumnMetadata(KeyValuePair<Vec2i, ColumnCounter> mostActiveCol, IMapChunk mapChunk)
                {
                        ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize, (ClientAPI.World.BlockAccessor.MapSizeY / chunkSize));
                        BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize,
@@ -716,12 +723,32 @@ namespace Automap
                                        AddNote(cmdData.Notation);
                                        break;
                        }
-#if DEBUG
+                       #if DEBUG
                        ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} ");
-#endif
+                       #endif
                }
                #endregion
 
+               private AChunkRenderer InstantiateChosenRenderer(string rendererName )
+               {
+               Logger.VerboseDebug("Using '{0}' style Shard Renderer", rendererName);
+               switch (rendererName) 
+               {                               
+               case StandardRenderer.Name:
+                       return new StandardRenderer(ClientAPI, Logger, this.configuration.SeasonalColors);
+               
+               case AlternateRenderer.Name:
+                       return new AlternateRenderer(ClientAPI, Logger, this.configuration.SeasonalColors);
+       
+               case FlatRenderer.Name:
+                       return new FlatRenderer(ClientAPI, Logger, this.configuration.SeasonalColors);  
+
+               default:
+                       throw new ArgumentOutOfRangeException("rendererName",rendererName,"That value isn't supported or known...");
+               }
+
+               return null;
+               }
        }
 
 }