Search in sources :

Example 1 with ZonePlannerMapChunk

use of buildcraft.robotics.zone.ZonePlannerMapChunk in project BuildCraft by BuildCraft.

the class GuiZonePlanner method rayTrace.

private BlockPos rayTrace(int screenX, int screenY) {
    BlockPos found = null;
    FloatBuffer projectionBuffer = BufferUtils.createFloatBuffer(16);
    FloatBuffer modelViewBuffer = BufferUtils.createFloatBuffer(16);
    IntBuffer viewportBuffer = BufferUtils.createIntBuffer(16);
    GlStateManager.getFloat(GL11.GL_PROJECTION_MATRIX, projectionBuffer);
    GlStateManager.getFloat(GL11.GL_MODELVIEW_MATRIX, modelViewBuffer);
    GlStateManager.glGetInteger(GL11.GL_VIEWPORT, viewportBuffer);
    FloatBuffer positionNearBuffer = BufferUtils.createFloatBuffer(3);
    FloatBuffer positionFarBuffer = BufferUtils.createFloatBuffer(3);
    GLU.gluUnProject(screenX, screenY, 0f, modelViewBuffer, projectionBuffer, viewportBuffer, positionNearBuffer);
    GLU.gluUnProject(screenX, screenY, 1f, modelViewBuffer, projectionBuffer, viewportBuffer, positionFarBuffer);
    Vector3d rayStart = new Vector3d(positionNearBuffer.get(0), positionNearBuffer.get(1), positionNearBuffer.get(2));
    Vector3d rayPosition = new Vector3d(rayStart);
    Vector3d rayDirection = new Vector3d(positionFarBuffer.get(0), positionFarBuffer.get(1), positionFarBuffer.get(2));
    rayDirection.sub(rayStart);
    rayDirection.normalize();
    for (int i = 0; i < 10000; i++) {
        int chunkX = (int) Math.round(rayPosition.getX()) >> 4;
        int chunkZ = (int) Math.round(rayPosition.getZ()) >> 4;
        ZonePlannerMapChunk zonePlannerMapChunk = ZonePlannerMapDataClient.INSTANCE.getChunk(mc.world, new ZonePlannerMapChunkKey(new ChunkPos(chunkX, chunkZ), mc.world.provider.getDimension(), container.tile.getLevel()));
        if (zonePlannerMapChunk != null) {
            BlockPos pos = new BlockPos(Math.round(rayPosition.getX()) - (chunkX << 4), Math.round(rayPosition.getY()), Math.round(rayPosition.getZ()) - (chunkZ << 4));
            MapColourData data = zonePlannerMapChunk.getData(pos.getX(), pos.getZ());
            if (data != null && data.posY >= pos.getY()) {
                found = new BlockPos(pos.getX() + (chunkX << 4), data.posY, pos.getZ() + (chunkZ << 4));
                break;
            }
        } else {
            break;
        }
        rayPosition.add(rayDirection);
    }
    return found;
}
Also used : ZonePlannerMapChunkKey(buildcraft.robotics.zone.ZonePlannerMapChunkKey) Vector3d(javax.vecmath.Vector3d) IntBuffer(java.nio.IntBuffer) BlockPos(net.minecraft.util.math.BlockPos) FloatBuffer(java.nio.FloatBuffer) ChunkPos(net.minecraft.util.math.ChunkPos) ZonePlannerMapChunk(buildcraft.robotics.zone.ZonePlannerMapChunk) MapColourData(buildcraft.robotics.zone.ZonePlannerMapChunk.MapColourData)

Example 2 with ZonePlannerMapChunk

use of buildcraft.robotics.zone.ZonePlannerMapChunk in project BuildCraft by BuildCraft.

the class GuiZonePlanner method drawForegroundLayer.

