Skip to content

Commit

Permalink
- full rewrite of how the world generates
Browse files Browse the repository at this point in the history
- server now generates the chunks and sends them to the client
  • Loading branch information
tddebart committed Apr 24, 2022
1 parent f474457 commit 02f1581
Show file tree
Hide file tree
Showing 19 changed files with 401 additions and 310 deletions.
8 changes: 8 additions & 0 deletions .idea/.idea.Minecraft/.idea/misc.xml

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

2 changes: 1 addition & 1 deletion Assets/Resources/Prefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ MonoBehaviour:
isFlying: 0
drawBounds: 0
sensitivity: 5
checkIncrement: 0.05
checkIncrement: 0.1
reach: 4.5
cam: {fileID: 3114816152506633376}
inventory: {fileID: 0}
Expand Down
12 changes: 6 additions & 6 deletions Assets/Scenes/MainMenu.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 1}
m_AnchorMax: {x: 0.5, y: 1}
m_AnchoredPosition: {x: -152.5, y: 215544.27}
m_AnchoredPosition: {x: -152.5, y: 248223.02}
m_SizeDelta: {x: 305, y: 0}
m_Pivot: {x: 0, y: 1}
--- !u!114 &845042230
Expand Down Expand Up @@ -3799,7 +3799,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 236816974}
m_Direction: 2
m_Value: 1
m_Size: 0.4168831
m_Size: 0.40985915
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -5757,7 +5757,7 @@ PrefabInstance:
type: 3}
propertyPath: transport
value:
objectReference: {fileID: 2330286423484457791}
objectReference: {fileID: 2330286423484457793}
- target: {fileID: 2330286422166840741, guid: ed098faebcb7c0a46a1b8cbc9753bb43,
type: 3}
propertyPath: m_Enabled
Expand All @@ -5771,16 +5771,16 @@ PrefabInstance:
m_RemovedComponents:
- {fileID: 2330286422166840739, guid: ed098faebcb7c0a46a1b8cbc9753bb43, type: 3}
m_SourcePrefab: {fileID: 100100000, guid: ed098faebcb7c0a46a1b8cbc9753bb43, type: 3}
--- !u!114 &2330286423484457791 stripped
--- !u!114 &2330286423484457793 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 2330286422166840743, guid: ed098faebcb7c0a46a1b8cbc9753bb43,
m_CorrespondingSourceObject: {fileID: 2330286422166840742, guid: ed098faebcb7c0a46a1b8cbc9753bb43,
type: 3}
m_PrefabInstance: {fileID: 2330286423484457790}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6b0fecffa3f624585964b0d0eb21b18e, type: 3}
m_Script: {fileID: 11500000, guid: 61b93ff779f4ef84da855aef42076949, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!224 &4834870413912905276
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scenes/World.unity
Original file line number Diff line number Diff line change
Expand Up @@ -3875,7 +3875,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e113280f9ab044168541f6fef84d3095, type: 3}
m_Name:
m_EditorClassIdentifier:
renderDistance: 2
renderDistance: 3
chunkSize: 16
chunkHeight: 16
worldHeight: 96
Expand All @@ -3884,7 +3884,7 @@ MonoBehaviour:
worldRenderer: {fileID: 1269315924}
terrainGenerator: {fileID: 1658937128}
mapSeedOffset: {x: -251, y: 150, z: 0}
GenerateMoreChunks: 1
GenerateMoreChunks: 0
gamma: 0
skyLightMultiplier: 0.9
blockLightMultiplier: 1.18
Expand Down
2 changes: 2 additions & 0 deletions Assets/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ made by me
- fix noise never going above 0.67

- also change the inventory on the server instead of only on the client (so that the server can save the inventory)

- broke generating more chunks than the initial ones

8 changes: 0 additions & 8 deletions Assets/ZeroFormatter.meta

This file was deleted.

2 changes: 2 additions & 0 deletions Assets/_Scripts/F3MenuManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class F3MenuManger : MonoBehaviour

