I've been all over google looking for an answer to my problem, hoping someone here can shed some light on the whole thing.
Okay, so, I'm working on a 2D Online RPG (Role Playing Game. Think Final Fantasy, but online). I've been working on it for awhile, but I've run into a bit of a problem. Now, I'm not 100% sure that compression will fix the issue, or if simply the loop is too large.
Note: I'm using the Lidgren Networking library. Open to suggestions on other options, as Lidgren doesn't support compression.
Some information on what I'm trying to do:
I'm trying to send a packet that contains all the information for the map. The smaller the map, the faster the packet goes, but when I try to send a 250x250 map (top down, tile based), it takes several minutes to do.
I have 2 theories on this. One is that it's taking awhile because the maps are pretty big in size at that point (around 5MB), the other is that the maps are so large, the loop itself is taking awhile.
The maps contain strings, bytes, int32s, int16s, etc. I have a compression method that requires a byte array, but not 100% sure on how to go about converting all the data to a byte array (I've only been working with C# for a little over a year, now, and I'm entirely self taught).
The loop is pretty large, all things considered. There are 4 tile layers, all of which need to be looped through. This is what leads me to believe that the loop might be the problem. When starting the server, it caches all the maps (meaning it builds the packets before they're ever needed), so it doesn't seem to be an issue when sending the map when the client needs it. It's pretty quick, in all honesty.
But when you edit the map and send it to be saved, it can take quite a bit of time for it to send the packet to the server and the server to send the updated map packet back to all the clients currently logged in and standing on that map. A little too much time, to be honest. I figured maybe compressing the packet would help, but as I've stated, I'm not 100% sure that's the problem.
Here's the loop that's in use:
public static void cacheMap(Int32 mapNum)
{
int X; int Y;
int MaxX = Types.Map[mapNum].MaxX;
int MaxY = Types.Map[mapNum].MaxY;
// ****** PreAllocate Buffer ******
int mapSize = Marshal.SizeOf(Types.Map[mapNum]);
int tileSize = Marshal.SizeOf(Types.Map[mapNum].Tile[0,0]);
int nLength = mapSize + ((tileSize * MaxX) * MaxY);
NetOutgoingMessage TempBuffer = ServerTCP.sSock.CreateMessage(nLength);
// Must Preallocate //
TempBuffer.Write(mapNum);
// ****** Map Info ******
TempBuffer.Write(Types.Map[mapNum].Name);
TempBuffer.Write(Types.Map[mapNum].Music);
TempBuffer.Write(Types.Map[mapNum].Revision);
TempBuffer.Write(Types.Map[mapNum].Moral);
TempBuffer.Write(Types.Map[mapNum].Weather);
TempBuffer.Write(Types.Map[mapNum].Tileset);
TempBuffer.Write(Types.Map[mapNum].Up);
TempBuffer.Write(Types.Map[mapNum].Down);
TempBuffer.Write(Types.Map[mapNum].Left);
TempBuffer.Write(Types.Map[mapNum].Right);
TempBuffer.Write(Types.Map[mapNum].BootMap);
TempBuffer.Write(Types.Map[mapNum].BootX);
TempBuffer.Write(Types.Map[mapNum].BootY);
TempBuffer.Write(Types.Map[mapNum].MaxX);
TempBuffer.Write(Types.Map[mapNum].MaxY);
// ****** Tiles ******
for (X = 0; X <= MaxX - 1; X++)
{
for (Y = 0; Y <= MaxY - 1; Y++)
{
for (int I = 0; I <= 4; I++)
{
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Layer[I].X);
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Layer[I].Y);
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Layer[I].Tileset);
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Autotile[I]);
}
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Type);
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Data1);
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Data2);
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].Data3);
TempBuffer.Write(Types.Map[mapNum].Tile[X, Y].DirBlock);
Application.DoEvents();
}
}
// ****** Send Map SoundID ******
for (X = 0; X <= MaxX - 1; X++)
{
for (Y = 0; Y <= MaxY - 1; Y++)
{
TempBuffer.Write(Types.Map[mapNum].SoundID[X, Y]);
}
}
TempBuffer.Write(Types.Map[mapNum].Instanced);
Types.MapCache[mapNum].Data = TempBuffer;
}
Any help that can be provided would be greatly appreciated. If you need me to provide more information, I can do that.