@SuppressWarnings("PointlessBitwiseExpression")
@Override
protected void drawForegroundLayer() {
    camY += scaleSpeed;
    scaleSpeed *= 0.7F;
    int posX = (int) positionX;
    int posZ = (int) positionZ;
    int dimension = mc.world.provider.getDimension();
    {
        ChunkPos chunkPos = new ChunkPos(posX >> 4, posZ >> 4);
        ZonePlannerMapChunk zonePlannerMapChunk = ZonePlannerMapDataClient.INSTANCE.getChunk(mc.world, new ZonePlannerMapChunkKey(chunkPos, dimension, container.tile.getLevel()));
        BlockPos pos = null;
        if (zonePlannerMapChunk != null) {
            MapColourData data = zonePlannerMapChunk.getData(posX, posZ);
            if (data != null) {
                pos = new BlockPos(posX, data.posY, posZ);
            }
        }
        if (pos != null && pos.getY() + 10 > camY) {
            camY = Math.max(camY, pos.getY() + 10);
        }
    }
    int x = guiLeft;
    int y = guiTop;
    if (lastSelected != null) {
        String text = "X: " + lastSelected.getX() + " Y: " + lastSelected.getY() + " Z: " + lastSelected.getZ();
        fontRenderer.drawString(text, x + 130, y + 130, 0x404040);
    }
    int offsetX = 8;
    int offsetY = 9;
    int sizeX = 213;
    int sizeY = 100;
    GlStateManager.pushMatrix();
    GlStateManager.matrixMode(GL11.GL_PROJECTION);
    GlStateManager.pushMatrix();
    GlStateManager.loadIdentity();
    ScaledResolution scaledResolution = new ScaledResolution(mc);
    int viewportX = (x + offsetX) * scaledResolution.getScaleFactor();
    int viewportY = mc.displayHeight - (sizeY + y + offsetY) * scaledResolution.getScaleFactor();
    int viewportWidth = sizeX * scaledResolution.getScaleFactor();
    int viewportHeight = sizeY * scaledResolution.getScaleFactor();
    GL11.glEnable(GL11.GL_SCISSOR_TEST);
    GL11.glScissor(viewportX, viewportY, viewportWidth, viewportHeight);
    GlStateManager.clear(GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glDisable(GL11.GL_SCISSOR_TEST);
    GlStateManager.viewport(viewportX, viewportY, viewportWidth, viewportHeight);
    GlStateManager.scale(scaledResolution.getScaleFactor(), scaledResolution.getScaleFactor(), 1);
    GLU.gluPerspective(70.0F, (float) sizeX / sizeY, 1F, 10000.0F);
    GlStateManager.matrixMode(GL11.GL_MODELVIEW);
    GlStateManager.loadIdentity();
    RenderHelper.enableStandardItemLighting();
    GlStateManager.enableRescaleNormal();
    // look down
    GlStateManager.rotate(90, 1, 0, 0);
    GlStateManager.pushMatrix();
    GlStateManager.translate(-positionX, -camY, -positionZ);
    GlStateManager.disableBlend();
    GlStateManager.disableAlpha();
    GlStateManager.disableTexture2D();
    int minScreenX = (x + offsetX) * scaledResolution.getScaleFactor();
    int minScreenY = (scaledResolution.getScaledHeight() - (y + offsetY)) * scaledResolution.getScaleFactor();
    int maxScreenX = (x + offsetX + sizeX) * scaledResolution.getScaleFactor();
    int maxScreenY = (scaledResolution.getScaledHeight() - (y + offsetY + sizeY)) * scaledResolution.getScaleFactor();
    int minChunkX = (posX >> 4) - 8;
    int minChunkZ = (posZ >> 4) - 8;
    int maxChunkX = (posX >> 4) + 8;
    int maxChunkZ = (posZ >> 4) + 8;
    // noinspection SuspiciousNameCombination
    List<ChunkPos> chunkPosBounds = Stream.of(Pair.of(minScreenX, minScreenY), Pair.of(minScreenX, maxScreenY), Pair.of(maxScreenX, minScreenY), Pair.of(maxScreenX, maxScreenY)).map(p -> rayTrace(p.getLeft(), p.getRight())).filter(Objects::nonNull).map(ChunkPos::new).collect(Collectors.toList());
    for (ChunkPos chunkPos : chunkPosBounds) {
        if (chunkPos.x < minChunkX) {
            minChunkX = chunkPos.x;
        }
        if (chunkPos.z < minChunkZ) {
            minChunkZ = chunkPos.z;
        }
        if (chunkPos.x > maxChunkX) {
            maxChunkX = chunkPos.x;
        }
        if (chunkPos.z > maxChunkZ) {
            maxChunkZ = chunkPos.z;
        }
    }
    minChunkX--;
    minChunkZ--;
    maxChunkX++;
    maxChunkZ++;
    for (int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) {
        for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) {
            ZonePlannerMapRenderer.INSTANCE.getChunkGlList(new ZonePlannerMapChunkKey(new ChunkPos(chunkX, chunkZ), dimension, container.tile.getLevel())).ifPresent(GlStateManager::callList);
        }
    }
    BlockPos found = null;
    int foundColor = 0;
    if (Mouse.getX() >= minScreenX && Mouse.getY() <= minScreenY && Mouse.getX() <= maxScreenX && Mouse.getY() >= maxScreenY) {
        found = rayTrace(Mouse.getX(), Mouse.getY());
    }
    if (found != null) {
        ZonePlannerMapChunk zonePlannerMapChunk = ZonePlannerMapDataClient.INSTANCE.getChunk(mc.world, new ZonePlannerMapChunkKey(new ChunkPos(found), mc.world.provider.getDimension(), container.tile.getLevel()));
        if (zonePlannerMapChunk != null) {
            MapColourData data = zonePlannerMapChunk.getData(found.getX(), found.getZ());
            if (data != null) {
                foundColor = data.colour;
            }
        }
    }
    if (found != null) {
        GlStateManager.disableDepth();
        GlStateManager.enableBlend();
        GlStateManager.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
        GlStateManager.glLineWidth(2);
        int r = (int) (((foundColor >> 16) & 0xFF) * 0.7);
        int g = (int) (((foundColor >> 8) & 0xFF) * 0.7);
        int b = (int) (((foundColor >> 0) & 0xFF) * 0.7);
        int a = 0x77;
        ZonePlannerMapRenderer.INSTANCE.setColor(r << 16 | g << 8 | b << 0 | a << 24);
        BufferBuilder builder = Tessellator.getInstance().getBuffer();
        builder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
        ZonePlannerMapRenderer.INSTANCE.drawBlockCuboid(builder, found.getX(), found.getY(), found.getZ());
        Tessellator.getInstance().draw();
        GlStateManager.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
        GlStateManager.disableBlend();
        GlStateManager.enableDepth();
    }
    GlStateManager.disableLighting();
    GlStateManager.enableBlend();
    for (int i = 0; i < container.tile.layers.length; i++) {
        if (getPaintbrushBrush() != null && getPaintbrushBrush().colour.getMetadata() != i) {
            continue;
        }
        ZonePlan layer = container.tile.layers[i];
        if (getPaintbrushBrush() != null && getPaintbrushBrush().colour.getMetadata() == i && bufferLayer != null) {
            layer = bufferLayer;
        }
        if (!layer.getChunkPoses().isEmpty()) {
            Tessellator.getInstance().getBuffer().begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
            for (int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) {
                for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) {
                    ChunkPos chunkPos = new ChunkPos(chunkX, chunkZ);
                    for (int blockX = chunkPos.getXStart(); blockX <= chunkPos.getXEnd(); blockX++) {
                        for (int blockZ = chunkPos.getZStart(); blockZ <= chunkPos.getZEnd(); blockZ++) {
                            if (!layer.get(blockX - container.tile.getPos().getX(), blockZ - container.tile.getPos().getZ())) {
                                continue;
                            }
                            int height;
                            ZonePlannerMapChunk zonePlannerMapChunk = ZonePlannerMapDataClient.INSTANCE.getChunk(mc.world, new ZonePlannerMapChunkKey(chunkPos, dimension, container.tile.getLevel()));
                            if (zonePlannerMapChunk != null) {
                                MapColourData data = zonePlannerMapChunk.getData(blockX, blockZ);
                                if (data != null) {
                                    height = data.posY;
                                } else {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            int color = EnumDyeColor.byMetadata(i).getColorValue();
                            int r = (color >> 16) & 0xFF;
                            int g = (color >> 8) & 0xFF;
                            int b = (color >> 0) & 0xFF;
                            int a = 0x55;
                            ZonePlannerMapRenderer.INSTANCE.setColor(r << 16 | g << 8 | b << 0 | a << 24);
                            ZonePlannerMapRenderer.INSTANCE.drawBlockCuboid(Tessellator.getInstance().getBuffer(), blockX, height + 0.1, blockZ, height, 0.6);
                        }
                    }
                }
            }
            Tessellator.getInstance().draw();
        }
    }
    GlStateManager.disableBlend();
    GlStateManager.disableLighting();
    GlStateManager.enableTexture2D();
    lastSelected = found;
    GlStateManager.popMatrix();
    GlStateManager.disableRescaleNormal();
    GlStateManager.matrixMode(GL11.GL_PROJECTION);
    GlStateManager.viewport(0, 0, mc.displayWidth, mc.displayHeight);
    GlStateManager.popMatrix();
    GlStateManager.matrixMode(GL11.GL_MODELVIEW);
    GlStateManager.popMatrix();
    RenderHelper.disableStandardItemLighting();
    GlStateManager.disableBlend();
}
Also used : ItemPaintbrush_BC8(buildcraft.core.item.ItemPaintbrush_BC8) FloatBuffer(java.nio.FloatBuffer) ZonePlannerMapChunkKey(buildcraft.robotics.zone.ZonePlannerMapChunkKey) BufferBuilder(net.minecraft.client.renderer.BufferBuilder) Vector3d(javax.vecmath.Vector3d) GuiIcon(buildcraft.lib.gui.GuiIcon) DefaultVertexFormats(net.minecraft.client.renderer.vertex.DefaultVertexFormats) ItemStack(net.minecraft.item.ItemStack) GuiBC8(buildcraft.lib.gui.GuiBC8) BCCoreItems(buildcraft.core.BCCoreItems) Pair(org.apache.commons.lang3.tuple.Pair) IntBuffer(java.nio.IntBuffer) RenderHelper(net.minecraft.client.renderer.RenderHelper) GL11(org.lwjgl.opengl.GL11) GuiRectangle(buildcraft.lib.gui.pos.GuiRectangle) ZonePlan(buildcraft.robotics.zone.ZonePlan) GLU(org.lwjgl.util.glu.GLU) ZonePlannerMapChunk(buildcraft.robotics.zone.ZonePlannerMapChunk) ScaledResolution(net.minecraft.client.gui.ScaledResolution) GlStateManager(net.minecraft.client.renderer.GlStateManager) ContainerZonePlanner(buildcraft.robotics.container.ContainerZonePlanner) ZonePlannerMapRenderer(buildcraft.robotics.zone.ZonePlannerMapRenderer) ChunkPos(net.minecraft.util.math.ChunkPos) IOException(java.io.IOException) BlockPos(net.minecraft.util.math.BlockPos) Mouse(org.lwjgl.input.Mouse) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) BufferUtils(org.lwjgl.BufferUtils) List(java.util.List) Stream(java.util.stream.Stream) EnumDyeColor(net.minecraft.item.EnumDyeColor) Tessellator(net.minecraft.client.renderer.Tessellator) ResourceLocation(net.minecraft.util.ResourceLocation) MapColourData(buildcraft.robotics.zone.ZonePlannerMapChunk.MapColourData) ZonePlannerMapDataClient(buildcraft.robotics.zone.ZonePlannerMapDataClient) ZonePlannerMapChunkKey(buildcraft.robotics.zone.ZonePlannerMapChunkKey) BufferBuilder(net.minecraft.client.renderer.BufferBuilder) ZonePlannerMapChunk(buildcraft.robotics.zone.ZonePlannerMapChunk) GlStateManager(net.minecraft.client.renderer.GlStateManager) ScaledResolution(net.minecraft.client.gui.ScaledResolution) ZonePlan(buildcraft.robotics.zone.ZonePlan) Objects(java.util.Objects) ChunkPos(net.minecraft.util.math.ChunkPos) BlockPos(net.minecraft.util.math.BlockPos) MapColourData(buildcraft.robotics.zone.ZonePlannerMapChunk.MapColourData)

Example 3 with ZonePlannerMapChunk

use of buildcraft.robotics.zone.ZonePlannerMapChunk in project BuildCraft by BuildCraft.

the class RenderZonePlanner method createTexture.

private static DynamicTextureBC createTexture(TileZonePlanner tile, EnumFacing side) {
    DynamicTextureBC texture = new DynamicTextureBC(TEXTURE_WIDTH, TEXTURE_HEIGHT);
    for (int textureX = 0; textureX < TEXTURE_WIDTH; textureX++) {
        for (int textureY = 0; textureY < TEXTURE_HEIGHT; textureY++) {
            int posX;
            int posZ;
            int scale = 4;
            int offset1 = (textureX - TEXTURE_WIDTH / 2) * scale;
            int offset2 = (textureY - TEXTURE_HEIGHT / 2) * scale;
            switch(side) {
                case NORTH:
                    posX = tile.getPos().getX() + offset1;
                    posZ = tile.getPos().getZ() - offset2;
                    break;
                case EAST:
                    posX = tile.getPos().getX() + offset2;
                    posZ = tile.getPos().getZ() + offset1;
                    break;
                case SOUTH:
                    posX = tile.getPos().getX() + offset1;
                    posZ = tile.getPos().getZ() + offset2;
                    break;
                case WEST:
                default:
                    posX = tile.getPos().getX() - offset2;
                    posZ = tile.getPos().getZ() + offset1;
                    break;
            }
            ChunkPos chunkPos = new ChunkPos(posX >> 4, posZ >> 4);
            texture.setColor(textureX, textureY, -1);
            ZonePlannerMapChunkKey key = new ZonePlannerMapChunkKey(chunkPos, tile.getWorld().provider.getDimension(), tile.getLevel());
            ZonePlannerMapChunk zonePlannerMapChunk = ZonePlannerMapDataClient.INSTANCE.getChunk(tile.getWorld(), key);
            if (zonePlannerMapChunk != null) {
                texture.setColor(textureX, textureY, zonePlannerMapChunk.getColour(posX, posZ) | 0xFF_00_00_00);
            } else {
                return null;
            }
        }
    }
    return texture;
}
Also used : DynamicTextureBC(buildcraft.lib.client.sprite.DynamicTextureBC) ZonePlannerMapChunkKey(buildcraft.robotics.zone.ZonePlannerMapChunkKey) ChunkPos(net.minecraft.util.math.ChunkPos) ZonePlannerMapChunk(buildcraft.robotics.zone.ZonePlannerMapChunk)

