(b8f75ee96) Fixed level inequality errors caused by 866621c. Accessing the level start/end location properties during level generation caused the seed of RandSync.Server to be changed. Now generating a new random location uses a new MTRandom instance and doesn't touch RandSync.Server.

This commit is contained in:
Joonas Rikkonen
2019-03-31 19:12:27 +03:00
parent 8668647049
commit fa2a338d73
7 changed files with 22 additions and 31 deletions
@@ -233,7 +233,8 @@ namespace Barotrauma
levelSeed = value; levelSeed = value;
backgroundSprite = LocationType.Random(levelSeed)?.GetPortrait(ToolBox.StringToInt(levelSeed)); int intSeed = ToolBox.StringToInt(levelSeed);
backgroundSprite = LocationType.Random(new MTRandom(intSeed))?.GetPortrait(intSeed);
seedBox.Text = levelSeed; seedBox.Text = levelSeed;
//lastUpdateID++; //lastUpdateID++;
@@ -148,7 +148,7 @@ namespace Barotrauma
lastUpdateID++; lastUpdateID++;
levelSeed = value; levelSeed = value;
LocationType.Random(levelSeed); //call to sync up with clients LocationType.Random(new MTRandom(ToolBox.StringToInt(levelSeed))); //call to sync up with clients
} }
} }
@@ -139,7 +139,7 @@ namespace Barotrauma
MTRandom rand = new MTRandom(ToolBox.StringToInt(seed)); MTRandom rand = new MTRandom(ToolBox.StringToInt(seed));
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
dummyLocations[i] = Location.CreateRandom(new Vector2((float)rand.NextDouble() * 10000.0f, (float)rand.NextDouble() * 10000.0f), null); dummyLocations[i] = Location.CreateRandom(new Vector2((float)rand.NextDouble() * 10000.0f, (float)rand.NextDouble() * 10000.0f), null, rand);
} }
} }
@@ -1519,14 +1519,6 @@ namespace Barotrauma
string outpostFile = outpostFiles.GetRandom(Rand.RandSync.Server); string outpostFile = outpostFiles.GetRandom(Rand.RandSync.Server);
var outpost = new Submarine(outpostFile, tryLoad: false); var outpost = new Submarine(outpostFile, tryLoad: false);
outpost.Load(unloadPrevious: false); outpost.Load(unloadPrevious: false);
if (i == 0)
{
if (GameMain.GameSession?.StartLocation != null) { outpost.Name = GameMain.GameSession.StartLocation.Name; }
}
else
{
if (GameMain.GameSession?.EndLocation != null) { outpost.Name = GameMain.GameSession.EndLocation.Name; }
}
outpost.MakeOutpost(); outpost.MakeOutpost();
Point? minSize = null; Point? minSize = null;
@@ -1567,10 +1559,12 @@ namespace Barotrauma
if ((i == 0) == !Mirrored) if ((i == 0) == !Mirrored)
{ {
StartOutpost = outpost; StartOutpost = outpost;
if (GameMain.GameSession?.StartLocation != null) { outpost.Name = GameMain.GameSession.StartLocation.Name; }
} }
else else
{ {
EndOutpost = outpost; EndOutpost = outpost;
if (GameMain.GameSession?.EndLocation != null) { outpost.Name = GameMain.GameSession.EndLocation.Name; }
} }
} }
} }
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -76,10 +77,10 @@ namespace Barotrauma
} }
} }
public Location(Vector2 mapPosition, int? zone) public Location(Vector2 mapPosition, int? zone, Random rand)
{ {
this.Type = LocationType.Random("", zone); this.Type = LocationType.Random(rand, zone);
this.Name = RandomName(Type); this.Name = RandomName(Type, rand);
this.MapPosition = mapPosition; this.MapPosition = mapPosition;
PortraitId = ToolBox.StringToInt(Name); PortraitId = ToolBox.StringToInt(Name);
@@ -87,9 +88,9 @@ namespace Barotrauma
Connections = new List<LocationConnection>(); Connections = new List<LocationConnection>();
} }
public static Location CreateRandom(Vector2 position, int? zone) public static Location CreateRandom(Vector2 position, int? zone , Random rand)
{ {
return new Location(position, zone); return new Location(position, zone, rand);
} }
public IEnumerable<Mission> GetMissionsInConnection(LocationConnection connection) public IEnumerable<Mission> GetMissionsInConnection(LocationConnection connection)
@@ -129,10 +130,10 @@ namespace Barotrauma
availableMissions.RemoveAll(m => m.Completed); availableMissions.RemoveAll(m => m.Completed);
} }
private string RandomName(LocationType type) private string RandomName(LocationType type, Random rand)
{ {
baseName = type.GetRandomName(); baseName = type.GetRandomName(rand);
nameFormatIndex = Rand.Int(type.NameFormats.Count, Rand.RandSync.Server); nameFormatIndex = rand.Next() % type.NameFormats.Count;
return type.NameFormats[nameFormatIndex].Replace("[name]", baseName); return type.NameFormats[nameFormatIndex].Replace("[name]", baseName);
} }
@@ -155,20 +155,15 @@ namespace Barotrauma
return portraits[Math.Abs(portraitId) % portraits.Count]; return portraits[Math.Abs(portraitId) % portraits.Count];
} }
public string GetRandomName() public string GetRandomName(Random rand)
{ {
return names[Rand.Int(names.Count, Rand.RandSync.Server)]; return names[rand.Next() % names.Count];
} }
public static LocationType Random(string seed = "", int? zone = null) public static LocationType Random(Random rand, int? zone = null)
{ {
Debug.Assert(List.Count > 0, "LocationType.list.Count == 0, you probably need to initialize LocationTypes"); Debug.Assert(List.Count > 0, "LocationType.list.Count == 0, you probably need to initialize LocationTypes");
if (!string.IsNullOrWhiteSpace(seed))
{
Rand.SetSyncedSeed(ToolBox.StringToInt(seed));
}
List<LocationType> allowedLocationTypes = zone.HasValue ? List.FindAll(lt => lt.CommonnessPerZone.ContainsKey(zone.Value)) : List; List<LocationType> allowedLocationTypes = zone.HasValue ? List.FindAll(lt => lt.CommonnessPerZone.ContainsKey(zone.Value)) : List;
if (allowedLocationTypes.Count == 0) if (allowedLocationTypes.Count == 0)
@@ -180,12 +175,12 @@ namespace Barotrauma
{ {
return ToolBox.SelectWeightedRandom( return ToolBox.SelectWeightedRandom(
allowedLocationTypes, allowedLocationTypes,
allowedLocationTypes.Select(a => a.CommonnessPerZone[zone.Value]).ToList(), allowedLocationTypes.Select(a => a.CommonnessPerZone[zone.Value]).ToList(),
Rand.RandSync.Server); rand);
} }
else else
{ {
return allowedLocationTypes[Rand.Int(allowedLocationTypes.Count, Rand.RandSync.Server)]; return allowedLocationTypes[rand.Next() % allowedLocationTypes.Count];
} }
} }
@@ -196,7 +196,7 @@ namespace Barotrauma
Vector2 position = points[positionIndex]; Vector2 position = points[positionIndex];
if (newLocations[1 - i] != null && newLocations[1 - i].MapPosition == position) position = points[1 - positionIndex]; if (newLocations[1 - i] != null && newLocations[1 - i].MapPosition == position) position = points[1 - positionIndex];
int zone = MathHelper.Clamp(generationParams.DifficultyZones - (int)Math.Floor(Vector2.Distance(position, mapCenter) / zoneRadius), 1, generationParams.DifficultyZones); int zone = MathHelper.Clamp(generationParams.DifficultyZones - (int)Math.Floor(Vector2.Distance(position, mapCenter) / zoneRadius), 1, generationParams.DifficultyZones);
newLocations[i] = Location.CreateRandom(position, zone); newLocations[i] = Location.CreateRandom(position, zone, Rand.GetRNG(Rand.RandSync.Server));
Locations.Add(newLocations[i]); Locations.Add(newLocations[i]);
} }