Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from Oliver-makes-code/integrated-server
Browse files Browse the repository at this point in the history
Integrated server
  • Loading branch information
Cassunshine authored Dec 10, 2023
2 parents 4592d12 + 01c2572 commit ccdce93
Show file tree
Hide file tree
Showing 86 changed files with 2,125 additions and 232 deletions.
2 changes: 1 addition & 1 deletion Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<RollForward>Major</RollForward>
<PublishReadyToRun>false</PublishReadyToRun>
<RootNamespace>Voxel</RootNamespace>
<RootNamespace>Voxel.Client</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
9 changes: 9 additions & 0 deletions Client/Content/shaders/debug.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 450

layout(location = 1) in vec4 fsin_Color;

layout(location = 0) out vec4 fsout_Color;

void main() {
fsout_Color = fsin_Color;
}
19 changes: 19 additions & 0 deletions Client/Content/shaders/debug.vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 450

layout(location = 0) in vec3 Position;
layout(location = 1) in vec4 Color;
layout(location = 2) in vec2 UV;


layout(location = 1) out vec4 fsin_Color;

layout (set = 0, binding = 0) uniform CameraData {
mat4 VPMatrix;
};

void main() {
vec4 pos = vec4(Position, 1) * VPMatrix;
gl_Position = pos;

fsin_Color = Color;
}
2 changes: 1 addition & 1 deletion Client/Keybinding/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System;
using System.Collections.Generic;
using GlmSharp;
using RenderSurface.Input.Gamepad;
using Veldrid;
using Voxel.Core.Input.Gamepad;
using VMouseButton = Veldrid.MouseButton;

namespace Voxel.Client.Keybinding;
Expand Down
4 changes: 2 additions & 2 deletions Client/Keybinding/Keybinds.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using RenderSurface.Input.Gamepad;
using Veldrid;
using Voxel.Core.Input.Gamepad;
using VMouseButton = Veldrid.MouseButton;

namespace Voxel.Client.Keybinding;
Expand Down Expand Up @@ -50,7 +50,7 @@ public static class Keybinds {
KeyButton.Get(Key.ShiftLeft),
ControllerButton.Get(GamepadButton.RightStick)
);

