Skip to main content
gamedev.net
🔒 Locked

2d network game, map-info

Started by Twixly Aug 28, 2006 at 7:03 AM 3 replies 750+ views
Original Post
Twixly
Twixly
Hello. I have a question about 2d graphics and network. Currently I am trying to do something like sending only the type of land a square is. ie. "G01" sent to the client represents "Grass01.png" and "Passable". To give some info, I use 64x64 mapsquares (or textures) and have a fixed resolution of 1024x768, so at any given time I can only see 16 (18 actually because I want 1 "row" as buffert") X length squares and 12(14) Y. So 18*14 squares need to be stored on the client, nothing more. When I move right I want to recieve only one "row" of squares to optimize the network traffic load. So I need 14 rows as I move in X++ way. Hope I aint explaining poorly. Now each mapsquare on client is a class containing the texture, and a few variables, like if its passable (grass, road) or not (rock, tree) etc.. theese all get set on clientside when I recieve the code for a specific type. Now how do I do this the best way? Do I somehow copy all the content of the array or vector on client "one step to the left" so that all the squares "jump" left. And if so, how? How do I copy inside a vector made of classes? Or is there some other way this is usually done in 2d games?
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
Bob Janova
Bob Janova
Firstly, you're doing the right thing by just telling the client what type of squares to draw :). Now extend that to movement: just tell the client "Move one square right, and here are the 14 new squares you need to know about". (Or perhaps make that two steps, "Move one square right", then the client checks if it needs any squares and requests them if so. That allows you to add client-side caching later more easily. Minor point.)

As for actually performing the "move right", that depends on how your data is structured. You can manipulate arrays or collections of objects just like arrays or collections of basic types (though depending on your language you may have to be careful about disposing of the tiles that are 'pushed off').

Personally, I think I'd recommend a priority hashmap, keyed on the X,Y pair, that let you cache tiles until there were a certain number saved, at which point the oldest were removed from the collection.
hplus0603
hplus0603
You can copy classes using std::copy() (or std::copy_backward()) as long as you class has a copy constructor.

In general, I would suggest that you want to send a binary map. If you have a table of all the possible tile types, then for each tile, you just need to send the index into that table. Then the displayed map could also contain just those indices. Depending on how many tile types you have, that's one or two bytes per tile.

For objects that live in tiles, use a separate data structure as an "overlay". A hash map would work; a plain 2D vector would work, too.
enum Bool { True, False, FileNotFound };
Ozymandias42
Ozymandias42
Bob, can I ask what a priority hashmap is? Do you have a link?

It sounds like a hash table whose conflict resolution is done with a priority queue instead of a linked list. Wouldn't it be better to just grow the hash a bit bigger?
Bob Janova
Bob Janova
It's something I just made up, so sorry, no link :P. It is just a hashmap, but once it's 'full' (i.e. has a certain number of elements), adding new elements results in the elements which have been unreferenced for the longest being removed. (Ot wouldn't have to be the very oldest, necessarily, just an item not used for at least X time/reads.)

The idea is it's for where just hashing everything you might ever have seen is too much. If your map is less than say 1000 tiles a side, you can probably just cache every tile you've ever seen with no trouble.

I don't know whether such a thing is in the STL, or even if it's implementable in a sane way. I was just musing about ways of caching recent parts of a large map.

For a simple, no-caching way, a simple 2D array (or 2D vector, I didn't know those existed either, but if the display region is fixed size an array will do) would do the job fine.

Personally, I don't think you want to copy items when the player moves, you want to move them into the next cell. E.g. moving right (note: moving the player right means moving every tile left):

foreach(row) dispose(row[0]); for(i = 1 to row.length - 1)  row = row[i+1]; row[row.length-1] = (new item)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.