0

I am developing a massive multiplayer online 2D game in Haxe/OpenFL, where world is divided into two-dimensional grid of sectors, therefore each sector acts as one object. On server, these sector objects may or may not exist in memory (depending on whether players are currently in them or not) and I need to access these sector objects via their x and y coordinate. Theoretically there can be several thousands of these objects at any given time.

My question is, what is the best (fastest, most efficient) way of storing and accessing these objects. I tried to use two-dimensional arrays with x and y as indexes, but my implementation was extremely slow.

wildfireheart
  • 373
  • 2
  • 4
  • 13
  • What's your upper limit on x and y? You could unwrap your two-dimensional array to a one-dimensional array with (x*(max-y)+y) as the coordinate if that wouldn't make the array too long. – Brian Sep 16 '14 at 18:33
  • My current limit for amount of sectors is 2^36, as far as I know the limit for array size is 2^32-1. So I will probably limit the size of world (it has a little overkill size anyway) and try your solution. Thank you very much. – wildfireheart Sep 16 '14 at 18:54
  • Not good. I'd got somewhere up to 2^27 max array length (uninitialised array) and application took around 1.51 GB of RAM, which had, obviously, rather major impact on performance. I'm compiling Haxe/OpenFL code for OS X 64 C++ target. – wildfireheart Sep 16 '14 at 20:50
  • Only pointers to your objects would take around 256 Gb to store, and I assume you dont have that much memory. Depending on access patterns you might be completely unable to handle it in RAM and gonna need a database for that. However, I would try quadtrees for the starters. – stroncium Sep 18 '14 at 12:43
  • I am actually using a database for storing static sector data, but I need also to have sector objects in memory for some "real time", or dynamic operations. HashMap of sectors seems to work for now, 'cause most of the sectors are uninitialized and stored in DB during gameplay, only sector objects which have currently players in them have some real time data stored in memory. Amount of players will be far less then amount of sectors, so with reasonable amount of sectors, this or similar solution should work, at least I hope. – wildfireheart Sep 19 '14 at 14:33
  • But thank you for insight of memory consumption, I'll have to rethink the size of the gaming world to some manageable size. – wildfireheart Sep 19 '14 at 14:34

1 Answers1

2

In server side you may use a hashmap data structure to save the sectors,the key would be x_y.Check the memory after You initialize all the sectors when server startup, if it cost two much, you may initialize some main sectors at first and another sector should be initialized only when it's actually been accessed.

Pan
  • 2,101
  • 3
  • 13
  • 17
  • Seems working with HashMap, when only sector objects currently used are initialised. StringMap also works, but using HashMap with custom key is probably better in this situation, I think. Thank you. – wildfireheart Sep 17 '14 at 11:45