Skip to content

Commit

Permalink
Be more consistent in map generation messages and error handling
Browse files Browse the repository at this point in the history
Also remove long deprecated Server.levels
  • Loading branch information
UnknownShadow200 committed Dec 19, 2022
1 parent 6d4cf79 commit 5bc9411
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 54 deletions.
4 changes: 4 additions & 0 deletions MCGalaxy/Generator/MapGen.cs
Expand Up @@ -113,7 +113,11 @@ public sealed class MapGen
try {
p.Message("Generating map \"{0}\"..", name);
lvl = new Level(name, x, y, z);

DateTime start = DateTime.UtcNow;
if (!gen.Generate(p, lvl, seed)) { lvl.Dispose(); return null; }
Logger.Log(LogType.SystemActivity, "Generation completed in {0:F3} seconds",
(DateTime.UtcNow - start).TotalSeconds);

string msg = seed.Length > 0 ? "λNICK&S created level {0}&S with seed \"{1}\"" : "λNICK&S created level {0}";
Chat.MessageFrom(p, string.Format(msg, lvl.ColoredName, seed));
Expand Down
78 changes: 35 additions & 43 deletions MCGalaxy/Generator/Realistic/MapGen.cs
Expand Up @@ -40,57 +40,49 @@ public sealed class RealisticMapGen

public bool Gen(Player p, Level lvl, string seed, RealisticMapGenArgs args) {
DateTime start = DateTime.UtcNow;
Logger.Log(LogType.SystemActivity, "Attempting map gen");
rng = MapGen.MakeRng(seed);
this.args = args;

try {
terrain = new float[lvl.Width * lvl.Length];
overlay = new float[lvl.Width * lvl.Length];
if (args.GenTrees) overlay2 = new float[lvl.Width * lvl.Length];

LiquidLevel = args.GetLiquidLevel(lvl.Height);
LiquidLevel = (ushort)Math.Min(LiquidLevel, lvl.MaxY);
terrain = new float[lvl.Width * lvl.Length];
overlay = new float[lvl.Width * lvl.Length];
if (args.GenTrees) overlay2 = new float[lvl.Width * lvl.Length];

LiquidLevel = args.GetLiquidLevel(lvl.Height);
LiquidLevel = (ushort)Math.Min(LiquidLevel, lvl.MaxY);

GenerateFault(terrain, lvl);
FilterAverage(lvl);
Logger.Log(LogType.SystemActivity, "Creating overlay");
GeneratePerlinNoise(overlay, lvl);
GenerateFault(terrain, lvl);
FilterAverage(lvl);
Logger.Log(LogType.SystemActivity, "Creating overlay");
GeneratePerlinNoise(overlay, lvl);

if (args.GenerateOverlay2) {
Logger.Log(LogType.SystemActivity, "Planning trees");
GeneratePerlinNoise(overlay2, lvl);
}
if (args.GenerateOverlay2) {
Logger.Log(LogType.SystemActivity, "Planning trees");
GeneratePerlinNoise(overlay2, lvl);
}

Logger.Log(LogType.SystemActivity, "Converting height map, and applying overlays");
float rangeLo = args.RangeLow;
float rangeHi = args.RangeHigh;
treeDens = args.TreeDensity;
treeDist = args.TreeDistance;
Logger.Log(LogType.SystemActivity, "Converting height map, and applying overlays");
float rangeLo = args.RangeLow;
float rangeHi = args.RangeHigh;
treeDens = args.TreeDensity;
treeDist = args.TreeDistance;

//loops though evey X/Z coordinate
for (int index = 0; index < terrain.Length; index++) {
ushort x = (ushort)(index % lvl.Width);
ushort z = (ushort)(index / lvl.Width); // TODO don't % /
ushort y;

if (args.FalloffEdges) {
float offset = NegateEdge(x, z, lvl);
y = Evaluate(lvl, Range(terrain[index], rangeLo, rangeHi) - offset);
} else {
y = Evaluate(lvl, Range(terrain[index], rangeLo, rangeHi));
}

if (!args.UseLavaLiquid)
GenNonLavaColumn(x, y, z, lvl, index);
else
GenLavaColumn(x, y, z, lvl, index);
//loops though evey X/Z coordinate
for (int index = 0; index < terrain.Length; index++) {
ushort x = (ushort)(index % lvl.Width);
ushort z = (ushort)(index / lvl.Width); // TODO don't % /
ushort y;

if (args.FalloffEdges) {
float offset = NegateEdge(x, z, lvl);
y = Evaluate(lvl, Range(terrain[index], rangeLo, rangeHi) - offset);
} else {
y = Evaluate(lvl, Range(terrain[index], rangeLo, rangeHi));
}
Logger.Log(LogType.SystemActivity, "Total time was {0} seconds.", (DateTime.UtcNow - start).TotalSeconds);
} catch (Exception e) {
Logger.LogError(e);
p.Message("&WGeneration failed. See error logs.");
return false;

if (!args.UseLavaLiquid)
GenNonLavaColumn(x, y, z, lvl, index);
else
GenLavaColumn(x, y, z, lvl, index);
}
return true;
}
Expand Down
4 changes: 1 addition & 3 deletions MCGalaxy/Generator/fCraft/MapGen.cs
Expand Up @@ -116,7 +116,7 @@ public sealed class fCraftMapGen {

#region Map Processing

public void GenerateMap(Level map) {
void GenerateMap(Level map) {
// Match water coverage
float desiredWaterLevel = .5f;
if( args.MatchWaterCoverage ) {
Expand Down Expand Up @@ -165,8 +165,6 @@ public sealed class fCraftMapGen {
ReportProgress( 5, "Processing: Planting trees" );
GenerateTrees( map );
}

ReportProgress( 0, "Generation complete" );
}

void Fill( Level map, float desiredWaterLevel, float aboveWaterMultiplier, float[] altmap ) {
Expand Down
11 changes: 7 additions & 4 deletions MCGalaxy/Levels/LevelInfo.cs
Expand Up @@ -28,11 +28,12 @@ public static class LevelInfo {

/// <summary> Array of all current loaded levels. </summary>
/// <remarks> Note this field is highly volatile, you should cache references to the items array. </remarks>
public static VolatileArray<Level> Loaded = new VolatileArray<Level>(true);
public static VolatileArray<Level> Loaded = new VolatileArray<Level>();

public static Level FindExact(string name) {
Level[] loaded = Loaded.Items;
foreach (Level lvl in loaded) {
foreach (Level lvl in loaded)
{
if (lvl.name.CaselessEq(name)) return lvl;
}
return null;
Expand All @@ -56,7 +57,8 @@ public static class LevelInfo {

public static string[] AllMapNames() {
string[] files = AllMapFiles();
for (int i = 0; i < files.Length; i++) {
for (int i = 0; i < files.Length; i++)
{
files[i] = Path.GetFileNameWithoutExtension(files[i]);
}
return files;
Expand Down Expand Up @@ -179,7 +181,8 @@ public static class LevelInfo {
public static bool IsRealmOwner(string map, LevelConfig cfg, string name) {
string[] owners = cfg.RealmOwner.SplitComma();
if (owners.Length > 0) {
foreach (string owner in owners) {
foreach (string owner in owners)
{
if (owner.CaselessEq(name)) return true;
}
return false;
Expand Down
2 changes: 0 additions & 2 deletions MCGalaxy/Server/Server.Fields.cs
Expand Up @@ -63,8 +63,6 @@ public sealed partial class Server {
public static readonly List<string> Opstats = new List<string>() { "ban", "tempban", "xban", "banip", "kick", "warn", "mute", "freeze", "setrank" };

public static Level mainLevel;
[Obsolete("Use LevelInfo.Loaded.Items")]
public static List<Level> levels;

public static PlayerList reviewlist = new PlayerList();
static string[] announcements = new string[0];
Expand Down
2 changes: 0 additions & 2 deletions MCGalaxy/Server/Server.cs
Expand Up @@ -86,11 +86,9 @@ public sealed partial class Server
IOperatingSystem.DetectOS().Init();
#pragma warning disable 0618
Player.players = PlayerInfo.Online.list;
Server.levels = LevelInfo.Loaded.list;
#pragma warning restore 0618

StartTime = DateTime.UtcNow;
shuttingDown = false;
Logger.Log(LogType.SystemActivity, "Starting Server");
ServicePointManager.Expect100Continue = false;
ForceEnableTLS();
Expand Down

0 comments on commit 5bc9411

Please sign in to comment.