Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions OpenPolytopia.Common/City.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace OpenPolytopia.Common;
public class CityManager(Grid grid) {
private readonly List<uint> _cities = [];

public IEnumerable<uint> Cities => _cities;

/// <summary>
/// Access to the <see cref="CityData"/>
/// </summary>
Expand Down
22 changes: 20 additions & 2 deletions OpenPolytopia.Common/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ public Tile this[uint index] {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint GridPositionToIndex(int x, int y) => (uint)((y * size) + x);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector2I IndexToGridPosition(uint index) {
var x = index % size;
var y = (index - x) / size;
return new Vector2I((int)x, (int)y);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void IndexToGridPosition(uint index, out uint x, out uint y) {
x = index % size;
y = (index - x) / size;
}

/// <summary>
/// Modifies a given tile
/// </summary>
Expand Down Expand Up @@ -215,7 +228,7 @@ public static Type GetModifier(TileKind kind) =>
/// Using 0 as the left-most bit and 63 as the right-most bit
/// <list type="bullet">
/// <item>0 -> 1: has road; 0 doesn't have any road; (bridge if on water)</item>
/// <item>1 -> 1: has ancient ruin; 0 doesn't have any ruin</item>
/// <item>1 -> 2: has ancient ruin; 0 doesn't have any ruin</item>
/// <item>[2, 4] -> <see cref="Kind"/></item>
/// <item>[5, 6] -> Tile modifier</item>
/// <item>[7, 9] -> Tile buildings</item>
Expand Down Expand Up @@ -251,7 +264,12 @@ public bool Ruin {
/// <summary>
/// The type of the tile
/// </summary>
public TileKind Kind => (TileKind)_inner.GetBits(THREE_BITS, TILEKIND_POSITION);
public TileKind Kind {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (TileKind)_inner.GetBits(THREE_BITS, TILEKIND_POSITION);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _inner.SetBits((ulong)value, THREE_BITS, TILEKIND_POSITION);
}

/// <summary>
/// The tile modifier castable to the corresponding enum
Expand Down
4 changes: 4 additions & 0 deletions OpenPolytopia.Common/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace OpenPolytopia.Common;

public record Player(TribeType Tribe, int Id) {
}
91 changes: 91 additions & 0 deletions OpenPolytopia.Common/TerrainGeneration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
namespace OpenPolytopia.Common;

using System.Runtime.CompilerServices;

/// <summary>
/// Terrain generation system
/// </summary>
/// <param name="grid">the grid to use</param>
/// <param name="cityManager">the city manager to use</param>
/// <param name="tribeManager">the tribe manager to use</param>
/// <param name="players">all the players in the game</param>
/// <param name="seed">the optional random seed</param>
public class TerrainGeneration(
Grid grid,
CityManager cityManager,
TribeManager tribeManager,
Player[] players,
int? seed = null) {
// create the random number generator from a seed if set
private readonly Random _rng = seed == null ? new Random() : new Random(seed.Value);

/// <summary>
/// Generates the map ready to use in-game
/// </summary>
/// <example>
/// <code>
/// var terrainGeneration = new TerrainGeneration(grid, cityManager, tribeManager);
/// await terrainGeneration.GenerateMapAsync();
/// </code>
/// </example>
public async Task GenerateMapAsync() {
await GenerateInitialCitiesAsync();
await GenerateTerrainAsync();
await GenerateCitiesAsync();
await GenerateResourcesAsync();
}

private async Task GenerateInitialCitiesAsync() {
// for each player
foreach (var player in players) {
// yield to the task executor
await Task.Yield();

// compute the index position of a random tile in the grid
var index = (uint)_rng.Next(0, (int)(grid.Size * grid.Size));

// check if the computed index is valid
var valid = false;
while (!valid) {
if (IsCityValid(index)) {
valid = true;
}
else {
index = (uint)_rng.Next(0, (int)(grid.Size * grid.Size));
}
}

// register the city
var cityId = cityManager.RegisterCity(index);

// set all data for the tile in the grid
grid.ModifyTile(index, (ref Tile tile) => {
tile.Owner = player.Id;
tile.Kind = TileKind.Village;
tile.Modifier = (int)VillageTileModifier.City;
});

// set all data for the city
cityManager.ModifyCity(cityId, (ref CityData city) => {
city.Capital = true;
city.Owner = player.Id;
city.Level = 1;
});
}
}

private async Task GenerateTerrainAsync() {
}

private async Task GenerateCitiesAsync() {
}

private async Task GenerateResourcesAsync() {
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsCityValid(uint index) {
grid.IndexToGridPosition(index, out var x, out var y);
return grid[index].Owner == 0 && x > 0 && y > 0 && x < grid.Size - 1 && y < grid.Size - 1;
}
}
6 changes: 6 additions & 0 deletions OpenPolytopia/test/src/GridTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public void TestPositionToIndex() {
grid.GridPositionToIndex(new Vector2I(2, 2)).ShouldBe(22u);
}

[Test]
public void TestIndexToPosition() {
var grid = new Grid(10);
grid.IndexToGridPosition(22u).ShouldBe(new Vector2I(2, 2));
}

[Test]
public void TestModifyTile() {
var grid = new Grid(10);
Expand Down