Reworked some wire connection logic

This commit is contained in:
Calclavia 2014-11-04 21:41:07 +08:00
parent ec19776cb6
commit 60341da346
3 changed files with 106 additions and 110 deletions

View file

@ -52,7 +52,7 @@ trait TNodePartConnector extends PartAbstract with INodeProvider
override def getNode[N <: INode](nodeType: Class[_ <: N], from: ForgeDirection): N = override def getNode[N <: INode](nodeType: Class[_ <: N], from: ForgeDirection): N =
{ {
if (node.getClass.isAssignableFrom(nodeType)) if (nodeType.isAssignableFrom(node.getClass))
return node.asInstanceOf[N] return node.asInstanceOf[N]
return null.asInstanceOf[N] return null.asInstanceOf[N]

View file

@ -75,46 +75,4 @@ trait TWire extends PartAbstract with TNodePartConnector with TMaterial[WireMate
super[TInsulatable].save(nbt) super[TInsulatable].save(nbt)
super[TColorable].save(nbt) super[TColorable].save(nbt)
} }
/**
* TODO: This is very messy. Need to reorganize canConnectTo
* Can this conductor connect with another potential wire object?
*/
def canConnectTo(obj: AnyRef): Boolean =
{
//If the object is a wire
if (obj != null && obj.getClass == getClass)
{
val wire = obj.asInstanceOf[TWire]
if (material == wire.material)
{
if (insulated && wire.insulated)
return this.getColor == wire.getColor || (getColor == TColorable.defaultColor || wire.getColor == TColorable.defaultColor)
return true
}
}
return false
}
/**
* Can this conductor connect with another potential wire object AND a DCNode?
*/
def canConnectTo(obj: AnyRef, from: ForgeDirection): Boolean =
{
if (canConnectTo(obj))
return true
else if (obj.isInstanceOf[INodeProvider])
{
//Object not a wire
val node = obj.asInstanceOf[INodeProvider].getNode(classOf[DCNode], from)
if (node != null)
return node.asInstanceOf[DCNode].canConnect(from)
}
return false
}
} }

View file

