From 3312b769c4a81786f0986e7b0adbb41b358ade62 Mon Sep 17 00:00:00 2001 From: high Date: Thu, 11 Aug 2011 00:27:56 -0400 Subject: [PATCH] Fixed teleporting --- TShockAPI/Net/BaseMsg.cs | 38 +++++++++++ TShockAPI/Net/SpawnMsg.cs | 30 +++++++++ TShockAPI/Net/WorldInfoMsg.cs | 23 ++----- TShockAPI/Properties/AssemblyInfo.cs | 4 +- TShockAPI/TSPlayer.cs | 98 +++++++++++++--------------- TShockAPI/TShockAPI.csproj | 4 +- 6 files changed, 124 insertions(+), 73 deletions(-) create mode 100644 TShockAPI/Net/BaseMsg.cs create mode 100644 TShockAPI/Net/SpawnMsg.cs diff --git a/TShockAPI/Net/BaseMsg.cs b/TShockAPI/Net/BaseMsg.cs new file mode 100644 index 00000000..ed41cf71 --- /dev/null +++ b/TShockAPI/Net/BaseMsg.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using TerrariaAPI; +using XNAHelpers; + +namespace TShockAPI.Net +{ + public class BaseMsg : IPackable + { + public virtual PacketTypes ID + { + get { throw new NotImplementedException("Msg ID not implemented"); } + } + public void PackFull(System.IO.Stream stream) + { + long start = stream.Position; + stream.WriteInt32(1); + stream.WriteInt8((byte)ID); + Pack(stream); + long end = stream.Position; + stream.Position = start; + stream.WriteInt32((int)(end - start) - 4); + stream.Position = end; + } + + public virtual void Unpack(System.IO.Stream stream) + { + throw new NotImplementedException(); + } + + public virtual void Pack(System.IO.Stream stream) + { + throw new NotImplementedException(); + } + } +} diff --git a/TShockAPI/Net/SpawnMsg.cs b/TShockAPI/Net/SpawnMsg.cs new file mode 100644 index 00000000..29e9e4c9 --- /dev/null +++ b/TShockAPI/Net/SpawnMsg.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using TerrariaAPI; +using XNAHelpers; + +namespace TShockAPI.Net +{ + public class SpawnMsg : BaseMsg + { + public override PacketTypes ID + { + get + { + return PacketTypes.PlayerSpawn; + } + } + + public int TileX { get; set; } + public int TileY {get;set;} + public byte PlayerIndex { get; set; } + + public override void Pack(Stream stream) + { + stream.WriteInt8(PlayerIndex); + stream.WriteInt32(TileX); + stream.WriteInt32(TileY); + } + } +} diff --git a/TShockAPI/Net/WorldInfoMsg.cs b/TShockAPI/Net/WorldInfoMsg.cs index c3a8fb8d..1ba56691 100644 --- a/TShockAPI/Net/WorldInfoMsg.cs +++ b/TShockAPI/Net/WorldInfoMsg.cs @@ -33,7 +33,7 @@ namespace TShockAPI.Net DownedBoss2 = 4, DownedBoss3 = 8, } - public class WorldInfoMsg : IPackable + public class WorldInfoMsg : BaseMsg { public int Time { get; set; } public bool DayTime { get; set; } @@ -48,18 +48,14 @@ namespace TShockAPI.Net public int WorldID { get; set; } public WorldInfoFlag WorldFlags { get; set; } public string WorldName { get; set; } - public void PackFull(Stream stream) + public override PacketTypes ID { - long start = stream.Position; - stream.WriteInt32(1); - stream.WriteInt8((byte)PacketTypes.WorldInfo); - Pack(stream); - long end = stream.Position; - stream.Position = start; - stream.WriteInt32((int)(end - start) - 4); - stream.Position = end; + get + { + return PacketTypes.WorldInfo; + } } - public void Pack(Stream stream) + public override void Pack(Stream stream) { stream.WriteInt32(Time); stream.WriteBoolean(DayTime); @@ -75,10 +71,5 @@ namespace TShockAPI.Net stream.WriteInt8((byte)WorldFlags); stream.WriteBytes(Encoding.ASCII.GetBytes(WorldName)); } - - public void Unpack(Stream stream) - { - throw new NotImplementedException(); - } } } diff --git a/TShockAPI/Properties/AssemblyInfo.cs b/TShockAPI/Properties/AssemblyInfo.cs index 5d67da17..18787b22 100644 --- a/TShockAPI/Properties/AssemblyInfo.cs +++ b/TShockAPI/Properties/AssemblyInfo.cs @@ -36,5 +36,5 @@ using System.Runtime.InteropServices; // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.2.5.0810")] -[assembly: AssemblyFileVersion("3.2.5.0810")] +[assembly: AssemblyVersion("3.2.6.0811")] +[assembly: AssemblyFileVersion("3.2.6.0811")] diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index b3efbbd7..673e98ca 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -23,6 +23,7 @@ using Microsoft.Xna.Framework; using Terraria; using TerrariaAPI; using TShockAPI.Net; +using XNAHelpers; namespace TShockAPI { @@ -157,31 +158,32 @@ namespace TShockAPI SendData(PacketTypes.Disconnect, reason); } - void SendTeleport(int tilex, int tiley) + + void SendWorldInfo(int tilex, int tiley, bool fakeid) { - var msg = new WorldInfoMsg - { - Time = (int)Main.time, - DayTime = Main.dayTime, - MoonPhase = (byte)Main.moonPhase, - BloodMoon = Main.bloodMoon, - MaxTilesX = Main.maxTilesX, - MaxTilesY = Main.maxTilesY, - SpawnX = tilex, - SpawnY = tiley, - WorldSurface = (int)Main.worldSurface, - RockLayer = (int)Main.rockLayer, - WorldID = Main.worldID, - WorldFlags = (WorldGen.shadowOrbSmashed ? WorldInfoFlag.OrbSmashed : WorldInfoFlag.None) | - (NPC.downedBoss1 ? WorldInfoFlag.DownedBoss1 : WorldInfoFlag.None) | - (NPC.downedBoss2 ? WorldInfoFlag.DownedBoss2 : WorldInfoFlag.None) | - (NPC.downedBoss3 ? WorldInfoFlag.DownedBoss3 : WorldInfoFlag.None), - WorldName = Main.worldName - }; - - using (var ms = new MemoryStream()) { + var msg = new WorldInfoMsg + { + Time = (int)Main.time, + DayTime = Main.dayTime, + MoonPhase = (byte)Main.moonPhase, + BloodMoon = Main.bloodMoon, + MaxTilesX = Main.maxTilesX, + MaxTilesY = Main.maxTilesY, + SpawnX = tilex, + SpawnY = tiley, + WorldSurface = (int)Main.worldSurface, + RockLayer = (int)Main.rockLayer, + //Sending a fake world id causes the client to not be able to find a stored spawnx/y. + //This fixes the bed spawn point bug. With a fake world id it wont be able to find the bed spawn. + WorldID = !fakeid ? Main.worldID : -1, + WorldFlags = (WorldGen.shadowOrbSmashed ? WorldInfoFlag.OrbSmashed : WorldInfoFlag.None) | + (NPC.downedBoss1 ? WorldInfoFlag.DownedBoss1 : WorldInfoFlag.None) | + (NPC.downedBoss2 ? WorldInfoFlag.DownedBoss2 : WorldInfoFlag.None) | + (NPC.downedBoss3 ? WorldInfoFlag.DownedBoss3 : WorldInfoFlag.None), + WorldName = Main.worldName + }; msg.PackFull(ms); SendRawData(ms.ToArray()); } @@ -191,7 +193,8 @@ namespace TShockAPI { InitSpawn = false; - SendTeleport(tilex, tiley); + + SendWorldInfo(tilex, tiley, true); //150 Should avoid all client crash errors //The error occurs when a tile trys to update which the client hasnt load yet, Clients only update tiles withen 150 blocks @@ -202,44 +205,31 @@ namespace TShockAPI return false; } - if (TPlayer.SpawnX > 0 && TPlayer.SpawnY > 0) - { - int spX = TPlayer.SpawnX; - int spY = TPlayer.SpawnY; - Main.tile[spX, spY].active = false; - SendTileSquare(spX, spY); - Spawn(); - Main.tile[spX, spY].active = true; - SendTileSquare(spX, spY); - oldSpawn = new Vector2(spX, spY); - } - else - { - //Checks if Player has spawn point set (Server may think player does not have spawn) - if (oldSpawn != Vector2.Zero) - { - Main.tile[(int)oldSpawn.X, (int)oldSpawn.Y].active = false; - SendTileSquare((int)oldSpawn.X, (int)oldSpawn.Y); - Spawn(); - Main.tile[(int)oldSpawn.X, (int)oldSpawn.Y].active = true; - SendTileSquare((int)oldSpawn.X, (int)oldSpawn.Y); - NetMessage.syncPlayers(); - } - //Player has no spawn point set - else - { - Spawn(); - } - } + Spawn(-1, -1); - SendTeleport(Main.spawnTileX, Main.spawnTileY); + SendWorldInfo(Main.spawnTileX, Main.spawnTileY, false); return true; } public void Spawn() { - SendData(PacketTypes.PlayerSpawn, "", Index, 0.0f, 0.0f, 0.0f); + Spawn(TPlayer.SpawnX, TPlayer.SpawnY); + } + + public void Spawn(int tilex, int tiley) + { + using (var ms = new MemoryStream()) + { + var msg = new SpawnMsg() + { + PlayerIndex = (byte)Index, + TileX = tilex, + TileY = tiley + }; + msg.PackFull(ms); + SendRawData(ms.ToArray()); + } } public virtual bool SendTileSquare(int x, int y, int size = 10) diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 02e54387..08f1fb60 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -111,7 +111,9 @@ + + @@ -178,7 +180,7 @@ - +