public void Update()
{
if(WorldServer.IsDedicated) return;

if (Input.GetKeyUp(KeyCode.F3))
{
if (!GameManager.Instance.localPlayer.f3KeyComboUsed)
Expand Down
8 changes: 7 additions & 1 deletion Assets/_Scripts/Networking/MinecraftNetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override void OnClientConnect()
base.OnClientConnect();
NetworkClient.Send(new WorldServer.StartPlayerMessage(SteamClient.SteamId));

// NetworkClient.Send(new WorldServer.SpawnPlayerMessage(SteamClient.SteamId));
NetworkClient.Send(new WorldServer.ChunkRequestMessage(Vector3Int.zero, World.Instance.renderDistance));
}

public override void OnStartServer()
Expand Down Expand Up @@ -55,4 +55,10 @@ public override void OnServerDisconnect(NetworkConnectionToClient conn)

base.OnServerDisconnect(conn);
}

public override void OnStopServer()
{
World.Instance.SaveWorld();
base.OnStopServer();
}
}
3 changes: 2 additions & 1 deletion Assets/_Scripts/World/ChunkData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ChunkData
public bool modifiedAfterSave = true;

public TreeData treeData = new TreeData();
[CanBeNull] public ChunkRenderer renderer => WorldDataHelper.GetChunk(worldRef, worldPos);
[CanBeNull] public ChunkRenderer renderer => WorldDataHelper.GetChunk(worldPos);

public readonly Queue<BlockLightNode> blockLightUpdateQueue = new();
public readonly Queue<BlockLightNode> blockLightRemoveQueue = new();
Expand Down Expand Up @@ -51,6 +51,7 @@ public static ChunkData Deserialize(ChunkSaveData saveData)
}
chunkData.sections = sections;
chunkData.modifiedAfterSave = false;
chunkData.isGenerated = true;

return chunkData;
}
Expand Down
70 changes: 44 additions & 26 deletions Assets/_Scripts/World/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
using Mirror;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using Debug = UnityEngine.Debug;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
#endif

