Hey everyone, I’m making a space game where every planet is going to have around 100+ AI that the players fight.
Currently the system works by saving the position, rotation and animation state of each AI as pure data on the server. It is stored as a SharedTable for access by multiple actors, as we use parallel luau to calculate the AI’s actions.
The server doesn’t actually render models for the NPCs - the data is replicated to all clients 30 times per second (so the npcs move at 30fps).
Currently, the data structure uses a table with one buffer for each npc. Inside the buffer the structure in use is this:
Bytes 1 and 2: Unsigned 16 bit integer representing the NPC ID
Bytes 2-14: Position (split into 3 signed 32 bit floats, thus occupying total of 12 bytes)
Bytes 14-26: Rotation (Euler angles, split in 3 similar to the position component)
Byte 27: Unsigned 8 bit integer representing AI state
Since the planets are far from each other, a player is only sent data for the NPCs present on the same planet as him. This optimizes a lot since there is no need to send updated data for NPCs that are far away on a different planet.
To replicate the data, the server first determines which NPCs are to be replicated for each player (using the logic above), then makes a copy of the sharedtable housing all the data, and then removes the NPCs that dont need replication from the copy which is then sent to the client using a remote event.
However, 128 NPCs on one planet currently result in around 115 kb/s of recieve. This is still more than double of the recommended 50kb/s.
It is important to note that it is a must to send both updated positional and rotational data since the AI can randomly wander around or also be actively targeting a player. In many TD games that face similar situations with AI, the AI in their case mostly has predictable position patterns, hence they are able to not send any positional or rotational data since it can be calculated on the client. That is however not the case with my game, thus I need some way of sending this data over.
Please let me know if you have any suggestions regarding how I could compress this data more.
Rotation can probably be stored with 16 bit floats:
1-1 sign
2-10 exponent
11-16 fraction
Not sure what sort of enemies you have, but if they’re just aliens or something that behave like humanoids then you can dismiss an entire axis for rotation, or even two.
If your map has a certain size limit then you might be able to shave off a byte or two from the position as well since you won’t need all of that range. Having the origin of the map in the corner can allow you to use the sign bit for more range, since your coordinates will always be positive.
firstly Rotation can be stored as one number: because x and z can be calculated on client (except aliens are flying). send Y value of rotation as simple number with no floating point. you can use for it 1 byte, (if 255 values or step = 1.4∘ will be enough for rotation) or 2 bytes (if you need pecision)
secondly: I think you can use chunk system. instead of absolute position you send chunk id and position (in such scenario position can take less data) relative to chunk (this variant will work good if your world is huge)
in my opinion float are pretty huge so you should use something smaller
btw, cool idea of planet visiting, I would play it
How important is the rotation? You could probably get away with storing the yaw only as one byte like @tomeplz said and unpack it like 2 * math.pi * (value / 255). If pitch/tilt is used in any way, you could still store those as just a single byte as well (especially pitch, you could maybe even store that in bits if your entities don’t bend up/down beyond 90 degrees).
I can’t really see how else you would optimize/compress the data you’re sending further unless you quite literally compressed it, though you would have to ask yourself if it’s worth it to do so (computational time to compress vs. time to send players the data/time for them to receive it).
I really wanted to use 16 bit floats, however the buffer library only allows ints of 16 bits, floats are only 32 and onward.
Good idea, I could probably do something like that using writebits; any idea how you’d decompose numbers like that?
What immediately comes to my mind is using math.floor to round down the integer part, then subtract from the original to get the decimal part.
Or I could convert the number to a string and seperate from the decimal, then convert back to a number and buffer it.
Yes I feel like you are correct. But there’s gonna be 3 types of NPCs: aerial, humanoid and aquatic. For non-humanoid classes both pitch and yaw will be utilized, however for humanoids I can just save the yaw and that can shave of 8 bytes by removing two 32bit floats. And I could probably sacrifice some precision on it as well so I’ll try the 1.4 step with an unsigned 8 bit and that should help a lot.
Yes! If there was an option to have smaller floats than 32 i could save each aliens position relative to its planet in a 16 float, and then the client could just reverse calculate that. But seems like 16 bit floats will have to be calculated through decomposing the bigger numbers if i were to do this.
The planets in my game are spherical, so the aliens and players walk around in a spherical matrix. Thus for the world CFrame I cannot sacrifize any of the 3 components.
However the points made above still are true, I can only send one or two components and then convert them relative to the planet cframe and that should still rotate them correctly, I can figure how to do that.
I believe CFrame.fromMatrix is probably the best solution, if you haven’t devised something already. You could calculate the up vector based on where the entity is in relative position of the planet’s center (which I think your position value should be to begin with, so it’d just be normalizing the position lol). There should be no trade-offs there.
Edit: Took a shot at the math, file below (very rough code, script is in Workspace)
Thanks for offering help, but we already have systems to orient the characters correctly on spherical surfaces and I could make the one number approach work since we internally store the global angles that are calculated via… black magic.
I completely forgot that function existed! I’ll think of putting it into bits.
However what I’m already noticing is that I’ll have to use 2 bytes for the expo part, then 2 for the decimal upto 3 digits of precision (multiply the decimal from modf by 100 and round).
But that results in a total of 4 bytes, equal to 32f currently in use.
I guess my best bet is still to shave off angular data. In humanoid aliens I can shave off 8 bytes, and 4 for aquatic/aerial.
Then I could reduce precision on the quantities being sent with 8bit integers, and stepping 1.4 degrees per bit
Yeah I’m not too sure of my experimental method, definitely start with the rotation and then see if there are any more ways of sending less data depending on the current situation that a player is in.
Ive been able to get it down to 75ish kb/s by sending only one byte of rotational data as @tomeplz mentioned. This is tested with 128 humanoid aliens, so im not sending another one byte of pitch component that would be required for aquatic and aerial ones. However the difference shouldn’t be much higher in any case.
Any ideas for compressing the data even more?
New buffer structure is this
bytes 1-2: id
bytes 2-14: position
bytes 14-15: yaw
bytes 15-16 animationstate
bytes 16-17: only created for aquatic/aerial AI, pitch component.
I can’t think of anything for position. It is possible to decompose into sign/exponent/fraction but thats going to add upto 4 bytes anyways per component of the vector.
If there’s no way then thats acceptable as well, and i’ll just mark tome’s reply as solution; many front page games have 100-150 recieve so it’s not going to be a deal breaker fs.
Thanks for the suggestions everyone! I wish I could mark everyone as a solution.
I have searched through google and interrogated LLM, So I have found a possible (or not possible) way to send less data every frame.
Position takes the most in all of the sending data, So It should be compressed.
what if we will use approuch like in mp4 file, instead of sending full photo every frame, update it a bit, and when photo changes really strong we update it fully (keyframe)
so you send bias to position, and when your enemy has gone to far from original position you update it fully,
biases can be 6 bytes (2 bytes for every axis)
so it is 65k studs of distance, it is quiet big for bias
lets divide it by 100, we have got 655.36 studs of max bias, with accuracy 0.01
it think it will satisfy your game
so we have got
full packet of data:
takes 1b: id
takes 6b: position bias
takes 1b: yaw
takes 1b: anim state
takes 1b: this last thing
and full position updater (you send it only when is needed (e.g every 5 seconds)),
and you should NOT update all enemys at one moment - it can result in recieve spikes (divide shipments into portions)
takes 1b: 1
takes 12b: full position
this approuch may not fit into your creation, but it is more optimized than sending full position every time
I am glad to see that developers are still optimizing their games
I thought of this, but this would require major overhauls to both the server and client replication systems.
Honestly, there aren’t going to be many planets which have more than 100 aliens. In the testing picture I sent you, there’s 128 aliens at 75kb/s. That’s already higher than 100, and I think that is quite optimized (each alien is almost 0.5kb), and as I said many big games with lots of physics have 100kb/s+ receives and still play just fine without any lag at all.
If I ever want to minimize it even more, I would absolutely look into your approach.
Thanks for the effort, I truly appreciate it, and for the help!