Skip to content

Instantly share code, notes, and snippets.

@odedw
Last active March 11, 2017 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odedw/f4597ef53ac3b73e9338b3430bc64248 to your computer and use it in GitHub Desktop.
Save odedw/f4597ef53ac3b73e9338b3430bc64248 to your computer and use it in GitHub Desktop.
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