Skip to content

Commit

Permalink
refactor: adjust according to rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ktos committed Jun 18, 2024
1 parent f705b5e commit 761dbec
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 136 deletions.
6 changes: 3 additions & 3 deletions Server/Modules/MapGenerator/Models/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class Node : IPathfindingNode
{
public int XId { get; set; }
public int YId { get; set; }
public Biome Biome { get; set; }
public Biome? Biome { get; set; }

public string Color { get; set; }
public string? Color { get; set; }
public float NoiseValue { get; set; }

public int GCost { get; set; }
Expand All @@ -17,7 +17,7 @@ public class Node : IPathfindingNode
public bool Walkable { get; set; }
public IPathfindingNode? PreviousNode { get; set; }

public Node(int xId, int yId, Biome biome)
public Node(int xId, int yId, Biome? biome)
{
XId = xId;
YId = yId;
Expand Down
128 changes: 58 additions & 70 deletions Server/Modules/MapGenerator/Services/MapGeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,16 @@ namespace Fracture.Server.Modules.MapGenerator.Services;

public class MapGeneratorService : IMapGeneratorService
{
private MapData _mapData;
private MapData _mapData = default!;

private float persistance = 0.5f;
private float lacunarity = 2f;
private int octaves = 5;
private float scale = 5f;
private readonly float _persistence = 0.5f;
private readonly float _lacunarity = 2f;
private readonly int _octaves = 5;
private readonly float _scale = 5f;

private Random rnd = new Random();
private Random _rnd = new Random();

//private Dictionary<(TemperatureType, HeightType), Biome> biomesDictionary = new Dictionary<(TemperatureType, HeightType), Biome>
//{
// [(TemperatureType.Cold, HeightType.Medium)] = new Biome(BiomeType.ShallowWater, Color.Blue, 1f, 0.3f)
//};

//private Biome[] biomes = {
// new Biome(BiomeType.ShallowWater, Color.Blue, 1f, 0.3f),
// new Biome(BiomeType.Grassland, Color.Green, 1f, 0.5f),
// new Biome(BiomeType.Desert, Color.Yellow, 1f, 0.5f),
// new Biome(BiomeType.Mountains, Color.Gray, 0f, 0.5f)
// };

private int seed;
private int _seed;

public MapData MapData
{
Expand All @@ -39,40 +27,37 @@ public Task<MapData> GetMap(NoiseParameters noiseParameters)
{
_mapData = GenerateMap(noiseParameters);
return Task.FromResult(_mapData);
//if (map == null || noiseParameters.GenerateNew)
//{
// map = GenerateMap(noiseParameters);
//}
//return Task.FromResult(map);
}

private MapData GenerateMap(NoiseParameters noiseParameters)
{
int width = 32;
int height = 32;
bool useFalloff = true;
seed = noiseParameters.UseRandomSeed ? rnd.Next(int.MaxValue) : noiseParameters.Seed;
_seed = noiseParameters.UseRandomSeed ? _rnd.Next(int.MaxValue) : noiseParameters.Seed;

var grid = new Node[width, height];

Node[,] grid = new Node[width, height];
float[,] falloffMap = FalloffGenerator.Generate(width);
float[,] heightMap = PerlinNoiseGenerator.Generate(
var falloffMap = FalloffGenerator.Generate(width);
var heightMap = PerlinNoiseGenerator.Generate(
width,
height,
seed,
scale,
octaves,
persistance,
lacunarity,
_seed,
_scale,
_octaves,
_persistence,
_lacunarity,
Vector2.Zero
);
float[,] temperatureMap = PerlinNoiseGenerator.Generate(

var temperatureMap = PerlinNoiseGenerator.Generate(
width,
height,
seed + 1,
scale,
octaves,
persistance,
lacunarity,
_seed + 1,
_scale,
_octaves,
_persistence,
_lacunarity,
Vector2.Zero
);

Expand All @@ -88,39 +73,42 @@ private MapData GenerateMap(NoiseParameters noiseParameters)
grid[x, y] = new Node(x, y, null);
grid[x, y].NoiseValue = heightMap[x, y];
grid[x, y].Walkable = grid[x, y].NoiseValue > 0.2f && grid[x, y].NoiseValue < 0.7f;

if (heightMap[x, y] < 0.02f)
{
grid[x, y].Color = "#21618C";
}
else if (heightMap[x, y] < 0.2f)
{
grid[x, y].Color = "#2E86C1";
}
else if (heightMap[x, y] < 0.40f)
{
grid[x, y].Color = "#F9E79F";
}
else if (heightMap[x, y] < 0.55f)
{
grid[x, y].Color = "#28B463";
}
else if (heightMap[x, y] < 0.7f)
{
grid[x, y].Color = "#1D8348";
}
else if (heightMap[x, y] < 0.85f)
{
grid[x, y].Color = "#616A6B";
}
else
{
grid[x, y].Color = "#515A5A";
}
HeightToColor(grid, heightMap, y, x);
}
}

MapData map = new MapData(grid);
return map;
return new MapData(grid);
}

private static void HeightToColor(Node[,] grid, float[,] heightMap, int y, int x)
{
if (heightMap[x, y] < 0.02f)
{
grid[x, y].Color = "#21618C";
}
else if (heightMap[x, y] < 0.2f)
{
grid[x, y].Color = "#2E86C1";
}
else if (heightMap[x, y] < 0.40f)
{
grid[x, y].Color = "#F9E79F";
}
else if (heightMap[x, y] < 0.55f)
{
grid[x, y].Color = "#28B463";
}
else if (heightMap[x, y] < 0.7f)
{
grid[x, y].Color = "#1D8348";
}
else if (heightMap[x, y] < 0.85f)
{
grid[x, y].Color = "#616A6B";
}
else
{
grid[x, y].Color = "#515A5A";
}
}
}
94 changes: 46 additions & 48 deletions Server/Modules/MapGenerator/UI/MapView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,59 @@
@if (Map != null && MapDisplayData != null)
{
<table class="@(IsMiniMap ? "miniMapTable" : "mapTable")">
@for (int y = 0; y < Map.Grid.GetLength(1); y++)
{
<tr>
@for (int x = 0; x < Map.Grid.GetLength(0); x++)
{
@if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.Position)
{
<td style='background: @GetTileColor(@Map.Grid[x,y])'>
x:@x<br />y:@y
</td>
}
else if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.Noise)
{
<td style="background: @GetTileColor(@Map.Grid[x,y])">
@Math.Round(Map.Grid[x, y].NoiseValue, 2)
</td>
}
else if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.None)
{
<td style='background: @GetTileColor(@Map.Grid[x,y])'>
</td>
}
else if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.Path)
@for (int y = 0; y < Map.Grid.GetLength(1); y++)
{
<tr>
@for (int x = 0; x < Map.Grid.GetLength(0); x++)
{
<td style='font-size: 16px; background: @GetTileColor(@Map.Grid[x,y])'>
@GetPathForTile(Map.Grid[x, y])
</td>
@if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.Position)
{
<td style='background: @GetTileColor(@Map.Grid[x,y])'>
x:@x<br />y:@y
</td>
}
else if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.Noise)
{
<td style="background: @GetTileColor(@Map.Grid[x,y])">
@Math.Round(Map.Grid[x, y].NoiseValue, 2)
</td>
}
else if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.None)
{
<td style='background: @GetTileColor(@Map.Grid[x,y])'>
</td>
}
else if (MapDisplayData.TileInformationDisplay == TileInformationDisplay.Path)
{
<td style='font-size: 16px; background: @GetTileColor(@Map.Grid[x,y])'>
@GetPathForTile(Map.Grid[x, y])
</td>
}
}
}
</tr>
}
</tr>
}
</table>
}
</div>

