Send data from server based on player position

175 views Asked by At

I have 2D multiplayer browser game. Game contains snake-like players and world filled with grid of differently colored blocks.

Currently I send these data from server:

  • if new player spawn - data about all blocks and players to him and data about newly spawned player to others
  • if someone change his move - data about his trail, position and direction to everyone
  • if color of some blocks are changed - data about it to everyone

So I don't send redundant data but I send all data to everyone. Cheater with modified client could see everything! So I need send data to clients based on their viewport.


For snake-like players I will probably check min and max, X and Y of player.

If player A gets close to player B, server will send client A data about player B. If player A get too far from player B server will remove player B from client A. Server will send data about changing of player B only if player A is near.


Main question

But I'm not sure how I should send data about blocks. I'm afraid I will send too many redundant data.

When player spawn I will send him data about blocks near him. Also when color of blocks near him will be changed I will send him it.

If player will travel to left I will send him data about blocks in left. But how often I should do it? Should I send him new blocks every time he travel one block distance?

What If player decide to go to place where he already has been? Should I send him again redundant data? Or could I check if blocks there were changed. But how I can remember that player already has seen some blocks? I probably can't remember all blocks that player has seen (amount of blocks is too big). It would be bad for performance.

How can I protect my game against zoom cheaters and don't decrease performance?

1

There are 1 answers

2
ChrisN On

So what you are trying to implement is sometimes called a network bubble. Every player gets data around him (like a bubble around him), but not all data. So you could define an amount of blocks around the player that get sent to him. For example send all blocks directly adjacent to his current block.

block1 - block2 - block3

block4 - player - block5

block6 - block7 - block8

For the question of how to remember blocks, you will need some kind of storage array on your client side (for example a hashmap), in which you store your blocks.

Your server then needs to provide a possibility to check if the clients block is still up-to-date. This can be done with a hash-function. Your server calculates a hash of the requested block, if data in the block changes, the hashvalue changes. The hashes of the server and the stored client blocks are compared and then blockdata is only sent when they dont match.