# How do I Connect random generated nodes together??

Get help using Construct 2

### » Sun May 15, 2016 12:51 am

I created a simple way of generating nodes, but I can't figure out a way to connect them in a logical way.

Here's the capx of what I have for the nodes: https://www.dropbox.com/s/on1krbh60y58u ... .capx?dl=0

I'm going to use the first node as my start and the furthest away as my exit later on.

I want to get something like this:
B
29
S
15
G
20
Posts: 56
Reputation: 12,545

### » Sun May 15, 2016 11:33 am

So you want to make a FTL-like random star map with lanes?

Turns out this is actually rather difficult. Difficult but not impossible. Took me ~3 hours to figure it out and I have some experience with doing random generation in construct. The solution involves a fair deal of loops, functions and some array operations. Not sure if there are simpler ways to do this.

First of all you need your lanes to know to what they connect, I added A and B variables which store the ID of nodes which the lane connects. Connecting them in a sensible manner is the tricky part.

Basic idea was: generate nodes, after nodes are generated create lanes to 3 nearest other nodes, after all lanes are generated you delete duplicate lanes. To find the 3 nearest node you need an event sequence that for each node stores the distance to all nodes (including itself) in y=0 and the node ID at y=1 in an array's x element. Then you sort the array by axis X, go through elements 1 to number of lanes and create lanes to them.

Hardest part was deleting duplicate lanes actually. Took me an hour before I figured it out, the solution was surprisingly simple and short.

Linked capx with my solution below, right click-release generates new node set. Please bear in mind that it has 2 problems which you will have to figure out yourself.

https://www.dropbox.com/s/z9hlr5i169lnc ... .capx?dl=1

1) It is possible to generate node "isles" that are fully disconnected from other isles, the smaller the numbers of MaxLanes the more likely this is to occur. Wonder how the FTL guy solved this. He probably wrote code that checks if there is a path between the starting and ending node and created one if needed, that's the efficient solution. The lazy brute-force solution is to check if there is a path and if not generate the whole nodemap again.
2) Sometimes nodes overlap, this should not be too hard to fix. I would do a function for that after all nodes are generated but before you start generating lanes, that offsets overlapping nodes.

Also, compared to the image you attached, it has overlapping lanes. But that could be fixed some clever overlap checks, you would put lane in a family, check if the family object overlaps a lane object, mark for deletion (similar logic to removing duplicates).
B
15
S
6
Posts: 85
Reputation: 1,195

### » Sun May 15, 2016 9:28 pm

wow thank you!
That's quite complicated, I'll look at it and try and learn as much as I can.
No wonder I couldn't figure it out

For problem 2 I have a solution for it that I used before.

Thanks again, I have a lot to digest!
B
29
S
15
G
20
Posts: 56
Reputation: 12,545

### » Mon May 16, 2016 12:27 am

B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Mon May 16, 2016 1:30 am

99Instances2Go, holy wow, nice!

A clinic in functions!
You're using a lot of things I haven't seen yet, I'll be studying this also! Thanks!!
B
29
S
15
G
20
Posts: 56
Reputation: 12,545

### » Mon May 16, 2016 9:36 am

There must be a better solution. This has me caged.

I just cant find the logic in it. I thougt, if you break a link in two, and the middlepoint is closer to another node then to the starting node or the end node, then its a link that can be forgotten. It works that way, but that is a serious iteration.

I like what Pulsar has done, but as he says, it has a few little problems.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Mon May 16, 2016 4:24 pm

Same idea. Much better coded. Now the events effectively wait till the objects are created/destroyed.

I know when i can pick a newly created object.
I still have problems with conditions picking allready destroyed objects. Would ne nice if someone explained me the rules arround that.

B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Mon May 16, 2016 6:47 pm

Oh nice!
Would there be an easy way to limit the space between nodes? For example it would be nice to not have nodes close together.
I guess you could just offset them if the distance of a line is too short?

A second question, how do I make this static once it's generated?
For example, in a run I would need to generate 10-15 of of these on the start of the game and I would need access to these during the game.
I thought taking an image of them but that wouldn't work since I need the nodes to be clickable to select the map/mission etc.

Each one of those nodes would be a generated map like you made.

Thanks
B
29
S
15
G
20
Posts: 56
Reputation: 12,545

### » Mon May 16, 2016 7:04 pm

Yes Sir. MUCH easyer. Because they lay on a vertical shifted grid. The node-lanes are now predifined by that grid. As well as the nodes. Those are well organised, following strict rules too. There almost nothing random anymore.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Mon May 16, 2016 9:45 pm

evillair wrote:Oh nice!
Would there be an easy way to limit the space between nodes? For example it would be nice to not have nodes close together.
I guess you could just offset them if the distance of a line is too short?

I managed to overcome that and get rid off lanes overlapping each other in the middle of space, see below capx. I have the events check if the distance to the closest node other than itself is less than 70 pixels. Then I have it offset the node and check again if any nodes are closer than 70 pixels to each other. Once no nodes are found I have it stop checking and moving nodes. There is a second way of avoiding the problem, it involves generating nodes inside grid sections and not allowing more than one node to be generated within a grid section. That requires different random node generation logic though. I may show how to do that later on.

Now I am stuck at checking if there is a path from start to exit. I have a function that marks all nodes connected to start, I know how to connect any "islands" if start is not connected to them. Problem is determining when the functions I did encountered a dead end as there are multiple instance of the connect function running in parallel.

https://www.dropbox.com/s/d0ltq4t9b6c1k ... .capx?dl=1

Regarding copying the FTL select sector map, you do two loops, one within the other. One is for the X axis the other for the Y axis. Next you generate nodes like this:

condition: mod(loopindex("x")/2)=1 action: set offset=100
condition: mod(loopindex("x")/2)=0 action: set offset=0

loopindex("x")*200,loopindex("y")*200+offset

I would show an example if I had the time, it is quite late and I am unfortunately rather busy tomorrow. Really want to solve that "node is connected" problem though.
B
15
S
6
Posts: 85
Reputation: 1,195

Next