From 283a723e389fed084a7755e3423a4f0eda063767 Mon Sep 17 00:00:00 2001 From: Chicken Bones Date: Mon, 3 Jun 2013 13:30:36 +1000 Subject: [PATCH] Completed levers, added redstone interaction --- codechicken/microblock/CommonMicroblock.scala | 2 + codechicken/multipart/BlockMultipart.scala | 21 +++ codechicken/multipart/IRedstonePart.scala | 166 ++++++++++++++++++ codechicken/multipart/PartMap.java | 88 +++++++--- codechicken/multipart/TMultiPart.scala | 1 + codechicken/multipart/TileMultipart.scala | 12 ++ codechicken/multipart/handler/Proxies.scala | 1 + .../multipart/minecraft/LeverPart.java | 19 +- 8 files changed, 289 insertions(+), 21 deletions(-) create mode 100644 codechicken/multipart/IRedstonePart.scala diff --git a/codechicken/microblock/CommonMicroblock.scala b/codechicken/microblock/CommonMicroblock.scala index 0c8523b..fce8fc6 100644 --- a/codechicken/microblock/CommonMicroblock.scala +++ b/codechicken/microblock/CommonMicroblock.scala @@ -247,6 +247,8 @@ abstract class Microblock(var shape:Byte = 0, var material:Int = 0) extends TCub } def isTransparent = MicroMaterialRegistry.getMaterial(material).isTransparent + + override def blocksRedstone = true } trait CommonMicroblockClient extends CommonMicroblock with MicroblockClient with TMicroOcclusionClient diff --git a/codechicken/multipart/BlockMultipart.scala b/codechicken/multipart/BlockMultipart.scala index 0beda0a..b384ef5 100644 --- a/codechicken/multipart/BlockMultipart.scala +++ b/codechicken/multipart/BlockMultipart.scala @@ -237,6 +237,27 @@ trait BlockMultipart extends Block val hitInfo:(Int, _) = ExtendedMOP.getData(hit) tile.partList(hitInfo._1).click(player, hit, player.getHeldItem()) } + + override def isProvidingStrongPower(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int):Int = + { + val tile = getTile(world, x, y, z) + if(tile != null) return tile.strongPowerLevel(side^1) + return 0 + } + + override def isProvidingWeakPower(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int):Int = + { + val tile = getTile(world, x, y, z) + if(tile != null) return tile.weakPowerLevel(side^1) + return 0 + } + + override def canConnectRedstone(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int):Boolean = + { + val tile = getTile(world, x, y, z) + if(tile != null) return tile.canConnectRedstone(side) + return false + } } class BlockMultipartImpl(id:Int) extends Block(id, Material.rock) with BlockMultipart diff --git a/codechicken/multipart/IRedstonePart.scala b/codechicken/multipart/IRedstonePart.scala new file mode 100644 index 0000000..7a7c9cd --- /dev/null +++ b/codechicken/multipart/IRedstonePart.scala @@ -0,0 +1,166 @@ +package codechicken.multipart + +import net.minecraft.world.World +import net.minecraft.block.Block +import net.minecraft.block.BlockRedstoneWire +import net.minecraft.world.IBlockAccess +import net.minecraft.util.Direction + +trait IRedstonePart +{ + def strongPowerLevel(side:Int):Int + def weakPowerLevel(side:Int):Int + def canConnectRedstone(side:Int):Boolean +} + +/** + * For parts like wires that adhere to a specific face, reduces redstone connections to the specific edge between two faces + */ +trait IFaceRedstonePart extends IRedstonePart +{ + def getFace():Int +} + +trait TRedstoneTile extends TileMultipart with IRedstoneConnector +{ + import PartMap._ + import RedstoneInteractions._ + + override def strongPowerLevel(side:Int):Int = + { + var max = 0 + partList.foreach(p => + if(p.isInstanceOf[IRedstonePart]) + { + val l = p.asInstanceOf[IRedstonePart].strongPowerLevel(side) + if(l > max) max = l + } + ) + return max + } + + def openConnections(side:Int):Int = + { + if(blocksRedstone(side)) + return 0 + + var m = 0x10 + var i = 0 + while(i < 4) + { + if(!blocksRedstone(edgeBetween(side, rotateSide(side, i)))) + m|=1< + if((connectionMask(p, side)&mask) > 0) + { + val l = p.asInstanceOf[IRedstonePart].weakPowerLevel(side) + if(l > max) max = l + }) + return max + } + + override def canConnectRedstone(side:Int):Boolean = + { + val vside = vanillaToSide(side) + return (getConnectionMap(vside) & otherConnectionMap(worldObj, xCoord, yCoord, zCoord, vside)) > 0 + } + + def getConnectionMap(side:Int):Int = + { + val mask = openConnections(side) + var res = 0 + partList.foreach(p => + res|=connectionMask(p, side)&mask) + return res + } + + def connectionMask(p:TMultiPart, side:Int):Int = + { + if(p.isInstanceOf[IRedstonePart] && p.asInstanceOf[IRedstonePart].canConnectRedstone(side)) + { + if(p.isInstanceOf[IFaceRedstonePart]) + return rotationTo(side, p.asInstanceOf[IFaceRedstonePart].getFace) + return 0x1F + } + return 0 + } +} + +trait IRedstoneConnector +{ + def getConnectionMap(side:Int):Int +} + +trait IRedstoneConnectorBlock +{ + def getConnectionMap(x:Int, y:Int, z:Int, side:Int):Int +} + +object RedstoneInteractions +{ + import net.minecraft.util.Facing._ + + val vanillaSideMap = Array(-2, -1, 0, 2, 3, 1) + val sideVanillaMap = Array(1, 2, 5, 3, 4) + + def vanillaToSide(vside:Int) = sideVanillaMap(vside+1) + + def otherConnectionMap(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int):Int = + getConnectionMap(world, x+offsetsXForSide(side), y+offsetsYForSide(side), z+offsetsZForSide(side), side^1) + + def getConnectionMap(world:IBlockAccess, x:Int, y:Int, z:Int, side:Int):Int = + { + val tile = world.getBlockTileEntity(x, y, z) + if(tile.isInstanceOf[IRedstoneConnector]) + return tile.asInstanceOf[IRedstoneConnector].getConnectionMap(side) + + val block = Block.blocksList(world.getBlockId(x, y, z)) + if(block == null) + return 0 + + if(block.isInstanceOf[IRedstoneConnectorBlock]) + return block.asInstanceOf[IRedstoneConnectorBlock].getConnectionMap(x, y, z, side) + + if(side == 0) + return 0 + + if(block == Block.redstoneWire) + { + if(side != 1) + return 4 + return 0x1F + } + + if(block == Block.redstoneComparatorActive || block == Block.redstoneComparatorIdle) + { + if(side != 1) + return 4 + return 0 + } + + val vside = vanillaSideMap(side) + if(block == Block.redstoneRepeaterActive || block == Block.redstoneRepeaterIdle)//stupid minecraft hardcodes + { + val meta = world.getBlockMetadata(x, y, z); + if(vside == (meta & 3) || vside == Direction.rotateOpposite(meta & 3)) + return 4 + return 0 + } + + if(block.canConnectRedstone(world, x, y, z, vside)) + return 0x1F + + return 0 + } +} \ No newline at end of file diff --git a/codechicken/multipart/PartMap.java b/codechicken/multipart/PartMap.java index bcd0f58..2bb3faa 100644 --- a/codechicken/multipart/PartMap.java +++ b/codechicken/multipart/PartMap.java @@ -1,5 +1,7 @@ package codechicken.multipart; +import java.util.Arrays; + public enum PartMap { BOTTOM(0), @@ -9,26 +11,26 @@ public enum PartMap WEST(4), EAST(5), CENTER(6), - CORNER_NNN(7), - CORNER_NPN(8), - CORNER_NNP(9), - CORNER_NPP(10), - CORNER_PNN(11), - CORNER_PPN(12), - CORNER_PNP(13), - CORNER_PPP(14), - EDGE_NYN(15), - EDGE_NYP(16), - EDGE_PYN(17), - EDGE_PYP(18), - EDGE_NNZ(19), - EDGE_PNZ(20), - EDGE_NPZ(21), - EDGE_PPZ(22), - EDGE_XNN(23), - EDGE_XPN(24), - EDGE_XNP(25), - EDGE_XPP(26); + CORNER_NNN(7),//0 + CORNER_NPN(8),//1 + CORNER_NNP(9),//2 + CORNER_NPP(10),//3 + CORNER_PNN(11),//4 + CORNER_PPN(12),//5 + CORNER_PNP(13),//6 + CORNER_PPP(14),//7 + EDGE_NYN(15),//0 + EDGE_NYP(16),//1 + EDGE_PYN(17),//2 + EDGE_PYP(18),//3 + EDGE_NNZ(19),//4 + EDGE_PNZ(20),//5 + EDGE_NPZ(21),//6 + EDGE_PPZ(22),//7 + EDGE_XNN(23),//8 + EDGE_XPN(24),//9 + EDGE_XNP(25),//10 + EDGE_XPP(26);//11 public final int i; public final int mask; @@ -86,4 +88,50 @@ public enum PartMap } throw new IllegalArgumentException("Switch Falloff"); } + + private static int[] edgeBetweenMap = new int[]{ + -1, -1, 8, 10, 4, 5, + -1, -1, 9, 11, 6, 7, + -1, -1,-1, -1, 0, 2, + -1, -1,-1, -1, 1, 3}; + + public static int edgeBetween(int s1, int s2) + { + if(s2 < s1) + return edgeBetween(s2, s1); + if((s1&6) == (s2&6)) + throw new IllegalArgumentException("Faces "+s1+" and "+s2+" are opposites"); + return 15+edgeBetweenMap[s1*6+s2]; + } + + private static int[] sideRotMap = new int[]{ + 3,5,2,4, + 3,4,2,5, + 1,5,0,4, + 1,4,0,5, + 1,2,0,3, + 1,3,0,2}; + + public static int rotateSide(int s, int r) + { + return sideRotMap[s<<2|r]; + } + + private static int[] rotSideMap = new int[]{ + -1,-1, 2, 0, 3, 1, + -1,-1, 2, 0, 1, 3, + 2, 0,-1,-1, 3, 1, + 2, 0,-1,-1, 1, 3, + 2, 0, 1, 3,-1,-1, + 2, 0, 3, 1,-1,-1}; + + /** + * Reverse of rotateSide + */ + public static int rotationTo(int s1, int s2) + { + if((s1&6) == (s2&6)) + throw new IllegalArgumentException("Faces "+s1+" and "+s2+" are opposites"); + return rotSideMap[s1*6+s2]; + } } diff --git a/codechicken/multipart/TMultiPart.scala b/codechicken/multipart/TMultiPart.scala index 8ae6076..039052a 100644 --- a/codechicken/multipart/TMultiPart.scala +++ b/codechicken/multipart/TMultiPart.scala @@ -37,6 +37,7 @@ abstract class TMultiPart } def getSlotMask:Int = 0 + def blocksRedstone:Boolean = false//Not necessarily sure this is the best way to test this def occlusionTest(npart:TMultiPart):Boolean = true def getSubParts:JSeq[IndexedCuboid6] = Seq() diff --git a/codechicken/multipart/TileMultipart.scala b/codechicken/multipart/TileMultipart.scala index 3a280ab..73a0a65 100644 --- a/codechicken/multipart/TileMultipart.scala +++ b/codechicken/multipart/TileMultipart.scala @@ -303,6 +303,18 @@ trait TileMultipart extends TileEntity }} tag.setTag("parts", taglist) } + + def strongPowerLevel(side:Int) = 0 + + def weakPowerLevel(side:Int) = 0 + + def canConnectRedstone(side:Int) = false + + def notifyNeighborChange(side:Int) + { + val pos = new BlockCoord(this).offset(side) + worldObj.notifyBlocksOfNeighborChange(pos.x, pos.y, pos.z, getBlockType().blockID) + } } trait TileMultipartClient extends TileMultipart diff --git a/codechicken/multipart/handler/Proxies.scala b/codechicken/multipart/handler/Proxies.scala index 28abfa1..abcbed2 100644 --- a/codechicken/multipart/handler/Proxies.scala +++ b/codechicken/multipart/handler/Proxies.scala @@ -35,6 +35,7 @@ class MultipartProxy_serverImpl MultipartGenerator.registerTrait("net.minecraftforge.liquids.ITankContainer", "codechicken.multipart.TLiquidTank") MultipartGenerator.registerTrait("codechicken.multipart.JPartialOcclusion", "codechicken.multipart.TPartialOcclusionTile") + MultipartGenerator.registerTrait("codechicken.multipart.IRedstonePart", "codechicken.multipart.TRedstoneTile") MultipartGenerator.registerTrait("codechicken.multipart.IRandomDisplayTick", "codechicken.multipart.TRandomDisplayTickTile", null) } diff --git a/codechicken/multipart/minecraft/LeverPart.java b/codechicken/multipart/minecraft/LeverPart.java index c1bb34c..019e1fb 100644 --- a/codechicken/multipart/minecraft/LeverPart.java +++ b/codechicken/multipart/minecraft/LeverPart.java @@ -13,8 +13,9 @@ import codechicken.core.lighting.LazyLightMatrix; import codechicken.core.vec.BlockCoord; import codechicken.core.vec.Cuboid6; import codechicken.core.vec.Vector3; +import codechicken.multipart.IRedstonePart; -public class LeverPart extends McSidedMetaPart +public class LeverPart extends McSidedMetaPart implements IRedstonePart { public static BlockLever lever = (BlockLever) Block.lever; public static int[] metaSideMap = new int[]{1, 4, 5, 2, 3, 0, 0, 1}; @@ -101,6 +102,7 @@ public class LeverPart extends McSidedMetaPart meta = (byte) (meta^8); sendDescUpdate(); tile().notifyPartChange(); + tile().notifyNeighborChange(metaSideMap[meta&7]); tile().markDirty(); return true; } @@ -112,4 +114,19 @@ public class LeverPart extends McSidedMetaPart renderBlocks.renderBlockLever(lever, getTile().xCoord, getTile().yCoord, getTile().zCoord); renderBlocks.blockAccess = actual; } + + public int weakPowerLevel(int side) + { + return (meta & 8) > 0 ? 15 : 0; + } + + public int strongPowerLevel(int side) + { + return (meta & 8) > 0 && side == metaSideMap[meta&7] ? 15 : 0; + } + + public boolean canConnectRedstone(int side) + { + return true; + } }