ReadMap.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. namespace Server.MirForms.VisualMapInfo.Class
  2. {
  3. public class ReadMap
  4. {
  5. public int Width, Height, MonsterCount;
  6. public Cell[,] Cells;
  7. public long LightningTime, FireTime;
  8. public Bitmap clippingZone;
  9. public string mapFormat, mapFile;
  10. private byte FindType(byte[] input)
  11. {
  12. if (input[0] == 0) // Mir 3 WeMade
  13. return 5;
  14. if ((input[0] == 0x0F) && (input[5] == 0x53) && (input[14] == 0x33)) // Mir 3 Shanda
  15. return 6;
  16. if ((input[0] == 0x15) && (input[4] == 0x32) && (input[6] == 0x41) && (input[19] == 0x31)) // Mir WeMade AntiHack
  17. return 4;
  18. if ((input[0] == 0x10) && (input[2] == 0x61) && (input[7] == 0x31) && (input[14] == 0x31)) // Mir 2010 WeMade
  19. return 1;
  20. if ((input[4] == 0x0F) && (input[18] == 0x0D) && (input[19] == 0x0A)) // Mir 2012 Shanda
  21. {
  22. int W = input[0] + (input[1] << 8),
  23. H = input[2] + (input[3] << 8);
  24. if (input.Length > (52 + (W * H * 14)))
  25. return 3;
  26. else
  27. return 2;
  28. }
  29. if ((input[0] == 0x0D) && (input[1] == 0x4C) && (input[7] == 0x20) && (input[11] == 0x6D)) // Mir 3/4 Heroes
  30. return 7;
  31. if ((input[0] == 0xC8) && (input[2] == 0xC8) && (input[4] == 0x0D)) // Shortys Map Save
  32. return 8;
  33. if ((input[2] == 0x43) && (input[3] == 0x23)) //C#
  34. return 100;
  35. return 0;
  36. }
  37. private void LoadMapCellsv0(byte[] fileBytes)
  38. {
  39. int offSet = 0;
  40. Width = BitConverter.ToInt16(fileBytes, offSet);
  41. offSet += 2;
  42. Height = BitConverter.ToInt16(fileBytes, offSet);
  43. Cells = new Cell[Width, Height];
  44. offSet = 52;
  45. clippingZone = new Bitmap(Width, Height);
  46. LockBitmap BitLock = new LockBitmap(clippingZone);
  47. BitLock.LockBits();
  48. for (int x = 0; x < Width; x++)
  49. for (int y = 0; y < Height; y++)
  50. {
  51. if (Cells[x, y] == null)
  52. BitLock.SetPixel(x, y, Color.WhiteSmoke);
  53. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  54. BitLock.SetPixel(x, y, Color.Black);
  55. offSet += 2;
  56. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  57. BitLock.SetPixel(x, y, Color.Black);
  58. offSet += 10;
  59. }
  60. BitLock.UnlockBits();
  61. clippingZone.Dispose();
  62. }
  63. private void LoadMapCellsv1(byte[] fileBytes)
  64. {
  65. int offSet = 21;
  66. int w = BitConverter.ToInt16(fileBytes, offSet);
  67. offSet += 2;
  68. int xor = BitConverter.ToInt16(fileBytes, offSet);
  69. offSet += 2;
  70. int h = BitConverter.ToInt16(fileBytes, offSet);
  71. Width = w ^ xor;
  72. Height = h ^ xor;
  73. Cells = new Cell[Width, Height];
  74. offSet = 54;
  75. clippingZone = new Bitmap(Width, Height);
  76. for (int x = 0; x < Width; x++)
  77. for (int y = 0; y < Height; y++)
  78. {
  79. if (Cells[x, y] == null)
  80. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  81. if (((BitConverter.ToInt32(fileBytes, offSet) ^ 0xAA38AA38) & 0x20000000) != 0)
  82. clippingZone.SetPixel(x, y, Color.Black);
  83. offSet += 6;
  84. if (((BitConverter.ToInt16(fileBytes, offSet) ^ xor) & 0x8000) != 0)
  85. clippingZone.SetPixel(x, y, Color.Black);
  86. offSet += 9;
  87. }
  88. }
  89. private void LoadMapCellsv2(byte[] fileBytes)
  90. {
  91. int offSet = 0;
  92. Width = BitConverter.ToInt16(fileBytes, offSet);
  93. offSet += 2;
  94. Height = BitConverter.ToInt16(fileBytes, offSet);
  95. Cells = new Cell[Width, Height];
  96. offSet = 52;
  97. clippingZone = new Bitmap(Width, Height);
  98. for (int x = 0; x < Width; x++)
  99. for (int y = 0; y < Height; y++)
  100. {
  101. if (Cells[x, y] == null)
  102. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  103. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  104. clippingZone.SetPixel(x, y, Color.Black);
  105. offSet += 2;
  106. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  107. clippingZone.SetPixel(x, y, Color.Black);
  108. offSet += 12;
  109. }
  110. }
  111. private void LoadMapCellsv3(byte[] fileBytes)
  112. {
  113. int offSet = 0;
  114. Width = BitConverter.ToInt16(fileBytes, offSet);
  115. offSet += 2;
  116. Height = BitConverter.ToInt16(fileBytes, offSet);
  117. Cells = new Cell[Width, Height];
  118. offSet = 52;
  119. clippingZone = new Bitmap(Width, Height);
  120. for (int x = 0; x < Width; x++)
  121. for (int y = 0; y < Height; y++)
  122. {
  123. if (Cells[x, y] == null)
  124. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  125. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  126. clippingZone.SetPixel(x, y, Color.Black);
  127. offSet += 2;
  128. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  129. clippingZone.SetPixel(x, y, Color.Black);
  130. offSet += 34;
  131. }
  132. }
  133. private void LoadMapCellsv4(byte[] fileBytes)
  134. {
  135. int offSet = 31;
  136. int w = BitConverter.ToInt16(fileBytes, offSet);
  137. offSet += 2;
  138. int xor = BitConverter.ToInt16(fileBytes, offSet);
  139. offSet += 2;
  140. int h = BitConverter.ToInt16(fileBytes, offSet);
  141. Width = w ^ xor;
  142. Height = h ^ xor;
  143. Cells = new Cell[Width, Height];
  144. offSet = 64;
  145. clippingZone = new Bitmap(Width, Height);
  146. for (int x = 0; x < Width; x++)
  147. for (int y = 0; y < Height; y++)
  148. {
  149. if (Cells[x, y] == null)
  150. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  151. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  152. clippingZone.SetPixel(x, y, Color.Black);
  153. offSet += 2;
  154. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  155. clippingZone.SetPixel(x, y, Color.Black);
  156. offSet += 10;
  157. }
  158. }
  159. private void LoadMapCellsv5(byte[] fileBytes)
  160. {
  161. int offSet = 22;
  162. Width = BitConverter.ToInt16(fileBytes, offSet);
  163. offSet += 2;
  164. Height = BitConverter.ToInt16(fileBytes, offSet);
  165. Cells = new Cell[Width, Height];
  166. offSet = 28 + (3 * ((Width / 2) + (Width % 2)) * (Height / 2));
  167. clippingZone = new Bitmap(Width, Height);
  168. for (int x = 0; x < Width; x++)
  169. for (int y = 0; y < Height; y++)
  170. {
  171. if ((fileBytes[offSet] & 0x01) != 1)
  172. clippingZone.SetPixel(x, y, Color.Black);
  173. else if ((fileBytes[offSet] & 0x02) != 2)
  174. clippingZone.SetPixel(x, y, Color.Black);
  175. else
  176. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  177. offSet += 14;
  178. }
  179. }
  180. private void LoadMapCellsv6(byte[] fileBytes)
  181. {
  182. int offSet = 16;
  183. Width = BitConverter.ToInt16(fileBytes, offSet);
  184. offSet += 2;
  185. Height = BitConverter.ToInt16(fileBytes, offSet);
  186. Cells = new Cell[Width, Height];
  187. offSet = 40;
  188. clippingZone = new Bitmap(Width, Height);
  189. for (int x = 0; x < Width; x++)
  190. for (int y = 0; y < Height; y++)
  191. {
  192. if ((fileBytes[offSet] & 0x01) != 1)
  193. clippingZone.SetPixel(x, y, Color.Black);
  194. else if ((fileBytes[offSet] & 0x02) != 2)
  195. clippingZone.SetPixel(x, y, Color.Black);
  196. else
  197. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  198. offSet += 20;
  199. }
  200. }
  201. private void LoadMapCellsv7(byte[] fileBytes)
  202. {
  203. int offSet = 21;
  204. Width = BitConverter.ToInt16(fileBytes, offSet);
  205. offSet += 4;
  206. Height = BitConverter.ToInt16(fileBytes, offSet);
  207. Cells = new Cell[Width, Height];
  208. offSet = 54;
  209. clippingZone = new Bitmap(Width, Height);
  210. for (int x = 0; x < Width; x++)
  211. for (int y = 0; y < Height; y++)
  212. {
  213. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  214. clippingZone.SetPixel(x, y, Color.Black);
  215. offSet += 2;
  216. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  217. clippingZone.SetPixel(x, y, Color.Black);
  218. if (Cells[x, y] == null)
  219. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  220. offSet += 13;
  221. }
  222. }
  223. private void LoadMapCellsv8(byte[] fileBytes)
  224. {
  225. int offSet = 0;
  226. Width = BitConverter.ToInt16(fileBytes, offSet);
  227. offSet += 2;
  228. Height = BitConverter.ToInt16(fileBytes, offSet);
  229. Cells = new Cell[Width, Height];
  230. offSet = 52;
  231. clippingZone = new Bitmap(Width, Height);
  232. for (int x = 0; x < Width; x++)
  233. for (int y = 0; y < Height; y++)
  234. {
  235. if (Cells[x, y] == null)
  236. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  237. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  238. clippingZone.SetPixel(x, y, Color.Black);
  239. offSet += 2;
  240. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  241. clippingZone.SetPixel(x, y, Color.Black);
  242. offSet += 10;
  243. }
  244. }
  245. private void LoadMapCellsv100(byte[] fileBytes)
  246. {
  247. int offSet = 4;
  248. if ((fileBytes[0] != 1) || (fileBytes[1] != 0)) return;//only support version 1 atm;
  249. Width = BitConverter.ToInt16(fileBytes, offSet);
  250. offSet += 2;
  251. Height = BitConverter.ToInt16(fileBytes, offSet);
  252. Cells = new Cell[Width, Height];
  253. offSet = 8;
  254. clippingZone = new Bitmap(Width, Height);
  255. for (int x = 0; x < Width; x++)
  256. for (int y = 0; y < Height; y++)
  257. {
  258. offSet += 2;
  259. if (Cells[x, y] == null)
  260. clippingZone.SetPixel(x, y, Color.WhiteSmoke);
  261. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  262. clippingZone.SetPixel(x, y, Color.Black);
  263. offSet += 10;
  264. if ((BitConverter.ToInt16(fileBytes, offSet) & 0x8000) != 0)
  265. clippingZone.SetPixel(x, y, Color.Black);
  266. offSet += 14;
  267. }
  268. }
  269. public void Load()
  270. {
  271. try
  272. {
  273. if (File.Exists(Path.Combine("Maps", mapFile + ".map")))
  274. {
  275. byte[] fileBytes = File.ReadAllBytes(Path.Combine("Maps", mapFile + ".map"));
  276. switch (FindType(fileBytes))
  277. {
  278. case 0:
  279. LoadMapCellsv0(fileBytes);
  280. mapFormat = "?";
  281. break;
  282. case 1:
  283. LoadMapCellsv1(fileBytes);
  284. mapFormat = "WEMADE 2010";
  285. break;
  286. case 2:
  287. LoadMapCellsv2(fileBytes);
  288. mapFormat = "Old SHANDA";
  289. break;
  290. case 3:
  291. LoadMapCellsv3(fileBytes);
  292. mapFormat = "SHANDA 2012";
  293. break;
  294. case 4:
  295. LoadMapCellsv4(fileBytes);
  296. mapFormat = "WEMADE Mir 2";
  297. break;
  298. case 5:
  299. LoadMapCellsv5(fileBytes);
  300. mapFormat = "WEMADE Mir 3";
  301. break;
  302. case 6:
  303. LoadMapCellsv6(fileBytes);
  304. mapFormat = "SHANDA Mir 3";
  305. break;
  306. case 7:
  307. LoadMapCellsv7(fileBytes);
  308. mapFormat = "Heroes";
  309. break;
  310. case 8:
  311. LoadMapCellsv8(fileBytes);
  312. mapFormat = "Shortys";
  313. break;
  314. case 100:
  315. LoadMapCellsv100(fileBytes);
  316. mapFormat = "C#";
  317. break;
  318. }
  319. }
  320. }
  321. catch (Exception) { }
  322. VisualizerGlobal.ClippingMap = clippingZone;
  323. }
  324. public Cell GetCell(Point location)
  325. {
  326. return Cells[location.X, location.Y];
  327. }
  328. public Cell GetCell(int x, int y)
  329. {
  330. return Cells[x, y];
  331. }
  332. }
  333. public class Cell
  334. {
  335. public static readonly Cell HighWall;
  336. public static readonly Cell LowWall;
  337. }
  338. }