Some more changes with networking code.

This commit is contained in:
UnknownShadow200 2015-06-28 14:33:30 +10:00
parent 1ea77fdfed
commit e6bbb6bb94
4 changed files with 32 additions and 32 deletions

View file

@ -16,7 +16,6 @@ namespace DefaultPlugin.Network {
bool sendHeldBlock = false;
bool useMessageTypes = false;
bool useBlockPermissions = false;
bool receivedFirstPosition = false;
public override void Connect( IPAddress address, int port ) {
base.Connect( address, port );
@ -31,9 +30,11 @@ namespace DefaultPlugin.Network {
}
}
public override void SendPosition( Vector3 pos, byte yaw, byte pitch ) {
public override void SendPosition( Vector3 pos, float yawDegrees, float pitchDegrees ) {
byte payload = sendHeldBlock ? (byte)Window.Inventory.HeldBlock : (byte)0xFF;
WritePacket( MakePositionPacket( pos, yaw, pitch, payload ) );
byte yawPacked = Utils.DegreesToPacked( yawDegrees );
byte pitchPacked = Utils.DegreesToPacked( pitchDegrees );
WritePacket( MakePositionPacket( pos, yawPacked, pitchPacked, payload ) );
}
public override void SendSetBlock( int x, int y, int z, byte block ) {
@ -69,10 +70,8 @@ namespace DefaultPlugin.Network {
}
Player player = Window.LocalPlayer;
if( receivedFirstPosition ) {
byte yawPacked = Utils.DegreesToPacked( player.YawDegrees );
byte pitchPacked = Utils.DegreesToPacked( player.PitchDegrees );
SendPosition( player.Position, yawPacked, pitchPacked );
if( receivedFirstPosition ) {
SendPosition( player.Position, player.YawDegrees, player.PitchDegrees );
}
CheckForNewTerrainAtlas();
CheckForWomEnvironment();

View file

@ -25,6 +25,7 @@ namespace ClassicalSharp {
public Game Window;
public bool Disconnected = false;
public bool UsingExtPlayerList; // TODO: remove this
public bool receivedFirstPosition = false;
public virtual void Connect( IPAddress address, int port) {
socket = new Socket( address.AddressFamily, SocketType.Stream, ProtocolType.Tcp );
@ -42,7 +43,7 @@ namespace ClassicalSharp {
public abstract void SendChat( string text );
public abstract void SendPosition( Vector3 pos, byte yaw, byte pitch );
public abstract void SendPosition( Vector3 pos, float yawDegrees, float pitchDegrees );
public abstract void SendSetBlock( int x, int y, int z, byte block );

View file

@ -150,12 +150,12 @@ namespace PluginBeta173.Network {
public override void ReadCallback( Game game ) {
LocationUpdate update = LocationUpdate.MakePosAndOri( x, y, z, yaw, pitch, false );
//game.Map.IsNotLoaded = false;
//if( !game.Network.receivedFirstPosition ) {
// game.RaiseOnNewMapLoaded();
//}
//game.Network.receivedFirstPosition = true;
//game.SetNewScreen( new NormalScreen( game ) );
//game.LocalPlayer.SetLocation( update, false );
if( !game.Network.receivedFirstPosition ) {
game.RaiseOnNewMapLoaded();
}
game.Network.receivedFirstPosition = true;
game.SetNewScreen( new NormalScreen( game ) );
game.LocalPlayer.SetLocation( update, false );
}
}

View file

@ -17,7 +17,6 @@ namespace PluginBeta173.Network {
NetReader reader;
NetWriter writer;
public string ServerName, ServerMotd;
public bool receivedFirstPosition = false;
public void Connect() {
IPAddress address = Window.IPAddress;
@ -42,7 +41,23 @@ namespace PluginBeta173.Network {
SendPacket( new ChatOutbound( text ) );
}
public override void SendPosition( Vector3 pos, byte yaw, byte pitch ) {
public override void SendPosition( Vector3 position, float yawDegrees, float pitchDegrees ) {
float yaw = yawDegrees - 180f;
bool ori = lastYaw != yaw || lastPitch != pitchDegrees;
bool pos = lastPos != position;
lastPos = position;
lastYaw = yaw;
lastPitch = pitchDegrees;
if( ori && pos ) {
SendPacket( new PlayerPosAndLookOutbound( true, lastPos, lastPos.Y + Player.EyeHeight, lastYaw, lastPitch ) );
} else if( pos ) {
SendPacket( new PlayerPosOutbound( true, lastPos, lastPos.Y + Player.EyeHeight ) );
} else if( ori ) {
SendPacket( new PlayerLookOutbound( true, lastYaw, lastPitch ) );
} else {
SendPacket( new PlayerOutbound( true ) );
}
}
public override void SendSetBlock( int x, int y, int z, byte block ) {
@ -95,22 +110,7 @@ namespace PluginBeta173.Network {
Player player = Window.LocalPlayer;
if( receivedFirstPosition ) {
// Figure out which is the most optimal packet to send.
float yaw = player.YawDegrees - 180f;
bool ori = lastYaw != yaw || lastPitch != player.PitchDegrees;
bool pos = lastPos != player.Position;
lastPos = player.Position;
lastYaw = yaw;
lastPitch = player.PitchDegrees;
if( ori && pos ) {
SendPacket( new PlayerPosAndLookOutbound( true, lastPos, lastPos.Y + Player.EyeHeight, lastYaw, lastPitch ) );
} else if( pos ) {
SendPacket( new PlayerPosOutbound( true, lastPos, lastPos.Y + Player.EyeHeight ) );
} else if( ori ) {
SendPacket( new PlayerLookOutbound( true, lastYaw, lastPitch ) );
} else {
SendPacket( new PlayerOutbound( true ) );
}
SendPosition( player.Position, player.YawDegrees, player.PitchDegrees );
}
}
}