@ -19,7 +19,7 @@ import resonant.api.grid.INodeProvider
import resonant.lib.grid.node.DCNode import resonant.lib.grid.node.DCNode
import resonantinduction.core.prefab.node.TMultipartNode import resonantinduction.core.prefab.node.TMultipartNode
import resonantinduction.core.prefab.part.ChickenBonesWrapper._ import resonantinduction.core.prefab.part.ChickenBonesWrapper._
import resonantinduction.core.prefab.part.connector.PartAbstract import resonantinduction.core.prefab.part.connector.{PartAbstract, TColorable}
import resonantinduction.core.util.MultipartUtil import resonantinduction.core.util.MultipartUtil
import resonantinduction.electrical.wire.base.TWire import resonantinduction.electrical.wire.base.TWire
@ -177,7 +177,7 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
if (packetID == 3) if (packetID == 3)
{ {
connectionMask = packet.readInt connectionMask = packet.readInt
tile.markRender tile.markRender()
} }
} }
@ -293,10 +293,7 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
* Rendering * Rendering
*/ */
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
def getIcon: IIcon = def getIcon: IIcon = RenderFlatWire.wireIcon
{
return RenderFlatWire.wireIcon
}
def useStaticRenderer: Boolean = true def useStaticRenderer: Boolean = true
@ -344,8 +341,6 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
{ {
override def reconstruct() override def reconstruct()
{ {
super.reconstruct()
if (!world.isRemote) if (!world.isRemote)
{ {
directionMap.clear() directionMap.clear()
@ -388,14 +383,20 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
{ {
val part = tile.partMap(absDir) val part = tile.partMap(absDir)
val to = ForgeDirection.getOrientation(absDir) val to = ForgeDirection.getOrientation(absDir)
val from = to.getOpposite
if (canConnectTo(part)) if(part != null)
{
val node = part.asInstanceOf[INodeProvider].getNode(classOf[DCNode], from)
if (canConnect(node, to))
{ {
//TODO: Check dir //TODO: Check dir
connect(part.asInstanceOf[INodeProvider].getNode(classOf[DCNode], to.getOpposite), to) connect(node, to)
skip = true skip = true
} }
} }
}
if ((calculatedMask & (1 << absDir)) == 0 && !skip) if ((calculatedMask & (1 << absDir)) == 0 && !skip)
{ {
@ -422,35 +423,31 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
if (tilePart != null && r != -1) if (tilePart != null && r != -1)
{ {
val part = tilePart.partMap(side) val part = tilePart.partMap(side)
if (canConnectTo(part, fromDir))
{
val otherR = (r + 2) % 4
val dcNode = getComponent(part, fromDir) val dcNode = getComponent(part, fromDir)
//Check if it's another flat wire. //Check if it's another flat wire.
if (dcNode != null) if (canConnect(dcNode, toDir))
{ {
val otherR = (r + 2) % 4
if (part.isInstanceOf[PartFlatWire]) if (part.isInstanceOf[PartFlatWire])
{ {
val wire = part.asInstanceOf[PartFlatWire] val wire = part.asInstanceOf[PartFlatWire]
if (wire.canConnectTo(PartFlatWire.this, fromDir) && wire.maskOpen(otherR)) //Check other wire connectability
if (dcNode.canConnect(this, fromDir) && wire.maskOpen(otherR))
{ {
connect(dcNode, toDir) connect(dcNode, toDir)
return true return true
} }
} }
else if (canConnectTo(part)) else if (canConnect(part, toDir))
{ {
connect(dcNode, toDir) connect(dcNode, toDir)
return true return true
} }
} }
}
disconnect(absDir) disconnect(absDir)
} }
@ -458,11 +455,11 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
* Can't find another wire. Try TileEntity. * Can't find another wire. Try TileEntity.
*/ */
val tileEntity = pos.getTileEntity(world) val tileEntity = pos.getTileEntity(world)
val tileComponent = getComponent(tileEntity, fromDir) val dcNode = getComponent(tileEntity, fromDir)
if (canConnectTo(tileEntity, fromDir)) if (canConnect(dcNode, fromDir))
{ {
connect(tileComponent, toDir) connect(dcNode, toDir)
return true return true
} }
@ -486,11 +483,12 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
val part = tpCorner.partMap(absDir ^ 1) val part = tpCorner.partMap(absDir ^ 1)
val absToDir = ForgeDirection.getOrientation(absDir) val absToDir = ForgeDirection.getOrientation(absDir)
val absFromDir = ForgeDirection.getOrientation(absDir).getOpposite val absFromDir = ForgeDirection.getOrientation(absDir).getOpposite
val node = part.asInstanceOf[INodeProvider].getNode(classOf[DCNode], absFromDir)
if (canConnectTo(part, absFromDir)) if (canConnect(node, absFromDir))
{ {
//TODO: Check dir //TODO: Check dir
connect(part.asInstanceOf[INodeProvider].getNode(classOf[DCNode], absFromDir), absToDir) connect(node, absToDir)
return true return true
} }
} }
@ -531,7 +529,6 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
{ {
if (maskOpen(r)) if (maskOpen(r))
{ {
if (connectStraight(r)) if (connectStraight(r))
{ {
newConn |= 0x10 << r newConn |= 0x10 << r
@ -630,7 +627,7 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
val facePart = tile.partMap(absDir) val facePart = tile.partMap(absDir)
val toDir = ForgeDirection.getOrientation(absDir) val toDir = ForgeDirection.getOrientation(absDir)
if (facePart != null && (!facePart.isInstanceOf[PartFlatWire] || !canConnectTo(facePart, toDir.getOpposite))) if (facePart != null && (!facePart.isInstanceOf[PartFlatWire] || !canConnect(facePart.asInstanceOf[INodeProvider].getNode(classOf[DCNode], toDir.getOpposite), toDir.getOpposite)))
{ {
return false return false
} }
@ -663,14 +660,13 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
if (t != null) if (t != null)
{ {
val part = t.partMap(absDir ^ 1) val part = t.partMap(absDir ^ 1)
val dcNode = getComponent(part, ForgeDirection.getOrientation(absDir))
if (canConnectTo(part, ForgeDirection.getOrientation(absDir))) if (canConnect(dcNode, ForgeDirection.getOrientation(absDir)))
{ {
val component = getComponent(part, ForgeDirection.getOrientation(absDir)) if (dcNode.isInstanceOf[FlatWireNode])
if (component.isInstanceOf[FlatWireNode])
{ {
if (component.asInstanceOf[FlatWireNode].connectCorner(PartFlatWire.this, Rotation.rotationTo(absDir ^ 1, side ^ 1))) if (dcNode.asInstanceOf[FlatWireNode].connectCorner(PartFlatWire.this, Rotation.rotationTo(absDir ^ 1, side ^ 1)))
{ {
if (!renderThisCorner(part.asInstanceOf[PartFlatWire])) if (!renderThisCorner(part.asInstanceOf[PartFlatWire]))
{ {
@ -701,19 +697,22 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
def connectStraight(r: Int): Boolean = def connectStraight(r: Int): Boolean =
{ {
val absDir: Int = Rotation.rotateSide(side, r) val absDir = Rotation.rotateSide(side, r)
val pos: BlockCoord = new BlockCoord(tile).offset(absDir) val pos = new BlockCoord(tile).offset(absDir)
val t: TileMultipart = MultipartUtil.getMultipartTile(world, pos) val t = MultipartUtil.getMultipartTile(world, pos)
val toDir = ForgeDirection.getOrientation(absDir)
val fromDir = toDir.getOpposite
if (t != null) if (t != null)
{ {
val part = t.partMap(side) val part = t.partMap(side)
if (canConnectTo(part, ForgeDirection.getOrientation(absDir))) val dcNode = getComponent(part, ForgeDirection.getOrientation(absDir))
{
val component = getComponent(part, ForgeDirection.getOrientation(absDir))
if (component.isInstanceOf[FlatWireNode]) if (canConnect(dcNode, ForgeDirection.getOrientation(absDir)))
{ {
return component.asInstanceOf[FlatWireNode].connectStraight(PartFlatWire.this, (r + 2) % 4) if (dcNode.isInstanceOf[FlatWireNode])
{
return dcNode.asInstanceOf[FlatWireNode].connectStraight(PartFlatWire.this, (r + 2) % 4)
} }
return true return true
} }
@ -721,26 +720,32 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
else else
{ {
val tileEntity = world.getTileEntity(pos.x, pos.y, pos.z) val tileEntity = world.getTileEntity(pos.x, pos.y, pos.z)
return canConnectTo(tileEntity, ForgeDirection.getOrientation(absDir)) val dcNode = getComponent(tileEntity, fromDir)
if (dcNode != null)
return canConnect(dcNode, fromDir)
} }
return false return false
} }
def connectInternal(r: Int): Boolean = def connectInternal(r: Int): Boolean =
{ {
val absDir: Int = Rotation.rotateSide(side, r) val absDir = Rotation.rotateSide(side, r)
val toDir = ForgeDirection.getOrientation(absDir)
if (tile.partMap(PartMap.edgeBetween(absDir, side)) != null) if (tile.partMap(PartMap.edgeBetween(absDir, side)) != null)
{ {
return false return false
} }
val part: TMultiPart = tile.partMap(absDir)
if (canConnectTo(part, ForgeDirection.getOrientation(absDir))) val part = tile.partMap(absDir)
val dcNode = getComponent(part, toDir)
if (canConnect(dcNode, toDir))
{ {
val component = getComponent(part, ForgeDirection.getOrientation(absDir)) if (dcNode.isInstanceOf[FlatWireNode])
return dcNode.asInstanceOf[FlatWireNode].connectInternal(PartFlatWire.this, Rotation.rotationTo(absDir, side))
if (component.isInstanceOf[FlatWireNode])
return component.asInstanceOf[FlatWireNode].connectInternal(PartFlatWire.this, Rotation.rotationTo(absDir, side))
} }
return false return false
@ -748,14 +753,17 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
def connectCenter: Boolean = def connectCenter: Boolean =
{ {
val tp: TMultiPart = tile.partMap(6) val tp = tile.partMap(6)
if (canConnectTo(tp)) //TODO: Check other appliances that may connect to center but are not wires?
{
if (tp.isInstanceOf[PartFlatWire]) if (tp.isInstanceOf[PartFlatWire])
return tp.asInstanceOf[PartFlatWire].getNode(classOf[FlatWireNode], null).connectInternal(PartFlatWire.this, side) {
val dcNode = tp.asInstanceOf[PartFlatWire].getNode(classOf[FlatWireNode], null)
return true if (canConnect(dcNode, null))
{
return dcNode.connectInternal(PartFlatWire.this, side)
}
} }
return false return false
@ -763,8 +771,12 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
def connectCorner(wire: PartFlatWire, r: Int): Boolean = def connectCorner(wire: PartFlatWire, r: Int): Boolean =
{ {
val absDir: Int = Rotation.rotateSide(side, r) val absDir = Rotation.rotateSide(side, r)
if (canConnectTo(wire, ForgeDirection.getOrientation(absDir)) && maskOpen(r)) val toDir = ForgeDirection.getOrientation(absDir)
val fromDir = toDir.getOpposite
val dcNode = getComponent(wire, fromDir)
if (canConnect(dcNode, fromDir) && maskOpen(r))
{ {
val oldConn: Int = PartFlatWire.this.connectionMask val oldConn: Int = PartFlatWire.this.connectionMask
PartFlatWire.this.connectionMask |= 0x1 << r PartFlatWire.this.connectionMask |= 0x1 << r
@ -783,8 +795,12 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
def connectStraight(wire: PartFlatWire, r: Int): Boolean = def connectStraight(wire: PartFlatWire, r: Int): Boolean =
{ {
val absDir: Int = Rotation.rotateSide(side, r) val absDir = Rotation.rotateSide(side, r)
if (canConnectTo(wire, ForgeDirection.getOrientation(absDir)) && maskOpen(r)) val toDir = ForgeDirection.getOrientation(absDir)
val fromDir = toDir.getOpposite
val dcNode = getComponent(wire, fromDir)
if (canConnect(dcNode, fromDir) && maskOpen(r))
{ {
val oldConn: Int = PartFlatWire.this.connectionMask val oldConn: Int = PartFlatWire.this.connectionMask
PartFlatWire.this.connectionMask |= 0x10 << r PartFlatWire.this.connectionMask |= 0x10 << r
@ -799,8 +815,12 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
def connectInternal(wire: PartFlatWire, r: Int): Boolean = def connectInternal(wire: PartFlatWire, r: Int): Boolean =
{ {
val absDir: Int = Rotation.rotateSide(side, r) val absDir = Rotation.rotateSide(side, r)
if (canConnectTo(wire, ForgeDirection.getOrientation(absDir))) val toDir = ForgeDirection.getOrientation(absDir)
val fromDir = toDir.getOpposite
val dcNode = getComponent(wire, fromDir)
if (canConnect(dcNode, fromDir))
{ {
val oldConn: Int = PartFlatWire.this.connectionMask val oldConn: Int = PartFlatWire.this.connectionMask
PartFlatWire.this.connectionMask |= 0x100 << r PartFlatWire.this.connectionMask |= 0x100 << r
@ -823,6 +843,24 @@ class PartFlatWire extends PartAbstract with TWire with TFacePart with TNormalOc
return null return null
} }
override def canConnect(node: AnyRef, from: ForgeDirection): Boolean =
{
if (node.isInstanceOf[FlatWireNode])
{
val wire = node.asInstanceOf[FlatWireNode].getParent.asInstanceOf[TWire]
if (material == wire.material)
{
if (insulated && wire.insulated)
return PartFlatWire.this.getColor == wire.getColor || (getColor == TColorable.defaultColor || wire.getColor == TColorable.defaultColor)
return true
}
}
return super.canConnect(node, from)
}
} }
} }