From a6689b894d2614d381f4b47684243578b1e7a731 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Sun, 20 Aug 2017 13:20:23 +0300 Subject: [PATCH] Map generation tweaking: some biomes are placed at the center of the map, some at the center and some randomly. --- .../GameSession/GameModes/SinglePlayerMode.cs | 2 +- .../BarotraumaClient/Source/Map/Map/Map.cs | 10 +-- .../Content/Map/LevelGenerationParameters.xml | 15 ++-- .../Map/Levels/LevelGenerationParams.cs | 20 +++++ .../BarotraumaShared/Source/Map/Map/Map.cs | 79 ++++++++++++++++++- 5 files changed, 110 insertions(+), 16 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/GameSession/GameModes/SinglePlayerMode.cs b/Barotrauma/BarotraumaClient/Source/GameSession/GameModes/SinglePlayerMode.cs index 4d69122d1..796ed03d9 100644 --- a/Barotrauma/BarotraumaClient/Source/GameSession/GameModes/SinglePlayerMode.cs +++ b/Barotrauma/BarotraumaClient/Source/GameSession/GameModes/SinglePlayerMode.cs @@ -112,7 +112,7 @@ namespace Barotrauma public void GenerateMap(string seed) { - Map = new Map(seed, 500); + Map = new Map(seed, 1000); } public override void Start() diff --git a/Barotrauma/BarotraumaClient/Source/Map/Map/Map.cs b/Barotrauma/BarotraumaClient/Source/Map/Map/Map.cs index 2e97ba120..db4c64e19 100644 --- a/Barotrauma/BarotraumaClient/Source/Map/Map/Map.cs +++ b/Barotrauma/BarotraumaClient/Source/Map/Map/Map.cs @@ -114,13 +114,11 @@ namespace Barotrauma new Rectangle((int)start.X, (int)start.Y, (int)dist + 2, width), new Rectangle(0, 0, iceCrack.Width, 60), crackColor, MathUtils.VectorToAngle(end - start), new Vector2(0, 30), SpriteEffects.None, 0.01f); - - if (i == 0) - { - - GUI.DrawString(spriteBatch, start, connection.Biome.Name, Color.White); - } } + + //TODO: remove + Vector2 center = rectCenter + (connection.CenterPos + offset) * scale; + GUI.DrawString(spriteBatch, center, connection.Biome.Name, Color.White); } rect.Inflate(8, 8); diff --git a/Barotrauma/BarotraumaShared/Content/Map/LevelGenerationParameters.xml b/Barotrauma/BarotraumaShared/Content/Map/LevelGenerationParameters.xml index c7e8ee73f..9d1c1ee93 100644 --- a/Barotrauma/BarotraumaShared/Content/Map/LevelGenerationParameters.xml +++ b/Barotrauma/BarotraumaShared/Content/Map/LevelGenerationParameters.xml @@ -4,24 +4,29 @@ + description= "asdgfdfg" + MapPlacement="Center, Random"/> + description= "asdgfdfg" + MapPlacement="Random"/> + description= "asdgfdfg" + MapPlacement="Random"/> + + description= "asdgfdfg" + MapPlacement="Edge"/> sites = new List(); - for (int i = 0; i < 50; i++) + for (int i = 0; i < 100; i++) { sites.Add(new Vector2(Rand.Range(0.0f, size, Rand.RandSync.Server), Rand.Range(0.0f, size, Rand.RandSync.Server))); } @@ -159,6 +159,7 @@ namespace Barotrauma connections.Add(new LocationConnection(newLocations[0], newLocations[1])); } + //remove connections that are too short float minDistance = 50.0f; for (int i = connections.Count - 1; i >= 0; i--) { @@ -201,6 +202,7 @@ namespace Barotrauma } } + foreach (LocationConnection connection in connections) { Vector2 start = connection.Locations[0].MapPosition; @@ -208,12 +210,54 @@ namespace Barotrauma int generations = (int)(Math.Sqrt(Vector2.Distance(start, end) / 10.0f)); connection.CrackSegments = MathUtils.GenerateJaggedLine(start, end, generations, 5.0f); } + + AssignBiomes(); + } + private void AssignBiomes() + { List biomeSeeds = new List(); - foreach (Biome biome in LevelGenerationParams.GetBiomes()) + + List centerBiomes = LevelGenerationParams.GetBiomes().FindAll(b => b.Placement.HasFlag(Biome.MapPlacement.Center)); + if (centerBiomes.Count > 0) + { + Vector2 mapCenter = new Vector2(locations.Sum(l => l.MapPosition.X), locations.Sum(l => l.MapPosition.Y)) / locations.Count; + foreach (Biome centerBiome in centerBiomes) + { + LocationConnection closestConnection = null; + float closestDist = float.PositiveInfinity; + foreach (LocationConnection connection in connections) + { + if (connection.Biome != null) continue; + + float dist = Vector2.Distance(connection.CenterPos, mapCenter); + if (closestConnection == null || dist < closestDist) + { + closestConnection = connection; + closestDist = dist; + } + } + + closestConnection.Biome = centerBiome; + biomeSeeds.Add(closestConnection); + } + } + + List edgeBiomes = LevelGenerationParams.GetBiomes().FindAll(b => b.Placement.HasFlag(Biome.MapPlacement.Edge)); + if (edgeBiomes.Count > 0) + { + List edges = GetMapEdges(); + foreach (LocationConnection edge in edges) + { + edge.Biome = edgeBiomes[Rand.Range(0, edgeBiomes.Count, Rand.RandSync.Server)]; + } + } + + List randomBiomes = LevelGenerationParams.GetBiomes().FindAll(b => b.Placement.HasFlag(Biome.MapPlacement.Random)); + foreach (Biome biome in randomBiomes) { LocationConnection seed = connections[0]; - while (biomeSeeds.Contains(seed)) + while (seed.Biome != null) { seed = connections[Rand.Range(0, connections.Count, Rand.RandSync.Server)]; } @@ -247,6 +291,25 @@ namespace Barotrauma ExpandBiomes(nextSeeds); } } + + private List GetMapEdges() + { + List verts = locations.Select(l => l.MapPosition).ToList(); + + List giftWrappedVerts = MathUtils.GiftWrap(verts); + + List edges = new List(); + foreach (LocationConnection connection in connections) + { + if (giftWrappedVerts.Contains(connection.Locations[0].MapPosition) || + giftWrappedVerts.Contains(connection.Locations[1].MapPosition)) + { + edges.Add(connection); + } + } + + return edges; + } private void GenerateDifficulties(Location start, List locations, float currDifficulty) @@ -339,6 +402,14 @@ namespace Barotrauma get { return level; } set { level = value; } } + + public Vector2 CenterPos + { + get + { + return (locations[0].MapPosition + locations[1].MapPosition) / 2.0f; + } + } public LocationConnection(Location location1, Location location2) {