Skip to content
Open
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
9 changes: 9 additions & 0 deletions OpenPolytopia.Common/City.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ namespace OpenPolytopia.Common;
public class CityManager(Grid grid) {
private readonly List<uint> _cities = [];

/// <summary>
/// Grid indexes of the registered cities
/// </summary>
/// <remarks>
/// The city with id <c>i</c> is at position <c>i - 1</c>; don't modify this list because the
/// ids are positions in it, use <see cref="RegisterCity(uint)"/> to add a city
/// </remarks>
public IReadOnlyList<uint> Cities => _cities;

/// <summary>
/// Access to the <see cref="CityData"/>
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions OpenPolytopia.Common/EmbeddedResources.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
namespace OpenPolytopia.Common;

using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Text.Unicode;

public static class EmbeddedResources {
// serialization conventions shared by all the json resources
private static readonly JsonSerializerOptions _jsonOptions = new() {
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(UnicodeRanges.All),
TypeInfoResolver = JsonTypeInfoResolver.Combine(TribeGenerationContext.Default, TroopGenerationContext.Default),
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};

/// <summary>
/// Get the troops data from the json file
/// </summary>
Expand All @@ -13,6 +23,20 @@ public static class EmbeddedResources {
/// </summary>
public static string TribesData => GetResource("OpenPolytopia.Common.resources.tribes.json");

/// <summary>
/// Deserializes the troops from the embedded json file
/// </summary>
/// <returns>the troops data, or null if the json is empty</returns>
public static TroopsSerializedData? LoadTroops() =>
JsonSerializer.Deserialize<TroopsSerializedData>(TroopsData, _jsonOptions);

/// <summary>
/// Deserializes the tribes from the embedded json file
/// </summary>
/// <returns>the tribes data, or null if the json is empty</returns>
public static TribesSerializedData? LoadTribes() =>
JsonSerializer.Deserialize<TribesSerializedData>(TribesData, _jsonOptions);

/// <summary>
/// Returns the content of an embedded resource
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions OpenPolytopia.Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static class ULongExtensions {
/// <param name="position">the position where to set bits starting from the right</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetBits(this ref ulong value, ulong data, ulong bits, int position) =>
value = value.ClearBits(bits, position) | (data << position);
value = value.ClearBits(bits, position) | ((data & bits) << position);

/// <summary>
/// Get the bits
Expand Down Expand Up @@ -77,7 +77,7 @@ public static class UIntExtensions {
/// <param name="position">the position where to set bits starting from the right</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetBits(this ref uint value, uint data, uint bits, int position) =>
value = value.ClearBits(bits, position) | (data << position);
value = value.ClearBits(bits, position) | ((data & bits) << position);

/// <summary>
/// Get the bits
Expand Down Expand Up @@ -110,7 +110,7 @@ public static class IntExtensions {
/// <param name="position">the position where to set bits starting from the right</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetBits(this ref int value, int data, int bits, int position) =>
value = value.ClearBits(bits, position) | (data << position);
value = value.ClearBits(bits, position) | ((data & bits) << position);

/// <summary>
/// Get the bits
Expand Down
20 changes: 19 additions & 1 deletion 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 / 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 / size;
}

/// <summary>
/// Modifies a given tile
/// </summary>
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) {
}
Loading
Loading