Last active
March 11, 2017 12:43
-
-
Save odedw/f4597ef53ac3b73e9338b3430bc64248 to your computer and use it in GitHub Desktop.
Map Processor for Karcero to remove corridors with less than X cells
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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