public GenEndPoint(Chunk chunk, ChunkConnector connector, int depth) { X = chunk.X + connector.X; Y = chunk.Y + connector.Y; Size = connector.Size; Face = (ConnectorFace )((connector.Horizontal ? 0 : 1) + (connector.BottomOrRight ? 2 : 0)); Chunk = chunk; Skin = connector.Skin; Depth = depth; }
/// <param name="side">0 - left /// 1 - top /// 2 - right /// 3 - bottom</param> public ChunkConnector[] GetConnectors(ConnectorFace side, int size = 0, int skin = -1) { if (size == 0) { return(Array.FindAll(myConnectors[(int)side], x => (skin == -1 || x.Skin == skin))); } if (size > (((int)side % 2) == 0 ? Height : Width) - 2) { return(new ChunkConnector[0]); } return(Array.FindAll(myOrganisedConnectors[(int)side][size - 1], x => (skin == -1 || x.Skin == skin))); }
/// <param name="side">0 - left /// 1 - top /// 2 - right /// 3 - bottom</param> public bool HasConnectors(ConnectorFace side) { return(myConnectors[(int)side].Length > 0); }
private GenChunkAddedStatus GenAddChunk(Random rand, Stack <GenEndPoint> endPoints, List <GenEndPoint> looseEnds, List <GenChunkRect> rects, ChunkTemplate[] templates, ref int area, int maxDepth = 0, bool first = false) { ChunkTemplate template; Chunk newChunk; List <GenEndPoint> endsToAdd; if (first) { template = templates[rand.Next(templates.Length)]; newChunk = new Chunk(0, 0, template, this); Chunks.Add(newChunk); endsToAdd = new List <GenEndPoint>(); for (int i = 0; i < 4; ++i) { foreach (ChunkConnector connector in template.GetConnectors((ConnectorFace)i)) { endsToAdd.Add(new GenEndPoint(newChunk, connector, 0)); } } while (endsToAdd.Count > 0) { int index = rand.Next(endsToAdd.Count); endPoints.Push(endsToAdd[index]); endsToAdd.RemoveAt(index); } rects.Add(new GenChunkRect(newChunk)); area += newChunk.Area; return(GenChunkAddedStatus.Added); } List <ChunkTemplate> validTemplates = new List <ChunkTemplate>(); GenEndPoint endPoint = endPoints.Pop(); ConnectorFace oppositeFace = (ConnectorFace)(((int)endPoint.Face + 2) % 4); foreach (ChunkTemplate temp in templates) { if (temp.GetConnectors(oppositeFace, endPoint.Size, endPoint.Skin).Length != 0) { validTemplates.Add(temp); } } if (validTemplates.Count == 0) { endPoints.Push(endPoint); return(GenChunkAddedStatus.ImpossibleToAdd); } template = validTemplates[rand.Next(validTemplates.Count)]; ChunkConnector[] cons = template.GetConnectors(oppositeFace, endPoint.Size, endPoint.Skin); if (cons.Length == 0) { endPoints.Push(endPoint); return(GenChunkAddedStatus.NotAdded); } ChunkConnector con = cons[rand.Next(cons.Length)]; int x = endPoint.X - con.X; int y = endPoint.Y - con.Y; switch (endPoint.Face) { case ConnectorFace.Left: --x; break; case ConnectorFace.Top: --y; break; case ConnectorFace.Right: ++x; break; case ConnectorFace.Bottom: ++y; break; } GenChunkRect newRect = new GenChunkRect(x, y, template); foreach (GenChunkRect rect in rects) { if (rect.Intersects(newRect)) { endPoints.Push(endPoint); return(GenChunkAddedStatus.NotAdded); } } newChunk = new Chunk(x, y, template, this); Chunks.Add(newChunk); endsToAdd = new List <GenEndPoint>(); for (int i = 0; i < 4; ++i) { foreach (ChunkConnector connector in template.GetConnectors((ConnectorFace)i)) { if (connector.X != con.X || connector.Y != con.Y) { GenEndPoint end = new GenEndPoint(newChunk, connector, endPoint.Depth + 1); if (maxDepth != 0 && endPoint.Depth >= maxDepth) { looseEnds.Add(end); } else { endsToAdd.Add(end); } } } } while (endsToAdd.Count > 0) { int index = rand.Next(endsToAdd.Count); endPoints.Push(endsToAdd[index]); endsToAdd.RemoveAt(index); } rects.Add(newRect); area += newChunk.Area; return(GenChunkAddedStatus.Added); }