source: binary-improvements/7dtd-server-fixes/src/MapRendering/MapRendering.cs@ 145

Last change on this file since 145 was 144, checked in by alloc, 10 years ago

Fixes

File size: 9.2 KB
RevLine 
[130]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading;
5using UnityEngine;
6
7namespace AllocsFixes.MapRendering
8{
9 public class MapRendering
10 {
11 private static MapRendering instance;
12
13 public static MapRendering Instance {
14 get {
15 if (instance == null) {
16 instance = new MapRendering ();
17 }
18 return instance;
19 }
20 }
21
22 private MapRenderBlockBuffer[] zoomLevelBuffers = new MapRenderBlockBuffer[Constants.ZOOMLEVELS];
[143]23 private Dictionary<Vector2i, Color[]> dirtyChunks = new Dictionary<Vector2i, Color[]> ();
[132]24 private System.Timers.Timer chunkSaveTimer = new System.Timers.Timer (500);
[130]25 private bool renderingFullMap = false;
[140]26 public static bool renderingEnabled = true;
[143]27 private MicroStopwatch msw = new MicroStopwatch ();
[130]28
29 private MapRendering ()
30 {
31 Constants.MAP_DIRECTORY = StaticDirectories.GetSaveGameDir () + "/map";
32
33 for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
34 zoomLevelBuffers [i] = new MapRenderBlockBuffer (i);
35 }
36
37 chunkSaveTimer.AutoReset = false;
[144]38 chunkSaveTimer.Elapsed += new System.Timers.ElapsedEventHandler (TimedRendering);
[130]39 }
40
41 public static void RenderSingleChunk (Chunk chunk)
42 {
[140]43 if (renderingEnabled) {
44 ThreadPool.QueueUserWorkItem ((o) =>
45 {
46 try {
47 if (!Instance.renderingFullMap) {
[143]48 Monitor.Enter (Instance.zoomLevelBuffers);
49 try {
50 Chunk c = (Chunk)o;
51 Vector3i cPos = c.GetWorldPos ();
52 Vector2i cPos2 = new Vector2i (cPos.x / Constants.MAP_CHUNK_SIZE, cPos.z / Constants.MAP_CHUNK_SIZE);
53
54 ushort[] mapColors = c.GetMapColors ();
55 if (mapColors != null) {
56 Color[] realColors = new Color[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
57 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) {
58 realColors [i_colors] = shortColorToColor (mapColors [i_colors]);
59 }
60 Instance.dirtyChunks [cPos2] = realColors;
61 Log.Out ("Add Dirty: " + cPos2);
62 Instance.chunkSaveTimer.Stop ();
63 Instance.chunkSaveTimer.Start ();
64 }
65 } finally {
66 Monitor.Exit (Instance.zoomLevelBuffers);
67 }
[140]68 }
69 } catch (Exception e) {
70 Log.Out ("Exception in MapRendering.RenderSingleChunk(): " + e);
[130]71 }
[140]72 }, chunk);
73 }
[130]74 }
75
76 public void RenderFullMap ()
77 {
78 MicroStopwatch microStopwatch = new MicroStopwatch ();
79
[132]80 string regionSaveDir = StaticDirectories.GetSaveGameRegionDir ();
[143]81 RegionFileManager rfm = new RegionFileManager (regionSaveDir, regionSaveDir, 0, false);
82 Texture2D fullMapTexture = null;
[132]83
[130]84 if (Directory.Exists (Constants.MAP_DIRECTORY))
85 Directory.Delete (Constants.MAP_DIRECTORY, true);
86
87 renderingFullMap = true;
88
89 Vector2i minChunk, maxChunk;
90 Vector2i minPos, maxPos;
91 int widthChunks, heightChunks, widthPix, heightPix;
[143]92 getWorldExtent (rfm, out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks, out widthPix, out heightPix);
[130]93
[135]94 Log.Out (String.Format ("RenderMap: min: {0}, max: {1}, minPos: {2}, maxPos: {3}, w/h: {4}/{5}, wP/hP: {6}/{7}",
95 minChunk.ToString (), maxChunk.ToString (),
96 minPos.ToString (), maxPos.ToString (),
97 widthChunks, heightChunks,
[136]98 widthPix, heightPix)
99 );
[135]100
[136]101 if (widthPix <= 8000 && heightPix <= 8000)
102 fullMapTexture = new Texture2D (widthPix, heightPix);
[130]103
[143]104 Monitor.Enter (Instance.zoomLevelBuffers);
105 try {
106 Vector2i curFullMapPos;
107 Vector2i curChunkPos;
108 for (curFullMapPos.x = 0; curFullMapPos.x < widthPix; curFullMapPos.x += Constants.MAP_CHUNK_SIZE) {
109 for (curFullMapPos.y = 0; curFullMapPos.y < heightPix; curFullMapPos.y += Constants.MAP_CHUNK_SIZE) {
110 curChunkPos.x = (curFullMapPos.x / Constants.MAP_CHUNK_SIZE) + minChunk.x;
111 curChunkPos.y = (curFullMapPos.y / Constants.MAP_CHUNK_SIZE) + minChunk.y;
[130]112
[143]113 try {
114 long chunkKey = WorldChunkCache.MakeChunkKey (curChunkPos.x, curChunkPos.y);
115 if (rfm.ContainsChunkSync (chunkKey)) {
116 Chunk c = rfm.GetChunkSync (chunkKey);
117 ushort[] mapColors = c.GetMapColors ();
118 if (mapColors != null) {
119 Color[] realColors = new Color[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];
120 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) {
121 realColors [i_colors] = shortColorToColor (mapColors [i_colors]);
122 }
123 dirtyChunks [curChunkPos] = realColors;
124 if (fullMapTexture != null)
125 fullMapTexture.SetPixels (curFullMapPos.x, curFullMapPos.y, Constants.MAP_CHUNK_SIZE, Constants.MAP_CHUNK_SIZE, realColors);
126 }
127 }
128 } catch (Exception e) {
129 Log.Out ("Exception: " + e);
130 }
131 }
[144]132
133 while (dirtyChunks.Count > 0) {
134 RenderDirtyChunks ();
135 }
136
[143]137 Log.Out (String.Format ("RenderMap: {0}/{1} ({2}%)", curFullMapPos.x, widthPix, (int)((float)curFullMapPos.x / widthPix * 100)));
[130]138 }
[143]139 } finally {
140 Monitor.Exit (Instance.zoomLevelBuffers);
[130]141 }
142
[136]143 if (fullMapTexture != null) {
144 byte[] array = fullMapTexture.EncodeToPNG ();
145 File.WriteAllBytes (Constants.MAP_DIRECTORY + "/map.png", array);
146 Texture2D.Destroy (fullMapTexture);
147 fullMapTexture = null;
148 }
[130]149
150 renderingFullMap = false;
151
152 Log.Out ("Generating map took: " + microStopwatch.ElapsedMilliseconds + " ms");
153 Log.Out ("World extent: " + minPos + " - " + maxPos);
154 }
155
156 private void SaveAllBlockMaps (object source, System.Timers.ElapsedEventArgs e)
157 {
[143]158 for (int i = 0; i < Constants.ZOOMLEVELS; i++) {
159 zoomLevelBuffers [i].SaveBlock ();
[130]160 }
161 }
162
[144]163 private void TimedRendering (object source, System.Timers.ElapsedEventArgs e)
[130]164 {
165 Monitor.Enter (zoomLevelBuffers);
166 try {
[144]167 RenderDirtyChunks ();
168 if (dirtyChunks.Count > 0)
169 Instance.chunkSaveTimer.Start ();
170 } finally {
171 Monitor.Exit (zoomLevelBuffers);
172 }
173 }
[143]174
[144]175 private void RenderDirtyChunks ()
176 {
177 msw.ResetAndRestart ();
[143]178
[144]179 if (dirtyChunks.Count > 0) {
180 List<Vector2i> keys = new List<Vector2i> (dirtyChunks.Keys);
181 List<Vector2i> chunksDone = new List<Vector2i> ();
[143]182
[144]183 Vector2i chunkPos = keys [0];
184 chunksDone.Add (chunkPos);
[143]185
[144]186 //Log.Out ("Start Dirty: " + chunkPos);
[130]187
[144]188 Vector2i block, blockOffset;
189 getBlockNumber (chunkPos, out block, out blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE);
[143]190
[144]191 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block);
192
193 Vector2i v_block, v_blockOffset;
194 foreach (Vector2i v in keys) {
195 getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MAP_CHUNK_SIZE);
196 if (v_block.Equals (block)) {
197 //Log.Out ("Dirty: " + v + " render: true");
198 chunksDone.Add (v);
199 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]);
200 } else {
201 //Log.Out ("Dirty: " + v + " render: false");
[130]202 }
[144]203 }
[130]204
[144]205 foreach (Vector2i v in chunksDone)
206 dirtyChunks.Remove (v);
[130]207
[144]208 RenderZoomLevel (Constants.ZOOMLEVELS - 1, block);
[130]209
[144]210 SaveAllBlockMaps (null, null);
[130]211 }
212 }
213
214 private void RenderZoomLevel (int level, Vector2i innerBlock)
215 {
216 if (level > 0) {
217 Vector2i block, blockOffset;
[135]218 getBlockNumber (innerBlock, out block, out blockOffset, 2, Constants.MAP_BLOCK_SIZE / 2);
[130]219
[135]220 zoomLevelBuffers [level - 1].LoadBlock (block);
221 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaled ());
[130]222
223 RenderZoomLevel (level - 1, block);
224 }
225 }
226
227 private void getBlockNumber (Vector2i innerPos, out Vector2i block, out Vector2i blockOffset, int scaleFactor, int offsetSize)
228 {
229 block.x = ((innerPos.x + 16777216) / scaleFactor) - (16777216 / scaleFactor);
230 block.y = ((innerPos.y + 16777216) / scaleFactor) - (16777216 / scaleFactor);
231 blockOffset.x = ((innerPos.x + 16777216) % scaleFactor) * offsetSize;
232 blockOffset.y = ((innerPos.y + 16777216) % scaleFactor) * offsetSize;
233 }
234
[143]235 private void getWorldExtent (RegionFileManager rfm, out Vector2i minChunk, out Vector2i maxChunk,
[130]236 out Vector2i minPos, out Vector2i maxPos,
237 out int widthChunks, out int heightChunks,
238 out int widthPix, out int heightPix)
239 {
240 long[] keys = rfm.GetAllChunkKeys ();
241 int minX = Int32.MaxValue;
242 int minY = Int32.MaxValue;
243 int maxX = Int32.MinValue;
244 int maxY = Int32.MinValue;
245 foreach (long key in keys) {
246 int x = WorldChunkCache.extractX (key);
247 int y = WorldChunkCache.extractZ (key);
248
249 if (x < minX)
250 minX = x;
251 if (x > maxX)
252 maxX = x;
253 if (y < minY)
254 minY = y;
255 if (y > maxY)
256 maxY = y;
257 }
258
259 minChunk.x = minX;
260 minChunk.y = minY;
261
262 maxChunk.x = maxX;
263 maxChunk.y = maxY;
264
265 minPos.x = minX * Constants.MAP_CHUNK_SIZE;
266 minPos.y = minY * Constants.MAP_CHUNK_SIZE;
267
268 maxPos.x = maxX * Constants.MAP_CHUNK_SIZE;
269 maxPos.y = maxY * Constants.MAP_CHUNK_SIZE;
270
271 widthChunks = maxX - minX + 1;
272 heightChunks = maxY - minY + 1;
273
274 widthPix = widthChunks * Constants.MAP_CHUNK_SIZE;
275 heightPix = heightChunks * Constants.MAP_CHUNK_SIZE;
276 }
277
[143]278 private static Color shortColorToColor (ushort col)
[130]279 {
280 return new Color (((float)(col >> 10 & 31) / 31f), ((float)(col >> 5 & 31) / 31f), ((float)(col & 31) / 31f), 255);
281 }
282
283 }
[143]284}
Note: See TracBrowser for help on using the repository browser.