PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/TileMapViewer/TileMapGenerator.cs

https://gitlab.com/SSStormy/cscape-old
C# | 327 lines | 263 code | 23 blank | 41 comment | 68 complexity | cafb32646ce9ff392143896c1dd7a6c7 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using CScapeCache;
  4. using CScapeCache.Caches;
  5. using CScapeCache.Model;
  6. namespace TileMapViewer
  7. {
  8. public sealed class TileMapGenerator
  9. {
  10. private Random _rng = new Random();
  11. private readonly int[] _textureMap = new int[0x10000];
  12. public TileMapGenerator()
  13. {
  14. GenerateTextures(0.80000000000000004D);
  15. }
  16. public delegate void DrawTile(int x, int y, byte rr, byte gg, byte bb);
  17. private int method182(MapModel map, int i, int j, int k)
  18. {
  19. if ((map.RenderRuleFlags[j, k, i] & 8) != 0)
  20. return 0;
  21. if (j > 0 && (map.RenderRuleFlags[1, k, i] & 2) != 0)
  22. return j - 1;
  23. return j;
  24. }
  25. public void RenderMap(CacheReader reader, MapModel map, DrawTile drawDelegate, int plane)
  26. {
  27. var anIntArray124 = new int[MapModel.CoordAxisLength];
  28. var anIntArray125 = new int[MapModel.CoordAxisLength];
  29. var anIntArray126 = new int[MapModel.CoordAxisLength];
  30. var anIntArray127 = new int[MapModel.CoordAxisLength];
  31. var anIntArray128 = new int[MapModel.CoordAxisLength];
  32. for (var z = 0; z < MapModel.MaxPlanes; z++)
  33. {
  34. for (var x = 0; x < MapModel.CoordAxisLength; x++)
  35. {
  36. for (var y = 0; y < MapModel.CoordAxisLength; y++)
  37. {
  38. var l12 = map.UnderlayFloorIds[z, x, y] & 0xff;
  39. if (l12 > 0)
  40. {
  41. var flo = reader.FloCache.GetId(l12 - 1);
  42. anIntArray124[y] += flo.anInt397;
  43. anIntArray125[y] += flo.anInt395;
  44. anIntArray126[y] += flo.anInt396;
  45. anIntArray127[y] += flo.anInt398;
  46. anIntArray128[y]++;
  47. }
  48. }
  49. int l9 = 0;
  50. int j13 = 0;
  51. int j14 = 0;
  52. int k15 = 0;
  53. int k16 = 0;
  54. for (var y = 0; y < MapModel.CoordAxisLength; y++)
  55. {
  56. l9 += anIntArray124[y];
  57. j13 += anIntArray125[y];
  58. j14 += anIntArray126[y];
  59. k15 += anIntArray127[y];
  60. k16 += anIntArray128[y];
  61. if ((map.RenderRuleFlags[0, x, y] & 2) != 0 ||
  62. (map.RenderRuleFlags[z, x, y] & 0x10) == 0 && method182(map, y, z, x) == plane)
  63. {
  64. int l18 = map.UnderlayFloorIds[z,x,y] & 0xff;
  65. int i19 = map.OverlayFloorIds[z, x, y] & 0xff;
  66. if (l18 > 0 || i19 > 0)
  67. {
  68. /*
  69. int j19 = map.VertexHeights[z, x, y];
  70. int k19 = tileVertexHeights[l][l6 + 1][k17];
  71. int l19 = tileVertexHeights[l][l6 + 1][k17 + 1];
  72. int i20 = tileVertexHeights[z, x, y + 1];
  73. int j20 = anIntArrayArray139[l6][k17];
  74. int k20 = anIntArrayArray139[l6 + 1][k17];
  75. int l20 = anIntArrayArray139[l6 + 1][k17 + 1];
  76. int i21 = anIntArrayArray139[l6][k17 + 1];
  77. */
  78. int j21 = -1;
  79. int k21 = -1;
  80. if (l18 > 0)
  81. {
  82. int l21 = (l9*256)/k15;
  83. int j22 = j13/k16;
  84. int l22 = j14/k16;
  85. j21 = method177(l21, j22, l22);
  86. // l21 = l21 + anInt123 & 0xff; (anInt123 = random -8 => n => 8)
  87. //l22 += anInt133; (anInt133 = random -16 => n => 16)
  88. if (l22 < 0)
  89. l22 = 0;
  90. else if (l22 > 255)
  91. l22 = 255;
  92. k21 = method177(l21, j22, l22);
  93. }
  94. /*
  95. if (z > 0)
  96. {
  97. var flag = true;
  98. if (l18 == 0 && map.OverlayClippingPaths[z,x,y] != 0)
  99. flag = false;
  100. if (i19 > 0 && !reader.FloCache.GetId(i19 - 1).aBoolean393)
  101. flag = false;
  102. if (flag && j19 == k19 && j19 == l19 && j19 == i20)
  103. anIntArrayArrayArray135[l][l6][k17] |= 0x924;
  104. }
  105. */
  106. int baseMapTexture = 0; // terrain etc, not walls or walkways
  107. if (j21 != -1)
  108. {
  109. baseMapTexture = _textureMap[method187(k21, 96)];
  110. }
  111. if (i19 == 0)
  112. {
  113. drawDelegate(x + map.BaseX, y + map.BaseY, (byte)(baseMapTexture >> 16), (byte)(baseMapTexture >> 8), (byte)(baseMapTexture));
  114. /*
  115. worldController.method279(l, l6, k17, 0, 0, -1, j19, k19, l19, i20,
  116. method187(j21, j20), method187(j21, k20), method187(j21, l20),
  117. method187(j21, i21), 0, 0, 0, 0, baseMapTexture, 0);
  118. */
  119. }
  120. else
  121. {
  122. //int k22 = map.OverlayClippingPaths[z,x,y] + 1;
  123. //byte byte4 = tileOverlayRotations[z, x, y];
  124. var flo_2 = reader.FloCache.GetId(i19 - 1);
  125. int i23 = flo_2.anInt391;
  126. int mapTexture;
  127. if (i23 >= 0)
  128. {
  129. // complex textures (wood, brick etc)
  130. // mapTexture = method369(i23);
  131. mapTexture = 0;
  132. }
  133. else if (flo_2.anInt390 == 0xff00ff)
  134. {
  135. mapTexture = 0;
  136. i23 = -1;
  137. }
  138. else
  139. {
  140. // walkways/roads
  141. mapTexture = _textureMap[method185(flo_2.anInt399, 96)];
  142. }
  143. drawDelegate(x + map.BaseX, y + map.BaseY, (byte)(mapTexture >> 16), (byte)(mapTexture >> 8), (byte)(mapTexture));
  144. /*
  145. worldController.method279(l, l6, k17, k22, byte4, i23, j19, k19, l19, i20,
  146. method187(j21, j20), method187(j21, k20), method187(j21, l20),
  147. method187(j21, i21), method185(worldTexture, j20), method185(worldTexture, k20),
  148. method185(worldTexture, l20), method185(worldTexture, i21), baseMapTexture,
  149. mapTexture);
  150. */
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. private int method185(int i, int j)
  159. {
  160. if (i == -2)
  161. return 0xbc614e;
  162. if (i == -1)
  163. {
  164. if (j < 0)
  165. j = 0;
  166. else
  167. if (j > 127)
  168. j = 127;
  169. j = 127 - j;
  170. return j;
  171. }
  172. j = (j * (i & 0x7f)) / 128;
  173. if (j < 2)
  174. j = 2;
  175. else
  176. if (j > 126)
  177. j = 126;
  178. return (i & 0xff80) + j;
  179. }
  180. public void GenerateTileMap(CacheReader reader, DrawTile drawDelegate, int plane = 0)
  181. {
  182. if (reader == null) throw new ArgumentNullException(nameof(reader));
  183. if (drawDelegate == null) throw new ArgumentNullException(nameof(drawDelegate));
  184. LoadIfNotLoaded(reader.FloCache);
  185. LoadIfNotLoaded(reader.MapIndexCache);
  186. LoadIfNotLoaded(reader.MapCache);
  187. foreach (var map in reader.MapIndexCache.All.Select(index => reader.MapCache.GetMap(index, 0, 0)))
  188. RenderMap(reader, map, drawDelegate, plane);
  189. }
  190. private static void LoadIfNotLoaded(AbstractCache cache)
  191. {
  192. if(!cache.IsLoaded)
  193. cache.Load();
  194. }
  195. public void GenerateTextures(double seed)
  196. {
  197. seed += _rng.NextDouble() * 0.029999999999999999D - 0.014999999999999999D;
  198. int j = 0;
  199. for (int k = 0; k < 512; k++)
  200. {
  201. double d1 = (double)(k / 8) / 64D + 0.0078125D;
  202. double d2 = (double)(k & 7) / 8D + 0.0625D;
  203. for (int k1 = 0; k1 < 128; k1++)
  204. {
  205. double d3 = (double)k1 / 128D;
  206. double d4 = d3;
  207. double d5 = d3;
  208. double d6 = d3;
  209. if (d2 != 0.0D)
  210. {
  211. double d7;
  212. if (d3 < 0.5D)
  213. d7 = d3 * (1.0D + d2);
  214. else
  215. d7 = (d3 + d2) - d3 * d2;
  216. double d8 = 2D * d3 - d7;
  217. double d9 = d1 + 0.33333333333333331D;
  218. if (d9 > 1.0D)
  219. d9--;
  220. double d10 = d1;
  221. double d11 = d1 - 0.33333333333333331D;
  222. if (d11 < 0.0D)
  223. d11++;
  224. if (6D * d9 < 1.0D)
  225. d4 = d8 + (d7 - d8) * 6D * d9;
  226. else
  227. if (2D * d9 < 1.0D)
  228. d4 = d7;
  229. else
  230. if (3D * d9 < 2D)
  231. d4 = d8 + (d7 - d8) * (0.66666666666666663D - d9) * 6D;
  232. else
  233. d4 = d8;
  234. if (6D * d10 < 1.0D)
  235. d5 = d8 + (d7 - d8) * 6D * d10;
  236. else
  237. if (2D * d10 < 1.0D)
  238. d5 = d7;
  239. else
  240. if (3D * d10 < 2D)
  241. d5 = d8 + (d7 - d8) * (0.66666666666666663D - d10) * 6D;
  242. else
  243. d5 = d8;
  244. if (6D * d11 < 1.0D)
  245. d6 = d8 + (d7 - d8) * 6D * d11;
  246. else
  247. if (2D * d11 < 1.0D)
  248. d6 = d7;
  249. else
  250. if (3D * d11 < 2D)
  251. d6 = d8 + (d7 - d8) * (0.66666666666666663D - d11) * 6D;
  252. else
  253. d6 = d8;
  254. }
  255. int l1 = (int)(d4 * 256D);
  256. int i2 = (int)(d5 * 256D);
  257. int j2 = (int)(d6 * 256D);
  258. int k2 = (l1 << 16) + (i2 << 8) + j2;
  259. k2 = method373(k2, seed);
  260. if (k2 == 0)
  261. k2 = 1;
  262. _textureMap[j++] = k2;
  263. }
  264. }
  265. }
  266. private int method177(int i, int j, int k)
  267. {
  268. if (k > 179)
  269. j /= 2;
  270. if (k > 192)
  271. j /= 2;
  272. if (k > 217)
  273. j /= 2;
  274. if (k > 243)
  275. j /= 2;
  276. return (i / 4 << 10) + (j / 32 << 7) + k / 2;
  277. }
  278. private static int method373(int i, double d)
  279. {
  280. double d1 = (double)(i >> 16) / 256D;
  281. double d2 = (double)(i >> 8 & 0xff) / 256D;
  282. double d3 = (double)(i & 0xff) / 256D;
  283. d1 = Math.Pow(d1, d);
  284. d2 = Math.Pow(d2, d);
  285. d3 = Math.Pow(d3, d);
  286. int j = (int)(d1 * 256D);
  287. int k = (int)(d2 * 256D);
  288. int l = (int)(d3 * 256D);
  289. return (j << 16) + (k << 8) + l;
  290. }
  291. private static int method187(int i, int j)
  292. {
  293. if (i == -1)
  294. return 0xbc614e;
  295. j = (j * (i & 0x7f)) / 128;
  296. if (j < 2)
  297. j = 2;
  298. else
  299. if (j > 126)
  300. j = 126;
  301. return (i & 0xff80) + j;
  302. }
  303. }
  304. }