# Data Structure for 2D Maps in Correlation to Display/Functionality

Page 1 of 1

## 2 Replies - 1253 Views - Last Post: 28 June 2013 - 01:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=323841&amp;s=e322cc1c03d4f0bbcf814b505741c3d6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JTG2003

Reputation: 0
• Posts: 51
• Joined: 29-February 12

# Data Structure for 2D Maps in Correlation to Display/Functionality

Posted 28 June 2013 - 11:58 AM

Intro)
I have been creating games for about a year now using a proprietary language my company utilizes. They are basic 2D top-down games, but I've incorporated some neat functionality including randomly generated maps and objectives. I am now getting into Java (although this is not a Java-specific question) and am wondering how things are generally done there.

Details)
In all my programs, I create a 2-dimensional array and use it as a map for my 2D game. The index of each array corresponds to a coordinate in the game. An example map:

Key:
Green - Grass
Brown - Dirt
Blue - Water
Black - Player
Red - Enemy

So, in my previous experience, I would have a data structure with 2 subscripts representing the X and Y coordinates.
Ex: Map(3,7) = Grass
Map(9,6) = Water
Map(6,3) = Enemy
(NOTE: I am simplifying this, most cases there are multiple pieces of data per data structure to indicate what type of "floor" exists, any objects on it, etc).

It makes it REALLY easy to draw the map. I simply calculate the range of coordinates to display, plug them into the structure one by one and draw them on the screen using the coordinate (index) + an offset.

Question 1) Is this a normal way to do this? I did this because I could without anyone telling me if it was good practice or not.

My experience so far has only been in single player games so I've been able to cheat. The player's position is stored in a local variable so I can easily take a look at enemies in the same area to check if they should chase/attack the player. Now, though, I'm thinking I need to be more dynamic in how my players/enemies/objects relate to each other.

Let's set up the following example:
The enemy (Red block) checks to see if there's any other living creatures around (should find the player, black block).

There are 2 ways I have thought of to accomplish this:

1) The enemy brute force checks all spaces within range X to see if there are any living creatures nearby. The longer the range, the more spaces to check. If range = 2, the enemy will need to check 25 squares.. and it STILL won't find the player. It seems like the most "accurate" method - the enemy will never miss seeing something it should have found, however it is far from effective.

2) Index everything. Instead of storing the enemy on the main Map structure at (6,3), create a new structure called Enemy and put an entry there for (6,3). Assuming the red square is the only enemy in the entire game, the Enemy structure will only contain that one entry. Likewise, there would be a Player structure containing one entry at (2,5). Now, when the enemy checks its surroundings, it only needs to look at these object specific structures within a set range. The drawback is now there's more confusion as to what belongs where, especially in a Draw Screen routine. The routine now needs to loop through the Map structure, then the Enemy structure, then the Player structure, possibly drawing over many of the previous spaces.
This could also be simplified a little bit, having one structure per "Layer". Ground (dirt, grass), Items (apple, shoe), Objects (Tree, wall), Entities (enemies, players) .. should cover anything I can come up with.

Question 2) Are either of these methods "Normal"? If not, how are interactions and relationships dealt with?

Is This A Good Question/Topic? 0

## Replies To: Data Structure for 2D Maps in Correlation to Display/Functionality

### #2 modi123_1

• Suitor #2

Reputation: 11621
• Posts: 45,813
• Joined: 12-June 08

## Re: Data Structure for 2D Maps in Correlation to Display/Functionality

Posted 28 June 2013 - 12:47 PM

Quote

Question 1) Is this a normal way to do this? I did this because I could without anyone telling me if it was good practice or not.

There's no real "best practice", but if it works for you then kudos. Keep using it. Heck - maybe make that a 2d array of a custom class/object for even more information at each point!

Quote

Question 2) Are either of these methods "Normal"? If not, how are interactions and relationships dealt with?

Sure.. you should have your player in a class, as well as your enemies, npcs, items, etc in their own classes.. and their locations (with respects to your 2d grid) should be a x,y point that corresponds appropriately to a space on the map..

As for drawing - yeah, that happens. You draw the map, then draw the player on top of his/her square obscuring what ever is underneath.

### #3 JTG2003

Reputation: 0
• Posts: 51
• Joined: 29-February 12

## Re: Data Structure for 2D Maps in Correlation to Display/Functionality

Posted 28 June 2013 - 01:02 PM

modi123_1, on 28 June 2013 - 12:47 PM, said:

There's no real "best practice", but if it works for you then kudos. Keep using it. Heck - maybe make that a 2d array of a custom class/object for even more information at each point!

Good idea

Quote

Sure.. you should have your player in a class, as well as your enemies, npcs, items, etc in their own classes.. and their locations (with respects to your 2d grid) should be a x,y point that corresponds appropriately to a space on the map..

I may have misspoke. I meant structure more like an array. In one scenario, you have a single 2 dimensional MAP array containing ALL map data.

This means if a creature is in the middle of a 10x10 map at (5,5) and can "see" the whole thing, I would need to check 100 squares every time the creature checks its surroundings.

My potential alternative was saving multiple arrays so that a player might be stored in PLAYER(3,4). Then the creature, instead of looping through the MAP array and checking 100 squares, check the CREATURE array, see it's empty, then check the PLAYER array and see 1 entry.. all this in the time the previous method would have checked 1-3 of the 100 squares.

A problem to the above is one I didn't think of before I posted my first post. If I have a player array with an entry at 5,5 , there are STILL 100 entries in that array even if only 1 has data in it... so I would still need to loop through 100 entries.

I've been working with this proprietary language too long and I think it's mucking with my programmer brain. One last attempt to explain what I'm trying to say: I am suggesting having a new structure for every "layer" of the map, so that you only need to check the layers you care about in an attempt to find them faster.