public void UpdateCache()
{
var blockPos = new Vector2((int)(EntityPosition.X / GameServer.BlockSize), (int)(EntityPosition.Y / GameServer.BlockSize));
short startX = (short)MathHelper.Clamp((int)blockPos.X - (750 / GameServer.BlockSize), 0, GameServer.WorldSizeX);
short startY = (short)MathHelper.Clamp((int)blockPos.Y - (750 / GameServer.BlockSize), 0, GameServer.WorldSizeY);
short endX = (short)MathHelper.Clamp(startX + 64, 0, GameServer.WorldSizeX);
short endY = (short)MathHelper.Clamp(startY + 64, 0, GameServer.WorldSizeY);
for (short x = startX; x < endX; x++)
{
Packet packet = new Packet();
//Generate two bitmasks where each bit determines if the block's ID/MD are updated.
long[] masks = GenerateRowBitMask(x);
long maskID = masks[0];
long maskMD = masks[1];
//No updates
if (maskID == 0 && maskMD == 0) continue;
int numBitsID = GetNumBitsSet(maskID);
int numBitsMD = GetNumBitsSet(maskMD);
//If we're only setting one block, just ignore the bitmask and send the block itself.
if (numBitsID == 1 && numBitsMD == 1)
{
int oneX = 0;
int oneY = 0;
for (int i = 0; i < 64; i++)
{
if ((maskID & ((long)1 << i)) == 0)
{
if ((maskMD & ((long)1 << i)) == 0) continue;
}
oneX = x;
oneY = startY + i;
}
PlayerBlockCache[oneX, oneY].ID = GameServer.WorldBlocks[oneX, oneY].ID;
PlayerBlockCache[oneX, oneY].MetaData = GameServer.WorldBlocks[oneX, oneY].MetaData;
Packet packet2 = new Packet1SCGameEvent(GameServer.GameEvents.Block_Set, (short)oneX, (short)oneY, GameServer.WorldBlocks[oneX, oneY].ID, GameServer.WorldBlocks[oneX, oneY].MetaData);
GameServer.ServerNetworkManager.SendPacket(packet2, NetConnection);
continue;
}
for (int i = 0; i < endY - startY; i++)
{
BlockData curData = GameServer.WorldBlocks[x, startY + i];
bool IDUpdate = (maskID & ((long) 1 << i)) != 0;
bool MDUpdate = (maskMD & ((long) 1 << i)) != 0;
if (!IDUpdate && !MDUpdate) continue;
if (IDUpdate)
{
packet.WriteShort(curData.ID);
PlayerBlockCache[x, startY + i].ID = curData.ID;
}
if(MDUpdate)
{
packet.WriteByte(curData.MetaData);
PlayerBlockCache[x, startY + i].MetaData = curData.MetaData;
}
}
Packet p = new Packet1SCGameEvent(GameServer.GameEvents.Block_Set_Line);
p.WriteShort(x);
p.WriteShort(startY);
p.WriteLong(maskID);
p.WriteLong(maskMD);
p.WriteBytes(packet.GetData());
GameServer.ServerNetworkManager.SendPacket(p, NetConnection);
}
}