Files
dungeon.sk
Code
1 425
dungeon.sk
# Dungeon Generator
# Minecraft: 1.21.x
# Skript: 2.8+
#
# Installation:
# 1. Save this file as plugins/Skript/scripts/dungeon.sk
# 2. Run: /sk reload dungeon
#
# This script uses only standard Skript syntax.
# It creates rooms and corridors from configurable block palettes,
# places blocks through a throttled queue, populates loot rooms, and
# snapshots overwritten blocks so the latest dungeon can be restored.
options:
prefix: &8[&5Dungeon&8]&r
# Generation settings
grid-size: 9
min-rooms: 6
max-rooms: 14
room-spacing: 12
room-radius: 3
room-height: 4
corridor-width: 3
corridor-height: 3
# Maximum number of blocks placed before yielding for one server tick
blocks-per-tick: 80
# Dungeon block palette
floor-block: polished deepslate
wall-block: deepslate bricks
ceiling-block: polished deepslate
corridor-floor-block: stone bricks
corridor-ceiling-block: deepslate tiles
light-block: glowstone
on script load:
delete {dungeon::generating::*}
# Adds one block operation to the dungeon queue.
# The original block is saved once so /dungeon clear can restore it.
function queueDungeonBlock(dungeon-id: text, block-location: location, block-type: itemtype):
set {_location-key} to "%{_block-location}%"
if {dungeon::%{_dungeon-id}%::snapshot::%{_location-key}%} is not set:
set {dungeon::%{_dungeon-id}%::snapshot::%{_location-key}%} to type of block at {_block-location}
add {_block-location} to {dungeon::%{_dungeon-id}%::snapshot-locations::*}
if {dungeon::%{_dungeon-id}%::queue-size} is not set:
set {dungeon::%{_dungeon-id}%::queue-size} to 0
add 1 to {dungeon::%{_dungeon-id}%::queue-size}
set {_queue-index} to {dungeon::%{_dungeon-id}%::queue-size}
set {dungeon::%{_dungeon-id}%::queue-location::%{_queue-index}%} to {_block-location}
set {dungeon::%{_dungeon-id}%::queue-type::%{_queue-index}%} to {_block-type}
# Executes queued block operations in batches to avoid a large single-tick lag spike.
function flushDungeonQueue(dungeon-id: text):
set {_queue-total} to {dungeon::%{_dungeon-id}%::queue-size}
if {_queue-total} is not set:
stop
set {_queue-index} to 1
while {_queue-index} <= {_queue-total}:
set block at {dungeon::%{_dungeon-id}%::queue-location::%{_queue-index}%} to {dungeon::%{_dungeon-id}%::queue-type::%{_queue-index}%}
if {_queue-index} mod {@blocks-per-tick} is 0:
wait 1 tick
add 1 to {_queue-index}
delete {dungeon::%{_dungeon-id}%::queue-location::*}
delete {dungeon::%{_dungeon-id}%::queue-type::*}
delete {dungeon::%{_dungeon-id}%::queue-size}
# Builds a square room shell with an open interior.
function buildDungeonRoom(dungeon-id: text, room-center: location, room-type: text):
set {_world} to world of {_room-center}
set {_center-x} to rounded down (x-coordinate of {_room-center})
set {_center-y} to rounded down (y-coordinate of {_room-center})
set {_center-z} to rounded down (z-coordinate of {_room-center})
set {_radius} to {@room-radius}
set {_height} to {@room-height}
set {_negative-radius} to 0 - {_radius}
set {_relative-x} to {_negative-radius}
while {_relative-x} <= {_radius}:
set {_relative-z} to {_negative-radius}
while {_relative-z} <= {_radius}:
set {_block-x} to {_center-x} + {_relative-x}
set {_block-z} to {_center-z} + {_relative-z}
set {_floor-location} to location at {_block-x}, {_center-y}, {_block-z} in {_world}
set {_ceiling-location} to location at {_block-x}, {_center-y} + {_height} + 1, {_block-z} in {_world}
queueDungeonBlock({_dungeon-id}, {_floor-location}, {@floor-block})
queueDungeonBlock({_dungeon-id}, {_ceiling-location}, {@ceiling-block})
set {_is-edge} to false
if {_relative-x} is {_negative-radius}:
set {_is-edge} to true
if {_relative-x} is {_radius}:
set {_is-edge} to true
if {_relative-z} is {_negative-radius}:
set {_is-edge} to true
if {_relative-z} is {_radius}:
set {_is-edge} to true
set {_relative-y} to 1
while {_relative-y} <= {_height}:
set {_wall-location} to location at {_block-x}, {_center-y} + {_relative-y}, {_block-z} in {_world}
if {_is-edge} is true:
queueDungeonBlock({_dungeon-id}, {_wall-location}, {@wall-block})
else:
queueDungeonBlock({_dungeon-id}, {_wall-location}, air)
add 1 to {_relative-y}
add 1 to {_relative-z}
add 1 to {_relative-x}
# Add a simple light near the rear of the room.
set {_light-location} to location at {_center-x}, {_center-y} + {@room-height}, {_center-z} in {_world}
queueDungeonBlock({_dungeon-id}, {_light-location}, {@light-block})
flushDungeonQueue({_dungeon-id})
set {_population-location} to location at {_center-x}, {_center-y} + 1, {_center-z} in {_world}
if {_room-type} is "standard":
set {_chest-location} to location at {_center-x}, {_center-y} + 1, {_center-z} in {_world}
set block at {_chest-location} to chest
# Each standard room receives six independently randomized loot slots.
set {_loot-count} to 0
while {_loot-count} < 6:
set {_slot} to random integer between 0 and 26
set {_loot-roll} to random integer between 1 and 8
if {_loot-roll} is 1:
set slot {_slot} of inventory of block at {_chest-location} to 2 bread
else if {_loot-roll} is 2:
set slot {_slot} of inventory of block at {_chest-location} to 4 torch
else if {_loot-roll} is 3:
set slot {_slot} of inventory of block at {_chest-location} to iron ingot
else if {_loot-roll} is 4:
set slot {_slot} of inventory of block at {_chest-location} to gold ingot
else if {_loot-roll} is 5:
set slot {_slot} of inventory of block at {_chest-location} to arrow
else if {_loot-roll} is 6:
set slot {_slot} of inventory of block at {_chest-location} to cooked beef
else if {_loot-roll} is 7:
set slot {_slot} of inventory of block at {_chest-location} to emerald
else:
set slot {_slot} of inventory of block at {_chest-location} to golden apple
add 1 to {_loot-count}
spawn 2 zombies at {_population-location}
spawn a spider at {_population-location}
else if {_room-type} is "boss":
spawn a ravager at {_population-location}
spawn 2 wither skeletons at {_population-location}
# Builds a corridor between two directly adjacent room centers.
function buildDungeonCorridor(dungeon-id: text, first-center: location, second-center: location):
set {_world} to world of {_first-center}
set {_first-x} to rounded down (x-coordinate of {_first-center})
set {_first-y} to rounded down (y-coordinate of {_first-center})
set {_first-z} to rounded down (z-coordinate of {_first-center})
set {_second-x} to rounded down (x-coordinate of {_second-center})
set {_second-z} to rounded down (z-coordinate of {_second-center})
set {_half-width} to rounded down ({@corridor-width} / 2)
set {_negative-half-width} to 0 - {_half-width}
# Horizontal corridor.
if {_first-z} is {_second-z}:
set {_start-x} to {_first-x}
set {_end-x} to {_second-x}
if {_start-x} > {_end-x}:
set {_temporary-x} to {_start-x}
set {_start-x} to {_end-x}
set {_end-x} to {_temporary-x}
set {_corridor-x} to {_start-x} + {@room-radius}
while {_corridor-x} <= {_end-x} - {@room-radius}:
set {_corridor-z-offset} to {_negative-half-width}
while {_corridor-z-offset} <= {_half-width}:
set {_corridor-z} to {_first-z} + {_corridor-z-offset}
set {_floor-location} to location at {_corridor-x}, {_first-y}, {_corridor-z} in {_world}