From 3fd8ff6d034823a922f22656905dd601675a0927 Mon Sep 17 00:00:00 2001 From: Chicken-Bones Date: Tue, 12 Nov 2013 11:38:57 +1000 Subject: [PATCH] Added packet scheduler for managing part updates Fixed improper AABB culling of multipart tiles Fixed some bounding box highlight issues Added ability for parts to override harvesting --- codechicken/multipart/BlockMultipart.scala | 14 ++-- codechicken/multipart/PacketScheduler.scala | 71 +++++++++++++++++++ codechicken/multipart/TMultiPart.scala | 20 +++++- codechicken/multipart/TileMultipart.scala | 26 ++++--- .../multipart/handler/PacketHandlers.scala | 3 + 5 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 codechicken/multipart/PacketScheduler.scala diff --git a/codechicken/multipart/BlockMultipart.scala b/codechicken/multipart/BlockMultipart.scala index b7e051d..b660335 100644 --- a/codechicken/multipart/BlockMultipart.scala +++ b/codechicken/multipart/BlockMultipart.scala @@ -65,7 +65,11 @@ object BlockMultipart return false val (index, mop) = reduceMOP(hit) - return tile.partList(index).drawHighlight(mop, player, frame) + if(tile.partList(index).drawHighlight(mop, player, frame)) + return true + + tile.partList(index).collisionRayTrace(RayTracer.getStartVec(player), RayTracer.getEndVec(player)) + return false } } @@ -120,14 +124,14 @@ class BlockMultipart(id:Int) extends Block(id, Material.rock) return true } - val hitInfo:(Int, _) = ExtendedMOP.getData(hit) + val (index, mop) = reduceMOP(hit) if(world.isRemote) { - tile.partList(hitInfo._1).addDestroyEffects(Minecraft.getMinecraft().effectRenderer) + tile.partList(index).addDestroyEffects(mop, Minecraft.getMinecraft().effectRenderer) return true } - return tile.harvestPart(hitInfo._1, !player.capabilities.isCreativeMode); + return tile.harvestPart(index, mop, player) } def dropAndDestroy(world:World, x:Int, y:Int, z:Int) @@ -209,7 +213,7 @@ class BlockMultipart(id:Int) extends Block(id, Material.rock) val tile = getTile(world, x, y, z) if(tile != null) { - val (index, mop) = reduceMOP(hit) + val (index, mop) = reduceMOP(hit) return tile.partList(index).pickItem(mop) } return null diff --git a/codechicken/multipart/PacketScheduler.scala b/codechicken/multipart/PacketScheduler.scala new file mode 100644 index 0000000..a890377 --- /dev/null +++ b/codechicken/multipart/PacketScheduler.scala @@ -0,0 +1,71 @@ +package codechicken.multipart +import scala.collection.mutable.{Map => MMap} +import codechicken.lib.data.MCDataOutput +import codechicken.lib.data.MCDataInput + +/** + * Static class for packing update data. + * When a specific property of a part changes and needs sending to the client, a bit can be set in the mask. + * This bit can then be checked in the writeScheduled callback. + * This prevents sending multiple packets if the same property updates more than once per tick. + */ +object PacketScheduler +{ + private val map = MMap[TMultiPart, Long]() + + /** + * Add bits to the current update mask for part. (binary OR) + */ + def schedulePacket(part:TMultiPart, mask:Long) = map.put(part, map.getOrElse(part, 0L)|mask) + + private[multipart] def sendScheduled() { + map.foreach{ e => + val (part, mask) = e + if(part.tile != null) { + val ipart = part.asInstanceOf[IScheduledPacketPart] + val w = part.getWriteStream + if(ipart.needsLong) + w.writeLong(mask) + else + w.writeInt(mask.toInt) + + ipart.writeScheduled(mask, w) + } + } + map.clear() + } +} + +/** + * Callback interface for PacketScheduler + */ +trait IScheduledPacketPart +{ + /** + * Write scheduled data to the packet, mask is the cumulative mask from calls to schedulePacket + */ + def writeScheduled(mask:Long, packet:MCDataOutput) + + /** + * If this returns true, the mask will be sent as a long rather than an int. Use if you have more than 32 flags + */ + def needsLong():Boolean + + /** + * Read data matching mask. Estiablishes a method for subclasses to override. This should be called from read + */ + def readScheduled(mask:Long, packet:MCDataInput) +} + +trait TScheduledPacketPart extends TMultiPart with IScheduledPacketPart +{ + def needsLong() = false + + final override def read(packet:MCDataInput) { + val mask = if(needsLong) packet.readLong else packet.readInt + readScheduled(mask, packet) + } + + def writeScheduled(mask:Long, packet:MCDataOutput){} + def readScheduled(mask:Long, packet:MCDataInput){} +} \ No newline at end of file diff --git a/codechicken/multipart/TMultiPart.scala b/codechicken/multipart/TMultiPart.scala index 7f4d92e..f6ca6fc 100644 --- a/codechicken/multipart/TMultiPart.scala +++ b/codechicken/multipart/TMultiPart.scala @@ -87,7 +87,7 @@ abstract class TMultiPart val offset = new Vector3(x, y, z) val boxes = getSubParts.map(c => new IndexedCuboid6(c.data, c.copy.add(offset))) return RayTracer.instance.rayTraceCuboids(new Vector3(start), new Vector3(end), boxes.toList, - new BlockCoord(x, y, z), tile.blockType).asInstanceOf[ExtendedMOP] + new BlockCoord(x, y, z), tile.getBlockType).asInstanceOf[ExtendedMOP] } /** * For the default collisionRayTrace implementation, returns a list of indexed bounding boxes. The data field of ExtendedMOP will be set to the index of the cuboid the raytrace hit. @@ -103,6 +103,17 @@ abstract class TMultiPart * @param hit An instance of ExtendedMOP from collisionRayTrace */ def getStrength(hit:MovingObjectPosition, player:EntityPlayer):Float = 1 + /** + * Harvest this part, removing it from the container tile and dropping items if necessary. + * @param hit An instance of ExtendedMOP from collisionRayTrace + * @param player The player harvesting the part + */ + def harvest(hit:MovingObjectPosition, player:EntityPlayer) + { + if(!player.capabilities.isCreativeMode) + tile.dropItems(getDrops) + tile.remPart(this) + } /** * The light level emitted by this part */ @@ -116,8 +127,13 @@ abstract class TMultiPart def addHitEffects(hit:MovingObjectPosition, effectRenderer:EffectRenderer){} /** * Add particles and other effects when a player broke this part + * @param hit An instance of ExtendedMOP from collisionRayTrace */ @SideOnly(Side.CLIENT) + def addDestroyEffects(hit:MovingObjectPosition, effectRenderer:EffectRenderer) {addDestroyEffects(effectRenderer)} + + @SideOnly(Side.CLIENT) + @Deprecated def addDestroyEffects(effectRenderer:EffectRenderer){} /** * Render the static, unmoving faces of this part into the world renderer. @@ -147,7 +163,7 @@ abstract class TMultiPart * @return true if highlight rendering was overridden. */ @SideOnly(Side.CLIENT) - def drawHighlight(hit:MovingObjectPosition, player:EntityPlayer, frame:Float) = false + def drawHighlight(hit:MovingObjectPosition, player:EntityPlayer, frame:Float):Boolean = false /** * Write all the data required to describe a client version of this part to the packet. diff --git a/codechicken/multipart/TileMultipart.scala b/codechicken/multipart/TileMultipart.scala index 7f9a8cc..c6a76ae 100644 --- a/codechicken/multipart/TileMultipart.scala +++ b/codechicken/multipart/TileMultipart.scala @@ -44,6 +44,7 @@ import net.minecraft.util.Vec3 import java.lang.Iterable import codechicken.multipart.handler.MultipartSaveLoad import scala.collection.mutable.{Map => MMap} +import net.minecraft.util.AxisAlignedBB class TileMultipart extends TileEntity { @@ -398,8 +399,11 @@ class TileMultipart extends TileEntity { clearParts() parts.foreach(p => addPart_do(p)) - if(worldObj != null) + if(worldObj != null) { + if(worldObj.isRemote) + operate(_.onWorldJoin()) notifyPartChange(null) + } } /** @@ -449,16 +453,14 @@ class TileMultipart extends TileEntity /** * Drop and remove part at index (internal mining callback) */ - def harvestPart(index:Int, drop:Boolean):Boolean = - { - val part = partList(index) - if(part == null) - return false - if(drop) - dropItems(part.getDrops) - remPart(part) - return partList.isEmpty - } + def harvestPart(index:Int, hit:ExtendedMOP, player:EntityPlayer):Boolean = + partList(index) match { + case null => false + case part => { + part.harvest(hit, player) + partList.isEmpty + } + } /** * Utility function for dropping items around the center of this space @@ -525,6 +527,8 @@ class TileMultipartClient extends TileMultipart MultipartRenderer.pass = pass true } + + override def getRenderBoundingBox() = AxisAlignedBB.getAABBPool.getAABB(xCoord, yCoord, zCoord, xCoord + 1, yCoord + 1, zCoord + 1) } /** diff --git a/codechicken/multipart/handler/PacketHandlers.scala b/codechicken/multipart/handler/PacketHandlers.scala index 35f94c8..fa8d9dd 100644 --- a/codechicken/multipart/handler/PacketHandlers.scala +++ b/codechicken/multipart/handler/PacketHandlers.scala @@ -28,6 +28,7 @@ import java.io.ByteArrayOutputStream import net.minecraft.world.WorldServer import net.minecraft.world.ChunkCoordIntPair import MultipartProxy._ +import codechicken.multipart.PacketScheduler class MultipartPH { @@ -116,6 +117,8 @@ object MultipartSPH extends MultipartPH with IServerPacketHandler def onTickEnd(players:Seq[EntityPlayerMP]) { + PacketScheduler.sendScheduled() + players.foreach{p => val m = updateMap.getOrElse(p.worldObj, null) if(m != null && !m.isEmpty)