Skip to content

Commit

Permalink
Add application level configuration, fix TerrainGen burst compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
JerritEic committed Dec 7, 2023
1 parent dbf4e63 commit b83596c
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
cacheGUID:
Value:
x: 3748174892
y: 475068495
z: 4139367847
w: 1580801149
x: 2650512374
y: 895508896
z: 2834433152
w: 849077948
3 changes: 3 additions & 0 deletions Assets/Scripts/Configuration.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Assets/Scripts/Configuration/GameConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Opencraft.Player.Emulation;
using PolkaDOTS;
using PolkaDOTS.Configuration;

namespace Opencraft
{
/// <summary>
/// Static global class holding game configuration parameters. Filled by <see cref="CmdArgsReader"/>
/// </summary>
[ArgumentClass]
public static class GameConfig
{
public static readonly CommandLineParser.EnumArgument<SimulationBehaviour> PlayerSimulationBehaviour = new CommandLineParser.EnumArgument<SimulationBehaviour>("-playerSimulationBehaviour", SimulationBehaviour.BoundedRandom);
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Configuration/GameConfig.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Assets/Scripts/Player/Emulation/PlayerSimulationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Opencraft.Terrain.Blocks;
using Opencraft.Terrain.Utilities;
using PolkaDOTS;
using PolkaDOTS.Emulation;
using PolkaDOTS.Deployment;
using Priority_Queue;
using Unity.Burst;
using Unity.Collections;
Expand Down Expand Up @@ -59,7 +59,7 @@ public partial struct PlayerSimulationSystem : ISystem

public void OnCreate(ref SystemState state)
{
if (Config.EmulationType != EmulationType.Simulation)
if (ApplicationConfig.EmulationType != EmulationType.Simulation)
state.Enabled = false;


Expand Down
21 changes: 21 additions & 0 deletions Assets/Scripts/Player/Emulation/TargetPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Unity.Mathematics;
using Unity.NetCode;

namespace Opencraft.Player.Emulation
{
public enum SimulationBehaviour
{
BoundedRandom = 0,
FixedDirection = 1,
}

public class TargetPosition
{

public int3 GetTargetPosition()
{

return int3.zero;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Player/Emulation/TargetPosition.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MultiplayPlayerController : MonoBehaviour
protected void Awake()
{
playerInput.onDeviceChange += OnDeviceChange;
username = $"{PolkaDOTS.Config.UserID}";
username = $"{PolkaDOTS.ApplicationConfig.UserID.Value}";
}

private void OnEnable()
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Player/PlayerInputSystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Opencraft.Player.Authoring;
using Opencraft.Player.Multiplay;
using PolkaDOTS;
using PolkaDOTS.Emulation;
using PolkaDOTS.Deployment;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
Expand Down Expand Up @@ -48,7 +48,7 @@ public void OnUpdate(ref SystemState state)
var playerController = playerObj.GetComponent<MultiplayPlayerController>();

// If inputs are simulated then don't collect them from the controller
if (Config.EmulationType != EmulationType.Simulation)
if (ApplicationConfig.EmulationType != EmulationType.Simulation)
{
input.ValueRW.Movement = default;
input.ValueRW.Jump = default;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Rendering/DebugRenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial struct DebugRenderSystem : ISystem
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<TerrainArea>();
if (!PolkaDOTS.Config.DebugEnabled)
if (!PolkaDOTS.ApplicationConfig.DebugEnabled)
{
state.Enabled = false;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Statistics/GameStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void OnUpdate(ref SystemState state)
{
if (first)
{
if (Config.LogStats)
if (ApplicationConfig.LogStats)
{
Debug.Log("Adding terrain areas statistics recorders");
PolkaDOTS.Statistics.StatisticsWriter writer = PolkaDOTS.Statistics.StatisticsWriterInstance.instance;
Expand Down
21 changes: 17 additions & 4 deletions Assets/Scripts/Terrain/TerrainGenerationSystem.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Opencraft.Terrain;
using Opencraft.Terrain.Authoring;
using Opencraft.Terrain.Blocks;
using Opencraft.Terrain.Layers;
using Opencraft.Terrain.Structures;
using Opencraft.Terrain.Utilities;
using Opencraft.ThirdParty;
using PolkaDOTS;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
Expand All @@ -14,6 +16,8 @@
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Profiling;
using System.Security.Cryptography;
using System.Text;

// Annoyingly this assembly directive must be outside the namespace.
[assembly: RegisterGenericJobType(typeof(SortJob<int2, Int2DistanceComparer>))]
Expand All @@ -34,6 +38,8 @@ public partial struct TerrainGenerationSystem : ISystem
private BufferLookup<TerrainColMinY> _terrainColMinLookup;
private BufferLookup<TerrainColMaxY> _terrainColMaxLookup;
private BufferLookup<TerrainStructuresToSpawn> _structuresToSpawnLookup;

private int _hashedSeed;
//private double lastUpdate;

public void OnCreate(ref SystemState state)
Expand All @@ -51,13 +57,20 @@ public void OnCreate(ref SystemState state)
_terrainColMinLookup = state.GetBufferLookup<TerrainColMinY>(isReadOnly: false);
_terrainColMaxLookup = state.GetBufferLookup<TerrainColMaxY>(isReadOnly: false);
_structuresToSpawnLookup = state.GetBufferLookup<TerrainStructuresToSpawn>(isReadOnly: false);

// Seed
MD5 md5Hasher = MD5.Create();
var hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(ApplicationConfig.Seed.Value));
_hashedSeed = BitConverter.ToInt32(hashed, 0);

}

public void OnDestroy(ref SystemState state)
{
_terrainGenLayers.Dispose();
}


[BurstCompile]
public void OnUpdate(ref SystemState state)
{
if (!_terrainGenLayers.IsCreated)
Expand Down Expand Up @@ -89,7 +102,7 @@ public void OnUpdate(ref SystemState state)
NativeArray<int2> columnsToSpawn = chunksColumnsSpawnBuffer.AsNativeArray();
// Sort the columns to spawn so ones closer to 0,0 are first
SortJob<int2, Int2DistanceComparer> sortJob = columnsToSpawn.SortJob<int2, Int2DistanceComparer>(new Int2DistanceComparer { });
JobHandle sortHandle = sortJob.Schedule();
JobHandle sortHandle = sortJob.Schedule(state.Dependency);

// Spawn the terrain area entities
int numColumnsToSpawn = columnsToSpawn.Length > Env.MAX_COL_PER_TICK
Expand All @@ -115,7 +128,7 @@ public void OnUpdate(ref SystemState state)
terrainColMaxLookup = _terrainColMaxLookup,
_structuresToSpawnLookup = _structuresToSpawnLookup,
columnsToSpawn = columnsToSpawn,
noiseSeed = PolkaDOTS.Config.Seed,
noiseSeed = _hashedSeed,
terrainGenLayers = _terrainGenLayers
}.Schedule(numColumnsToSpawn, 1, sortHandle); // Each thread gets 1 column
populateHandle.Complete();
Expand Down
2 changes: 1 addition & 1 deletion Packages/PolkaDOTS

0 comments on commit b83596c

Please sign in to comment.