Play the game at www.RainingChain.com.
Check the source code on Github.

Thursday, 12 February 2015

Compressing Creation Data

Data transmission is a big part of the CPU usage for the Nodejs server (and bandwidth obviously). The goal here is to minimize the size of the data we send. One important thing to understand is that unlike conventional websites, a HTML5 MMORPG send small packages very often rather than big ones.

In this post, I will only focus on data that always follow the same pattern. A good example is the Creation Package concerning the base information about an entity the first time the player sees it. 

Uncompressed (and simplified), it looks like:
{
    "x":100,
    "y":100,
    "sprite":"bob",
    "hitpoints":10,
    "maximumHitpoints":10,
    "combat":true,
    "name":"Goblin"
}

One could think of using a compression library to minimize the size. However, if you already know the structure of what you're going to send, it's always better to compress it with custom functions first and most of the time it's enough. It offers better compression and is way faster to perform.

Considering that we always send the same attributes every time, a good compression method would be to get rid of the attributes like so:

[100,100,"bob",10,10,true,"Goblin"]

More optimizations could be done such as having default values and having different types of package. In Raining Chain, combat Npc and non-combat Npc use different packages.

At this point, using a compression library would still work but it would be quite CPU intensive and would achieve little improvement. Generic libraries are handy when dealing with large files but when dealing with fast packages, custom compression is better.

One very important thing when creating homemade compression is to never manipulate a compressed object. It is really tempting sometimes to just change a little thing but always uncompress, change, recompress. The last thing you do before sending is the compression and the first thing you do when receiving it is to uncompress. It is also highly recommended to put the uncompress and the compress functions next to each other in the same file. And finally, only use this when needed. Custom compression makes the code more complex and harder to maintain. 

Check here for the Actor package compression implementation in Raining Chain.

No comments:

Post a Comment