diff --git a/Assets/level_tiles.tres b/Assets/level_tiles.tres index 9a895d6..87dbb98 100755 --- a/Assets/level_tiles.tres +++ b/Assets/level_tiles.tres @@ -81,7 +81,7 @@ texture_region_size = Vector2i(32, 32) 2:0/5/custom_data_0 = 5 2:0/5/custom_data_3 = true 2:0/5/custom_data_4 = 4 -3:0/next_alternative_id = 3 +3:0/next_alternative_id = 4 3:0/0 = 0 3:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, -3, -16, -3, -3, -16, -3) 3:0/0/physics_layer_0/polygon_1/points = PackedVector2Array(3, -16, 16, -16, 16, -3, 3, -3) @@ -104,6 +104,14 @@ texture_region_size = Vector2i(32, 32) 3:0/2/physics_layer_0/polygon_3/points = PackedVector2Array(3, 3, 16, 3, 16, 16, 3, 16) 3:0/2/custom_data_0 = 15 3:0/2/custom_data_2 = true +3:0/3 = 3 +3:0/3/modulate = Color(1.7, 0.95, 0.3, 1) +3:0/3/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, -3, -16, -3, -3, -16, -3) +3:0/3/physics_layer_0/polygon_1/points = PackedVector2Array(3, -16, 16, -16, 16, -3, 3, -3) +3:0/3/physics_layer_0/polygon_2/points = PackedVector2Array(-16, 3, -3, 3, -3, 16, -16, 16) +3:0/3/physics_layer_0/polygon_3/points = PackedVector2Array(3, 3, 16, 3, 16, 16, 3, 16) +3:0/3/custom_data_0 = 15 +3:0/3/custom_data_3 = true 4:0/next_alternative_id = 6 4:0/0 = 0 4:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, -3, -16, -3, 3, 16, 3, 16, 16, -16, 16) diff --git a/Levels/Maze/maze_carver.gd b/Levels/Maze/maze_carver.gd index 5958027..d43ff94 100644 --- a/Levels/Maze/maze_carver.gd +++ b/Levels/Maze/maze_carver.gd @@ -1,9 +1,9 @@ class_name MazeCarver extends RefCounted -# Turns a MazeGraph into tiles: rock everywhere, a labyrinth of corridors in -# each region (MazeCorridors), tubes between regions and through walls -# (MazeTubeRouter), then Start and End on corridor floors. +# Turns a MazeGraph into tiles: rock everywhere, a labyrinth of corridors and +# a few open halls in each region (MazeCorridors), tubes (MazeTubeRouter): +# between regions, long open rides, wall pipes. Then Start and End on floors. # # Each screen (one RoomCamera room) is split into a grid of regions, one per # graph region. A region keeps REGION_BORDER tiles of rock round its edge, so @@ -11,9 +11,13 @@ extends RefCounted # regions. const REGION_BORDER = 2 # Rock tiles round the inside of each region: room for tubes between regions. +const MIN_OPEN_RIDE = 12 # Tiles an open tube is long, at least. +const OPEN_RIDE_TRIES = 3 # Per open tube: pairs of places to try. +const FAR_SAMPLES = 8 # To pick a far-off end for an open tube, the furthest of this many random tiles. var regionRects: Array[Rect2i] = [] var regionAir: Array[Dictionary] = [] # Per region: its air tiles (tile -> true). +var openAir := {} # Air tiles tubes may run through: halls, and corridors 3 or more wide. var bounds := Rect2i() # The whole maze, in tiles. var startCell := Vector2i.ZERO # The air tile Start sits on (on a corridor floor). var endCell := Vector2i.ZERO @@ -38,16 +42,20 @@ func _init(graph: MazeGraph, screens: Vector2i, layer: TileMapLayer, random: Ran var interiors: Array[Rect2i] = [] for rect in regionRects: interiors.append(rect.grow(-REGION_BORDER)) - _router = MazeTubeRouter.new(layer, _tiles, random, bounds, level.winding, interiors) + _router = MazeTubeRouter.new(layer, _tiles, random, bounds, level.winding, interiors, regionAir, openAir) isComplete = true for tube in graph.tubes: var fallback := MazeGraph.pick_two_way_kind(random, level.steeredShare) - if _router.route(regionAir[tube.from], regionAir[tube.to], tube.kind, fallback): + var fromAir := regionAir[tube.from] + var toAir := regionAir[tube.to] + if _router.route(fromAir, MazeTubeRouter.middle(toAir), toAir, MazeTubeRouter.middle(fromAir), tube.kind, fallback): routedTubeCount += 1 elif tube.isRequired: isComplete = false return - _add_wall_pipes(roundi(level.wallPipesPerScreen * screens.x * screens.y), level.steeredShare) + var screenCount: int = screens.x * screens.y + _add_open_tubes(graph, roundi(level.openTubesPerScreen * screenCount), level.steeredShare) + _add_wall_pipes(roundi(level.wallPipesPerScreen * screenCount), level.steeredShare) startCell = _random_floor(regionAir[graph.start]) var endFrom := startCell if graph.start == graph.end else _random_floor(regionAir[graph.end]) endCell = _furthest_floor(regionAir[graph.end], endFrom) @@ -65,9 +73,15 @@ func _carve_regions(graph: MazeGraph, screenTiles: Vector2i, level: Dictionary) for region in graph.region_count(): var rect := _region(graph.grid_cell(region), split, screenTiles) regionRects.append(rect) + var area := rect.grow(-REGION_BORDER) var air := {} - for tile in MazeCorridors.carve(_layer, rect.grow(-REGION_BORDER), level.corridorWidth, level.braid, _random): + for tile in MazeCorridors.carve(_layer, area, level.corridorWidth, level.braid, _random): air[tile] = true + if level.corridorWidth >= 3: + openAir[tile] = true + for tile in MazeCorridors.carve_halls(_layer, area, level.hallsPerRegion, _random): + air[tile] = true + openAir[tile] = true regionAir.append(air) @@ -86,6 +100,37 @@ static func _region(gridCell: Vector2i, split: Vector2i, screenTiles: Vector2i) return Rect2i(position, size) +# Long rides: from somewhere in a region to somewhere far off in it or in a +# neighbouring region (maybe on another screen), through rock and open air. +func _add_open_tubes(graph: MazeGraph, count: int, steeredShare: float) -> void: + for i in count: + var twoWay := MazeGraph.pick_two_way_kind(_random, steeredShare) + var kind := MazeGraph.ONE_WAY if _random.randf() < 0.35 else twoWay + for attempt in OPEN_RIDE_TRIES: + var from := _random.randi_range(0, graph.region_count() - 1) + var to := graph.region_at((graph.grid_cell(from) + Vector2i(_random.randi_range(-1, 1), _random.randi_range(-1, 1))).clamp(Vector2i.ZERO, graph.gridSize - Vector2i.ONE)) + if regionAir[from].is_empty() or regionAir[to].is_empty(): + continue + var start := _random_tile(regionAir[from]) + var end := _far_tile(regionAir[to], start) + if _router.route(regionAir[from], Vector2(start), regionAir[to], Vector2(end), kind, twoWay, MIN_OPEN_RIDE): + break + + +func _random_tile(air: Dictionary) -> Vector2i: + var tiles: Array = air.keys() + return tiles[_random.randi_range(0, tiles.size() - 1)] + + +func _far_tile(air: Dictionary, from: Vector2i) -> Vector2i: + var best := _random_tile(air) + for i in FAR_SAMPLES: + var tile := _random_tile(air) + if Vector2(tile).distance_squared_to(Vector2(from)) > Vector2(best).distance_squared_to(Vector2(from)): + best = tile + return best + + # Short tubes through single walls, in random regions: shortcuts, and one-way # ones where that's safe. func _add_wall_pipes(count: int, steeredShare: float) -> void: diff --git a/Levels/Maze/maze_corridors.gd b/Levels/Maze/maze_corridors.gd index fe5b130..a147b3a 100644 --- a/Levels/Maze/maze_corridors.gd +++ b/Levels/Maze/maze_corridors.gd @@ -8,6 +8,8 @@ extends RefCounted # Every corridor tile ends up connected to every other one. const STEPS: Array[Vector2i] = [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT] +const HALL_MIN = Vector2i(4, 3) +const HALL_MAX = Vector2i(7, 5) # Carves into `area` (tiles) and returns the air tiles it made. @@ -37,6 +39,19 @@ static func carve(layer: TileMapLayer, area: Rect2i, width: int, braid: float, return air +# Carves `count` open halls (rooms of 4-7 x 3-5 tiles) into `area`, over the +# corridors, and returns their tiles. They join corridors into loops and give +# tubes open air to run through. +static func carve_halls(layer: TileMapLayer, area: Rect2i, count: int, random: RandomNumberGenerator) -> Array[Vector2i]: + var hall: Array[Vector2i] = [] + for i in count: + var size := Vector2i(random.randi_range(HALL_MIN.x, HALL_MAX.x), random.randi_range(HALL_MIN.y, HALL_MAX.y)).min(area.size) + var corner := Vector2i(random.randi_range(area.position.x, area.end.x - size.x), + random.randi_range(area.position.y, area.end.y - size.y)) + _carve_rect(layer, Rect2i(corner, size), hall) + return hall + + # Per grid cell: which sides are open (TubeSteer.side_bit bits). static func _backtrack(gridSize: Vector2i, random: RandomNumberGenerator) -> PackedInt32Array: var openings := PackedInt32Array() diff --git a/Levels/Maze/maze_settings.gd b/Levels/Maze/maze_settings.gd index f222410..05d1e08 100644 --- a/Levels/Maze/maze_settings.gd +++ b/Levels/Maze/maze_settings.gd @@ -17,22 +17,25 @@ const MAX_COMPLEXITY = 5 # corridorWidth: corridor width in tiles (walls are 1 tile). 1 is the # narrowest the slime gets through (checked in the game). # braid: the share of dead ends opened into loops (0 = a perfect maze). +# hallsPerRegion: open rooms carved over the corridors. # oneWayOnRoute: how many tubes between regions on the Start-End route are one-way. # extraTubesPerScreen: tubes between regions on top of the spanning tree. +# openTubesPerScreen: long rides through open air and rock, within or +# between neighbouring regions. # wallPipesPerScreen: short tubes through a wall inside a region (shortcuts). # steeredShare: the share of two-way tubes that are manual or goo, not auto. -# winding: how much tubes between regions wander (0 = shortest path). +# winding: how much tubes wander (0 = shortest path). const LEVELS: Array[Dictionary] = [ - {regionsPerScreen = Vector2i(2, 1), corridorWidth = 3, braid = 0.5, oneWayOnRoute = 0, - extraTubesPerScreen = 0.0, wallPipesPerScreen = 0.0, steeredShare = 0.0, winding = 0.5}, - {regionsPerScreen = Vector2i(2, 1), corridorWidth = 2, braid = 0.3, oneWayOnRoute = 1, - extraTubesPerScreen = 0.5, wallPipesPerScreen = 1.0, steeredShare = 0.2, winding = 1.0}, - {regionsPerScreen = Vector2i(2, 1), corridorWidth = 2, braid = 0.15, oneWayOnRoute = 2, - extraTubesPerScreen = 1.0, wallPipesPerScreen = 2.0, steeredShare = 0.3, winding = 2.0}, - {regionsPerScreen = Vector2i(1, 1), corridorWidth = 1, braid = 0.05, oneWayOnRoute = 3, - extraTubesPerScreen = 1.0, wallPipesPerScreen = 3.0, steeredShare = 0.35, winding = 3.0}, - {regionsPerScreen = Vector2i(1, 1), corridorWidth = 1, braid = 0.0, oneWayOnRoute = 4, - extraTubesPerScreen = 1.5, wallPipesPerScreen = 5.0, steeredShare = 0.4, winding = 4.0}, + {regionsPerScreen = Vector2i(2, 1), corridorWidth = 3, braid = 0.5, hallsPerRegion = 0, oneWayOnRoute = 0, + extraTubesPerScreen = 0.5, openTubesPerScreen = 1.5, wallPipesPerScreen = 0.0, steeredShare = 0.3, winding = 2.0}, + {regionsPerScreen = Vector2i(2, 1), corridorWidth = 2, braid = 0.3, hallsPerRegion = 1, oneWayOnRoute = 1, + extraTubesPerScreen = 1.0, openTubesPerScreen = 2.0, wallPipesPerScreen = 1.0, steeredShare = 0.35, winding = 3.0}, + {regionsPerScreen = Vector2i(2, 1), corridorWidth = 2, braid = 0.15, hallsPerRegion = 1, oneWayOnRoute = 2, + extraTubesPerScreen = 1.5, openTubesPerScreen = 3.0, wallPipesPerScreen = 2.0, steeredShare = 0.4, winding = 4.0}, + {regionsPerScreen = Vector2i(1, 1), corridorWidth = 1, braid = 0.05, hallsPerRegion = 2, oneWayOnRoute = 3, + extraTubesPerScreen = 1.5, openTubesPerScreen = 3.5, wallPipesPerScreen = 3.0, steeredShare = 0.4, winding = 5.0}, + {regionsPerScreen = Vector2i(1, 1), corridorWidth = 1, braid = 0.0, hallsPerRegion = 3, oneWayOnRoute = 4, + extraTubesPerScreen = 2.0, openTubesPerScreen = 4.5, wallPipesPerScreen = 5.0, steeredShare = 0.45, winding = 6.0}, ] static var width := 2 # In screens. diff --git a/Levels/Maze/maze_tube_router.gd b/Levels/Maze/maze_tube_router.gd index b5c59ab..2913059 100644 --- a/Levels/Maze/maze_tube_router.gd +++ b/Levels/Maze/maze_tube_router.gd @@ -1,10 +1,15 @@ class_name MazeTubeRouter extends RefCounted -# Lays tubes into a carved maze: long tubes between regions, routed through -# the rock with AStarGrid2D (random weights so they wander), and short "wall -# pipes" through a single wall inside a region. Tubes never share a tile, so -# they can't join by accident, and their openings only face their own tiles. +# Lays tubes into a carved maze, routed with AStarGrid2D (random weights so +# they wander): long tubes between regions and across screens, "open" tubes +# that run through open air (halls, wide corridors) where the slime can still +# get round them, and short "wall pipes" through a single wall. +# Tubes may cross another tube of the same kind at a straight tile, making a +# cross tile: auto and one-way tubes go straight through it (two separate +# rides), manual and goo tubes join into one network you steer through. +# Otherwise tubes never share a tile, and their openings only face their own +# tiles, so they can't join by accident. # # One-way tubes move the slime by themselves: the tile in front of the # entrance sucks it in, and the blow zone in front of the exit @@ -20,17 +25,20 @@ extends RefCounted const MOUTH_CHOICES = 10 # Of the spots for a mouth nearest the other end, how many we pick from. const ROUTE_TRIES = 16 # Pairs of mouth spots to try before giving up on a tube. +const CROSSING_RETRIES = 4 # Re-routes when a path uses a tube tile it can't cross. const WALL_PIPE_TRIES = 30 const MIN_SHORTCUT = 8 # A wall pipe must save at least this many tiles of corridor. # Extra cost for routing through the walls inside a region rather than the # rock between regions. In a 1-tile-wall maze the walls form a tree, and a tube # through it cuts off the rock beyond, so later tubes couldn't get through. -# (Mouths also sit next to that rock, see _touches_channel.) +# (Mouths also sit next to that rock or open air, see _touches_channel.) const INTERIOR_COST = 12.0 +const OPEN_AIR_COST = 0.6 # Open air is cheap: tubes there are seen and ridden past. +const CROSSING_COST = 3.0 var mouthFronts := {} # Air tile in front of a mouth -> true. var forcedCells := {} # Air tile the air moves the slime off (blow zones, entrance tiles) -> true. -var _keepFree := {} # Air tiles a blown slime lands on or falls through: never forced. +var _keepFree := {} # Air tiles a blown slime lands on or falls through: never forced or covered. var _layer: TileMapLayer var _tiles: MazeTiles @@ -39,46 +47,55 @@ var _bounds: Rect2i var _pathfinder := AStarGrid2D.new() var _winding := 0.0 var _interiors: Array[Rect2i] = [] # Where the corridors and their walls are. +var _regionAir: Array[Dictionary] = [] # Per region: its air tiles (shared with MazeCarver). +var _openAir := {} # Air tiles tubes may run through. +var _tubeKinds := {} # Tube tile -> its kind. func _init(layer: TileMapLayer, tiles: MazeTiles, random: RandomNumberGenerator, bounds: Rect2i, - winding: float, interiors: Array[Rect2i]) -> void: - _interiors = interiors + winding: float, interiors: Array[Rect2i], regionAir: Array[Dictionary], openAir: Dictionary) -> void: _layer = layer _tiles = tiles _random = random _bounds = bounds _winding = winding + _interiors = interiors + _regionAir = regionAir + _openAir = openAir _pathfinder.region = bounds _pathfinder.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER _pathfinder.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN _pathfinder.update() - # Tubes go through rock only, and never through the maze's outer edge. - for y in range(bounds.position.y, bounds.end.y): - for x in range(bounds.position.x, bounds.end.x): - var cell := Vector2i(x, y) - var isEdge := x == bounds.position.x or y == bounds.position.y or x == bounds.end.x - 1 or y == bounds.end.y - 1 - _pathfinder.set_point_solid(cell, isEdge or _is_air(cell)) -# Routes a tube from region `fromAir` to region `toAir` (their air tiles, as -# sets). A one-way tube with nowhere safe to blow out becomes `fallbackKind`. -func route(fromAir: Dictionary, toAir: Dictionary, kind: StringName, fallbackKind: StringName) -> bool: - _randomize_weights() +# Routes a tube from region `fromAir` (mouth near `fromTowards`) to region +# `toAir` (near `toTowards`); the regions may be the same. A one-way tube with +# nowhere safe for its ends becomes `fallbackKind`. Fails if no route at least +# `minLength` tiles long fits. +func route(fromAir: Dictionary, fromTowards: Vector2, toAir: Dictionary, toTowards: Vector2, + kind: StringName, fallbackKind: StringName, minLength := 1) -> bool: + _prepare_pathfinder(kind) var isOneWay := kind == MazeGraph.ONE_WAY - var fromMouths := _mouth_spots(fromAir, _middle(toAir), &"entrance" if isOneWay else &"") - var toMouths := _mouth_spots(toAir, _middle(fromAir), &"exit" if isOneWay else &"") + var fromMouths := _mouth_spots(fromAir, fromTowards, &"entrance" if isOneWay else &"") + var toMouths := _mouth_spots(toAir, toTowards, &"exit" if isOneWay else &"") if isOneWay and (fromMouths.is_empty() or toMouths.is_empty()): kind = fallbackKind - fromMouths = _mouth_spots(fromAir, _middle(toAir), &"") - toMouths = _mouth_spots(toAir, _middle(fromAir), &"") + _prepare_pathfinder(kind) + fromMouths = _mouth_spots(fromAir, fromTowards, &"") + toMouths = _mouth_spots(toAir, toTowards, &"") for attempt in ROUTE_TRIES: if fromMouths.is_empty() or toMouths.is_empty(): return false var fromMouth: Dictionary = fromMouths[_random.randi_range(0, fromMouths.size() - 1)] var toMouth: Dictionary = toMouths[_random.randi_range(0, toMouths.size() - 1)] - var path := _pathfinder.get_id_path(fromMouth.spot, toMouth.spot) - if path.is_empty(): + if fromMouth.spot == toMouth.spot: + continue + # Both ends in one region: they mustn't get in each other's way. + if kind == MazeGraph.ONE_WAY and fromAir == toAir \ + and not _is_one_way_safe(fromAir, fromMouth.spot, fromMouth.outward, toMouth.spot, toMouth.outward): + continue + var path := _find_path(fromMouth, toMouth, kind) + if path.size() < minLength or not _fits(path, fromMouth, toMouth, kind): continue _put_tube(path, fromMouth.outward, toMouth.outward, kind) return true @@ -89,9 +106,11 @@ func route(fromAir: Dictionary, toAir: Dictionary, kind: StringName, fallbackKin # air tiles), joining two corridors that are far apart the long way round. func add_wall_pipe(air: Dictionary, kind: StringName, fallbackKind: StringName) -> bool: var tiles: Array = air.keys() + if tiles.is_empty(): + return false for attempt in WALL_PIPE_TRIES: var wall: Vector2i = tiles[_random.randi_range(0, tiles.size() - 1)] + MazeCorridors.STEPS[_random.randi_range(0, 3)] - if _pathfinder.is_point_solid(wall) or _is_air(wall): + if not _bounds.grow(-1).has_point(wall) or _tubeKinds.has(wall) or _is_air(wall): continue for step: Vector2i in [Vector2i.RIGHT, Vector2i.DOWN]: var sideA := wall + step @@ -120,6 +139,112 @@ func add_wall_pipe(air: Dictionary, kind: StringName, fallbackKind: StringName) return false +# ---- Routing ---- + +# Sets which tiles a `kind` tube may go through, and random weights. +func _prepare_pathfinder(kind: StringName) -> void: + for y in range(_bounds.position.y, _bounds.end.y): + for x in range(_bounds.position.x, _bounds.end.x): + var cell := Vector2i(x, y) + var isEdge := x == _bounds.position.x or y == _bounds.position.y or x == _bounds.end.x - 1 or y == _bounds.end.y - 1 + var weight := 1.0 + _random.randf() * _winding + var isSolid := isEdge + if _tubeKinds.has(cell): + isSolid = isSolid or not _can_cross(cell, kind) + weight *= CROSSING_COST + elif _is_air(cell): + isSolid = isSolid or not _openAir.has(cell) or _is_protected(cell) + weight *= OPEN_AIR_COST + elif _is_interior(cell): + weight *= INTERIOR_COST + _pathfinder.set_point_solid(cell, isSolid) + _pathfinder.set_point_weight_scale(cell, weight) + + +# Whether a new `kind` tube may cross the tube tile at `cell`: same kind, a +# straight tile (it then goes straight across it), and not a mouth. +func _can_cross(cell: Vector2i, kind: StringName) -> bool: + if _tubeKinds[cell] != kind: + return false + var openings := TubeSteer.get_openings(_layer, cell) + if openings != 5 and openings != 10: + return false + for step in MazeCorridors.STEPS: + if openings & TubeSteer.side_bit(step) and _is_air(cell + step): + return false + return true + + +# A path between two mouth spots that crosses tubes only straight across. +func _find_path(fromMouth: Dictionary, toMouth: Dictionary, kind: StringName) -> Array[Vector2i]: + var blocked: Array[Vector2i] = [fromMouth.spot + fromMouth.outward, toMouth.spot + toMouth.outward] + for tile in blocked: + _pathfinder.set_point_solid(tile) + var path: Array[Vector2i] = [] + for attempt in CROSSING_RETRIES: + path = _pathfinder.get_id_path(fromMouth.spot, toMouth.spot) + var badCrossing := _bad_crossing(path) + if badCrossing == Vector2i(-1, -1): + break + _pathfinder.set_point_solid(badCrossing) + blocked.append(badCrossing) + path = [] + for tile in blocked: + _pathfinder.set_point_solid(tile, _tubeKinds.has(tile) and not _can_cross(tile, kind) or (_is_air(tile) and (not _openAir.has(tile) or _is_protected(tile)))) + return path + + +# The first tube tile `path` uses without going straight across it, or (-1, -1). +func _bad_crossing(path: Array[Vector2i]) -> Vector2i: + for i in path.size(): + var cell := path[i] + if not _tubeKinds.has(cell): + continue + if i == 0 or i == path.size() - 1: + return cell + var across := path[i + 1] - path[i - 1] + var openings := TubeSteer.get_openings(_layer, cell) + var isStraightAcross := (across.x != 0 and openings == 5) or (across.y != 0 and openings == 10) + if not isStraightAcross: + return cell + return Vector2i(-1, -1) + + +# Whether laying `path` keeps the rules: its own mouth fronts and air moves +# stay clear, and every region it covers air in stays in one piece. +func _fits(path: Array[Vector2i], fromMouth: Dictionary, toMouth: Dictionary, kind: StringName) -> bool: + var own := {fromMouth.spot + fromMouth.outward: true, toMouth.spot + toMouth.outward: true} + if kind == MazeGraph.ONE_WAY: + for tile in _blow_landing_tiles(toMouth.spot, toMouth.outward): + own[tile] = true + for tile in OneWayTube.blow_zone(_layer, toMouth.spot, toMouth.outward): + own[tile] = true + var covered := {} # Region -> air tiles the path covers. + for tile in path: + if own.has(tile): + return false + if _is_air(tile): + var region := _region_of(tile) + if not covered.has(region): + covered[region] = [] + covered[region].append(tile) + for region: int in covered: + if not _stays_connected(_regionAir[region], covered[region]): + return false + return true + + +func _region_of(tile: Vector2i) -> int: + for region in _regionAir.size(): + if _regionAir[region].has(tile): + return region + return -1 + + +func _is_protected(tile: Vector2i) -> bool: + return mouthFronts.has(tile) or forcedCells.has(tile) or _keepFree.has(tile) + + # ---- Mouths ---- # Rock tiles beside the region's air where a mouth can go: @@ -132,7 +257,8 @@ func _mouth_spots(air: Dictionary, towards: Vector2, oneWayEnd: StringName) -> A continue for step in MazeCorridors.STEPS: var spot: Vector2i = tile - step # The mouth faces `step`, onto `tile`. - if not _pathfinder.is_point_solid(spot) and not _is_air(spot) and _touches_channel(spot): + if not _is_air(spot) and not _tubeKinds.has(spot) and not _pathfinder.is_point_solid(spot) \ + and _touches_channel(spot, step): spots.append({spot = spot, outward = step}) spots.sort_custom(func(a: Dictionary, b: Dictionary) -> bool: return Vector2(a.spot).distance_squared_to(towards) < Vector2(b.spot).distance_squared_to(towards)) @@ -149,12 +275,13 @@ func _mouth_spots(air: Dictionary, towards: Vector2, oneWayEnd: StringName) -> A return chosen -# Whether `spot` is next to free rock between regions, so its tube can run -# there without going through (and cutting up) the walls inside the region. -func _touches_channel(spot: Vector2i) -> bool: +# Whether `spot` (a mouth facing `outward`) is next to free rock between +# regions or open air, so its tube can run there without going through (and +# cutting up) the walls inside a region. +func _touches_channel(spot: Vector2i, outward: Vector2i) -> bool: for step in MazeCorridors.STEPS: var next := spot + step - if not _is_interior(next) and not _is_air(next) and not _pathfinder.is_point_solid(next): + if step != outward and not _pathfinder.is_point_solid(next) and (not _is_interior(next) or _is_air(next)): return true return false @@ -189,6 +316,7 @@ func _is_one_way_safe(air: Dictionary, entranceSpot: Vector2i, entranceOutward: for tile in zone: if _keepFree.has(tile): return false + for tile in zone + [landing]: if exitOutward.y == 0 and (_is_air(tile + Vector2i.UP) or _is_air(tile + Vector2i.DOWN)): return false for tile in zone: @@ -206,7 +334,7 @@ func _is_one_way_safe(air: Dictionary, entranceSpot: Vector2i, entranceOutward: # Whether the region's air, minus every forced tile and `newForced`, is one # piece. -func _stays_connected(air: Dictionary, newForced: Array[Vector2i]) -> bool: +func _stays_connected(air: Dictionary, newForced: Array) -> bool: var open := {} for tile: Vector2i in air: if not forcedCells.has(tile) and not newForced.has(tile): @@ -245,41 +373,46 @@ static func _distance(air: Dictionary, from: Vector2i, to: Vector2i) -> int: # ---- Laying tiles ---- -func _randomize_weights() -> void: - for y in range(_bounds.position.y, _bounds.end.y): - for x in range(_bounds.position.x, _bounds.end.x): - var weight := 1.0 + _random.randf() * _winding - if _is_interior(Vector2i(x, y)): - weight *= INTERIOR_COST - _pathfinder.set_point_weight_scale(Vector2i(x, y), weight) - - # Lays tube tiles along `path`. Its first tile's mouth faces `outwardIn`, its -# last tile's `outwardOut`. A one-way tube goes in at the first mouth. +# last tile's `outwardOut`. A one-way tube goes in at the first mouth. Tube +# tiles it goes straight across become cross tiles; air it covers stops being +# air. func _put_tube(path: Array[Vector2i], outwardIn: Vector2i, outwardOut: Vector2i, kind: StringName) -> void: var last := path.size() - 1 for i in path.size(): var cell := path[i] + if _is_air(cell): + _regionAir[_region_of(cell)].erase(cell) + _openAir.erase(cell) + if _tubeKinds.has(cell): + _tiles.set_tile(_layer, cell, 15, kind) # Straight across another tube. + continue var back := outwardIn if i == 0 else path[i - 1] - cell var ahead := outwardOut if i == last else path[i + 1] - cell var openings := TubeSteer.side_bit(back) | TubeSteer.side_bit(ahead) var entrySide := TubeSteer.side_bit(back) if kind == MazeGraph.ONE_WAY and i == 0 else 0 _tiles.set_tile(_layer, cell, openings, kind, entrySide) - _pathfinder.set_point_solid(cell) + _tubeKinds[cell] = kind mouthFronts[path[0] + outwardIn] = true mouthFronts[path[last] + outwardOut] = true if kind == MazeGraph.ONE_WAY: forcedCells[path[0] + outwardIn] = true for tile in OneWayTube.blow_zone(_layer, path[last], outwardOut): forcedCells[tile] = true - # Where the blow drops the slime: the landing tile, the one after (a - # sideways blow can slide it on) and, blowing down, the fall below. - var landing := path[last] + outwardOut * (OneWayTube.BLOW_RANGE + 1) - _keepFree[landing] = true - _keepFree[landing + outwardOut] = true - while outwardOut == Vector2i.DOWN and _is_air(landing + Vector2i.DOWN): - landing += Vector2i.DOWN - _keepFree[landing] = true + for tile in _blow_landing_tiles(path[last], outwardOut): + _keepFree[tile] = true + + +# Where a blow out of the exit at `spot` facing `outward` drops the slime: the +# landing tile, the one after (a sideways blow can slide it on) and, blowing +# down, the fall below. +func _blow_landing_tiles(spot: Vector2i, outward: Vector2i) -> Array[Vector2i]: + var landing := spot + outward * (OneWayTube.BLOW_RANGE + 1) + var tiles: Array[Vector2i] = [landing, landing + outward] + while outward == Vector2i.DOWN and _is_air(landing + Vector2i.DOWN): + landing += Vector2i.DOWN + tiles.append(landing) + return tiles func _is_interior(cell: Vector2i) -> bool: @@ -293,7 +426,7 @@ func _is_air(cell: Vector2i) -> bool: return _layer.get_cell_source_id(cell) == -1 -static func _middle(air: Dictionary) -> Vector2: +static func middle(air: Dictionary) -> Vector2: var total := Vector2.ZERO for tile: Vector2i in air: total += Vector2(tile) diff --git a/docs/MAZE.md b/docs/MAZE.md index 94d2336..9c75f4f 100644 --- a/docs/MAZE.md +++ b/docs/MAZE.md @@ -31,20 +31,30 @@ generates a puzzle-book labyrinth with tubes, a Start and an End. 8. A headless check builds many mazes and runs the validator on each, plus negative tests the validator must reject. -| Complexity | Regions per screen | Corridor width | Loops (braid) | One-way on route | Extra tubes / screen | Wall pipes / screen | Steered share | -|---|---|---|---|---|---|---|---| -| 1 | 2 x 1 | 3 | 50 % | 0 | 0 | 0 | 0 % | -| 2 | 2 x 1 | 2 | 30 % | 1 | 0.5 | 1 | 20 % | -| 3 | 2 x 1 | 2 | 15 % | 2 | 1 | 2 | 30 % | -| 4 | 1 x 1 | 1 | 5 % | 3 | 1 | 3 | 35 % | -| 5 | 1 x 1 | 1 | 0 % | 4 | 1.5 | 5 | 40 % | +9. **Lots of long tube rides**, many through open space: tubes run through + open halls and wide corridors where the slime can still get round them, + wind through rock and cross screens. Tubes of the same kind may **cross** + at a cross tile: auto and one-way tubes go straight across (two separate + rides); manual and goo tubes join into one network you steer through + (checked in the game: a manual junction turns, an auto one goes straight). + +| Complexity | Regions / screen | Corridor | Loops | Halls / region | One-way on route | Extra tubes / screen | Open rides / screen | Wall pipes / screen | Steered share | +|---|---|---|---|---|---|---|---|---|---| +| 1 | 2 x 1 | 3 | 50 % | 0 | 0 | 0.5 | 1.5 | 0 | 30 % | +| 2 | 2 x 1 | 2 | 30 % | 1 | 1 | 1 | 2 | 1 | 35 % | +| 3 | 2 x 1 | 2 | 15 % | 1 | 2 | 1.5 | 3 | 2 | 40 % | +| 4 | 1 x 1 | 1 | 5 % | 2 | 3 | 1.5 | 3.5 | 3 | 40 % | +| 5 | 1 x 1 | 1 | 0 % | 3 | 4 | 2 | 4.5 | 5 | 45 % | + +Open rides are at least 12 tiles long and go from one spot to a far one in the +same or a neighbouring region (often on another screen). Tube winding goes up +with complexity. ## Non-goals - No change to player movement, existing levels or other menus (besides the one button). The player only gains what one-way tubes need: they only let it in at the entrance, `pull_into_tube()` and `blow_away()`. -- No tube crossings or branching tube networks in mazes. - No hazards, items or enemies. No saving of mazes; the seed isn't shown. ## Approaches considered (rework for maze-book layouts) @@ -53,8 +63,11 @@ generates a puzzle-book labyrinth with tubes, a Start and an End. region graph (spanning tree rooted at End, one-way tubes towards End on the route, extra tubes). Inside each region carve a classic recursive-backtracker maze on a coarse grid (cell = corridor width, wall = 1 tile) and braid some - dead ends. Tubes between regions run in the rock between regions; short - "wall pipes" go through one wall inside a region as shortcuts. + dead ends, plus open halls. Tubes between regions run in the rock between + regions; open rides run through rock and open air (halls, corridors 3+ + wide) as long as each region's air stays in one piece; short "wall pipes" + go through one wall inside a region as shortcuts. Same-kind tubes may cross + straight across each other. Safe by construction: corridors are two-way, and the tree still leads to End from every region. One-way ends only go where their forced tiles don't cut a region (see `MazeTubeRouter`). @@ -85,7 +98,10 @@ Works only from the TileMapLayer, with two models: an air tile stuck to one solid side. Moves: slide along a surface, turn an inside corner, wrap an outside corner, cross a 1-tile gap, let go of a ceiling and fall, go into a tube mouth in the surface it's on (coming out - stuck to the exit mouth's face). Forced tiles: the tile in front of an + stuck to the exit mouth's face; auto and one-way tubes go straight across + cross tiles, manual and goo networks let it leave by any of their mouths). + Tube tiles standing in open air are solid like rock: the slime crawls on + them. Forced tiles: the tile in front of an entrance (sucked in) and blow zones (blown to known spots: sideways onto the landing tile's floor or the next; down, falling from the landing tile). A state on a forced tile only counts if every place it can be moved to counts. @@ -123,6 +139,9 @@ zone; suction only pulls into a tube that leads somewhere with room. Run the check: `godot --headless --audio-driver Dummy --script res://tests/maze_check.gd` +Junction test: one hand-made map with a cross tile must pass as manual tubes +and fail as auto tubes. + Negative tests: a one-way trap (a region whose only way in is a one-way tube), Start in a blow zone, and two hand-made maps where Start and End are joined by air (so an area-level check passes) but a blow zone or a sucking diff --git a/tests/maze_check.gd b/tests/maze_check.gd index 93721b8..977fedf 100644 --- a/tests/maze_check.gd +++ b/tests/maze_check.gd @@ -10,15 +10,18 @@ extends SceneTree # - a hand-made blow-zone trap and a suction trap. In both, Start and End are # joined by air, so a check that only looks at which areas join (the old # chamber model) would pass them. +# 3. Junctions: one hand-made map with a cross tile, as manual tubes (you steer +# at the junction: End is reachable, must pass) and as auto tubes (they go +# straight across: End is out of reach, must fail). const SEEDS_PER_SETTING = 4 const TILE_SET = preload("res://Assets/level_tiles.tres") # Hand-made maps: "#" rock, "." air, "S" Start, "E" End, letters are tube tiles # (see TUBE_KEY): [openings, kind, entry side bit]. +# "|", "-" and "+" (cross) take the kind the map is checked with. const TUBE_KEY := { - "|": [5, MazeGraph.AUTO, 0], "-": [10, MazeGraph.AUTO, 0], - "L": [3, MazeGraph.AUTO, 0], "J": [9, MazeGraph.AUTO, 0], + "|": [5, &"", 0], "-": [10, &"", 0], "+": [15, &"", 0], "v": [5, MazeGraph.ONE_WAY, 0], "^": [5, MazeGraph.ONE_WAY, 1], } # A down blower over the crossing (3, 2): walking from S to the left blows the @@ -35,6 +38,17 @@ const WIND_TRAP: Array[String] = [ # Walking from S to E crosses the tile over a one-way entrance (4, 2): it # sucks the slime into a pocket whose only way out leads back to the start # side. End can't be reached at all. +# S's only tube crosses another at (3, 3). Steering (manual) turns up to E; +# an auto tube goes straight on to the dead end P (and back). +const JUNCTION: Array[String] = [ + "#######", + "###E###", + "###|###", + "#S-+-.#", + "###|###", + "###.###", + "#######", +] const SUCTION_TRAP: Array[String] = [ "#########", "#########", @@ -77,14 +91,17 @@ func _initialize() -> void: var results := { "one-way trap": _check_one_way_trap(layer), "Start in a blow zone": _check_start_in_blow_zone(layer), - "blow-zone trap (hand-made)": _check_map(layer, WIND_TRAP), - "suction trap (hand-made)": _check_map(layer, SUCTION_TRAP), + "blow-zone trap (hand-made)": not _check_map(layer, WIND_TRAP, MazeGraph.AUTO), + "suction trap (hand-made)": not _check_map(layer, SUCTION_TRAP, MazeGraph.AUTO), + "auto crossing going straight (hand-made)": not _check_map(layer, JUNCTION, MazeGraph.AUTO), } + var junctionPasses := _check_map(layer, JUNCTION, MazeGraph.MANUAL) + print("Validator passes a manual junction you steer through: %s" % ("yes" if junctionPasses else "NO")) var allCaught := true for test: String in results: print("Validator catches %s: %s" % [test, "yes" if results[test] else "NO"]) allCaught = allCaught and results[test] - quit(0 if validCount == mazeCount and allCaught else 1) + quit(0 if validCount == mazeCount and allCaught and junctionPasses else 1) # In a good maze, take a one-way tube's exit region X (not End's) and block @@ -131,9 +148,9 @@ func _check_start_in_blow_zone(layer: TileMapLayer) -> bool: return false -# Builds a hand-made map. The validator must reject it, though Start and End -# are in the same patch of air. -func _check_map(layer: TileMapLayer, map: Array[String]) -> bool: +# Builds a hand-made map with `kind` for the generic tube symbols, and returns +# whether the validator passes it. +func _check_map(layer: TileMapLayer, map: Array[String], kind: StringName) -> bool: layer.clear() var tiles := MazeTiles.new(layer.tile_set) var start := Vector2i.ZERO @@ -145,15 +162,16 @@ func _check_map(layer: TileMapLayer, map: Array[String]) -> bool: if symbol == "#": tiles.set_tile(layer, cell, 0, MazeTiles.ROCK) elif TUBE_KEY.has(symbol): - tiles.set_tile(layer, cell, TUBE_KEY[symbol][0], TUBE_KEY[symbol][1], TUBE_KEY[symbol][2]) + var symbolKind: StringName = TUBE_KEY[symbol][1] if TUBE_KEY[symbol][1] != &"" else kind + tiles.set_tile(layer, cell, TUBE_KEY[symbol][0], symbolKind, TUBE_KEY[symbol][2]) elif symbol == "S": start = cell elif symbol == "E": end = cell var bounds := Rect2i(0, 0, map[0].length(), map.size()) var validator := MazeValidator.new(layer, bounds, start, end) - print(" hand-made map: %s" % validator.problem) - return not validator.isValid + print(" hand-made map (%s): %s" % [kind, validator.problem if not validator.isValid else "valid"]) + return validator.isValid func _region_with(carver: MazeCarver, cell: Vector2i) -> int: