Skip to main content

Structures

The Structures feature allows you to place hand-made structures inside procedurally generated levels. For example, the level below contains a bunch of hand-made chests, a pier with a fire, and a cave.

Showcase

Getting started

Creating your first structure

Structures build on top of the Level editor, so you need to be familiar with it. Go to "Create -> Frigga -> Structure" to create a new structure. This creates a new level editor prefab for you.

Assign the theme that you want to use in the level generator, initialize the output, and create the structure. For this example, I created a structure containing a treasure chest.

Chest

Notice that the structure contains 2 fixed tiles and a bunch of other tiles. The fixed tiles will always look exactly the same when placed in a level because the theme engine is not allowed to touch them. However, the other tiles might look different. For example, the tile that contains the tree might not have a tree there when placed in a level. The only guarantee is that there will be a ground tile there.

You can use these non-fixed tiles as a buffer zone around the fixed tiles that guarantees what kinds of int grid tiles will surround the structure.

Int grid tiles on the border

Later, when the structure is being placed in the level, the placement algorithm looks at the int grid tiles that are on the border of the structure to determine a good position. For example, in the chest structure from the previous section, all the border int grid tiles are set to "Ground". You can see this if you click the "Toggle Int Grid" button. This means that when the structure is placed, the algorithm will look for a position that contains ground tiles in the same shape as the border of the structure.

info

Even if you have fixed tiles on the border of a structure, you should still place int grid tiles underneath them to guide the placement algorithm.

Structure border

Configuring the generator

Head inside the Walker generator component, find the "Structures" section and click the plus button in the list.

Empty structure

You can see a bunch of properties that you can configure:

  • Name: The name of the structure, used for debugging purposes.
  • Count: Whether to spawn multiple structures or just one.
  • Probability: Probability of spawning the structure if Count is set to Single.
  • Min Count and Max Count: If Count is set to Multiple, these properties define the minimum and maximum number of structures to spawn.
  • Min Accuracy: Configures how nicely the structure must fit in the level. The generator looks for a position where the int grid tiles on the border of a structure match the int grid tiles inside the level. A 100% precise placement is not always possible, so this property configures the percentage of border tiles that must match. The placement algorithm will try to find the best matching position that satisfies the minimum accuracy. If no such position is found, the structure will not be spawned.
  • Items: List of structures to choose from, where each structure has a certain weight that affects the probability of being chosen.

Let's place 3 to 5 chests in the level, with a minimum accuracy of 100%:

Structure config

And the result might look like this:

Example

Tips

Structures vs multi-tile rules

Multi-tile rules are great for handling simple cases that are easily expressed in a rule. For example, you can easily create a rule for a column that consists of 3 tiles stacked vertically.

Structures are great for handling more complex cases or for when you need to spawn an exact count of objects in a level. For example, if you want to spawn exactly 3 elite enemies in a level, it's impossible to do that with normal rules but quite easy with structures.

Placement

You can control the placement of structures by adding placement rules to the definition of the structure.

Placement

Basics

When the generator places a structure, the rule with the highest priority is to place the structure at a position where the neighboring tiles match the tiles of the structure. For example, if you have a structure with water tiles on the border, the generator will try to find a position where the neighboring tiles are water. This is the core rule that the generator always prioritizes.

In many cases, there are multiple positions where the structure fits equally well. In that case, you can influence the placement by specifying placement rules. Some rules are of a global nature - e.g., placing the structure as far as possible from other structures. While other rules are local - e.g., enforcing a minimum distance from another structure.

For performance reasons, the placement of structures is only approximate. For example, if you choose to place a structure as far as possible from another structure, the generator will find an area of the level that fits this requirement and then pick a random position inside that area. Therefore, it is possible that the structure will be placed a few tiles away from the optimal position.

Placement types

Far From and Close To

Places the structure as far as (or as close as) possible from another structure.

Region

Places the structure in a specific region of the level.

Min Distance and Max Distance

Enforces a minimum or maximum distance from another structure.

Relation to other structures

The Far From, Close To, Min Distance and Max Distance rule let you specify to which other structure the placement rule should apply. For example, if you want all chests to be placed as far as possible from the "Spawn" structure, you can write "Spawn" in the field.

The configuration is quite flexible. You can leave the name field empty and it will apply to all other structures. You can also use the name of the structure that is currently being placed. For example, when placing 20 chests, you can enforce that they are placed at least 10 tiles away from each other.

Conflicting rules

Sometimes it might happen that you have 2 or more conflicting rules. For example, you might want to place a structure in the top right corner of the level, but also as far as possible from the "Spawn" structure, which might also be placed in the top right corner. In that case, you can specify a weight for the rule. The higher the weight, the more the rule will be prioritized.

Impossible rules

It might happen that the Min Distance and Max Distance rules cannot be satisfied. For example, if you have a small level and want to place a hundred treasure chests with a minimum distance of 100 tiles, it will just not work. In that case, the generator will try to place as many structures following the rule as possible. When that's no longer possible, the generator will ignore all distance requirements.

Limitations

Too many structures

The performance will degrade if you want to spawn too many structures or if the level is too big. You should be fine if you have less than 20 structures.

Structures too big

The performance will degrade if your structures are too big. You should be fine if your structures are smaller than 30x30 tiles.