221 lines
5.1 KiB
Markdown
221 lines
5.1 KiB
Markdown
---
|
|
title: Prefabs
|
|
type: docs
|
|
weight: 5
|
|
---
|
|
|
|
Prefabs are pre-built structures that can be placed during world generation, such as buildings, ruins, and dungeons.
|
|
|
|
**Package:** `com.hypixel.hytale.server.worldgen.prefab`
|
|
|
|
## Prefab System
|
|
|
|
The prefab system handles structure placement:
|
|
|
|
```java
|
|
public class PrefabPlacer {
|
|
// Place prefab at position
|
|
public boolean place(Prefab prefab, World world, Vector3i position);
|
|
|
|
// Check if prefab can be placed
|
|
public boolean canPlace(Prefab prefab, World world, Vector3i position);
|
|
|
|
// Find valid position for prefab
|
|
public Vector3i findPlacement(Prefab prefab, World world, int chunkX, int chunkZ);
|
|
}
|
|
```
|
|
|
|
## Prefab Definition
|
|
|
|
```yaml
|
|
# prefabs/village_house.yaml
|
|
Type: Prefab
|
|
Id: village_house
|
|
Size: [7, 5, 9]
|
|
Origin: [3, 0, 4]
|
|
Palette:
|
|
W: oak_planks
|
|
S: stone_bricks
|
|
G: glass_pane
|
|
D: oak_door
|
|
A: air
|
|
Structure:
|
|
- "SSSSSSS"
|
|
- "SWWGWWS"
|
|
- "SWWAWWS"
|
|
- "SWWDWWS"
|
|
- "SSSSSSS"
|
|
# ... more layers
|
|
PlacementRules:
|
|
RequiresSolidGround: true
|
|
MinGroundLevel: 60
|
|
MaxGroundLevel: 100
|
|
AllowedBiomes: [plains, forest]
|
|
```
|
|
|
|
## Prefab Class
|
|
|
|
```java
|
|
public class Prefab {
|
|
private String id;
|
|
private Vector3i size;
|
|
private Vector3i origin;
|
|
private Map<Character, BlockType> palette;
|
|
private List<String[]> structure;
|
|
private PlacementRules rules;
|
|
|
|
// Get block at relative position
|
|
public BlockType getBlock(int x, int y, int z);
|
|
|
|
// Get size
|
|
public Vector3i getSize();
|
|
|
|
// Check placement rules
|
|
public boolean canPlace(World world, Vector3i position);
|
|
}
|
|
```
|
|
|
|
## Placement Rules
|
|
|
|
```java
|
|
public class PlacementRules {
|
|
private boolean requiresSolidGround;
|
|
private int minGroundLevel;
|
|
private int maxGroundLevel;
|
|
private Set<String> allowedBiomes;
|
|
private float spacing;
|
|
private int maxSlope;
|
|
|
|
public boolean evaluate(World world, Vector3i position) {
|
|
// Check ground level
|
|
if (position.y < minGroundLevel || position.y > maxGroundLevel) {
|
|
return false;
|
|
}
|
|
|
|
// Check biome
|
|
Biome biome = world.getBiome(position.x, position.z);
|
|
if (!allowedBiomes.contains(biome.getId())) {
|
|
return false;
|
|
}
|
|
|
|
// Check solid ground
|
|
if (requiresSolidGround && !hasSolidGround(world, position)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Structure Placement
|
|
|
|
```java
|
|
public class StructurePlacer {
|
|
public void placeStructure(Prefab prefab, World world, Vector3i position, Rotation rotation) {
|
|
Vector3i size = prefab.getSize();
|
|
|
|
for (int y = 0; y < size.y; y++) {
|
|
for (int z = 0; z < size.z; z++) {
|
|
for (int x = 0; x < size.x; x++) {
|
|
BlockType block = prefab.getBlock(x, y, z);
|
|
if (block != null && block != BlockTypes.AIR) {
|
|
Vector3i rotated = rotate(x, y, z, rotation, size);
|
|
Vector3i worldPos = position.add(rotated);
|
|
world.setBlock(worldPos, block);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Rotation Support
|
|
|
|
```java
|
|
public enum Rotation {
|
|
NONE(0),
|
|
CW_90(90),
|
|
CW_180(180),
|
|
CW_270(270);
|
|
|
|
public Vector3i rotate(Vector3i pos, Vector3i size) {
|
|
switch (this) {
|
|
case NONE:
|
|
return pos;
|
|
case CW_90:
|
|
return new Vector3i(size.z - pos.z - 1, pos.y, pos.x);
|
|
case CW_180:
|
|
return new Vector3i(size.x - pos.x - 1, pos.y, size.z - pos.z - 1);
|
|
case CW_270:
|
|
return new Vector3i(pos.z, pos.y, size.x - pos.x - 1);
|
|
}
|
|
return pos;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Prefab Variants
|
|
|
|
Support for randomized variants:
|
|
|
|
```yaml
|
|
# prefabs/tree_oak.yaml
|
|
Type: PrefabGroup
|
|
Id: tree_oak
|
|
Variants:
|
|
- prefabs/tree_oak_small
|
|
- prefabs/tree_oak_medium
|
|
- prefabs/tree_oak_large
|
|
Weights: [0.5, 0.35, 0.15]
|
|
```
|
|
|
|
## Generation Integration
|
|
|
|
```java
|
|
public class PrefabGenerator {
|
|
private List<PrefabConfig> configs;
|
|
|
|
public void generatePrefabs(GenerationContainer container, int chunkX, int chunkZ) {
|
|
for (PrefabConfig config : configs) {
|
|
if (random.nextFloat() < config.frequency) {
|
|
Vector3i position = findValidPosition(config.prefab, container, chunkX, chunkZ);
|
|
if (position != null) {
|
|
Rotation rotation = Rotation.values()[random.nextInt(4)];
|
|
placePrefab(config.prefab, container, position, rotation);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Entity Spawners
|
|
|
|
Prefabs can include entity spawn points:
|
|
|
|
```yaml
|
|
# prefabs/dungeon_room.yaml
|
|
Type: Prefab
|
|
Id: dungeon_room
|
|
EntitySpawns:
|
|
- Type: skeleton
|
|
Position: [5, 1, 5]
|
|
Count: 2
|
|
- Type: chest
|
|
Position: [7, 1, 7]
|
|
LootTable: dungeon_loot
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
{{< callout type="info" >}}
|
|
**Prefab Guidelines:**
|
|
- Use palettes for easy block substitution
|
|
- Define clear placement rules
|
|
- Support rotation for variety
|
|
- Include variants for visual diversity
|
|
- Consider performance with complex structures
|
|
{{< /callout >}}
|