Skip to content

Instantly share code, notes, and snippets.

@odedw

odedw/CorridorCleaner.cs

Last active Mar 11, 2017
Embed
What would you like to do?
Map Processor for Karcero to remove corridors with less than X cells
using Karcero.Engine.Contracts;
using Karcero.Engine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Karcero.Visualizer
{
public class CorridorCleaner : IMapProcessor<Cell>
{
public int MinCorridorLength { get; set; }
public CorridorCleaner(int minCorridorLength = 3)
{
MinCorridorLength = minCorridorLength;
}
public void ProcessMap(Map<Cell> map, DungeonConfiguration configuration, IRandomizer randomizer)
{
map.AllCells
.Where(cell => IsDeadEnd(cell, map))
.Select(cell => BuildCorridor(cell, map))
.Where(c => c.Count < MinCorridorLength)
.ToList()
.ForEach(ClearCorridor);
}
private void ClearCorridor(List<Cell> corridor)
{
corridor.ForEach(cell => cell.Terrain = TerrainType.Rock);
}
private List<Cell> BuildCorridor(Cell cell, Map<Cell> map)
{
var corridor = new List<Cell>();
var currentCell = cell;
do
{
var possibleCells = map.GetAllAdjacentCells(currentCell)
.Where(c => !corridor.Contains(c) && c.Terrain != TerrainType.Rock);
if (possibleCells.Count() == 1)
{
corridor.Add(currentCell);
currentCell = possibleCells.First();
}
else
{
currentCell = null;
}
}
while (currentCell != null);
return corridor;
}
private bool IsDeadEnd(Cell cell, Map<Cell> map)
{
return cell.Terrain != TerrainType.Rock &&
map.GetAllAdjacentCells(cell).Count(neighbour => neighbour.Terrain != TerrainType.Rock) == 1;
}
}
}
var generator = new DungeonGenerator<Cell>();
generator.AddMapProcessor(new CorridorCleaner());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment