diff --git a/Subsurface/Source/Characters/BackgroundSprite/BackgroundSpriteManager.cs b/Subsurface/Source/Characters/BackgroundSprite/BackgroundSpriteManager.cs index f9c42db9d..a8c6007b5 100644 --- a/Subsurface/Source/Characters/BackgroundSprite/BackgroundSpriteManager.cs +++ b/Subsurface/Source/Characters/BackgroundSprite/BackgroundSpriteManager.cs @@ -133,53 +133,74 @@ namespace Barotrauma if (!prefab.SpawnOnWalls) return randomPos; - var cells = level.GetCells(randomPos); - - if (!cells.Any()) return null; - - VoronoiCell cell = cells[Rand.Int(cells.Count, false)]; List edges = new List(); List normals = new List(); - foreach (GraphEdge edge in cell.edges) + + var cells = level.GetCells(randomPos); + + if (cells.Any()) { - if (!edge.isSolid || edge.OutsideLevel) continue; + VoronoiCell cell = cells[Rand.Int(cells.Count, false)]; - Vector2 normal = edge.GetNormal(cell); + foreach (GraphEdge edge in cell.edges) + { + if (!edge.isSolid || edge.OutsideLevel) continue; + + Vector2 normal = edge.GetNormal(cell); - if (prefab.Alignment.HasFlag(Alignment.Bottom) && normal.Y < -0.5f) - { - edges.Add(edge); - } - else if (prefab.Alignment.HasFlag(Alignment.Top) && normal.Y > 0.5f) - { - edges.Add(edge); - } - else if (prefab.Alignment.HasFlag(Alignment.Left) && normal.X < -0.5f) - { - edges.Add(edge); - } - else if (prefab.Alignment.HasFlag(Alignment.Right) && normal.X > 0.5f) - { - edges.Add(edge); - } - else - { - continue; - } + if (prefab.Alignment.HasFlag(Alignment.Bottom) && normal.Y < -0.5f) + { + edges.Add(edge); + } + else if (prefab.Alignment.HasFlag(Alignment.Top) && normal.Y > 0.5f) + { + edges.Add(edge); + } + else if (prefab.Alignment.HasFlag(Alignment.Left) && normal.X < -0.5f) + { + edges.Add(edge); + } + else if (prefab.Alignment.HasFlag(Alignment.Right) && normal.X > 0.5f) + { + edges.Add(edge); + } + else + { + continue; + } - normals.Add(normal); + normals.Add(normal); + } + } + + foreach (RuinGeneration.Ruin ruin in Level.Loaded.Ruins) + { + Rectangle expandedArea = ruin.Area; + expandedArea.Inflate(ruin.Area.Width, ruin.Area.Height); + if (!expandedArea.Contains(randomPos)) continue; + + foreach (var ruinShape in ruin.RuinShapes) + { + foreach (var wall in ruinShape.Walls) + { + if (!prefab.Alignment.HasFlag(ruinShape.GetLineAlignment(wall))) continue; + + edges.Add(new GraphEdge(wall.A, wall.B)); + normals.Add((wall.A + wall.B) / 2.0f - ruinShape.Center); + } + } } if (!edges.Any()) return null; - int index = Rand.Int(edges.Count,false); + int index = Rand.Int(edges.Count, false); closestEdge = edges[index]; edgeNormal = normals[index]; float length = Vector2.Distance(closestEdge.point1, closestEdge.point2); Vector2 dir = (closestEdge.point1 - closestEdge.point2) / length; Vector2 pos = closestEdge.point2 + dir * Rand.Range(prefab.Sprite.size.X / 2.0f, length - prefab.Sprite.size.X / 2.0f, false); - + return pos; } diff --git a/Subsurface/Source/Map/Levels/Level.cs b/Subsurface/Source/Map/Levels/Level.cs index da3c4e9f1..a7be1b181 100644 --- a/Subsurface/Source/Map/Levels/Level.cs +++ b/Subsurface/Source/Map/Levels/Level.cs @@ -732,7 +732,13 @@ namespace Barotrauma { if (MathUtils.GetLineRectangleIntersection(e.point1, e.point2, ruinShape.Rect) != null) { - cell.CellType = CellType.Empty; + cell.CellType = CellType.Removed; + + int x = (int)Math.Floor(cell.Center.X / GridCellSize); + int y = (int)Math.Floor(cell.Center.Y / GridCellSize); + + cellGrid[x, y].Remove(cell); + cells.Remove(cell); break; } } diff --git a/Subsurface/Source/Map/Levels/Voronoi.cs b/Subsurface/Source/Map/Levels/Voronoi.cs index 41a2252fa..2804390e0 100644 --- a/Subsurface/Source/Map/Levels/Voronoi.cs +++ b/Subsurface/Source/Map/Levels/Voronoi.cs @@ -538,10 +538,8 @@ namespace Voronoi2 private void pushGraphEdge( Site leftSite, Site rightSite, Vector2 point1, Vector2 point2 ) { - GraphEdge newEdge = new GraphEdge (); + GraphEdge newEdge = new GraphEdge(point1, point2); allEdges.Add ( newEdge ); - newEdge.point1 = point1; - newEdge.point2 = point2; newEdge.site1 = leftSite; newEdge.site2 = rightSite; diff --git a/Subsurface/Source/Map/Levels/VoronoiElements.cs b/Subsurface/Source/Map/Levels/VoronoiElements.cs index 6755c57f8..4ba871c21 100644 --- a/Subsurface/Source/Map/Levels/VoronoiElements.cs +++ b/Subsurface/Source/Map/Levels/VoronoiElements.cs @@ -155,18 +155,14 @@ namespace Voronoi2 for (int i = 1; i < vertices.Length; i++ ) { - GraphEdge ge = new GraphEdge(); - ge.point1 = vertices[i-1]; - ge.point2 = vertices[i]; + GraphEdge ge = new GraphEdge(vertices[i-1], vertices[i]); System.Diagnostics.Debug.Assert(ge.point1 != ge.point2); edges.Add(ge); } - GraphEdge lastEdge = new GraphEdge(); - lastEdge.point1 = vertices[0]; - lastEdge.point2 = vertices[vertices.Length-1]; + GraphEdge lastEdge = new GraphEdge(vertices[0], vertices[vertices.Length-1]); edges.Add(lastEdge); @@ -208,6 +204,12 @@ namespace Voronoi2 get { return (point1 + point2) / 2.0f; } } + public GraphEdge(Vector2 point1, Vector2 point2) + { + this.point1 = point1; + this.point2 = point2; + } + public VoronoiCell AdjacentCell(VoronoiCell cell) { if (cell1==cell)