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

/newcode/src/com/prupe/mcpatcher/mob/MobRuleList.java

https://bitbucket.org/prupe/mcpatcher
Java | 190 lines | 170 code | 20 blank | 0 comment | 50 complexity | dd39902743ae145a50c7e7de833c57bb MD5 | raw file
  1. package com.prupe.mcpatcher.mob;
  2. import com.prupe.mcpatcher.MCLogger;
  3. import com.prupe.mcpatcher.MCPatcherUtils;
  4. import com.prupe.mcpatcher.mal.biome.BiomeAPI;
  5. import com.prupe.mcpatcher.mal.resource.PropertiesFile;
  6. import com.prupe.mcpatcher.mal.resource.TexturePackAPI;
  7. import com.prupe.mcpatcher.mal.util.WeightedIndex;
  8. import net.minecraft.src.ResourceLocation;
  9. import java.util.*;
  10. class MobRuleList {
  11. private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.RANDOM_MOBS);
  12. public static final String ALTERNATIVES_REGEX = "_(eyes|overlay|tame|angry|collar|fur|invulnerable|shooting)\\.properties$";
  13. private static final Map<ResourceLocation, MobRuleList> allRules = new HashMap<ResourceLocation, MobRuleList>();
  14. private final ResourceLocation baseSkin;
  15. private final List<ResourceLocation> allSkins;
  16. private final int skinCount;
  17. private final List<MobRuleEntry> entries;
  18. private MobRuleList(ResourceLocation baseSkin) {
  19. this.baseSkin = baseSkin;
  20. String newPath = baseSkin.getPath().replaceFirst("^textures/entity/", TexturePackAPI.MCPATCHER_SUBDIR + "mob/");
  21. ResourceLocation newSkin = new ResourceLocation(baseSkin.getNamespace(), newPath);
  22. allSkins = new ArrayList<ResourceLocation>();
  23. allSkins.add(baseSkin);
  24. for (int i = 2; ; i++) {
  25. ResourceLocation skin = TexturePackAPI.transformResourceLocation(newSkin, ".png", String.valueOf(i) + ".png");
  26. if (!TexturePackAPI.hasResource(skin)) {
  27. break;
  28. }
  29. allSkins.add(skin);
  30. }
  31. skinCount = allSkins.size();
  32. if (skinCount <= 1) {
  33. entries = null;
  34. return;
  35. }
  36. logger.fine("found %d variations for %s", skinCount, baseSkin);
  37. ResourceLocation filename = TexturePackAPI.transformResourceLocation(newSkin, ".png", ".properties");
  38. ResourceLocation altFilename = new ResourceLocation(newSkin.getNamespace(), filename.getPath().replaceFirst(ALTERNATIVES_REGEX, ".properties"));
  39. PropertiesFile properties = PropertiesFile.get(logger, filename);
  40. if (properties == null && !filename.equals(altFilename)) {
  41. properties = PropertiesFile.get(logger, altFilename);
  42. if (properties != null) {
  43. logger.fine("using %s for %s", altFilename, baseSkin);
  44. }
  45. }
  46. ArrayList<MobRuleEntry> tmpEntries = new ArrayList<MobRuleEntry>();
  47. if (properties != null) {
  48. for (int i = 0; ; i++) {
  49. MobRuleEntry entry = MobRuleEntry.load(properties, i, skinCount);
  50. if (entry == null) {
  51. if (i > 0) {
  52. break;
  53. }
  54. } else {
  55. logger.fine(" %s", entry.toString());
  56. tmpEntries.add(entry);
  57. }
  58. }
  59. }
  60. entries = tmpEntries.isEmpty() ? null : tmpEntries;
  61. }
  62. ResourceLocation getSkin(long key, int i, int j, int k, Integer biome) {
  63. if (entries == null) {
  64. int index = (int) (key % skinCount);
  65. if (index < 0) {
  66. index += skinCount;
  67. }
  68. return allSkins.get(index);
  69. } else {
  70. if (j < 0) {
  71. j = 0;
  72. }
  73. for (MobRuleEntry entry : entries) {
  74. if (entry.match(i, j, k, biome)) {
  75. int index = entry.weightedIndex.choose(key);
  76. return allSkins.get(entry.skins[index]);
  77. }
  78. }
  79. }
  80. return baseSkin;
  81. }
  82. static MobRuleList get(ResourceLocation texture) {
  83. MobRuleList list = allRules.get(texture);
  84. if (list == null) {
  85. list = new MobRuleList(texture);
  86. allRules.put(texture, list);
  87. }
  88. return list;
  89. }
  90. static void clear() {
  91. allRules.clear();
  92. }
  93. private static class MobRuleEntry {
  94. final int[] skins;
  95. final WeightedIndex weightedIndex;
  96. private final BitSet biomes;
  97. private final BitSet height;
  98. static MobRuleEntry load(PropertiesFile properties, int index, int limit) {
  99. String skinList = properties.getString("skins." + index, "").toLowerCase();
  100. int[] skins;
  101. if (skinList.equals("*") || skinList.equals("all") || skinList.equals("any")) {
  102. skins = new int[limit];
  103. for (int i = 0; i < skins.length; i++) {
  104. skins[i] = i;
  105. }
  106. } else {
  107. skins = MCPatcherUtils.parseIntegerList(skinList, 1, limit);
  108. if (skins.length <= 0) {
  109. return null;
  110. }
  111. for (int i = 0; i < skins.length; i++) {
  112. skins[i]--;
  113. }
  114. }
  115. WeightedIndex chooser = WeightedIndex.create(skins.length, properties.getString("weights." + index, ""));
  116. if (chooser == null) {
  117. return null;
  118. }
  119. BitSet biomes;
  120. String biomeList = properties.getString("biomes." + index, "");
  121. if (biomeList.isEmpty()) {
  122. biomes = null;
  123. } else {
  124. biomes = new BitSet();
  125. BiomeAPI.parseBiomeList(biomeList, biomes);
  126. }
  127. BitSet height = BiomeAPI.getHeightListProperty(properties, "." + index);
  128. return new MobRuleEntry(skins, chooser, biomes, height);
  129. }
  130. MobRuleEntry(int[] skins, WeightedIndex weightedIndex, BitSet biomes, BitSet height) {
  131. this.skins = skins;
  132. this.weightedIndex = weightedIndex;
  133. this.biomes = biomes;
  134. this.height = height;
  135. }
  136. boolean match(int i, int j, int k, Integer biome) {
  137. if (biomes != null) {
  138. if (biome == null || !biomes.get(biome)) {
  139. return false;
  140. }
  141. }
  142. if (height != null && !height.get(j)) {
  143. return false;
  144. }
  145. return true;
  146. }
  147. @Override
  148. public String toString() {
  149. StringBuilder sb = new StringBuilder();
  150. sb.append("skins:");
  151. for (int i : skins) {
  152. sb.append(' ').append(i + 1);
  153. }
  154. if (biomes != null) {
  155. sb.append(", biomes:");
  156. for (int i = biomes.nextSetBit(0); i >= 0; i = biomes.nextSetBit(i + 1)) {
  157. sb.append(' ').append(i);
  158. }
  159. }
  160. if (height != null) {
  161. sb.append(", height:");
  162. for (int i = height.nextSetBit(0); i >= 0; i = height.nextSetBit(i + 1)) {
  163. sb.append(' ').append(i);
  164. }
  165. }
  166. sb.append(", weights: ").append(weightedIndex.toString());
  167. return sb.toString();
  168. }
  169. }
  170. }