Storing level data

Hello, anybody knows a good way of storing level data? I mean, I have for example a 2d map made of squares and its dimensions are 1k by 1k. So it makes 1 million squares. Any ideas for storing such things?

PS

Minecraft has like even larger map, and it is 3d. So it makes it 50 if not more times larger. How does minecraft store such things?!

Really depends on what each square in your level map represents. For example, if it’s just (say) the colour of that tile then you could easily represent that as a simple in-memory 2D array for a a few 10s of Mb. If however you’re talking about height-map data, references to textures, objects within the world, etc. then that approach will obviously break down very quickly.

All games that have large maps use pagination.

The ‘world’ is segmented into tiles (or chunks of the map) and only those in view reside in memory. As the character avatar or camera or whatever moves around the map, chunks are loaded into memory and added to the view, chunks that are no longer needed are discarded (usually on a background thread with suitable caching). What data is in each tile and the granularity of the tiles is really down to the individual application / game.

I’ve used this approach to represent very large 3D world maps with each tile containing the actual terrain height-map with additional information such as cosmetic objects (trees models, rocks, etc), lighting, ambient colours, AI spawn/routing data, yada yada.

Make sure you know how much things that appear on world (grass tile, tree tile, count it) and map it to binary. It is faster and easy. In the end, you have one big number to draw whole world.

To simplify the concept of pagination, as explained above by StrideColossus, think of it as having two places to store your information:

  • Hard Drive
  • System Memory

Stuff stored in memory you can use “right now”, while stuff in the hard drive needs to be loaded into memory first. Kind of like having books on your desk or stored in a cabinet.

The point is to find an strategy to decide what stuff is kept on memory and what stuff is stored for later, being aware that bringing stuff back from storage is sloooooow.

How much space would an array of 1 million integers take in RAM?

1 Million Integers?

1 Integer = 4 Bytes
1 Million Integers = 4 Million Bytes

4 Million divided by 1024 = 3906,25 Kilobyte
3906,25 Kilobyte divided by 1024 = 3,814697265625 Megabyte

Correct me if im wrong.

  • Longor1996

That’s right, but to answer that question I think we need to count the address/pointer space too or it’ll just be raw data sitting there. It may be very small but still.

Primitives can’t have references, so the size is just 4 bytes per int. You would however need an array object too so you’d need those 4 000 000 bytes plus, uh, 12 bytes for the array object, so yeah. 4 000 012 bytes. That’s assuming you use a single array (int[1000 * 1000]), not an array of arrays (int[1000][1000]).

My mistake

[quote]1 Million Integers?
[/quote]

I think that would be 8 bytes + 4 bytes for the int primitive data per Integer instance (assuming they aren’t being cached), so 12 bytes per Integer. The Integer[] array needs 12 bytes for the object header+length and then length*4 bytes for the references to the Integer instances (hmm, on 64-bit VMs the reference would be 8 bytes I think?).
Array: 12 + 4 000 000
Data: 12 000 000

Total: 16 000 012 bytes.

Not with CompressedOops, which is on by default for 64 bit VMs. There’s also structure alignment though, so every object’s size is effectively rounded up to a multiple of 8 bytes.