public static readonly Keybind Look = new(
"camera.full",
ControllerJoystickButton.Get(ControllerJoystickButton.GamepadJoystick.Right)
Expand Down
9 changes: 9 additions & 0 deletions Client/Network/C2SConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Voxel.Common.Network;
using Voxel.Common.Network.Packets.S2C;

namespace Voxel.Client.Network;

public abstract class C2SConnection : ConnectionBase<S2CPacket> {

public abstract void Tick();
}
79 changes: 79 additions & 0 deletions Client/Network/ClientConnectionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using Voxel.Client.World;
using Voxel.Common.Network.Packets;
using Voxel.Common.Network.Packets.C2S;
using Voxel.Common.Network.Packets.S2C;
using Voxel.Common.Network.Packets.S2C.Gameplay;
using Voxel.Common.Network.Packets.S2C.Handshake;
using Voxel.Common.Network.Packets.Utils;

namespace Voxel.Client.Network;

/// <summary>
/// This controls a connection from the client to a server.
///
/// Holds stuff like connection status and whatnot.
/// </summary>
public class ClientConnectionContext {
public bool isDead => Connection.isDead;

public readonly VoxelClient Client;
private readonly C2SConnection Connection;

private readonly PacketHandler<S2CPacket> HandshakeHandler;
private readonly PacketHandler<S2CPacket> GameplayHandler;

public ClientConnectionContext(VoxelClient client, C2SConnection connection) {
Client = client;
Connection = connection;

HandshakeHandler = new PacketHandler<S2CPacket>();
HandshakeHandler.RegisterHandler<S2CHandshakeDone>(HandleHandshakeDone);

GameplayHandler = new PacketHandler<S2CPacket>();
GameplayHandler.RegisterHandler<SetupWorld>(HandleSetupWorld);
GameplayHandler.RegisterHandler<ChunkData>(HandleChunkData);
GameplayHandler.RegisterHandler<ChunkUnload>(HandleChunkUnload);

Connection.packetHandler = HandshakeHandler;
}

public void Tick() {
if (Connection.isDead)
return;

Connection.Tick();
}

private void HandleSetupWorld(SetupWorld packet) {
Client.SetupWorld();
}

private void HandleHandshakeDone(S2CHandshakeDone packet) {
Connection.packetHandler = GameplayHandler;
Console.WriteLine("Client:Server Says Handshake Done");
}

private void HandleChunkData(ChunkData packet) {
if (Client.world == null)
return;

var chunk = Client.world.GetOrCreateChunk(packet.position);
packet.Apply(chunk);
}

private void HandleChunkUnload(ChunkUnload packet) {
if (Client.world == null)
return;

if (Client.world.TryGetChunkRaw(packet.position, out var chunk))
packet.Apply(chunk);
}

public void SendPacket(C2SPacket packet) {
Connection.DeliverPacket(packet);
PacketPool.Return(packet);
}

public void Close() => Connection.Close();
}
126 changes: 126 additions & 0 deletions Client/Network/InternetC2SConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Net;
using System.Net.Sockets;
using LiteNetLib;
using Voxel.Common.Network.Packets;
using Voxel.Common.Network.Packets.C2S;
using Voxel.Common.Network.Packets.C2S.Handshake;
using Voxel.Common.Network.Packets.S2C;
using Voxel.Common.Network.Packets.Utils;
using Voxel.Common.Util.Serialization.Compressed;

namespace Voxel.Client.Network;

public class InternetC2SConnection : C2SConnection, INetEventListener {

private readonly NetManager NetClient;
private NetPeer? peer;

private readonly PacketMap<C2SPacket> PacketMap = new();
private readonly CompressedVDataReader Reader = new();
private readonly CompressedVDataWriter Writer = new();

private bool synced = false;

public InternetC2SConnection(string address, int port = 24564) {
PacketMap.FillOutgoingMap();
NetClient = new NetManager(this);

OnClosed += () => {
//Disconnect the peer if it was connected.
if (peer != null && peer.ConnectionState != ConnectionState.Disconnected) {
peer.Disconnect();
peer = null;
NetClient.Stop(true);
}
};


NetClient.Start();
NetClient.Connect(address, port, string.Empty);
}

public override void Tick() {
NetClient.PollEvents();
}

public override void DeliverPacket(Packet toSend) {
if (peer == null)
throw new InvalidOperationException("Cannot send packet on disconnected line");

var writer = Writer;
writer.Reset();

if (!PacketMap.outgoingMap.TryGetValue(toSend.GetType(), out var rawID))
throw new InvalidOperationException($"Cannot send unknown packet {toSend}");

writer.Write(rawID);
writer.Write(toSend);
peer.Send(writer.currentBytes, 0, DeliveryMethod.ReliableOrdered);

//Console.WriteLine($"Sending {writer.currentBytes.Length} bytes to server");
}

public void OnPeerConnected(NetPeer peer) {
this.peer = peer;

Console.Out.WriteLine("Client: Connected!");

//SYNC MAPS HERE
var writer = Writer;
writer.Reset();

PacketMap.WriteOutgoingMap(writer);
peer.Send(writer.currentBytes, 0, DeliveryMethod.ReliableOrdered);

//After maps have been synced, client handshake is done.
DeliverPacket(new C2SHandshakeDone());
}

public void OnNetworkReceive(NetPeer _, NetPacketReader nReader, byte channelNumber, DeliveryMethod deliveryMethod) {
if (packetHandler == null)
return;

Reader.LoadData(nReader.RawData.AsSpan(nReader.UserDataOffset, nReader.UserDataSize));

if (!synced) {
PacketMap.ReadIncomingMap(Reader);
synced = true;
//Console.Out.WriteLine("Client: S2C Map Synced");
return;
}

var rawID = Reader.ReadUint();
if (!PacketMap.incomingMap.TryGetValue(rawID, out var packetType))
return;

//Console.WriteLine($"Got packet {packetType.Name} from server");

var packet = PacketPool.GetPacket<S2CPacket>(packetType);
packet.Read(Reader);

packetHandler.HandlePacket(packet);
}

public void OnPeerDisconnected(NetPeer _, DisconnectInfo disconnectInfo) {

}

public void OnNetworkError(IPEndPoint endPoint, SocketError socketError) {

}


public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType) {

}

public void OnNetworkLatencyUpdate(NetPeer peer, int latency) {

}

//UNUSED
public void OnConnectionRequest(ConnectionRequest request) {

}
}
4 changes: 0 additions & 4 deletions Client/Rendering/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
namespace Voxel.Client.Rendering;

public class Camera {
public dvec3 oldPosition = new(0, 10, 0);
public dvec3 position = new(0, 10, 0);
public vec2 oldRotationVec = vec2.Zero;
public vec2 rotationVec = vec2.Zero;

public quat rotationY => quat.Identity.Rotated(rotationVec.y, new(0, 1, 0));
public quat oldRotationY => quat.Identity.Rotated(oldRotationVec.y, new(0, 1, 0));
public quat rotation => rotationY.Rotated(rotationVec.x, new(1,0,0));
public quat oldRotation => oldRotationY.Rotated(oldRotationVec.x, new(1,0,0));

/// <summary>
/// FOV in degrees from the top to the bottom of the camera.
Expand Down
10 changes: 3 additions & 7 deletions Client/Rendering/CameraStateManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using GlmSharp;
using RenderSurface.Rendering;
using Veldrid;
using Voxel.Client.Rendering.Utils;
using Voxel.Common.Util;
using Voxel.Core.Rendering;

namespace Voxel.Client.Rendering;

Expand Down Expand Up @@ -39,14 +39,10 @@ public CameraStateManager(RenderSystem system) {
}

public void SetToCamera(Camera c, double timeSinceLastTick) {
double deltaTicks = timeSinceLastTick * Constants.TicksPerSecond;
currentCameraPosition = dvec3.Lerp(c.oldPosition, c.position, deltaTicks);
currentCameraPosition = c.position;

var data = new CameraData();
var cRotation = quat.SLerp(c.oldRotation, c.rotation, (float)deltaTicks);
if (quat.IsNaN(cRotation).Any)
cRotation = c.rotation;
data.VPMatrix = cRotation.ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed;
data.VPMatrix = c.rotation.ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed;
CameraBuffer.value = data;
}

Expand Down
Loading

0 comments on commit ccdce93

Please sign in to comment.