@code {
[Parameter]
public MapData? Map { get; set; }

[Parameter]
public MapDisplayData? MapDisplayData { get; set; }

[Parameter]
public List<IPathfindingNode>? Path { get; set; }

[Parameter] public bool IsMiniMap { get; set; } = true;
[Parameter]
public bool IsMiniMap { get; set; } = true;

private string GetTileColor(Node node)
{
if (MapDisplayData!.ShowColorMap)
{
return node.Color;
return node.Color ?? string.Empty;
}

string hexValue = string.Format("{0:x}", (int)(node.NoiseValue * 255));
Expand Down Expand Up @@ -85,22 +88,17 @@

private string GetArrowForDirection(int xDirection, int yDirection)
{
if (yDirection == 1 && xDirection == 0)
return SpecialChars.Arrows.Up;
else if (yDirection == 1 && xDirection == 1)
return SpecialChars.Arrows.UpRight;
else if (yDirection == 0 && xDirection == 1)
return SpecialChars.Arrows.Right;
else if (yDirection == -1 && xDirection == 1)
return SpecialChars.Arrows.DownRight;
else if (yDirection == -1 && xDirection == 0)
return SpecialChars.Arrows.Down;
else if (yDirection == -1 && xDirection == -1)
return SpecialChars.Arrows.DownLeft;
else if (yDirection == 0 && xDirection == -1)
return SpecialChars.Arrows.Left;
else if (yDirection == 1 && xDirection == -1)
return SpecialChars.Arrows.UpLeft;
return "";
return yDirection switch
{
1 when xDirection == 0 => SpecialChars.Arrows.Up,
1 when xDirection == 1 => SpecialChars.Arrows.UpRight,
0 when xDirection == 1 => SpecialChars.Arrows.Right,
-1 when xDirection == 1 => SpecialChars.Arrows.DownRight,
-1 when xDirection == 0 => SpecialChars.Arrows.Down,
-1 when xDirection == -1 => SpecialChars.Arrows.DownLeft,
0 when xDirection == -1 => SpecialChars.Arrows.Left,
1 when xDirection == -1 => SpecialChars.Arrows.UpLeft,
_ => ""
};
}
}
1 change: 0 additions & 1 deletion Server/Modules/NoiseGenerator/Models/NoiseParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ public class NoiseParameters
public int Seed { get; set; }
public bool UseRandomSeed { get; set; } = true;
public bool GenerateNew { get; set; }
//...
}
8 changes: 4 additions & 4 deletions Server/Modules/NoiseGenerator/Services/FalloffGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class FalloffGenerator
{
public static float[,] Generate(int size)
{
float[,] map = new float[size, size];
var map = new float[size, size];

for (int i = 0; i < size; i++)
{
Expand All @@ -13,7 +13,7 @@ public static class FalloffGenerator
float x = i / (float)size * 2 - 1;
float y = j / (float)size * 2 - 1;

float value = Math.Max(Math.Abs(x), Math.Abs(y));
var value = Math.Max(Math.Abs(x), Math.Abs(y));
map[i, j] = Evaluate(value);
}
}
Expand All @@ -23,8 +23,8 @@ public static class FalloffGenerator

private static float Evaluate(float value)
{
float a = 3;
float b = 4.2f;
const float a = 3;
const float b = 4.2f;

return (float)(Math.Pow(value, a) / (Math.Pow(value, a) + Math.Pow(b - b * value, a)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class PerlinNoiseGenerator
int seed,
float scale,
int octaves,
float persistance,
float persistence,
float lacunarity,
Vector2 offset
)
Expand Down Expand Up @@ -57,7 +57,7 @@ Vector2 offset

noiseHeight += perlinValue * amplitude;

amplitude *= persistance;
amplitude *= persistence;
frequency *= lacunarity;
}

Expand Down
2 changes: 1 addition & 1 deletion Server/Modules/Pathfinding/Services/IPathfindingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Fracture.Server.Modules.Pathfinding.Services;

public interface IPathfindingService
{
List<IPathfindingNode> FindPath(IPathfindingNode start, IPathfindingNode stop);
List<IPathfindingNode>? FindPath(IPathfindingNode start, IPathfindingNode stop);
}
Loading

0 comments on commit 761dbec

Please sign in to comment.