0
\$\begingroup\$

I have a top/down grid based multi-player game. So I have a 2D array on the server that holds the entire map and a much smaller 2D array on the client that holds only what the client needs to see of the bigger server map. For now we'll say the client 2D is 5x5 but the server is 400x400.

When the player starts, the server see's what tile it's on and gets the 5x5 area around the player (with the player being at the center of that) and sends the tile info to them. The client then updates their 5x5 array and draws the objects in the tiles. This is all great and working.

Now, when the player moves I want to basically always keep the 5x5 array updating around them, loading and unloading the objects in those tiles from the server. Now in reality it'll be bigger than 5x5 but I'm using this for testing purposes only since it's smaller to manage.

I don't want to always send the 5x5 array (will be larger in final version) to the player because it's a waste of bandwidth to do so. If the player moves right 1 tile then the majority of the information in their 5x5 array is still the same. Really it's only the right most column that changed and needs to be updated while every other column gets shifted 1 to the left. The same happens if they move up, down, left, or any of the diagonals.

The question I'm after would be around how the server can track and easily send just the changes of tiles for clients. So on the server I send tile updates once a second, NOT when they tell me they changed tiles. When I send them data I store their current position as last position, so this means I can get the 5x5 array from their last position (which this is what their 5x5 array looks like) and I can get the new 5x5 array from their current position. I the need to know what changed from the last position 5x5 array to this new 5x5 array and only send them those changes along with how to shift their 5x5 array around to match this new 5x5 array. Is there any neat tricks out there for comparing 2 2D arrays to find out such information? Does anyone have any other ideas on how to handle this situation? I'm open to new ideas around this.

\$\endgroup\$
1
\$\begingroup\$

Correct me if I am wrong, but you have 2 issues here.

First issue is how to implement minimal update from server to client. When the player moves, you only want to send the new tile, and not the whole area.

Second is how to implement minimal update from client to server. It sounds like, while playing, the player can dirty up the play area, so the server needs to be aware of these changes. So, the question is, how do you send only the changes and not the whole play area.

Regarding the first issue, I would try using linked lists on the client side to store the map data. You can set up 2d linked lists to represent the play area. With linked list you can have tail and head insertions. This will allow you to add and remove whole rows at a time. So, you can set up 5 linked list to store each row, and have an outer linked list that holds all of the row lists. This will make vertical movement simple. The server sends only the new row and it gets inserted at either the head or the tail of the outer linked list, and the opposite end gets removed. Left and right movement will be a bit more tedious, but is done in much the same way. The server sends the new column data, and each row list will insert their tile at either the head or the tail, and the opposite end gets removed. The down side of using linked list vs arrays is the lose of random access. If traversing the map becomes to cumbersome, then I would consider adding a second "view" of the map data. Have the server update the linked list version of the play area. Every time that gets updated, recreate a 2d array view. At this point it should be business as usual for you.

In regards to the second issue, I would make each tile responsible for knowing when it changes. Keep track of all changed tiles, then only update those select tiles.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.