aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/detrav/gui/textures/DetravMapTexture.java
blob: 56db8717c73c9ebfd85bc6e183e6368fb5d9dd2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.detrav.gui.textures;

import com.detrav.net.ProspectingPacket;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.resources.IResourceManager;
import org.lwjgl.opengl.GL11;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

/**
 * Created by wital_000 on 21.03.2016.
 */
public class DetravMapTexture extends AbstractTexture {

    public final ProspectingPacket packet;
    private String selected = "All";
    public int width = -1;
    public int height = -1;

    public DetravMapTexture(ProspectingPacket aPacket)
    {
        packet = aPacket;
    }

    private BufferedImage getImage() {
        int wh = (packet.size * 2 + 1) * 16;

        BufferedImage image = new BufferedImage(wh, wh, BufferedImage.TYPE_INT_ARGB);
        WritableRaster raster = image.getRaster();

        int playerI = packet.posX - (packet.chunkX - packet.size) * 16 - 1; // Correct player offset
        int playerJ = packet.posZ - (packet.chunkZ - packet.size) * 16 - 1;
        for (int i = 0; i < wh; i++) {
            for (int j = 0; j < wh; j++) {
                image.setRGB(i, j, Color.WHITE.getRGB());
                if (packet.map[i][j] != null) {
                    if (packet.ptype == 0 || packet.ptype == 1) {
                        for (short meta : packet.map[i][j].values()) {
                            final String name = packet.metaMap.get(meta);
                            if (!selected.equals("All") && !selected.equals(name)) continue;
                            image.setRGB(i, j, packet.ores.get(name) | 0XFF000000);
                            break;
                        }
                    } else if (packet.ptype == 2) {
                        final short fluidId = packet.map[i][j].get((byte) 1), fluidSize = packet.map[i][j].get((byte) 2);
                        final String name = packet.metaMap.get(fluidId);
                        
                        // Variables used to locate within a chunk.
                        final int k = (i % 16), l = (j % 16);
                        
                        if( ( (k + l * 16) * 3) < (fluidSize + 48) && (selected.equals("All") || selected.equals(name))) {
                            image.setRGB(i, j, packet.ores.get(name) | 0XFF000000);
                        }
                    }else if (packet.ptype == 3) {
                        final short meta = packet.map[i][j].get((byte) 1);
                        image.setRGB(i, j, ((meta & 0xFF) << 16) + ((meta & 0xFF) << 8) + ((meta & 0xFF)) | 0XFF000000);                        
                    }
                }
                // draw player pos
                if (i == playerI || j == playerJ) {
                    raster.setSample(i, j, 0, (raster.getSample(i, j, 0) + 255) / 2);
                    raster.setSample(i, j, 1, raster.getSample(i, j, 1) / 2);
                    raster.setSample(i, j, 2, raster.getSample(i, j, 2) / 2);
                }
                // draw grid
                if ((i) % 16 == 0 || (j) % 16 == 0) {
                    raster.setSample(i, j, 0, raster.getSample(i, j, 0) / 2);
                    raster.setSample(i, j, 1, raster.getSample(i, j, 1) / 2);
                    raster.setSample(i, j, 2, raster.getSample(i, j, 2) / 2);
                }

            }
        }
        
        return image;
    }
    
    @Override
    public void loadTexture(IResourceManager resourceManager){
        this.deleteGlTexture();
        if(packet != null) {
            int tId = getGlTextureId();
            if(tId <0) return;
            TextureUtil.uploadTextureImageAllocate(this.getGlTextureId(), getImage(), false, false);
            width = packet.getSize();
            height = packet.getSize();
        }
    }
    
    public void loadTexture(IResourceManager resourceManager, String selected){
        this.selected = selected;
        loadTexture(resourceManager);
    }

    public int glBindTexture() {
        if (this.glTextureId < 0) return this.glTextureId;
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.getGlTextureId());
        return this.glTextureId;
    }

    public void draw(int x, int y) {
        float f = 1F / (float)width;
        float f1 = 1F / (float)height;
        int u = 0, v = 0;
        Tessellator tessellator = Tessellator.instance;
        tessellator.startDrawingQuads();
        tessellator.addVertexWithUV((double)(x), (double)(y + height), 0, (double)((float)(u) * f), (double)((float)(v + height) * f1));
        tessellator.addVertexWithUV((double)(x + width), (double)(y + height), 0, (double)((float)(u + width) * f), (double)((float)(v + height) * f1));
        tessellator.addVertexWithUV((double)(x + width), (double)(y), 0, (double)((float)(u + width) * f), (double)((float)(v) * f1));
        tessellator.addVertexWithUV((double)(x), (double)(y), 0, (double)((float)(u) * f), (double)((float)(v) * f1));
        tessellator.draw();
    }

}