public partial class World : MonoBehaviour
{
public static World Instance;
Expand Down Expand Up @@ -55,12 +56,16 @@ public partial class World : MonoBehaviour
public bool IsWorldCreated { get; private set; }

private Stopwatch fullStopwatch = new Stopwatch();

public Dictionary<Vector3Int, BlockType> blocksToPlaceAfterGeneration = new Dictionary<Vector3Int, BlockType>();

public bool validateDone;
[HideInInspector] public bool isInPlayMode;

#if UNITY_EDITOR
static World()
{
EditorApplication.update += EditorUpdate;
}
#endif

private void Awake()
{
Expand All @@ -78,19 +83,25 @@ private void Start()
NetworkClient.RegisterHandler<StartWorldMessage>(message =>
{
StartWorld();
GenerateWorld(message.position);
// GenerateWorld(message.position);
});

NetworkClient.RegisterHandler<WorldServer.ChunkDataMessage>(message =>
{
Debug.Log("Received chunk");

var chunkData = ChunkData.Deserialize(message.chunkSaveData);

if (worldData.chunkDataDict.ContainsKey(chunkData.worldPos) && NetworkServer.active)
{
worldData.chunkDataDict[chunkData.worldPos] = chunkData;
}
else
{
worldData.chunkDataDict.TryAdd(chunkData.worldPos, chunkData);
}
dataToMeshQueue.Enqueue(chunkData);
});

// NetworkClient.RegisterHandler<WorldServer.ChunkReceiveMessage>(message =>
// {
// Debug.Log("Received chunk");
//
// var chunkData = ChunkData.Deserialize(message.chunkSaveData);
//
// worldData.chunkDataDict[chunkData.worldPos] = chunkData;
//
// GenerateOnlyMesh();
// });
}

private void OnValidate()
Expand All @@ -100,7 +111,7 @@ private void OnValidate()
{
worldData = new WorldData
{
chunkDataDict = new Dictionary<Vector3Int, ChunkData>(),
chunkDataDict = new ConcurrentDictionary<Vector3Int, ChunkData>(),
chunkDict = new Dictionary<Vector3Int, ChunkRenderer>(),
worldName = worldName,
};
Expand All @@ -116,7 +127,7 @@ private void OnValidate()
Shader.SetGlobalVectorArray("lightColors", LightTextureCreator.lightColors);
}

private WorldGenerationData GetPositionsInRenderDistance(Vector3Int playerPos)
public WorldGenerationData GetPositionsInRenderDistance(Vector3Int playerPos)
{
var allChunkPositionsNeeded = WorldDataHelper.GetChunkPositionsInRenderDistance(this, playerPos);
var allChunkDataPositionsNeeded = WorldDataHelper.GetDataPositionsInRenderDistance(this, playerPos);
Expand Down Expand Up @@ -196,7 +207,7 @@ public void SetBlock(Vector3 pos, BlockType blockType)
public void SetBlock(Vector3Int blockPos, BlockType blockType)
{
var chunkPos = WorldDataHelper.GetChunkPosition(this, blockPos);
var chunk = WorldDataHelper.GetChunk(this, chunkPos);
var chunk = WorldDataHelper.GetChunk(chunkPos);
if (chunk == null) return;
chunk.ModifiedByPlayer = true;
chunk.ChunkData.modifiedAfterSave = true;
Expand All @@ -206,7 +217,7 @@ public void SetBlock(Vector3Int blockPos, BlockType blockType)
public void SetBlocks(IEnumerable<Vector3Int> blockPoss, BlockType blockType)
{
var chunkPos = WorldDataHelper.GetChunkPosition(this, blockPoss.First());
var chunk = WorldDataHelper.GetChunk(this, chunkPos);
var chunk = WorldDataHelper.GetChunk(chunkPos);
SetBlocks(chunk, blockPoss, blockType);
}

Expand All @@ -232,7 +243,7 @@ public async void SetBlocks(ChunkRenderer chunk, IEnumerable<Vector3Int> blockPo
{
if(neighChunkData == null) continue;

ChunkRenderer neighbourChunk = WorldDataHelper.GetChunk(neighChunkData.worldRef, neighChunkData.worldPos);
ChunkRenderer neighbourChunk = WorldDataHelper.GetChunk(neighChunkData.worldPos);
if(neighbourChunk != null)
{
neightBourUpdates.Add(neighbourChunk);
Expand Down Expand Up @@ -301,17 +312,15 @@ public void LoadAdditionalChunks(GameObject localPlayer)
if (!GenerateMoreChunks) return;

// Debug.Log("Loading additional chunks");
GenerateWorld(Vector3Int.RoundToInt(localPlayer.transform.position), () =>
{
OnNewChunksGenerated?.Invoke();
});
GenerateWorld(Vector3Int.RoundToInt(localPlayer.transform.position));
OnNewChunksGenerated?.Invoke();
}

private void Clear()
{
worldData = new WorldData
{
chunkDataDict = new Dictionary<Vector3Int, ChunkData>(),
chunkDataDict = new ConcurrentDictionary<Vector3Int, ChunkData>(),
chunkDict = new Dictionary<Vector3Int, ChunkRenderer>(),
worldName = worldName
};
Expand All @@ -320,8 +329,17 @@ private void Clear()
worldRenderer.chunkPool?.Clear();
isSaving = false;

// Clear queues
doneDataQueue.Clear();
dataToMeshQueue.Clear();
meshToRenderQueue.Clear();

actionOnChunkDone.Clear();

chunksToUpdate?.Clear();

disabled = false;

MyNoise.noiseStopwatch.Reset();
MyNoise.noise3DStopwatch.Reset();

Expand Down Expand Up @@ -394,6 +412,6 @@ public StartWorldMessage(Vector3Int position)
public struct WorldData
{
public string worldName;
public Dictionary<Vector3Int, ChunkData> chunkDataDict;
public ConcurrentDictionary<Vector3Int, ChunkData> chunkDataDict;
public Dictionary<Vector3Int, ChunkRenderer> chunkDict;
}
Loading

0 comments on commit 02f1581

Please sign in to comment.