PageRenderTime 60ms CodeModel.GetById 38ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/newcode/src/com/prupe/mcpatcher/cc/ColorizeBlock.java

https://bitbucket.org/michael_rodrigues/mcpatcher
Java | 310 lines | 271 code | 39 blank | 0 comment | 57 complexity | 47c91a2cee8a97b58909ff46340a9a8d MD5 | raw file
  1package com.prupe.mcpatcher.cc;
  2
  3import com.prupe.mcpatcher.Config;
  4import com.prupe.mcpatcher.MCLogger;
  5import com.prupe.mcpatcher.MCPatcherUtils;
  6import com.prupe.mcpatcher.TexturePackAPI;
  7import net.minecraft.src.Block;
  8import net.minecraft.src.IBlockAccess;
  9import net.minecraft.src.ResourceLocation;
 10import org.lwjgl.opengl.GL11;
 11
 12import java.util.Arrays;
 13import java.util.HashMap;
 14import java.util.Map;
 15import java.util.Properties;
 16
 17public class ColorizeBlock {
 18    private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.CUSTOM_COLORS);
 19
 20    private static final ResourceLocation REDSTONE_COLORS = TexturePackAPI.newMCPatcherResourceLocation("colormap/redstone.png");
 21    private static final ResourceLocation STEM_COLORS = TexturePackAPI.newMCPatcherResourceLocation("colormap/stem.png");
 22    private static final ResourceLocation PUMPKIN_STEM_COLORS = TexturePackAPI.newMCPatcherResourceLocation("colormap/pumpkinstem.png");
 23    private static final ResourceLocation MELON_STEM_COLORS = TexturePackAPI.newMCPatcherResourceLocation("colormap/melonstem.png");
 24    private static final ResourceLocation SWAMPGRASSCOLOR = TexturePackAPI.newMCPatcherResourceLocation("colormap/swampgrass.png");
 25    private static final ResourceLocation SWAMPFOLIAGECOLOR = TexturePackAPI.newMCPatcherResourceLocation("colormap/swampfoliage.png");
 26    private static final ResourceLocation PINECOLOR = TexturePackAPI.newMCPatcherResourceLocation("colormap/pine.png");
 27    private static final ResourceLocation BIRCHCOLOR = TexturePackAPI.newMCPatcherResourceLocation("colormap/birch.png");
 28    private static final ResourceLocation FOLIAGECOLOR = TexturePackAPI.newMCPatcherResourceLocation("colormap/foliage.png");
 29    private static final ResourceLocation WATERCOLOR = TexturePackAPI.newMCPatcherResourceLocation("colormap/water.png");
 30    private static final ResourceLocation UNDERWATERCOLOR = TexturePackAPI.newMCPatcherResourceLocation("colormap/underwater.png");
 31    private static final ResourceLocation FOGCOLOR0 = TexturePackAPI.newMCPatcherResourceLocation("colormap/fog0.png");
 32    private static final ResourceLocation SKYCOLOR0 = TexturePackAPI.newMCPatcherResourceLocation("colormap/sky0.png");
 33
 34    private static final String PALETTE_BLOCK_KEY = "palette.block.";
 35
 36    private static final int BLOCK_ID_PUMPKIN_STEM = 104;
 37    private static final int BLOCK_ID_MELON_STEM = 105;
 38
 39    private static final ColorMap[] blockColorMaps = new ColorMap[Block.blocksList.length]; // bitmaps from palette.block.*
 40    private static final Map<Float, ColorMap> blockMetaColorMaps = new HashMap<Float, ColorMap>(); // bitmaps from palette.block.*
 41    private static int lilypadColor; // lilypad
 42    private static float[][] redstoneColor; // colormap/redstone.png
 43    private static int[] pumpkinStemColors; // colormap/pumpkinstem.png
 44    private static int[] melonStemColors; // colormap/melonstem.png
 45
 46    private static final int blockBlendRadius = Config.getInt(MCPatcherUtils.CUSTOM_COLORS, "blockBlendRadius", 1);
 47    private static final float blockBlendScale = (float) Math.pow(2 * blockBlendRadius + 1, -2);
 48
 49    public static float[] waterColor;
 50
 51    static {
 52        try {
 53            reset();
 54        } catch (Throwable e) {
 55            e.printStackTrace();
 56        }
 57    }
 58
 59    static void reset() {
 60        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_SWAMP_GRASS] = new ColorMap(0x4e4e4e);
 61        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_SWAMP_FOLIAGE] = new ColorMap(0x4e4e4e);
 62        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_PINE] = new ColorMap(0x619961);
 63        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_BIRCH] = new ColorMap(0x80a755);
 64        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_FOLIAGE] = new ColorMap(0x48b518);
 65        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_WATER] = new ColorMap(0xffffff);
 66        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_UNDERWATER] = new ColorMap(0x050533);
 67        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_FOG0] = new ColorMap(0xc0d8ff);
 68        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_SKY0] = new ColorMap(0xffffff);
 69
 70        Arrays.fill(blockColorMaps, null);
 71        blockMetaColorMaps.clear();
 72
 73        lilypadColor = 0x208030;
 74        waterColor = new float[]{0.2f, 0.3f, 1.0f};
 75        redstoneColor = null;
 76        pumpkinStemColors = null;
 77        melonStemColors = null;
 78    }
 79
 80    static void reloadColorMaps(Properties properties) {
 81        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_SWAMP_GRASS].loadColorMap(Colorizer.useSwampColors, SWAMPGRASSCOLOR);
 82        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_SWAMP_FOLIAGE].loadColorMap(Colorizer.useSwampColors, SWAMPFOLIAGECOLOR);
 83        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_PINE].loadColorMap(Colorizer.useTreeColors, PINECOLOR);
 84        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_BIRCH].loadColorMap(Colorizer.useTreeColors, BIRCHCOLOR);
 85        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_FOLIAGE].loadColorMap(Colorizer.useTreeColors, FOLIAGECOLOR);
 86        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_FOLIAGE].clear();
 87        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_WATER].loadColorMap(Colorizer.useWaterColors, WATERCOLOR);
 88        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_UNDERWATER].loadColorMap(Colorizer.useWaterColors, UNDERWATERCOLOR);
 89        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_FOG0].loadColorMap(Colorizer.useFogColors, FOGCOLOR0);
 90        Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_SKY0].loadColorMap(Colorizer.useFogColors, SKYCOLOR0);
 91    }
 92
 93    static void reloadSwampColors(Properties properties) {
 94        int[] temp = new int[]{lilypadColor};
 95        Colorizer.loadIntColor("lilypad", temp, 0);
 96        lilypadColor = temp[0];
 97    }
 98
 99    static void reloadBlockColors(Properties properties) {
100        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
101            if (!(entry.getKey() instanceof String) || !(entry.getValue() instanceof String)) {
102                continue;
103            }
104            String key = (String) entry.getKey();
105            String value = (String) entry.getValue();
106            if (!key.startsWith(PALETTE_BLOCK_KEY)) {
107                continue;
108            }
109            ResourceLocation address = TexturePackAPI.parseResourceLocation(Colorizer.COLOR_PROPERTIES, key.substring(PALETTE_BLOCK_KEY.length()).trim());
110            if (address == null) {
111                continue;
112            }
113            ColorMap colorMap = new ColorMap(0xffffff);
114            colorMap.loadColorMap(true, address);
115            if (!colorMap.isCustom()) {
116                continue;
117            }
118            for (String idString : value.split("\\s+")) {
119                String[] tokens = idString.split(":");
120                int[] tokensInt = new int[tokens.length];
121                try {
122                    for (int i = 0; i < tokens.length; i++) {
123                        tokensInt[i] = Integer.parseInt(tokens[i]);
124                    }
125                } catch (NumberFormatException e) {
126                    continue;
127                }
128                switch (tokensInt.length) {
129                    case 1:
130                        if (tokensInt[0] < 0 || tokensInt[0] >= blockColorMaps.length) {
131                            continue;
132                        }
133                        blockColorMaps[tokensInt[0]] = colorMap;
134                        break;
135
136                    case 2:
137                        blockMetaColorMaps.put(ColorMap.getBlockMetaKey(tokensInt[0], tokensInt[1]), colorMap);
138                        break;
139
140                    default:
141                        continue;
142                }
143                logger.finer("using %s for block %s, default color %06x", key, idString, colorMap.colorize());
144            }
145        }
146    }
147
148    static void reloadRedstoneColors(Properties properties) {
149        int[] rgb = MCPatcherUtils.getImageRGB(TexturePackAPI.getImage(REDSTONE_COLORS));
150        if (rgb != null && rgb.length >= 16) {
151            redstoneColor = new float[16][];
152            for (int i = 0; i < 16; i++) {
153                float[] f = new float[3];
154                Colorizer.intToFloat3(rgb[i], f);
155                redstoneColor[i] = f;
156            }
157        }
158    }
159
160    static void reloadStemColors(Properties properties) {
161        int[] stemColors = getStemRGB(STEM_COLORS);
162        pumpkinStemColors = getStemRGB(PUMPKIN_STEM_COLORS);
163        if (pumpkinStemColors == null) {
164            pumpkinStemColors = stemColors;
165        }
166        melonStemColors = getStemRGB(MELON_STEM_COLORS);
167        if (melonStemColors == null) {
168            melonStemColors = stemColors;
169        }
170    }
171
172    private static int[] getStemRGB(ResourceLocation resource) {
173        int[] rgb = MCPatcherUtils.getImageRGB(TexturePackAPI.getImage(resource));
174        return rgb == null || rgb.length < 8 ? null : rgb;
175    }
176
177    public static int colorizeBiome(int defaultColor, int index, double temperature, double rainfall) {
178        return Colorizer.fixedColorMaps[index].colorize(defaultColor, temperature, rainfall);
179    }
180
181    public static int colorizeBiome(int defaultColor, int index) {
182        return Colorizer.fixedColorMaps[index].colorize(defaultColor);
183    }
184
185    public static int colorizeBiome(int defaultColor, int index, int i, int j, int k) {
186        return Colorizer.fixedColorMaps[index].colorize(defaultColor, i, j, k);
187    }
188
189    public static int colorizeBiomeWithBlending(int defaultColor, int index, int i, int j, int k) {
190        return colorizeWithBlending(Colorizer.fixedColorMaps[index], defaultColor, i, j, k);
191    }
192
193    public static int colorizeWater(Object dummy, int i, int k) {
194        return Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_WATER].colorize(BiomeHelper.getWaterColorMultiplier(i, 64, k), i, 64, k);
195    }
196
197    public static int colorizeBlock(Block block, int i, int j, int k, int metadata) {
198        ColorMap colorMap = null;
199        if (!blockMetaColorMaps.isEmpty()) {
200            colorMap = blockMetaColorMaps.get(ColorMap.getBlockMetaKey(block.blockID, metadata));
201        }
202        if (colorMap == null && block.blockID >= 0 && block.blockID < blockColorMaps.length) {
203            colorMap = blockColorMaps[block.blockID];
204        }
205        return colorizeWithBlending(colorMap, 0xffffff, i, j, k);
206    }
207
208    private static int colorizeWithBlending(ColorMap colorMap, int defaultColor, int i, int j, int k) {
209        if (colorMap == null || !colorMap.isCustom() || blockBlendRadius <= 0) {
210            return defaultColor;
211        }
212        float[] sum = new float[3];
213        float[] sample = new float[3];
214        for (int di = -blockBlendRadius; di <= blockBlendRadius; di++) {
215            for (int dk = -blockBlendRadius; dk <= blockBlendRadius; dk++) {
216                int rgb = colorMap.colorize(defaultColor, i + di, j, k + dk);
217                Colorizer.intToFloat3(rgb, sample);
218                sum[0] += sample[0];
219                sum[1] += sample[1];
220                sum[2] += sample[2];
221            }
222        }
223        sum[0] *= blockBlendScale;
224        sum[1] *= blockBlendScale;
225        sum[2] *= blockBlendScale;
226        return Colorizer.float3ToInt(sum);
227    }
228
229    public static int colorizeBlock(Block block) {
230        ColorMap colorMap = blockColorMaps[block.blockID];
231        if (colorMap == null) {
232            return 0xffffff;
233        } else {
234            return colorMap.colorize(0xffffff);
235        }
236    }
237
238    public static int colorizeStem(int defaultColor, Block block, int blockMetadata) {
239        int[] colors;
240        switch (block.blockID) {
241            case BLOCK_ID_PUMPKIN_STEM:
242                colors = pumpkinStemColors;
243                break;
244
245            case BLOCK_ID_MELON_STEM:
246                colors = melonStemColors;
247                break;
248
249            default:
250                return defaultColor;
251        }
252        return colors == null ? defaultColor : colors[blockMetadata & 0x7];
253    }
254
255    public static int getLilyPadColor() {
256        return lilypadColor;
257    }
258
259    public static int getItemColorFromDamage(int defaultColor, int blockID, int damage) {
260        if (blockID == 8 || blockID == 9) {
261            return colorizeBiome(defaultColor, Colorizer.COLOR_MAP_WATER);
262        } else {
263            return defaultColor;
264        }
265    }
266
267    public static boolean computeRedstoneWireColor(int current) {
268        if (redstoneColor == null) {
269            return false;
270        } else {
271            System.arraycopy(redstoneColor[Math.max(Math.min(current, 15), 0)], 0, Colorizer.setColor, 0, 3);
272            return true;
273        }
274    }
275
276    public static int colorizeRedstoneWire(IBlockAccess blockAccess, int i, int j, int k, int defaultColor) {
277        if (redstoneColor == null) {
278            return defaultColor;
279        } else {
280            int metadata = Math.max(Math.min(blockAccess.getBlockMetadata(i, j, k), 15), 0);
281            return Colorizer.float3ToInt(redstoneColor[metadata]);
282        }
283    }
284
285    public static boolean computeWaterColor(double x, double y, double z) {
286        if (Colorizer.useParticleColors && Colorizer.fixedColorMaps[Colorizer.COLOR_MAP_WATER].isCustom()) {
287            int rgb = colorizeBiome(0xffffff, Colorizer.COLOR_MAP_WATER, (int) x, (int) y, (int) z);
288            float[] multiplier = new float[3];
289            Colorizer.intToFloat3(rgb, multiplier);
290            for (int i = 0; i < 3; i++) {
291                waterColor[i] = multiplier[i] * ColorizeEntity.waterBaseColor[i];
292            }
293            return true;
294        } else {
295            return false;
296        }
297    }
298
299    public static void computeWaterColor() {
300        int rgb = colorizeBiome(0xffffff, Colorizer.COLOR_MAP_WATER);
301        Colorizer.intToFloat3(rgb, waterColor);
302    }
303
304    public static void colorizeWaterBlockGL(int blockID) {
305        if (blockID == 8 || blockID == 9) {
306            computeWaterColor();
307            GL11.glColor4f(waterColor[0], waterColor[1], waterColor[2], 1.0f);
308        }
309    }
310}