{{Infobox structure
| defaultimagesize = 150px
| image = Jigsaw Block.png
| biome = None
| blocks = 
* {{BlockLink|Jigsaw}}
|canspawn=No
}}
'''Jigsaw structures''' are [[structure]]s that generate using [[Jigsaw Block]]s.
They are used to generate {{EnvLink|pillager outpost|pillager outposts}}, {{EnvLink|village|villages}}, {{EnvLink|bastion remnant|bastion remnants}}, {{EnvLink|ancient city|ancient cities}}, {{EnvLink|trail ruins}}, and {{EnvLink|trial chambers}}.
The Jigsaw structure generation can be customized using [[data pack]]s {{in|java}} or [[add-on]]s {{in|bedrock}}{{OnlyExperimental|Data-Driven Jigsaw Structures}}.

== Generation ==
The generation places a number of pieces. A piece is usually a [[structure template]], but can also be a [[placed feature]]. The pieces are organized into [[template pool]]s. During generation pieces are randomly selected from a given template pool.

When a structure is generated starting from a given chunk, it generates in a number of steps, detailed below. 

=== Step 1: Starting piece ===
A jigsaw structure defines a {{nbt|string}}{{nbt|compound|start_pool}}. A piece is selected from this pool and placed at the starting position. Rotation and the starting template are chosen randomly, based on the chunk coordinates and the world seed. If the selected piece is a {{cd|minecraft:empty_pool_element}}, the structure fails to generate.<ref group="note" name="fail">When the structure fails to generate, it can't be located using {{cmd|/locate}} and other structures from the same [[structure set]] can take its place.</ref> The starting position is the 0, 0 chunk coordinate of the starting chunk. If the {{nbt|string|project_start_to_heightmap}} is set, the start height is based on the specified [[heightmap]], offset by the values result of {{nbt|int}}{{nbt|compound|start_height}}. Otherwise, {{nbt|int}}{{nbt|compound|start_height}} is used to determine the start height.

If the structure defines as {{nbt|string|start_jigsaw_name}}, then a jigsaw block with that name is randomly selected and in the placed at the starting position. If no matching jigsaw block is found, the structure fails to generate.<ref group="note" name="fail" /> If the structure has {{nbt|string|start_jigsaw_name}} unset, the north west corner of the (unrotated) piece is placed at the starting position.

If the final starting piece position is not at least {{nbt|int|dimension_padding.bottom}} blocks away from the lower build limit and {{nbt|int|dimension_padding.top}} blocks away from the upper build limit, the structure fails to generate.<ref group="note" name="fail" />

Finally, the starting piece is put into a generation queue.

=== Step 2: Connecting pieces ===
The generation queue is processed sequentially. A piece is removed from the queue when is processed. Of the processed (parent) piece all jigsaw blocks are then used to generate a connecting piece. The jigsaw blocks are handled in order from high to low selection priority set in each jigsaw block and a random order of jigsaw blocks with equal selection priority. 

