Moved the partMap array to a tile trait to save memory when not needed.

This commit is contained in:
Chicken Bones committed 2013-06-24 13:17:27 +10:00
1 parent 5dd78b681c
commit 83e1713f72
7 files changed
+87 -21

No files matched your search

@@ -30,6 +30,7 @@ import codechicken.core.data.MCDataOutput
import codechicken.core.data.MCDataInput
import codechicken.core.render.RenderUtils
import codechicken.core.render.IFaceRenderer
import codechicken.multipart.TSlottedPart
object CommonMicroblock
{
@@ -193,7 +194,7 @@ trait CommonMicroblockClient extends CommonMicroblock with MicroblockClient with
}
}
abstract class CommonMicroblock(shape$:Byte = 0, material$:Int = 0) extends Microblock(shape$, material$) with JPartialOcclusion with TMicroOcclusion
abstract class CommonMicroblock(shape$:Byte = 0, material$:Int = 0) extends Microblock(shape$, material$) with JPartialOcclusion with TMicroOcclusion with TSlottedPart
{
def microClass():MicroblockClass
@@ -246,8 +246,6 @@ class PostMicroblock(shape$:Byte = 0, material$:Int = 0) extends Microblock(shap
def getBounds = PostMicroClass.aBounds(shape)
override def getSlotMask = 0
def getOcclusionBoxes = Seq(getBounds)
def getPartialOcclusionBoxes = getOcclusionBoxes
-1
View File
@@ -41,7 +41,6 @@ abstract class TMultiPart
tile = t
}
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
+13
View File
@@ -0,0 +1,13 @@
package codechicken.multipart
/**
* Interface for parts that fill a slot based configuration as defined in PartMap.
* If this is implemented, calling partMap(slot) on the host tile will return this part if the corresponding bit in the slotMask is set
*/
trait TSlottedPart
{
/**
* a bitmask of slots that this part fills. slot x is 1<<x
*/
def getSlotMask:Int
}
+11 -17
View File
@@ -42,19 +42,22 @@ import net.minecraft.entity.Entity
trait TileMultipart extends TileEntity
{
var partList = ArrayBuffer[TMultiPart]()
var partMap = new Array[TMultiPart](27)
private var doesTick = false
def loadFrom(that:TileMultipart)//Potentially auto gen this
{
partList = that.partList
partMap = that.partMap
doesTick = that.doesTick
partList.foreach(_.bind(this))
}
/**
* Overidden in TSlottedTile when a part that goes in a slot is added
*/
def partMap(slot:Int):TMultiPart = null
def jPartList():List[TMultiPart] = partList.asJava
override def canUpdate() = doesTick//TODO: part merging true
@@ -142,11 +145,6 @@ trait TileMultipart extends TileEntity
{
if(partList.contains(part))
return false
val slotMask = part.getSlotMask
for(i <- 0 until partMap.length)
if((slotMask&1<<i) != 0 && partMap(i) != null)
return false
return occlusionTest(partList, part)
}
@@ -194,11 +192,7 @@ trait TileMultipart extends TileEntity
throw new IllegalArgumentException("Tried to add more than 250 parts to the one tile. You're doing it wrong")
partList+=part
val mask = part.getSlotMask
for(i <- 0 until 27)
if ((mask&1<<i) > 0)
partMap(i) = part
bindPart(part)
part.bind(this)
if(!doesTick && part.doesTick)
@@ -207,6 +201,11 @@ trait TileMultipart extends TileEntity
partAdded(part)
}
/**
* Bind this part to an internal cache
*/
def bindPart(part:TMultiPart){}
def partAdded(part:TMultiPart){}
def remPart(part:TMultiPart):TileMultipart =
@@ -238,9 +237,6 @@ trait TileMultipart extends TileEntity
throw new IllegalArgumentException("Tried to remove a non-existant part")
partList-=part
for(i <- 0 until 27)
if(partMap(i) == part)
partMap(i) = null
partRemoved(part, r)
part.onRemoved()
@@ -267,8 +263,6 @@ trait TileMultipart extends TileEntity
private[multipart] def loadParts(parts:ListBuffer[TMultiPart])
{
clearParts()
for(i <- 0 until partMap.length)
partMap(i) = null
parts.foreach(p => addPart_do(p))
if(worldObj != null)
notifyPartChange()
@@ -35,6 +35,7 @@ class MultipartProxy_serverImpl
config = new ConfigFile(new File(cfgdir, "multipart.cfg"))
.setComment("Multipart API config file")
MultipartGenerator.registerTrait("codechicken.multipart.TSlottedPart", "codechicken.multipart.scalatraits.TSlottedTile")
MultipartGenerator.registerTrait("net.minecraftforge.liquids.ITankContainer", "codechicken.multipart.scalatraits.TTankContainerTile")
MultipartGenerator.registerTrait("codechicken.multipart.JPartialOcclusion", "codechicken.multipart.scalatraits.TPartialOcclusionTile")
MultipartGenerator.registerTrait("codechicken.multipart.IRedstonePart", "codechicken.multipart.scalatraits.TRedstoneTile")
@@ -0,0 +1,60 @@
package codechicken.multipart.scalatraits
import codechicken.multipart.TileMultipart
import codechicken.multipart.TMultiPart
import codechicken.multipart.TSlottedPart
import codechicken.multipart.TSlottedPart
trait TSlottedTile extends TileMultipart
{
var v_partMap = new Array[TMultiPart](27)
override def loadFrom(that:TileMultipart)
{
super.loadFrom(that)
if(that.isInstanceOf[TSlottedTile])
v_partMap = that.asInstanceOf[TSlottedTile].v_partMap
}
override def partMap(slot:Int) = v_partMap(slot)
override def clearParts()
{
super.clearParts()
for(i <- 0 until v_partMap.length)
v_partMap(i) = null
}
override def partRemoved(part:TMultiPart, p:Int)
{
if(part.isInstanceOf[TSlottedPart])
for(i <- 0 until 27)
if(partMap(i) == part)
v_partMap(i) = null
}
override def canAddPart(part:TMultiPart):Boolean =
{
if(part.isInstanceOf[TSlottedPart])
{
val slotMask = part.asInstanceOf[TSlottedPart].getSlotMask
for(i <- 0 until v_partMap.length)
if((slotMask&1<<i) != 0 && partMap(i) != null)
return false
}
return super.canAddPart(part)
}
override def bindPart(part:TMultiPart)
{
super.bindPart(part)
if(part.isInstanceOf[TSlottedPart])
{
val mask = part.asInstanceOf[TSlottedPart].getSlotMask
for(i <- 0 until 27)
if ((mask&1<<i) > 0)
v_partMap(i) = part
}
}
}