From 35fa930eceb7a8441bb91537b508d494e836bc09 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 3 Apr 2016 15:17:57 +1000 Subject: [PATCH] Fix texture pack/terrain.png not changing anymore when loaded from texcache, also 'always yes/no' should not be shown in the 'sure you want to visit url' dialog. --- ClassicalSharp/2D/Screens/ChatScreen.cs | 2 +- ClassicalSharp/2D/Screens/WarningScreen.cs | 28 +++---- .../2D/Widgets/Chat/TextGroupWidget.cs | 2 +- ClassicalSharp/Entities/Entity.Bounds.cs | 75 ++++++++++++------- ClassicalSharp/Network/INetworkProcessor.cs | 18 ++--- 5 files changed, 73 insertions(+), 52 deletions(-) diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index 3beb21f9f..31dbd6250 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -395,7 +395,7 @@ namespace ClassicalSharp.Gui { if( Utils.IsUrlPrefix( url, 0 ) ) { game.ShowWarning( new WarningScreen( - game, url, false, "Are you sure you want to go to this url?", + game, url, false, false, "Are you sure you want to go to this url?", OpenUrl, AppendUrl, null, url, "Be careful - urls from strangers may link to websites that", " may have viruses, or things you may not want to open/see." diff --git a/ClassicalSharp/2D/Screens/WarningScreen.cs b/ClassicalSharp/2D/Screens/WarningScreen.cs index a155940ef..15939851b 100644 --- a/ClassicalSharp/2D/Screens/WarningScreen.cs +++ b/ClassicalSharp/2D/Screens/WarningScreen.cs @@ -7,7 +7,7 @@ namespace ClassicalSharp.Gui { public sealed class WarningScreen : MenuScreen { - public WarningScreen( Game game, object metadata, bool confirmNo, string title, + public WarningScreen( Game game, object metadata, bool showAlways, bool confirmNo, string title, Action yesClick, Action noClick, Action renderFrame, params string[] body ) : base( game ) { this.Metadata = metadata; @@ -17,13 +17,14 @@ namespace ClassicalSharp.Gui { this.title = title; this.body = body; this.confirmNo = confirmNo; + this.showAlways = showAlways; } internal Screen lastScreen; internal bool wasCursorVisible; string title, lastTitle; string[] body, lastBody; - bool confirmNo, confirmMode; + bool confirmNo, confirmMode, showAlways; public override void Init() { titleFont = new Font( game.FontName, 16, FontStyle.Bold ); @@ -51,7 +52,7 @@ namespace ClassicalSharp.Gui { labels[i].Dispose(); } this.title = title; - this.body = body; + this.body = body; labels = new TextWidget[body.Length + 1]; labels[0] = TextWidget.Create( game, 0, -120, title, @@ -100,16 +101,17 @@ namespace ClassicalSharp.Gui { } void InitStandardButtons() { - widgets = new ButtonWidget[] { - ButtonWidget.Create( game, -110, 30, 160, 35, "Yes", Anchor.Centre, - Anchor.Centre, titleFont, OnYesClick ), - ButtonWidget.Create( game, 110, 30, 160, 35, "No", Anchor.Centre, - Anchor.Centre, titleFont, OnNoClick ), - ButtonWidget.Create( game, -110, 80, 160, 35, "Always yes", Anchor.Centre, - Anchor.Centre, titleFont, OnYesAlwaysClick ), - ButtonWidget.Create( game, 110, 80, 160, 35, "Always no", Anchor.Centre, - Anchor.Centre, titleFont, OnNoAlwaysClick ), - }; + widgets = new ButtonWidget[showAlways ? 4 : 2]; + widgets[0] = ButtonWidget.Create( game, -110, 30, 160, 35, "Yes", Anchor.Centre, + Anchor.Centre, titleFont, OnYesClick ); + widgets[1] = ButtonWidget.Create( game, 110, 30, 160, 35, "No", Anchor.Centre, + Anchor.Centre, titleFont, OnNoClick ); + if( !showAlways ) return; + + widgets[2] = ButtonWidget.Create( game, -110, 80, 160, 35, "Always yes", Anchor.Centre, + Anchor.Centre, titleFont, OnYesAlwaysClick ); + widgets[3] = ButtonWidget.Create( game, 110, 80, 160, 35, "Always no", Anchor.Centre, + Anchor.Centre, titleFont, OnNoAlwaysClick ); } Action yesClick, noClick, renderFrame; diff --git a/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs b/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs index 943df21e8..4ce9a5874 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs @@ -137,7 +137,7 @@ namespace ClassicalSharp.Gui { for( int i = 0; i < Textures.Length; i++ ) { Texture tex = Textures[i]; if( tex.IsValid && tex.Bounds.Contains( mouseX, mouseY ) ) - return GetUrl( i, mouseX ) ?? lines[i]; + return GetUrl( i, mouseX ) ?? lines[i]; } return null; } diff --git a/ClassicalSharp/Entities/Entity.Bounds.cs b/ClassicalSharp/Entities/Entity.Bounds.cs index 1932417fb..1bc69c83e 100644 --- a/ClassicalSharp/Entities/Entity.Bounds.cs +++ b/ClassicalSharp/Entities/Entity.Bounds.cs @@ -48,25 +48,12 @@ namespace ClassicalSharp.Entities { Vector3 max = new Vector3( x, y, z ) + info.MaxBB[block]; BoundingBox blockBB = new BoundingBox( min, max ); - if( !blockBB.Intersects( bounds ) ) continue; + if( !blockBB.Intersects( bounds ) ) continue; if( condition( block ) ) return true; } return false; } - /// Constant offset used to avoid floating point roundoff errors. - public const float Adjustment = 0.001f; - static readonly Vector3 liqExpand = new Vector3( 0.25f/16f, 0/16f, 0.25f/16f ); - - /// Determines whether any of the blocks that intersect the - /// bounding box of this entity are lava or still lava. - public bool TouchesAnyLava() { - BoundingBox bounds = CollisionBounds.Expand( liqExpand ); - AdjustLiquidTestBounds( ref bounds ); - return TouchesAny( bounds, - b => b == (byte)Block.Lava || b == (byte)Block.StillLava ); - } - /// Determines whether any of the blocks that intersect the /// bounding box of this entity are rope. public bool TouchesAnyRope() { @@ -75,24 +62,56 @@ namespace ClassicalSharp.Entities { return TouchesAny( bounds, b => b == (byte)Block.Rope ); } + /// Constant offset used to avoid floating point roundoff errors. + public const float Adjustment = 0.001f; + + + static readonly Vector3 liqExpand = new Vector3( 0.25f/16f, 0/16f, 0.25f/16f ); + + // If liquid block above, leave height same + // otherwise reduce water BB height by 0.5 blocks + bool TouchesAnyLiquid( BoundingBox bounds, byte block1, byte block2 ) { + Vector3I bbMin = Vector3I.Floor( bounds.Min ); + Vector3I bbMax = Vector3I.Floor( bounds.Max ); + int height = game.World.Height; + + // Order loops so that we minimise cache misses + for( int y = bbMin.Y; y <= bbMax.Y; y++ ) + for( int z = bbMin.Z; z <= bbMax.Z; z++ ) + for( int x = bbMin.X; x <= bbMax.X; x++ ) + { + if( !game.World.IsValidPos( x, y, z ) ) continue; + byte block = game.World.GetBlock( x, y, z ); + byte below = (y - 1) < 0 ? (byte)0 : game.World.GetBlock( x, y - 1, z ); + byte above = (y + 1) >= height ? (byte)0 : game.World.GetBlock( x, y + 1, z ); + + // TODO: use recording to find right constants when I have more time + Vector3 min = new Vector3( x, y, z ) + info.MinBB[block]; + Vector3 max = new Vector3( x, y, z ) + info.MaxBB[block]; + //if( game.BlockInfo.Collide[below] != CollideType.SwimThrough ) + // min.Y += 4/16f; + //if( game.BlockInfo.Collide[above] != CollideType.SwimThrough ) + // max.Y -= 4/16f; + + BoundingBox blockBB = new BoundingBox( min, max ); + if( !blockBB.Intersects( bounds ) ) continue; + if( block == block1 || block == block2 ) return true; + } + return false; + } + + /// Determines whether any of the blocks that intersect the + /// bounding box of this entity are lava or still lava. + public bool TouchesAnyLava() { + BoundingBox bounds = CollisionBounds.Expand( liqExpand ); + return TouchesAnyLiquid( bounds, (byte)Block.Lava, (byte)Block.StillLava ); + } + /// Determines whether any of the blocks that intersect the /// bounding box of this entity are water or still water. public bool TouchesAnyWater() { - // TODO: proper way to check seems to be: - // If liquid block above, leave height same - // otherwise reduce water BB height by 0.5 blocks BoundingBox bounds = CollisionBounds.Expand( liqExpand ); - AdjustLiquidTestBounds( ref bounds ); - return TouchesAny( bounds, - b => b == (byte)Block.Water || b == (byte)Block.StillWater ); - } - - void AdjustLiquidTestBounds( ref BoundingBox bounds ) { - // Even though we collide with lava 3 blocks above our feet, vanilla client thinks - // that we do not.. so we have to maintain compatibility here. - float height = bounds.Max.Y - bounds.Min.Y; - const float pHeight = (28.5f - 4f)/16f; - bounds.Max.Y = bounds.Min.Y + Math.Min( height, pHeight ); + return TouchesAnyLiquid( bounds, (byte)Block.Water, (byte)Block.StillWater ); } } } \ No newline at end of file diff --git a/ClassicalSharp/Network/INetworkProcessor.cs b/ClassicalSharp/Network/INetworkProcessor.cs index cfc3059d3..2804019d6 100644 --- a/ClassicalSharp/Network/INetworkProcessor.cs +++ b/ClassicalSharp/Network/INetworkProcessor.cs @@ -53,7 +53,7 @@ namespace ClassicalSharp { public bool ServerSupportsFullCP437; - #region Texture pack / terrain.png + #region Texture pack / terrain.png protected Game game; @@ -76,7 +76,7 @@ namespace ClassicalSharp { if( !game.AcceptedUrls.HasUrl( url ) && !game.DeniedUrls.HasUrl( url ) ) { game.AsyncDownloader.RetrieveContentLength( url, true, "CL_" + url ); game.ShowWarning( new WarningScreen( - game, "CL_" + url, true, "Do you want to download the server's texture pack?", + game, "CL_" + url, true, true, "Do you want to download the server's texture pack?", DownloadTexturePack, null, WarningScreenTick, "Texture pack url:", url, "Download size: Determining..." ) ); @@ -127,14 +127,14 @@ namespace ClassicalSharp { game.World.TextureUrl = item.Url; } else if( Is304Status( item.WebEx ) ) { Bitmap bmp = TextureCache.GetBitmapFromCache( item.Url ); - if( bmp != null ) game.World.TextureUrl = item.Url; - if( bmp == null ) // Should never happen, but handle anyways. ExtractDefault(); else if( item.Url != game.World.TextureUrl ) - game.ChangeTerrainAtlas( bmp ); + game.ChangeTerrainAtlas( bmp ); + + if( bmp != null ) game.World.TextureUrl = item.Url; } else { - ExtractDefault(); + ExtractDefault(); } } @@ -146,14 +146,14 @@ namespace ClassicalSharp { game.World.TextureUrl = item.Url; } else if( Is304Status( item.WebEx ) ) { byte[] data = TextureCache.GetDataFromCache( item.Url ); - if( data != null ) game.World.TextureUrl = item.Url; - if( data == null ) { // Should never happen, but handle anyways. ExtractDefault(); } else if( item.Url != game.World.TextureUrl ) { TexturePackExtractor extractor = new TexturePackExtractor(); extractor.Extract( data, game ); - } + } + + if( data != null ) game.World.TextureUrl = item.Url; } else { ExtractDefault(); }