3

I'm making a program that implements procedural content generation (PCG) to create maps in a 2d game.

I use the graph data structure as the basis. then the graph will be transformed into a map like in the example image I attached.

graph example

map example

with graph specifications as follows:

-vertex can have more than 4 edges

-allowed the formation of cycles in the graph

any suggestions on what method I can use to transform the graph to a 2d map in a grid with space-tight results?

thanks

c0der
  • 18,467
  • 6
  • 33
  • 65
  • This doesn’t really answer your question, but one option I have seen be used before is to generate the graph on the grid in the first place, so you don’t need the separate transformation step. – Ryan1729 Jan 26 '22 at 23:23
  • Also, some possible graphs will be impossible to fit onto a grid, without crossing over each other. Your generator might generate those infrequently enough that you can get away with restarting generation if that happens though. – Ryan1729 Jan 26 '22 at 23:27
  • @Ryan1729 thanks, this gave me an idea about creating the grid first. To prevent graphs that don't fit the grid, I think I'll have to apply some additional rules. – muhammad anshar Jan 27 '22 at 18:37

1 Answers1

1

Uh, that is a tough one. The first problem you will encounter is whether this is even possible for the graph you use. See more below for that specific topic.

Let's say we ignore the fact that your graph could be impossible to map to a grid. I faced the same issue in my Master's Thesis a few years back. (PDF available here; 3.4 World Generation; page 25). I tried to find an algorithm, that could generate my world from a graph structure but ultimately failed. I tried placing one element after the other and implemented some backtracking in case it got stuck. But in the end you're facing a similar complexity to calculating chess moves. At some point you know you messed up, but you don't know how many steps you should go back/reverse, before trying the next one. If you try to solve this by brute force, you're not going to have a good time. And I did not come up with good heuristics to solve it in an adequate time.

My solution: I decided in the end to go with AnswerSet Programming. You're basically not solving the problem with an algorithm, but you find a (more or less) elegant logical representation of your problem and let a logic solver (program specifically made to find a valid solution to your logical problem-representation) do the work. Have a look in my thesis about the details, it was a few years ago and I didn't use one since. I remember however, that this process was not easy and it took me a few days to find a good logical representation of my problem.

Another question to ask: Could you work on the grid directly? Or maybe on a graph structure representing a grid? In the end a grid is nothing else than a graph; every cell is a node and neighbouring connections are the edges. I have quite some experience in the field and would be happy to help you, if you'd like to share what you want to achieve with your generator. I have also a vast collection of resources about procedural generation, maybe you find something helpful there, too.

More on the planarity of a graph: For your graph to be mappable to a plane, it needs to be planar, and checking so is also not trivial. The easiest way - if I'm not mistaken - is to prove the existence of a non-planar sub-graph, e.g. the K5 (the smallest non-planar a complete graph) or K3,3 (the smallest non-planar complete bipartite graph). And even if your graph is planar, it is not necessarily guaranteed that you can put it on your grid.

Ardor
  • 154
  • 11