armark1ng

Untitled

May 28th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. package com.rs.game;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.rs.Settings;
  7. import com.rs.cache.Cache;
  8. import com.rs.cache.loaders.ObjectDefinitions;
  9. import com.rs.io.InputStream;
  10. import com.rs.utils.Logger;
  11. import com.rs.utils.MapArchiveKeys;
  12.  
  13. public class DynamicRegion extends Region {
  14.  
  15. /**
  16. * Contains render coordinates.
  17. */
  18. private int[][][][] regionCoords;
  19. private boolean[][][] needsReload;
  20. private boolean recheckReload;
  21.  
  22. public DynamicRegion(int regionId) {
  23. super(regionId);
  24. // plane,x,y,(real x, real y,or real plane coord, or rotation)
  25. regionCoords = new int[4][8][8][4];
  26. needsReload = new boolean[4][8][8];
  27. for (int z = 0; z < 4; z++) {
  28. for (int x = 0; x < 8; x++) {
  29. for (int y = 0; y < 8; y++) {
  30. needsReload[z][x][y] = true;
  31. }
  32. }
  33. }
  34. recheckReload = false;
  35. }
  36.  
  37. @Override
  38. public void checkLoadMap() {
  39. if (recheckReload) {
  40. setLoadMapStage(0);
  41. recheckReload = false;
  42. }
  43. super.checkLoadMap();
  44. }
  45.  
  46. @Override
  47. public void loadRegionMap() {
  48.  
  49. for (int dynZ = 0; dynZ < 4; dynZ++) {
  50. for (int dynX = 0; dynX < 8; dynX++) {
  51. for (int dynY = 0; dynY < 8; dynY++) {
  52. if (!needsReload[dynZ][dynX][dynY])
  53. continue;
  54. unloadChunk(dynX, dynY, dynZ);
  55. }
  56. }
  57. }
  58.  
  59. for (int dynZ = 0; dynZ < 4; dynZ++) {
  60. for (int dynX = 0; dynX < 8; dynX++) {
  61. for (int dynY = 0; dynY < 8; dynY++) {
  62. if (!needsReload[dynZ][dynX][dynY])
  63. continue;
  64. needsReload[dynZ][dynX][dynY] = false;
  65.  
  66. int renderChunkX = regionCoords[dynZ][dynX][dynY][0];
  67. int renderChunkY = regionCoords[dynZ][dynX][dynY][1];
  68. int renderChunkZ = regionCoords[dynZ][dynX][dynY][2];
  69. int rotation = regionCoords[dynZ][dynX][dynY][3];
  70. int renderLocalChunkX = renderChunkX
  71. - ((renderChunkX >> 3) << 3);
  72. int renderLocalChunkY = renderChunkY
  73. - ((renderChunkY >> 3) << 3);
  74.  
  75. if (renderChunkX == 0 && renderChunkY == 0
  76. && renderChunkZ == 0 && rotation == 0) {
  77. continue;
  78. }
  79.  
  80. int mapID = (renderChunkX >> 3) << 8 | (renderChunkY >> 3);
  81. int landArchiveId = Cache.STORE.getIndexes()[5]
  82. .getArchiveId("l" + (mapID >> 8) + "_"
  83. + (mapID & 0xFF));
  84. int mapArchiveId = Cache.STORE.getIndexes()[5]
  85. .getArchiveId("m" + (mapID >> 8) + "_"
  86. + (mapID & 0xFF));
  87. byte[] mapContainerData = mapArchiveId == -1 ? null
  88. : Cache.STORE.getIndexes()[5].getFile(mapArchiveId,
  89. 0);
  90. byte[] landContainerData = landArchiveId == -1 ? null
  91. : Cache.STORE.getIndexes()[5].getFile(
  92. landArchiveId, 0,
  93. MapArchiveKeys.getMapKeys(mapID));
  94. byte[][][] mapSettings = mapContainerData == null ? null
  95. : new byte[4][64][64];
  96. if (mapContainerData != null) {
  97. InputStream mapStream = new InputStream(
  98. mapContainerData);
  99. for (int plane = 0; plane < 4; plane++) {
  100. for (int x = 0; x < 64; x++) {
  101. for (int y = 0; y < 64; y++) {
  102. while (true) {
  103. int value = mapStream
  104. .readUnsignedByte();
  105. if (value == 0) {
  106. break;
  107. } else if (value == 1) {
  108. mapStream.readByte();
  109. break;
  110. } else if (value <= 49) {
  111. mapStream.readByte();
  112.  
  113. } else if (value <= 81) {
  114. mapSettings[plane][x][y] = (byte) (value - 49);
  115. }
  116. }
  117. }
  118. }
  119. }
  120.  
  121. for (int z = 0; z < 4; z++) {
  122. for (int x = 0; x < 64; x++) {
  123. for (int y = 0; y < 64; y++) {
  124. if ((mapSettings[z][x][y] & 0x1) == 1) {
  125. int realZ = z;
  126. if ((mapSettings[1][x][y] & 0x2) == 2) {
  127. realZ--;
  128. }
  129. if (realZ == renderChunkZ
  130. && (x >> 3) == renderLocalChunkX
  131. && (y >> 3) == renderLocalChunkY) {
  132. int[] coords = translate(x & 0x7,
  133. y & 0x7, rotation);
  134. forceGetRegionMap().addUnwalkable(
  135. dynZ,
  136. (dynX << 3) | coords[0],
  137. (dynY << 3) | coords[1]);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. } else {
  144. for (int z = 0; z < 4; z++) {
  145. for (int x = 0; x < 64; x++) {
  146. for (int y = 0; y < 64; y++) {
  147. if (z == renderChunkZ
  148. && (x >> 3) == renderLocalChunkX
  149. && (y >> 3) == renderLocalChunkY) {
  150. int[] coords = translate(x & 0x7,
  151. y & 0x7, rotation);
  152. forceGetRegionMap().addUnwalkable(dynZ,
  153. (dynX << 3) | coords[0],
  154. (dynY << 3) | coords[1]);
  155. }
  156. }
  157. }
  158. }
  159. }
  160.  
  161. if (landContainerData != null) {
  162. InputStream landStream = new InputStream(
  163. landContainerData);
  164. int objectId = -1;
  165. int incr;
  166. while ((incr = landStream.readSmart2()) != 0) {
  167. objectId += incr;
  168. int location = 0;
  169. int incr2;
  170. while ((incr2 = landStream.readUnsignedSmart()) != 0) {
  171. location += incr2 - 1;
  172. int x = (location >> 6 & 0x3f);
  173. int y = (location & 0x3f);
  174. int z = location >> 12;
  175. int objectData = landStream.readUnsignedByte();
  176. int type = objectData >> 2;
  177. int rot = objectData & 0x3;
  178. int realZ = z;
  179. if (mapSettings != null
  180. && (mapSettings[1][x][y] & 2) == 2)
  181. realZ--;
  182. if (realZ == renderChunkZ
  183. && (x >> 3) == renderLocalChunkX
  184. && (y >> 3) == renderLocalChunkY) {
  185. ObjectDefinitions definition = ObjectDefinitions
  186. .getObjectDefinitions(objectId);
  187. int[] coords = translate(x & 0x7, y & 0x7,
  188. rotation, definition.sizeX,
  189. definition.sizeY, rot);
  190. spawnObject(
  191. new WorldObject(
  192. objectId,
  193. type,
  194. (rotation + rot) & 0x3,
  195. (dynX << 3)
  196. + coords[0]
  197. + ((getRegionId() >> 8) << 6),
  198. (dynY << 3)
  199. + coords[1]
  200. + ((getRegionId() & 0xFF) << 6),
  201. dynZ), dynZ, (dynX << 3)
  202. + coords[0], (dynY << 3)
  203. + coords[1], true);
  204. }
  205. }
  206. }
  207. }
  208.  
  209. if (Settings.DEBUG && landContainerData == null
  210. && landArchiveId != -1
  211. && MapArchiveKeys.getMapKeys(mapID) != null)
  212. Logger.log(this, "Missing xteas for region " + mapID
  213. + ".");
  214. }
  215. }
  216. }
  217. }
  218.  
  219. private void unloadChunk(int chunkX, int chunkY, int chunkZ) {
  220. for (int x = 0; x < 8; x++) {
  221. for (int y = 0; y < 8; y++) {
  222. int fullX = (chunkX << 3) | x;
  223. int fullY = (chunkY << 3) | y;
  224. if (objects != null) {
  225. for (int slot = 0; slot < 4; slot++) {
  226. objects[chunkZ][fullX][fullY][slot] = null;
  227. }
  228. }
  229. if (map != null)
  230. map.setMask(chunkZ, fullX, fullY, 0);
  231. if (clipedOnlyMap != null)
  232. clipedOnlyMap.setMask(chunkZ, fullX, fullY, 0);
  233.  
  234. List<WorldObject> ro = new ArrayList<WorldObject>(
  235. removedOriginalObjects);
  236. // List<WorldObject> ao = new
  237. // ArrayList<WorldObject>(spawnedObjects);
  238. for (WorldObject removed : ro)
  239. if (removed.getPlane() == chunkZ
  240. && removed.getChunkX() == chunkX
  241. && removed.getChunkY() == chunkY)
  242. removedOriginalObjects.remove(removed);
  243. /*
  244. * for (WorldObject added : ro) if (added.getPlane() == chunkZ
  245. * && added.getChunkX() == chunkX && added.getChunkY() ==
  246. * chunkY) spawnedObjects.remove(ao);
  247. */
  248. }
  249. }
  250. }
  251.  
  252. public static int[] translate(int x, int y, int rotation) {
  253. int[] coords = new int[2];
  254. if (rotation == 0) {
  255. coords[0] = x;
  256. coords[1] = y;
  257. } else if (rotation == 1) {
  258. coords[0] = y;
  259. coords[1] = 7 - x;
  260. } else if (rotation == 2) {
  261. coords[0] = 7 - x;
  262. coords[1] = 7 - y;
  263. } else {
  264. coords[0] = 7 - y;
  265. coords[1] = x;
  266. }
  267. return coords;
  268. }
  269.  
  270. public static int[] translate(int x, int y, int mapRotation, int sizeX,
  271. int sizeY, int objectRotation) {
  272. int[] coords = new int[2];
  273. if ((objectRotation & 0x1) == 1) {
  274. int prevSizeX = sizeX;
  275. sizeX = sizeY;
  276. sizeY = prevSizeX;
  277. }
  278. if (mapRotation == 0) {
  279. coords[0] = x;
  280. coords[1] = y;
  281. } else if (mapRotation == 1) {
  282. coords[0] = y;
  283. coords[1] = 7 - x - (sizeX - 1);
  284. } else if (mapRotation == 2) {
  285. coords[0] = 7 - x - (sizeX - 1);
  286. coords[1] = 7 - y - (sizeY - 1);
  287. } else if (mapRotation == 3) {
  288. coords[0] = 7 - y - (sizeY - 1);
  289. coords[1] = x;
  290. }
  291. return coords;
  292. }
  293.  
  294. @Override
  295. public int getRotation(int plane, int x, int y) {
  296. return regionCoords[plane][x][y][3];
  297. }
  298.  
  299. public void setRotation(int plane, int x, int y, int rotation) {
  300. regionCoords[plane][x][y][3] = rotation;
  301. setReloadObjects(plane, x, y);
  302. }
  303.  
  304. public void setReloadObjects(int plane, int x, int y) {
  305. needsReload[plane][x][y] = true;
  306. recheckReload = true;
  307. }
  308.  
  309. public int[][][][] getRegionCoords() {
  310. return regionCoords;
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment