Jump to content

using a 3-Dimentional char array to represent a multiblock structure pattern


TheEpicTekkit

Recommended Posts

Hi.

 

I have been working on a multiblock system for my mod, this mod will have a lot of multiblocks in it in the future, so I want this system to be dynamic and easy to use. So I decided to use a 3D array of characters to represent blocks in the multiblock.

 

I have this working pretty smothely, but I am now stuck on actually comparing blocks in the world to the 3D char array.

 

These are the classes that handle the characters:

 

MultiblockHandler.java

 

 

package net.modulartechnology.modtech.moduleModtech.common.multiblocks;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.modulartechnology.modtech.moduleModtech.common.init.ModBlocks;

import java.util.HashMap;
import java.util.Map;

/**
* Handles multiblocks. Set the shape of the multiblock
* using chars to represent blocks.
* Note: the chars can be anything you want, but the master MUST be an 'M'
* Note: the char can not be a '0' - this represents invalid
* @author TheEpicTekkit
*/
public class MultiblockHandler
{

    public static MultiblockPattern patternDistillationChamber;

    public static void init()
    {
        initDistillationChamber();
    }

    public static void initDistillationChamber()
    {
        Map<Character, Block> blocks = new HashMap<Character, Block>();

        blocks.put('D', ModBlocks.distillation_chamber);
        blocks.put('M', ModBlocks.distillation_chamber); //master
        blocks.put('V', ModBlocks.distillation_chamber); //This should be ModBlocks.distillation_chamber_valve but I temperarily changed it to distillation_chamber because the actual rotation of the multiblock is not implemented properly yet.
        blocks.put('A', Blocks.air);

        char[][][] structure =
                {
                        { //Bottom
                                {'A','A','A','A','A'}, //000, 001, 002, 003, 004
                                {'A','D','D','D','A'}, //010, 011, 012, 013, 014
                                {'A','D','M','D','A'}, //020, 021, 022, 023, 024
                                {'A','D','D','D','A'}, //030, 031, 032, 033, 034
                                {'A','A','A','A','A'}  //040, 041, 042, 043, 044
                        },
                        { //L1
                                {'A','D','D','D','A'}, //100, 101, 102, 103, 104
                                {'D','A','A','A','D'}, //110, 111, 112, 113, 114
                                {'D','A','A','A','D'}, //120, 121, 122, 123, 124
                                {'D','A','A','A','D'}, //130, 131, 132, 133, 134
                                {'A','D','D','D','A'}  //140, 141, 142, 143, 144
                        },
                        { //L2 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L3
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L4 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L5
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L6 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L7
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L8 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L9
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L10 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L11
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L12 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L13
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L14 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L15
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L16 (Valve)
                                {'A','D','V','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        },
                        { //L17
                                {'A','D','D','D','A'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'D','A','A','A','D'},
                                {'A','D','D','D','A'}
                        }

                };

        patternDistillationChamber = new MultiblockPattern(5, 17, 5, blocks, structure);
    }
}

 

 

MultiblockPattern.java

 

 

package net.modulartechnology.modtech.moduleModtech.common.multiblocks;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.modulartechnology.modtech.moduleModtech.common.energy.network.Node;
import org.lwjgl.util.vector.Matrix4f;

import java.util.*;

/**
* @author TheEpicTekkit
*/
public class MultiblockPattern
{

    private List<Character[][]> layers = new ArrayList<Character[][]>();
    private Map<Character, Block> keys;
    private int width, height, depth;
    private Node relativeMasterLocation; //This is redundant
    private int numBlocks; //This nis also redundant


    public MultiblockPattern(int width, int height, int depth, Map<Character, Block> keys, char[][]... layerMatrix)
    {
        this.width = width;
        this.height = height;
        this.depth = depth;

        this.keys = keys;
        for (char[][] c : layerMatrix)
        {
            Character[][] layer = new Character[width][depth];

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < depth; j++)
                {
                    layer[i][j] = new Character(c[i][j]);
                }
            }

            numBlocks = layerMatrix.length * layerMatrix[0].length * layerMatrix[0][0].length;
            this.layers.add(layer);
        }


        for (int i = 0; i < width; i++)
        {
            for (int j = height; j > 0; j--)
            {
                for (int k = 0; k < depth; k++)
                {
                    if (layerMatrix[j][i][k] == 'M')
                    {
                        relativeMasterLocation = new Node(i, j, k);
                    }
                }
            }
        }
    }

    public List<Character[][]> getLayers()
    {
        return this.layers;
    }

    public Block getValue(char c)
    {
        if (keys == null || keys.get(c) == null)
        {
            return null;
        }

        return keys.get(c);
    }

    public char getKey(Block block)
    {
        for (char c : keys.keySet())
        {
            if (keys.get(c).equals(block))
            {
                return c;
            }
        }
        return '0';
    }

    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }

    public int getDepth()
    {
        return depth;
    }

    public int getNumBlocks()
    {
        int numBlocks = 0;

        for (int i = 0; i < layers.size(); i++)
        {
            Character[][] layer = layers.get(i);

            for (int j = 0; j < layer.length; j++)
            {
                for (int k = 0; k < layer[0].length; k++)
                {
                    Block block = keys.get(layer[j][k]);
                    if (block != null && block != Blocks.air && layer[j][k] != '0')
                    {
                        numBlocks++;
                    }
                }
            }
        }
        return numBlocks; //213 blocks in total excluding air blocks
    }

    /**
     * Rotate a matrix (2D array) 90 degrees counterclockwise
     * @param arr
     * @return
     */
    public char[][] rotate(char[][] arr) //This method isn't used yet
    {
        char[][] newArray = new char[arr[0].length][arr.length];

        for (int i = 0; i < arr[0].length; i++)
        {
            for (int j = arr.length - 1; j >= 0; j--)
            {
                newArray[i][j] = arr[j][i];
            }
        }
        return newArray;
    }

    /**
     * the integer array must have a size of 4 and contain only numbers between 0 and 4
     * @param orientations
     */
    public void setPossibleOrientations(int[] orientations)
    {
        assert (orientations.length <= 4 && orientations.length > 0) : String.format("Orientation matrix for %s must be between 0 and 4", this);


    }

    public Node getRelativeMasterOffset()
    {
        Node result = new Node(-1, -1, -1);

        //000, 001, 002, 003, 004
        //010, 011, 012, 013, 014
        //020, 021, 022, 023, 024
        //030, 031, 032, 033, 034
        //040, 041, 042, 043, 044

        //100, 101, 102, 103, 104
        //110, 111, 112, 113, 114
        //120, 121, 122, 123, 124
        //130, 131, 132, 133, 134
        //140, 141, 142, 143, 144

        for (int i = 0; i < getHeight(); i++)
        {
            Character[][] layer = layers.get(i);

            for (int j = 0; j < getWidth(); j++)
            {
                for (int k = 0; k < getDepth(); k++)
                {
                    if (layer[j][k] == 'M')
                    {
                        result = new Node(j, i, k);
                        return result;
                    }
                }
            }
        }

        return result;
    }

    /**
     * Returns the offset of the block from the master.
     * @param x
     * @param y
     * @param z
     * @return
     */
    public Node getOffsetFromMaster(int x, int y, int z)
    {
        Node master = getRelativeMasterOffset();
        Node result = new Node(master.x() - x, master.y() - y, master.z() - z);
        return result;
    }

    public String toString()
    {
        return this.getClass().getName();
    }
}

 

 

 

Here is the method that is currently called in the tileentity every 2 seconds to check the form: (This isn't working)

 

 

public boolean checkMultiBlockForm()
    {

        Block a = pattern.getValue('A');
        Block d = pattern.getValue('D');
        Block v = pattern.getValue('V');
        Block m = pattern.getValue('M');

        int index = pattern.getNumBlocks();

        for (int i = -pattern.getHeight(); i < pattern.getHeight(); i++)
        {
            for (int j = -pattern.getWidth(); j < pattern.getWidth(); j++)
            {
                for (int k = -pattern.getDepth(); k < pattern.getDepth(); k++)
                {
                    int x = xCoord + i;
                    int y = yCoord + j;
                    int z = zCoord + k;

                    TileEntity tile = worldObj.getTileEntity(x, y, z);
                    Block block = worldObj.getBlock(x, y, z);

                    if (tile != null && block != null && tile instanceof TileEntityDistillationChamber)
                    {
                        index++;
                        Node masterOffset = pattern.getOffsetFromMaster(i, j, k);
                        TileEntity master = worldObj.getTileEntity(xCoord + masterOffset.x(), yCoord + masterOffset.y(), zCoord + masterOffset.z());
                        if (master != null && master instanceof TileEntityDistillationChamber)
                        {
                            this.master = (TileEntityDistillationChamber) master;
                        }
                    }
                }
            }
        }


        if (master != null && this == master) System.out.println(index + " / " + pattern.getNumBlocks() + " for master at " + master.xCoord + ", " + master.yCoord + ", " + master.zCoord);
        if ((index) == pattern.getNumBlocks())
        {
            return true;
        }
        return false;
    }

 

 

 

Any help would be much appreciated.

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Link to comment
Share on other sites

console is being weird...

[15:51:27] [Client thread/INFO] [sTDOUT]: [net.modulartechnology.modtech.moduleModtech.common.tileentity.TileEntityDistillationChamber:checkMultiBlockForm:245]: 285 / 213 for master at 166, 81, 230
[15:51:27] [Client thread/INFO] [sTDOUT]: [net.modulartechnology.modtech.moduleModtech.common.tileentity.TileEntityDistillationChamber:checkMultiBlockForm:245]: 285 / 213 for master at 164, 81, 232
[15:51:27] [server thread/INFO] [sTDOUT]: [net.modulartechnology.modtech.moduleModtech.common.tileentity.TileEntityDistillationChamber:checkMultiBlockForm:245]: 282 / 213 for master at 164, 65, 232
[15:51:28] [Client thread/INFO] [sTDOUT]: [net.modulartechnology.modtech.moduleModtech.common.tileentity.TileEntityDistillationChamber:checkMultiBlockForm:245]: 282 / 213 for master at 164, 65, 232

 

its finding multiple masters, and 282 out of 213 blocks in the structure...

 

I feel like my method to check the pattern needs to be completely redone ;/

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Link to comment
Share on other sites

Okay, I just realised that int x, y, and z were being set wrong in the method. x was being set to xCoord + i whixh is the height, and y was yCoord + j which is the width.

I fixed this, and now its finding even more masters and has 426 out of 213 blocks... so its finding twice the number of blocks there are.

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Link to comment
Share on other sites

Again, that's because your

index

value is wrong. You are initially setting the

index

value to the number of blocks in the structure (213), loop through the structure and increment the

index

213 times, which results in a total of 628 blocks.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.