/// <summary> /// Places a random block inside the level, using the still open exits. If <c>false</c> is returned /// it means that no blocks can be added animaore (within a tolerance level). /// </summary> /// <returns><c>true</c>, if random block was added, <c>false</c> otherwise.</returns> /// <param name="blocks">Blocks.</param> public bool AddRandomBlock(ProbabilityVector <ConstructionBlock> blocks) { var exit = exits.Random(); var iter = 0; var x = 0; var y = 0; if (exit != null) { // search for a valid exit while (this.IsOpenExit(exit.X, exit.Y, exit.Direction) == false && iter++ < MAX_ITERATIONS) { if (exit.CanBeClosed) { // the exit was closed, we can just close it and remove it //this.Data [exit.X, exit.Y] = this.CloseTile; //this.exits.Remove (exit); } exit = exits.Random(); } // got one, now search for a block with a valid connection ConstructionBlock next = null; BlockExit access = null; do { next = blocks.Random(); access = next.GetAccess(exit.Direction); if (access != null) { x = exit.X - access.X; y = exit.Y - access.Y; x += (access.Direction == "W") ? 1 : (access.Direction == "E") ? -1 : 0; y += (access.Direction == "N") ? 1 : (access.Direction == "S") ? -1 : 0; if (this.CanPlaceBlock(next, x, y) == false) { access = null; } } } while (access == null && iter++ < MAX_ITERATIONS); // found a good block? place it if (iter < MAX_ITERATIONS) { // BEFORE placing the block remove the used exit this.exits.Remove(exit); this.PlaceBlock(next, x, y); return(true); } } return(false); }
public bool AddBlock(ConstructionBlock next) { // search for a valid exit BlockExit access; BlockExit exit; var iter = 0; var x = 0; var y = 0; do { exit = exits.Random(); while (this.IsOpenExit(exit.X, exit.Y, exit.Direction) == false && iter++ < MAX_ITERATIONS) { if (exit.CanBeClosed) { // the exit was closed, we can just close it and remove it //this.Data [exit.X, exit.Y] = this.CloseTile; //this.exits.Remove (exit); } exit = exits.Random(); } access = next.GetAccess(exit.Direction); if (access != null) { x = exit.X - access.X; y = exit.Y - access.Y; x += (access.Direction == "W") ? 1 : (access.Direction == "E") ? -1 : 0; y += (access.Direction == "N") ? 1 : (access.Direction == "S") ? -1 : 0; if (this.CanPlaceBlock(next, x, y) == false) { access = null; } } } while (access == null && iter++ < MAX_ITERATIONS); if (iter < MAX_ITERATIONS) { // BEFORE placing the block remove the used exit this.exits.Remove(exit); this.PlaceBlock(next, x, y); return(true); } return(false); }