• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Tom KQT

Finding enclosed tiles

6 posts in this topic

Hi,
I'm looking for a way how to detect areas on a grid (hex in my case, but I guess the basic principle would be the same for squares) that are fully enclosed by specific tiles ("walls").
Check the picture for better understanding - that's a random situation, dark blue tiles are the "walls" and bright green are areas (tiles) that I am trying to find. There can be more areas, I need to find all of them.

What came to my mind first was flood fill, but that works from a starting point, which I don't know - I don't know which tile can potentially be in the enclosed area (well, almost all tiles can be).
Then I thought I could simply do the opposite and flood fill the other tiles (white), which would leave the green tiles. But if I started for example in the upper left corner on the picture, the fill would stop very soon.

Do you have any suggestions how this could be done effectively?

Thank you very much for any input.

Tom

[img]http://s12.postimage.org/9b4lgu45p/vymazat.jpg[/img]
0

Share this post


Link to post
Share on other sites
The search term you need for googling purposes is "connected component labeling" ... The easiest way to do it is to

1. Construct a temporary blank grid.
2. Iterate over the cells of the input grid.
3. For each cell A that is not blue ( in your case ) and that hasn't been marked as discovered do the following
.......... Output A as a seed of a unique connected component.
.......... Perform a floodfill seeded at A that marks the cells of the connected component that A is part of as 'discovered' in the temporary grid.

There are more efficient ways of doing this but probably the above will suffice unless your grids are gigantic.
1

Share this post


Link to post
Share on other sites
Thx, that sounds good, I'll give it a try.
No, the grid isn't going to be huge, I don't know the exact dimensions yet but it will be something like 2x - 4x the size on the image.
0

Share this post


Link to post
Share on other sites
Just add every node to an "unvisited" list. Store in each node a pointer (or iterator) to it's counterpart in this list. Now the process is to:

1) Pop a node off the unvisited list and create a new list with the node as it's first element
2) Visit each neighbour of the node, recursively (i.e. flood fill), add them to the current list, and remove from the unvisited list (as each node has a pointer to it's location in the unvisited list, this is O(1)).
3) One of two things will occur; The flood fill will either terminiate, in which case you output the list as fully enclosed, or you'll hit the outer wall, in which case you discard the list as it's not enclosed. In either case, you return to step 1) and repeat until the unvisited list is empty.


This algorithm is O(n) with respect to the number of nodes.

0

Share this post


Link to post
Share on other sites
If you want to optimise this solution, you can roll your own list structure that's backed by an array. Insert and delete operations can then be performed without allocation/deallocation, which will speed things up by a great amount. In fact you don't even need separate lists for each set of nodes. You can build the other lists inside the memory of the initial allocation, which allows this algorithm to run with a grand total of 1 call to new and delete.
0

Share this post


Link to post
Share on other sites
[quote name='jwezorek' timestamp='1322165700' post='4887377']
The search term you need for googling purposes is "connected component labeling" ... The easiest way to do it is to

1. Construct a temporary blank grid.
2. Iterate over the cells of the input grid.
3. For each cell A that is not blue ( in your case ) and that hasn't been marked as discovered do the following
.......... Output A as a seed of a unique connected component.
.......... Perform a floodfill seeded at A that marks the cells of the connected component that A is part of as 'discovered' in the temporary grid.

There are more efficient ways of doing this but probably the above will suffice unless your grids are gigantic.
[/quote]

Thank you very much again, I managed to implement it succesfully.

The key here indeed was the [b]connected-component labeling [/b]([url="http://en.wikipedia.org/wiki/Connected-component_labeling"]http://en.wikipedia....ponent_labeling[/url]). I used only it, quite modified because I don't need to differentiate separate areas, I need just two ("outer" and "inner"). So for every label I just store a bool value saying whether tiles with that label are in an enclosed area or not, instead of the otherwise needed equivalence relationship table.

It's all done in two passes (by the CCL), I'm not performing any floodfill.
0

Share this post


Link to post
Thank you very much, both of you. I managed to implement it succesfully.

[quote name='jwezorek' timestamp='1322165700' post='4887377']
The search term you need for googling purposes is "connected component labeling" ... The easiest way to do it is to

1. Construct a temporary blank grid.
2. Iterate over the cells of the input grid.
3. For each cell A that is not blue ( in your case ) and that hasn't been marked as discovered do the following
.......... Output A as a seed of a unique connected component.
.......... Perform a floodfill seeded at A that marks the cells of the connected component that A is part of as 'discovered' in the temporary grid.

There are more efficient ways of doing this but probably the above will suffice unless your grids are gigantic.
[/quote]

The key here indeed was the [b]connected-component labeling[/b] ([url="http://en.wikipedia....ponent_labeling"]http://en.wikipedia....ponent_labeling[/url]). I used only it, modified for hexa grid.

It's all done in two passes (by the CCL), I'm not performing any floodfill, so it's pretty straightforward.
0

Share this post


Link to post
Share on other sites
i have not read up on the other solutions, but just to think about your reverse flood fill: if you would fill the top left, everything else would be "enclosed". why aren't the white ones enclosed, though? because they have one at the edge.

so simple solution would be to surround the whole grid with a white one tile big border, and floodfill that. then reverse.

i guess the other solution is better, but this is just an idea to solve your theoretical issue from the first post.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0