public void AddKeysDoors(Chamber ch, Chamber ch1, Chamber ch2, Coord pass) { var corners = new List <Coord>(); if (IsInChamber(ch, Exit)) { if (!(IsInChamber(ch1, Exit) && IsInChamber(ch1, ch.Enter)) || !(IsInChamber(ch2, Exit) && IsInChamber(ch2, ch.Enter))) { Doors.Add(Tag, pass); if (ch1.Enter == ch.Enter) { corners.Add(new Coord(ch1.TopLeft.X, ch1.TopLeft.Y)); corners.Add(new Coord(ch1.RightBottom.X, ch1.TopLeft.Y)); corners.Add(new Coord(ch1.TopLeft.X, ch1.RightBottom.Y)); corners.Add(new Coord(ch1.RightBottom.X, ch1.RightBottom.Y)); } else { corners.Add(new Coord(ch2.TopLeft.X, ch2.TopLeft.Y)); corners.Add(new Coord(ch2.RightBottom.X, ch2.TopLeft.Y)); corners.Add(new Coord(ch2.TopLeft.X, ch2.RightBottom.Y)); corners.Add(new Coord(ch2.RightBottom.X, ch2.RightBottom.Y)); } int index = getRandomInRange(0, 3); Keys.Add(Tag, corners[index]); corners.RemoveAt(index); Coins.AddRange(corners); Tag++; } } }
//Check whether we can build a wall at a random position public bool passIsNotOk(int p, Chamber ch, List <Coord> pass, bool direction) { if (direction) { return(pass.Exists(element => element.CheckCoord(p, ch.TopLeft.Y - 1)) || pass.Exists(element => element.CheckCoord(p, ch.RightBottom.Y + 1))); } else { return(pass.Exists(element => element.CheckCoord(ch.RightBottom.X + 1, p)) || pass.Exists(element => element.CheckCoord(ch.TopLeft.X - 1, p))); } }
public void setEnters(Chamber ch, Chamber ch1, Chamber ch2, Coord pass) { if (IsInChamber(ch1, ch.Enter)) { ch1.Enter = ch.Enter; ch2.Enter = pass; } else { ch2.Enter = ch.Enter; ch1.Enter = pass; } }
//Function where all devision logic happens public List <Chamber> Divide(Chamber mainChamber, List <Coord> allWalls, List <Coord> allPassages) { List <Chamber> res = new List <Chamber>(); List <Coord> walls = new List <Coord>(); Coord pass = new Coord(); Chamber chamber1; Chamber chamber2; if (!mainChamber.canBeDivided()) { res.Add(mainChamber); return(res); } //Check how the wall should be build: horizontally or vertically bool xBase = mainChamber.RightBottom.X - mainChamber.TopLeft.X > mainChamber.RightBottom.Y - mainChamber.TopLeft.Y; int p, hole; do { if (xBase) { p = getRandomInRange(mainChamber.TopLeft.X + 1, mainChamber.RightBottom.X - 1); } else { p = getRandomInRange(mainChamber.TopLeft.Y + 1, mainChamber.RightBottom.Y - 1); } } while (passIsNotOk(p, mainChamber, allPassages, xBase)); if (xBase) { hole = getRandomInRange(mainChamber.TopLeft.Y, mainChamber.RightBottom.Y); } else { hole = getRandomInRange(mainChamber.TopLeft.X, mainChamber.RightBottom.X); } if (xBase) { for (int y = mainChamber.TopLeft.Y; y <= mainChamber.RightBottom.Y; y++) {//Build the wall and passage if (y == hole || allWalls.Exists(element => element.X == p && element.Y == y)) { continue; } walls.Add(new Coord(p, y)); } pass = new Coord(p, hole); //Result of devision - 2 new chambers chamber1 = new Chamber(mainChamber.TopLeft, new Coord(p - 1, mainChamber.RightBottom.Y)); chamber2 = new Chamber(new Coord(p + 1, mainChamber.TopLeft.Y), mainChamber.RightBottom); } else { for (int x = mainChamber.TopLeft.X; x <= mainChamber.RightBottom.X; x++) { if (x == hole || allWalls.Exists(element => element.X == x && element.Y == p)) { continue; } walls.Add(new Coord(x, p)); } pass = new Coord(hole, p); chamber1 = new Chamber(mainChamber.TopLeft, new Coord(mainChamber.RightBottom.X, p - 1)); chamber2 = new Chamber(new Coord(mainChamber.TopLeft.X, p + 1), mainChamber.RightBottom); } //Add chambers to the result res.Add(chamber1); res.Add(chamber2); //Set the enters of the chambers setEnters(mainChamber, chamber1, chamber2, pass); if (Doors.Keys.Count < 4) { //Generate keys,doors,coins AddKeysDoors(mainChamber, chamber1, chamber2, pass); } allPassages.Add(pass); allWalls.AddRange(walls); return(res); }
//Check if hole is in chamber public bool IsInChamber(Chamber ch, Coord hole) { return(((ch.TopLeft.X <= hole.X && hole.X <= ch.RightBottom.X) && (hole.Y == ch.TopLeft.Y - 1 || hole.Y == ch.RightBottom.Y + 1)) || ((ch.TopLeft.Y <= hole.Y && hole.Y <= ch.RightBottom.Y) && (hole.X == ch.TopLeft.X - 1 || hole.X == ch.RightBottom.X + 1))); }