==== Connecting piece selection ====
The jigsaw block is referencing a "target pool". The "target pool" is used as an alias by the [[#Pool aliases|pool aliases]] referenced in {{nbt|list|pool_aliases}} to determine which [[template pool]] to use. If no pool alias is used then the "target pool" directly references the template pool to use.

The connecting piece is determined using that template pool in following order:
# All the pieces from the template pool &mdash; in random order according to their weight.
# All the pieces from the fallback pool specified in the template pool &mdash; in random order according to their weight.

If the maximum depth (defined by {{nbt|int|size}}) is reached, point 1 is skipped.

==== Connecting piece try ====
From the tried piece a random jigsaw block is selected, that
# is facing outside of the bounds of the connecting piece
# has a "name" that matches the "target name" of the parent jigsaw block,
# has an orientation that is compatible with the parent jigsaw orientation (pieces can be rotated horizontally only).

The piece is placed in the world such that the jigsaw blocks connect to each other. If the currently handled piece or the newly selected piece is set to terrain matching, the piece is moved vertically to align with the terrain.

==== Piece placement condition ====
The placement is then checked for validity. A piece has to:
# Not exceed the outer bounding box. If it is placed within another piece, then the bounding box of that piece is the outer bounding box<ref group="note">When {{nbt|bool|use_expansion_hack}} is set to {{cd|true}}, the bounding box of the pieces is extended to the height of the highest possible child piece.</ref>, otherwise the outer bounding box is:
#* A cube with side-length {{cd|2 * }}{{nbt|int|max_distance_from_center}}, centered on the starting position.
#* The cube then gets truncated on the top or bottom such that it is at least {{nbt|int|dimension_padding.bottom}} blocks away from the lower build limit and {{nbt|int|dimension_padding.top}} blocks away from the upper build limit. 
# Not intersect with any other piece (except those that it is placed inside of)

If the piece can't be placed, then the placement is retried with the next piece (see [[#Connecting piece selection]]). If all pieces can't be placed than no piece is placed for this jigsaw block.

If the piece can be placed, then the newly placed piece is added to the generation queue according to the placement priority specified in the parent jigsaw block, but behind elements of equal placement priority already in the queue. The generation continues with the next jigsaw block of the parent piece.

=== Step 3: Processing ===
Finally, each piece is processed, changing or removing blocks from the template.

The processing is done using the [[processor list]] specified for each piece in the template pool. Additionally, jigsaw blocks are replaced with the specified "Turns into" final state using the {{cd|minecraft:jigsaw_replacement}} processor. For pieces set to terrain matching, a {{cd|minecraft:gravity}}, processor is applied to make each column of blocks match the terrain.
<!-- upcoming pool aliases -->

'''Notes:'''
<references group="note"/>

== Pool aliases ==
'''Pool aliases''' are used to redirect the alias "target pool" referenced in [[jigsaw block]]s to a (different) [[template pool]]s. The redirections are determined before the generation starts, so in a single structure all references to a specific alias template pool are rewired to the same target template pool. Any alias that is not redirected by a pool alias is used directly as reference to a template pool.

=== direct ===
All references to {{nbt|string|alias}} are rewired to {{nbt|string|target}}.
<div class="treeview">
* {{nbt|compound}}: a pool alias
** {{nbt|string|type}}: {{cd|direct}}
** {{nbt|string|alias}}: [[Resource location|ID]] &mdash; alias to be redirected
** {{nbt|string|target}}: {{json ref|template pool}} &mdash; target for redirection
</div>

=== random ===
A target selected from {{nbt|list|targets}} per structure. All references to {{nbt|string|alias}} are rewired that target.
<div class="treeview">
* {{nbt|compound}}: a pool alias
** {{nbt|string|type}}: {{cd|random}}
** {{nbt|string|alias}}: [[Resource location|ID]]
** {{nbt|list|targets}}: list of possible rewiring targets
*** {{nbt|compound}}: a rewiring target
**** {{nbt|int|weight}}: The chance of this target to be selected
**** {{nbt|string|data}}: {{json ref|template pool}} &mdash; target for redirection if selected
</div>

=== random_group ===
A alias group is selected from {{nbt|list|groups}} per structure. All pool aliases in that group are used.

<div class="treeview">
* {{nbt|compound}}: a pool alias
** {{nbt|string|type}}: {{cd|random_group}}
** {{nbt|list|groups}} list of possible rewiring groups. One group is selected per structure generation.
*** {{nbt|compound}}: a group
**** {{nbt|int|weight}}: The chance of this group to be selected
**** {{nbt|list|data}}: list of pool aliases to be used if the group is selected
***** {{nbt|compound}}: any pool alias
</div>

== Data values ==
=== ID ===
{{el|je}}:
{{ID table
|firstcolumnname=Structure type
|displayname=Jigsaw
|spritename=jigsaw
|spritetype=block
|nameid=jigsaw
|foot=1}}

=== Config ===
{{el|java}}:
{{/Config}}

== History ==
{{HistoryTable
|{{HistoryLine|java}}
|{{HistoryLine||1.14|dev=18w46a|Added [[jigsaw block]]s.}}
|{{HistoryLine|||dev=18w47a|Added jigsaw structures in the form of [[pillager outpost]].}}
|{{HistoryLine||1.16.2|dev=20w28a|Jigsaw structures can now be customized in [[data pack]]s, using {{cd|configured_structure_features}} of type {{cd|village}}, {{cd|pillager_outpost}}, and {{cd|bastion_remnant}}. They have a maximum radius of 80 blocks.}}
|{{HistoryLine||1.19|dev=Deep Dark Experimental Snapshot 1|Jigsaw structures with radius 128{{verify|might be a bit smaller than that}} can now be generated using the {{cd|configured_structure_features}} type {{cd|ancient_city}}.}}
|{{HistoryLine|||dev=22w11a
|Merged the {{cd|structure}} types {{cd|village}}, {{cd|pillager_outpost}}, and {{cd|bastion_remnant}} into a single type {{cd|jigsaw}}.
|Added {{nbt|int}}{{nbt|compound|start_height}}, {{nbt|string|project_start_to_heightmap}}, and {{nbt|boolean|use_expansion_hack}} fields.}}
|{{HistoryLine|||dev=22w12a|Added {{nbt|int|max_distance_from_center}} field.}}
|{{HistoryLine|||dev=22w13a|Replaced {{nbt|boolean|adapt_noise}} field with {{nbt|string|terrain_adaptation}}.}}
|{{HistoryLine|||dev=22w17a|Added {{nbt|string|start_jigsaw_name}} field.}}
|{{HistoryLine||1.20.3|dev=23w42a|Added {{nbt|list|pool_aliases}}.}}
|{{HistoryLine||1.21|dev=24w19a|Added {{nbt|int|dimension_padding}}.}}
|{{HistoryLine|||dev=24w20a|{{nbt|int}}{{nbt|compound|dimension_padding}} can now also be specified separately for top and bottom padding.}}
|{{HistoryLine|||dev=pre1|Added {{nbt|string|liquid_settings}}.}}
}}

== Navigation ==
{{Navbox generated structures}}

[[fr:Structure en puzzle]]
[[pt:Estrutura de bloco-quebra-cabeça]]
