mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-14 10:05:44 -04:00
Some simplifications in utility functions, remove some old unused debug code, do not use magic constants for writing packet ids.
This commit is contained in:
parent
a9756fcb64
commit
c08e98d517
41
Entities/LocationUpdate.cs
Normal file
41
Entities/LocationUpdate.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using OpenTK;
|
||||
|
||||
namespace ClassicalSharp {
|
||||
|
||||
public struct LocationUpdate {
|
||||
|
||||
public Vector3 Pos;
|
||||
public float Yaw, Pitch;
|
||||
|
||||
public bool IncludesPosition;
|
||||
public bool RelativePosition;
|
||||
public bool IncludesOrientation;
|
||||
|
||||
public LocationUpdate( float x, float y, float z, float yaw, float pitch,
|
||||
bool incPos, bool relPos, bool incOri ) {
|
||||
Pos = new Vector3( x, y, z );
|
||||
Yaw = yaw;
|
||||
Pitch = pitch;
|
||||
IncludesPosition = incPos;
|
||||
RelativePosition = relPos;
|
||||
IncludesOrientation = incOri;
|
||||
}
|
||||
|
||||
public static LocationUpdate MakeOri( float yaw, float pitch ) {
|
||||
return new LocationUpdate( 0, 0, 0, yaw, pitch, false, false, true );
|
||||
}
|
||||
|
||||
public static LocationUpdate MakePos( float x, float y, float z, bool rel ) {
|
||||
return new LocationUpdate( x, y, z, 0, 0, true, rel, false );
|
||||
}
|
||||
|
||||
public static LocationUpdate MakePos( Vector3 pos, bool rel ) {
|
||||
return new LocationUpdate( pos.X, pos.Y, pos.Z, 0, 0, true, rel, false );
|
||||
}
|
||||
|
||||
public static LocationUpdate MakePosAndOri( float x, float y, float z, float yaw, float pitch, bool rel ) {
|
||||
return new LocationUpdate( x, y, z, yaw, pitch, true, rel, true );
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@
|
||||
namespace ClassicalSharp {
|
||||
|
||||
public enum PacketId {
|
||||
ServerIdentification = 0,
|
||||
Handshake = 0,
|
||||
Ping = 1,
|
||||
LevelInitialise = 2,
|
||||
LevelDataChunk = 3,
|
||||
@ -38,6 +38,7 @@ namespace ClassicalSharp {
|
||||
CpeEnvWeatherType = 31,
|
||||
CpeHackControl = 32,
|
||||
CpeExtAddEntity2 = 33,
|
||||
CpePlayerClick = 34,
|
||||
}
|
||||
|
||||
public enum CpeMessageType {
|
||||
|
@ -1,5 +1,4 @@
|
||||
//#define NET_DEBUG
|
||||
using System;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
#if __MonoCS__
|
||||
@ -57,7 +56,7 @@ namespace ClassicalSharp {
|
||||
}
|
||||
}
|
||||
|
||||
public void SendPosition( Vector3 pos, byte yaw, byte pitch ) {
|
||||
public void SendPosition( Vector3 pos, float yaw, float pitch ) {
|
||||
byte payload = sendHeldBlock ? (byte)Window.HeldBlock : (byte)0xFF;
|
||||
MakePositionPacket( pos, yaw, pitch, payload );
|
||||
SendPacket();
|
||||
@ -93,25 +92,15 @@ namespace ClassicalSharp {
|
||||
return;
|
||||
}
|
||||
|
||||
int received = reader.size;
|
||||
while( received > 0 ) {
|
||||
while( reader.size > 0 ) {
|
||||
byte opcode = reader.buffer[0];
|
||||
if( received < packetSizes[opcode] ) break;
|
||||
#if NET_DEBUG
|
||||
Utils.LogDebug( "Remaining {0} bytes ({1} size {2} bytes)", received, (PacketId)opcode, packetSizes[opcode] );
|
||||
#endif
|
||||
if( reader.size < packetSizes[opcode] ) break;
|
||||
ReadPacket( opcode );
|
||||
#if NET_DEBUG
|
||||
Utils.LogDebug( "Left {0} bytes", reader.size );
|
||||
#endif
|
||||
received = reader.size;
|
||||
}
|
||||
|
||||
Player player = Window.LocalPlayer;
|
||||
if( receivedFirstPosition ) {
|
||||
byte yawPacked = Utils.DegreesToPacked( player.YawDegrees );
|
||||
byte pitchPacked = Utils.DegreesToPacked( player.PitchDegrees );
|
||||
SendPosition( player.Position, yawPacked, pitchPacked );
|
||||
SendPosition( player.Position, player.YawDegrees, player.PitchDegrees );
|
||||
}
|
||||
CheckForNewTerrainAtlas();
|
||||
CheckForWomEnvironment();
|
||||
@ -136,7 +125,7 @@ namespace ClassicalSharp {
|
||||
static byte[] outBuffer = new byte[131];
|
||||
static int outIndex = 0;
|
||||
private static void MakeLoginPacket( string username, string verKey ) {
|
||||
WriteUInt8( 0 ); // packet id
|
||||
WriteUInt8( (byte)PacketId.Handshake );
|
||||
WriteUInt8( 7 ); // protocol version
|
||||
WriteString( username );
|
||||
WriteString( verKey );
|
||||
@ -144,7 +133,7 @@ namespace ClassicalSharp {
|
||||
}
|
||||
|
||||
private static void MakeSetBlockPacket( short x, short y, short z, bool place, byte block ) {
|
||||
WriteUInt8( 0x05 ); // packet id
|
||||
WriteUInt8( (byte)PacketId.SetBlockClient );
|
||||
WriteInt16( x );
|
||||
WriteInt16( y );
|
||||
WriteInt16( z );
|
||||
@ -152,42 +141,42 @@ namespace ClassicalSharp {
|
||||
WriteUInt8( block );
|
||||
}
|
||||
|
||||
private static void MakePositionPacket( Vector3 pos, byte yaw, byte pitch, byte playerId ) {
|
||||
WriteUInt8( 0x08 ); // packet id
|
||||
WriteUInt8( playerId ); // player id (-1 is self)
|
||||
private static void MakePositionPacket( Vector3 pos, float yaw, float pitch, byte payload ) {
|
||||
WriteUInt8( (byte)PacketId.EntityTeleport );
|
||||
WriteUInt8( payload ); // held block when using HeldBlock, otherwise just 255
|
||||
WriteInt16( (short)( pos.X * 32 ) );
|
||||
WriteInt16( (short)( (int)( pos.Y * 32 ) + 51 ) );
|
||||
WriteInt16( (short)( pos.Z * 32 ) );
|
||||
WriteUInt8( yaw );
|
||||
WriteUInt8( pitch );
|
||||
WriteUInt8( (byte)Utils.DegreesToPacked( yaw, 256 ) );
|
||||
WriteUInt8( (byte)Utils.DegreesToPacked( pitch, 256 ) );
|
||||
}
|
||||
|
||||
private static void MakeMessagePacket( string text ) {
|
||||
WriteUInt8( 0x0D ); // packet id
|
||||
WriteUInt8( (byte)PacketId.Message );
|
||||
WriteUInt8( 0xFF ); // unused
|
||||
WriteString( text );
|
||||
}
|
||||
|
||||
private static void MakeExtInfo( string appName, int extensionsCount ) {
|
||||
WriteUInt8( 0x10 ); // packet id
|
||||
WriteUInt8( (byte)PacketId.CpeExtInfo );
|
||||
WriteString( appName );
|
||||
WriteInt16( (short)extensionsCount );
|
||||
}
|
||||
|
||||
private static void MakeExtEntry( string extensionName, int extensionVersion ) {
|
||||
WriteUInt8( 0x11 ); // packet id
|
||||
WriteUInt8( (byte)PacketId.CpeExtEntry );
|
||||
WriteString( extensionName );
|
||||
WriteInt32( extensionVersion );
|
||||
}
|
||||
|
||||
private static void MakeCustomBlockSupportLevel( byte version ) {
|
||||
WriteUInt8( 0x13 ); // packet id
|
||||
WriteUInt8( (byte)PacketId.CpeCustomBlockSupportLevel );
|
||||
WriteUInt8( version );
|
||||
}
|
||||
|
||||
private static void MakePlayerClick( byte button, byte action, short yaw, short pitch, byte targetEntity,
|
||||
Vector3I targetPos, byte targetFace ) {
|
||||
WriteUInt8( 0x22 ); // packet id
|
||||
WriteUInt8( (byte)PacketId.CpePlayerClick );
|
||||
WriteUInt8( button );
|
||||
WriteUInt8( action );
|
||||
WriteInt16( yaw );
|
||||
@ -232,9 +221,6 @@ namespace ClassicalSharp {
|
||||
outIndex = 0;
|
||||
if( Disconnected ) return;
|
||||
|
||||
#if NET_DEBUG
|
||||
Utils.LogDebug( "writing {0} bytes ({1})", packetLength, (PacketId)outBuffer[0] );
|
||||
#endif
|
||||
try {
|
||||
stream.Write( outBuffer, 0, packetLength );
|
||||
} catch( IOException ex ) {
|
||||
@ -253,19 +239,16 @@ namespace ClassicalSharp {
|
||||
DateTime receiveStart;
|
||||
DeflateStream gzipStream;
|
||||
GZipHeaderReader gzipHeader;
|
||||
int mapSizeIndex;
|
||||
byte[] mapSize = new byte[4];
|
||||
byte[] map;
|
||||
int mapIndex;
|
||||
int mapSizeIndex, mapIndex;
|
||||
byte[] mapSize = new byte[4], map;
|
||||
FixedBufferStream gzippedMap;
|
||||
int womCounter = 0;
|
||||
bool sendWomId = false, sentWomId = false;
|
||||
|
||||
void ReadPacket( byte opcode ) {
|
||||
reader.Remove( 1 ); // remove opcode
|
||||
|
||||
switch( (PacketId)opcode ) {
|
||||
case PacketId.ServerIdentification:
|
||||
case PacketId.Handshake:
|
||||
{
|
||||
byte protocolVer = reader.ReadUInt8();
|
||||
ServerName = reader.ReadString();
|
||||
@ -319,7 +302,7 @@ namespace ClassicalSharp {
|
||||
|
||||
if( gzipHeader.done || gzipHeader.ReadHeader( gzippedMap ) ) {
|
||||
if( mapSizeIndex < 4 ) {
|
||||
mapSizeIndex += gzipStream.Read( mapSize, 0, 4 - mapSizeIndex );
|
||||
mapSizeIndex += gzipStream.Read( mapSize, mapSizeIndex, 4 - mapSizeIndex );
|
||||
}
|
||||
|
||||
if( mapSizeIndex == 4 ) {
|
||||
@ -483,10 +466,10 @@ namespace ClassicalSharp {
|
||||
case PacketId.CpeHoldThis:
|
||||
{
|
||||
byte blockType = reader.ReadUInt8();
|
||||
bool noChanging = reader.ReadUInt8() != 0;
|
||||
bool canChange = reader.ReadUInt8() == 0;
|
||||
Window.CanChangeHeldBlock = true;
|
||||
Window.HeldBlock = (Block)blockType;
|
||||
Window.CanChangeHeldBlock = !noChanging;
|
||||
Window.CanChangeHeldBlock = canChange;
|
||||
} break;
|
||||
|
||||
case PacketId.CpeExtAddPlayerName:
|
||||
|
@ -8,6 +8,8 @@ namespace ClassicalSharp {
|
||||
public partial class NetworkProcessor {
|
||||
|
||||
string womEnvIdentifier = "womenv_0", womTerrainIdentifier = "womterrain_0";
|
||||
int womCounter = 0;
|
||||
|
||||
void CheckForWomEnvironment() {
|
||||
DownloadedItem item;
|
||||
Window.AsyncDownloader.TryGetItem( womEnvIdentifier, out item );
|
||||
|
@ -103,26 +103,14 @@ namespace ClassicalSharp {
|
||||
return radians * 180.0 / Math.PI;
|
||||
}
|
||||
|
||||
// Basically this works as a special unit circle in the range of [0..255].
|
||||
public static byte RadiansToPacked( double radians ) {
|
||||
return DegreesToPacked( radians * 180.0 / Math.PI );
|
||||
}
|
||||
|
||||
public static byte DegreesToPacked( double degrees ) {
|
||||
int packed = (int)( degrees * 256.0 / 360.0 ) % 256; // 256 = period
|
||||
if( packed < 0 )
|
||||
packed += 256; // Normalise into [0..255];
|
||||
return (byte)packed;
|
||||
public static int DegreesToPacked( double degrees, int period ) {
|
||||
return (int)( degrees * period / 360.0 ) % period;
|
||||
}
|
||||
|
||||
public static double PackedToDegrees( byte packed ) {
|
||||
return packed * 360.0 / 256.0;
|
||||
}
|
||||
|
||||
public static double PackedToRadians( byte packed ) {
|
||||
return PackedToDegrees( packed ) * Math.PI / 180.0;
|
||||
}
|
||||
|
||||
public static float DistanceSquared( Vector3 p1, Vector3 p2 ) {
|
||||
float dx = p2.X - p1.X;
|
||||
float dy = p2.Y - p1.Y;
|
||||
|
Loading…
x
Reference in New Issue
Block a user