Aggregations

ZonePlannerMapChunk (buildcraft.robotics.zone.ZonePlannerMapChunk)3 ZonePlannerMapChunkKey (buildcraft.robotics.zone.ZonePlannerMapChunkKey)3 ChunkPos (net.minecraft.util.math.ChunkPos)3 MapColourData (buildcraft.robotics.zone.ZonePlannerMapChunk.MapColourData)2 FloatBuffer (java.nio.FloatBuffer)2 IntBuffer (java.nio.IntBuffer)2 Vector3d (javax.vecmath.Vector3d)2 BlockPos (net.minecraft.util.math.BlockPos)2 BCCoreItems (buildcraft.core.BCCoreItems)1 ItemPaintbrush_BC8 (buildcraft.core.item.ItemPaintbrush_BC8)1 DynamicTextureBC (buildcraft.lib.client.sprite.DynamicTextureBC)1 GuiBC8 (buildcraft.lib.gui.GuiBC8)1 GuiIcon (buildcraft.lib.gui.GuiIcon)1 GuiRectangle (buildcraft.lib.gui.pos.GuiRectangle)1 ContainerZonePlanner (buildcraft.robotics.container.ContainerZonePlanner)1 ZonePlan (buildcraft.robotics.zone.ZonePlan)1 ZonePlannerMapDataClient (buildcraft.robotics.zone.ZonePlannerMapDataClient)1 ZonePlannerMapRenderer (buildcraft.robotics.zone.ZonePlannerMapRenderer)1 IOException (java.io.IOException)1 List (java.util.List)1