PageRenderTime 40ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/newcode/src/com/prupe/mcpatcher/ctm/CTMUtils.java

https://bitbucket.org/SevenBits/mcpatcher
Java | 334 lines | 292 code | 41 blank | 1 comment | 54 complexity | 99a4977f5584aada3d8323cc0ebc2087 MD5 | raw file
  1. package com.prupe.mcpatcher.ctm;
  2. import com.prupe.mcpatcher.*;
  3. import com.prupe.mcpatcher.mal.block.BlockAPI;
  4. import com.prupe.mcpatcher.mal.block.RenderPassAPI;
  5. import com.prupe.mcpatcher.mal.resource.ResourceList;
  6. import net.minecraft.src.*;
  7. import java.util.*;
  8. public class CTMUtils {
  9. private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.CONNECTED_TEXTURES, "CTM");
  10. private static final boolean enableStandard = Config.getBoolean(MCPatcherUtils.CONNECTED_TEXTURES, "standard", true);
  11. private static final boolean enableNonStandard = Config.getBoolean(MCPatcherUtils.CONNECTED_TEXTURES, "nonStandard", true);
  12. private static final List<ITileOverride> allOverrides = new ArrayList<ITileOverride>();
  13. private static final Map<Block, List<ITileOverride>> blockOverrides = new IdentityHashMap<Block, List<ITileOverride>>();
  14. private static final Map<String, List<ITileOverride>> tileOverrides = new HashMap<String, List<ITileOverride>>();
  15. private static TileLoader tileLoader;
  16. private static ITileOverride lastOverride;
  17. private static Icon blankIcon;
  18. private static final TileOverrideIterator.IJK ijkIterator = new TileOverrideIterator.IJK(blockOverrides, tileOverrides);
  19. private static final TileOverrideIterator.Metadata metadataIterator = new TileOverrideIterator.Metadata(blockOverrides, tileOverrides);
  20. private static boolean haveBlockFace;
  21. private static final BlockOrientation blockOrientation = new BlockOrientation();
  22. static {
  23. try {
  24. Class.forName(MCPatcherUtils.RENDER_PASS_CLASS).getMethod("finish").invoke(null);
  25. } catch (Throwable e) {
  26. }
  27. TexturePackChangeHandler.register(new TexturePackChangeHandler(MCPatcherUtils.CONNECTED_TEXTURES, 3) {
  28. @Override
  29. public void initialize() {
  30. }
  31. @Override
  32. public void beforeChange() {
  33. RenderPassAPI.instance.clear();
  34. try {
  35. GlassPaneRenderer.clear();
  36. } catch (Throwable e) {
  37. // nothing
  38. }
  39. blockOrientation.clear();
  40. ijkIterator.clear();
  41. metadataIterator.clear();
  42. allOverrides.clear();
  43. blockOverrides.clear();
  44. tileOverrides.clear();
  45. lastOverride = null;
  46. blankIcon = null;
  47. tileLoader = new TileLoader("textures/blocks", logger);
  48. BlockOrientation.reset();
  49. RenderPassAPI.instance.refreshBlendingOptions();
  50. if (enableStandard || enableNonStandard) {
  51. for (ResourceLocation resource : ResourceList.getInstance().listResources(TexturePackAPI.MCPATCHER_SUBDIR + "ctm", ".properties", true)) {
  52. registerOverride(TileOverride.create(resource, tileLoader));
  53. }
  54. }
  55. for (ResourceLocation resource : BlendMethod.getAllBlankResources()) {
  56. tileLoader.preloadTile(resource, false);
  57. }
  58. }
  59. @Override
  60. public void afterChange() {
  61. for (ITileOverride override : allOverrides) {
  62. override.registerIcons();
  63. }
  64. for (Map.Entry<Block, List<ITileOverride>> entry : blockOverrides.entrySet()) {
  65. for (ITileOverride override : entry.getValue()) {
  66. if (override.getRenderPass() >= 0) {
  67. RenderPassAPI.instance.setRenderPassForBlock(entry.getKey(), override.getRenderPass());
  68. }
  69. }
  70. }
  71. for (List<ITileOverride> overrides : blockOverrides.values()) {
  72. Collections.sort(overrides);
  73. }
  74. for (List<ITileOverride> overrides : tileOverrides.values()) {
  75. Collections.sort(overrides);
  76. }
  77. setBlankResource();
  78. }
  79. });
  80. }
  81. private static void clearBlockFace() {
  82. haveBlockFace = false;
  83. }
  84. public static Icon getBlockIcon(Icon icon, RenderBlocks renderBlocks, Block block, IBlockAccess blockAccess, int i, int j, int k, int face) {
  85. lastOverride = null;
  86. if (blockAccess != null && checkFace(face)) {
  87. if (!haveBlockFace) {
  88. blockOrientation.setBlock(block, blockAccess, i, j, k);
  89. blockOrientation.setFace(face);
  90. }
  91. lastOverride = ijkIterator.go(blockOrientation, icon);
  92. if (lastOverride != null) {
  93. icon = ijkIterator.getIcon();
  94. }
  95. }
  96. clearBlockFace();
  97. return lastOverride == null && skipDefaultRendering(block) ? blankIcon : icon;
  98. }
  99. public static Icon getBlockIcon(Icon icon, RenderBlocks renderBlocks, Block block, int face, int metadata) {
  100. lastOverride = null;
  101. if (checkFace(face) && checkRenderType(block)) {
  102. blockOrientation.setup(block, metadata, face);
  103. lastOverride = metadataIterator.go(blockOrientation, icon);
  104. if (lastOverride != null) {
  105. icon = metadataIterator.getIcon();
  106. }
  107. }
  108. return icon;
  109. }
  110. public static Icon getBlockIcon(Icon icon, RenderBlocks renderBlocks, Block block, int face) {
  111. return getBlockIcon(icon, renderBlocks, block, face, 0);
  112. }
  113. public static void reset() {
  114. }
  115. private static boolean checkFace(int face) {
  116. return face < 0 ? enableNonStandard : enableStandard;
  117. }
  118. private static boolean checkRenderType(Block block) {
  119. switch (block.getRenderType()) {
  120. case 11: // fence
  121. case 21: // fence gate
  122. return false;
  123. default:
  124. return true;
  125. }
  126. }
  127. private static boolean skipDefaultRendering(Block block) {
  128. return RenderPassAPI.instance.skipDefaultRendering(block);
  129. }
  130. private static void registerOverride(ITileOverride override) {
  131. if (override != null && !override.isDisabled()) {
  132. boolean registered = false;
  133. if (override.getMatchingBlocks() != null) {
  134. for (Block block : override.getMatchingBlocks()) {
  135. if (block == null) {
  136. continue;
  137. }
  138. List<ITileOverride> list = blockOverrides.get(block);
  139. if (list == null) {
  140. list = new ArrayList<ITileOverride>();
  141. blockOverrides.put(block, list);
  142. }
  143. list.add(override);
  144. logger.fine("using %s for block %s", override, BlockAPI.getBlockName(block));
  145. registered = true;
  146. }
  147. }
  148. if (override.getMatchingTiles() != null) {
  149. for (String name : override.getMatchingTiles()) {
  150. List<ITileOverride> list = tileOverrides.get(name);
  151. if (list == null) {
  152. list = new ArrayList<ITileOverride>();
  153. tileOverrides.put(name, list);
  154. }
  155. list.add(override);
  156. logger.fine("using %s for tile %s", override, name);
  157. registered = true;
  158. }
  159. }
  160. if (registered) {
  161. allOverrides.add(override);
  162. }
  163. }
  164. }
  165. static void setBlankResource() {
  166. blankIcon = tileLoader.getIcon(RenderPassAPI.instance.getBlankResource());
  167. }
  168. public static class Ext18 {
  169. public static void setBlock(IBlockAccess blockAccess, Block block, Position position) {
  170. blockOrientation.setBlock(block, blockAccess, position.getI(), position.getJ(), position.getK());
  171. blockOrientation.setFakeRenderType();
  172. }
  173. public static void setFace(Direction paramFace, Direction textureFace, Direction blockFace, BlockModelFace modelFace) {
  174. haveBlockFace = true;
  175. int blockFaceNum;
  176. if (paramFace != null) {
  177. blockFaceNum = paramFace.ordinal();
  178. } else if (blockFace != null) {
  179. blockFaceNum = blockFace.ordinal();
  180. } else {
  181. blockFaceNum = -1;
  182. }
  183. int rotation = getRotation(modelFace, blockFaceNum);
  184. blockOrientation.setFace(blockFaceNum, textureFace == null ? -1 : textureFace.ordinal(), rotation);
  185. if (blockOrientation.logIt()) {
  186. logger.info("%s:%d @ %d,%d,%d p=%s t=%s b=%s -> %d rotation %d",
  187. BlockAPI.getBlockName(blockOrientation.block), blockOrientation.metadata,
  188. blockOrientation.i, blockOrientation.j, blockOrientation.k,
  189. paramFace, textureFace, blockFace, blockFaceNum, rotation
  190. );
  191. logRotation(modelFace, blockFaceNum);
  192. int[] offset = blockOrientation.getOffset(BlockOrientation.REL_U);
  193. logger.info(" rel up=%d %d %d", offset[0], offset[1], offset[2]);
  194. offset = blockOrientation.getOffset(BlockOrientation.REL_D);
  195. logger.info(" rel down=%d %d %d", offset[0], offset[1], offset[2]);
  196. offset = blockOrientation.getOffset(BlockOrientation.REL_L);
  197. logger.info(" rel left=%d %d %d", offset[0], offset[1], offset[2]);
  198. offset = blockOrientation.getOffset(BlockOrientation.REL_R);
  199. logger.info(" rel right=%d %d %d", offset[0], offset[1], offset[2]);
  200. }
  201. }
  202. private static int getRotation(BlockModelFace modelFace, int face) {
  203. return getXYZRotation(modelFace, face) - getUVRotation(modelFace);
  204. }
  205. private static int getUVRotation(BlockModelFace modelFace) {
  206. int[] b = modelFace.getShadedIntBuffer();
  207. float du = Float.intBitsToFloat(b[4]) - Float.intBitsToFloat(b[18]); // u0 - u2
  208. float dv = Float.intBitsToFloat(b[5]) - Float.intBitsToFloat(b[19]); // v0 - v2
  209. return getRotation(du, dv);
  210. }
  211. private static int getXYZRotation(BlockModelFace modelFace, int face) {
  212. int[] b = modelFace.getShadedIntBuffer();
  213. float dx = Float.intBitsToFloat(b[0]) - Float.intBitsToFloat(b[14]); // x0 - x2
  214. float dy = Float.intBitsToFloat(b[1]) - Float.intBitsToFloat(b[15]); // y0 - y2
  215. float dz = Float.intBitsToFloat(b[2]) - Float.intBitsToFloat(b[16]); // z0 - z2
  216. float du;
  217. float dv;
  218. switch (face) {
  219. case BlockOrientation.BOTTOM_FACE:
  220. du = dx;
  221. dv = -dz;
  222. break;
  223. case BlockOrientation.TOP_FACE:
  224. du = dx;
  225. dv = dz;
  226. break;
  227. case BlockOrientation.NORTH_FACE:
  228. du = -dx;
  229. dv = -dy;
  230. break;
  231. case BlockOrientation.SOUTH_FACE:
  232. du = dx;
  233. dv = -dy;
  234. break;
  235. case BlockOrientation.WEST_FACE:
  236. du = dz;
  237. dv = -dy;
  238. break;
  239. case BlockOrientation.EAST_FACE:
  240. du = -dz;
  241. dv = -dy;
  242. break;
  243. default:
  244. return 0;
  245. }
  246. return getRotation(du, dv);
  247. }
  248. private static int getRotation(float s, float t) {
  249. if (s <= 0) {
  250. if (t <= 0) {
  251. return 0; // no rotation
  252. } else {
  253. return 2; // rotate 90 ccw
  254. }
  255. } else {
  256. if (t <= 0) {
  257. return 6; // rotate 90 cw
  258. } else {
  259. return 4; // rotate 180
  260. }
  261. }
  262. }
  263. private static void logRotation(BlockModelFace modelFace, int face) {
  264. int[] b = modelFace.getShadedIntBuffer();
  265. int x0 = (int) (Float.intBitsToFloat(b[0]) * 16.0f);
  266. int y0 = (int) (Float.intBitsToFloat(b[1]) * 16.0f);
  267. int z0 = (int) (Float.intBitsToFloat(b[2]) * 16.0f);
  268. int u0 = (int) Float.intBitsToFloat(b[4]);
  269. int v0 = (int) Float.intBitsToFloat(b[5]);
  270. int x1 = (int) (Float.intBitsToFloat(b[7]) * 16.0f);
  271. int y1 = (int) (Float.intBitsToFloat(b[8]) * 16.0f);
  272. int z1 = (int) (Float.intBitsToFloat(b[9]) * 16.0f);
  273. int u1 = (int) Float.intBitsToFloat(b[11]);
  274. int v1 = (int) Float.intBitsToFloat(b[12]);
  275. int x2 = (int) (Float.intBitsToFloat(b[14]) * 16.0f);
  276. int y2 = (int) (Float.intBitsToFloat(b[15]) * 16.0f);
  277. int z2 = (int) (Float.intBitsToFloat(b[16]) * 16.0f);
  278. int u2 = (int) Float.intBitsToFloat(b[18]);
  279. int v2 = (int) Float.intBitsToFloat(b[19]);
  280. int x3 = (int) (Float.intBitsToFloat(b[21]) * 16.0f);
  281. int y3 = (int) (Float.intBitsToFloat(b[22]) * 16.0f);
  282. int z3 = (int) (Float.intBitsToFloat(b[23]) * 16.0f);
  283. int u3 = (int) Float.intBitsToFloat(b[25]);
  284. int v3 = (int) Float.intBitsToFloat(b[26]);
  285. logger.info("x0,y0,z0=%d %d %d, x1,y1,z1=%d %d %d, x2,y2,z2=%d %d %d, x3,y3,z3=%d %d %d, rotation %d",
  286. x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3, getXYZRotation(modelFace, face)
  287. );
  288. logger.info("u0,v0=%d %d, u1,v1=%d %d, u2,v2=%d %d, u3,v3=%d %d, rotation %d",
  289. u0, v0, u1, v1, u2, v2, u3, v3, getUVRotation(modelFace)
  290. );
  291. }
  292. }
  293. }