From 9e9a739932b5e4e3fb7cd4bd11e38ab69c941696 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 30 Sep 2015 09:10:55 +1000 Subject: [PATCH] Fix Direct3D9 build clipping when standing next to a block, blocks should not be placed over players. (Thanks Empy) --- ClassicalSharp/Game/Game.InputHandling.cs | 34 ++++++++++++++++++--- ClassicalSharp/GraphicsAPI/Direct3D9Api.cs | 1 + ClassicalSharp/GraphicsAPI/IGraphicsApi.cs | 2 ++ ClassicalSharp/Physics/BoundingBox.cs | 8 ++--- ClassicalSharp/Rendering/PickingRenderer.cs | 2 +- ClassicalSharp/Utils/Camera.cs | 3 +- readme.md | 4 +-- 7 files changed, 42 insertions(+), 12 deletions(-) diff --git a/ClassicalSharp/Game/Game.InputHandling.cs b/ClassicalSharp/Game/Game.InputHandling.cs index 6b68d6997..f8454d422 100644 --- a/ClassicalSharp/Game/Game.InputHandling.cs +++ b/ClassicalSharp/Game/Game.InputHandling.cs @@ -168,14 +168,40 @@ namespace ClassicalSharp { Vector3I pos = SelectedPos.TranslatedPos; if( !Map.IsValidPos( pos ) ) return; - Block block = inv.HeldBlock; - if( !CanPick( Map.GetBlock( pos ) ) && inv.CanPlace[(int)block] ) { - UpdateBlock( pos.X, pos.Y, pos.Z, (byte)block ); - Network.SendSetBlock( pos.X, pos.Y, pos.Z, true, (byte)block ); + byte block = (byte)inv.HeldBlock; + if( !CanPick( Map.GetBlock( pos ) ) && inv.CanPlace[block] + && CheckIsFree( pos, block ) ) { + UpdateBlock( pos.X, pos.Y, pos.Z, block ); + Network.SendSetBlock( pos.X, pos.Y, pos.Z, true, block ); } } } + bool CheckIsFree( Vector3I pos, byte newBlock ) { + float height = BlockInfo.Height[newBlock]; + BoundingBox blockBB = new BoundingBox( pos.X, pos.Y, pos.Z, + pos.X + 1, pos.Y + height, pos.Z + 1 ); + + for( int id = 0; id < 255; id++ ) { + Player player = Players[id]; + if( player == null ) continue; + if( player.CollisionBounds.Intersects( blockBB ) ) return false; + } + + BoundingBox localBB = LocalPlayer.CollisionBounds; + if( localBB.Intersects( blockBB ) ) { + localBB.Min.Y += 0.25f + Entity.Adjustment; + if( localBB.Intersects( blockBB ) ) return false; + + // Push player up if they are jumping and trying to place a block underneath them. + Vector3 p = LocalPlayer.Position; + p.Y = pos.Y + height + Entity.Adjustment; + LocationUpdate update = LocationUpdate.MakePos( p, false ); + LocalPlayer.SetLocation( update, false ); + } + return true; + } + void ButtonStateChanged( MouseButton button, bool pressed, byte targetId ) { if( buttonsDown[(int)button] ) { Network.SendPlayerClick( button, false, targetId, SelectedPos ); diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index 0f7f8d326..ef95c8e22 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -31,6 +31,7 @@ namespace ClassicalSharp.GraphicsAPI { CreateFlags createFlags = CreateFlags.HardwareVertexProcessing; public Direct3D9Api( Game game ) { + MinZNear = 0.05f; IntPtr windowHandle = ((WinWindowInfo)game.WindowInfo).WindowHandle; d3d = new Direct3D(); int adapter = d3d.Adapters[0].Adapter; diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index b30e0436f..dfab09060 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -17,6 +17,8 @@ namespace ClassicalSharp.GraphicsAPI { public abstract bool Texturing { set; } + internal float MinZNear = 0.1f; + public int CreateTexture( Bitmap bmp ) { Rectangle rec = new Rectangle( 0, 0, bmp.Width, bmp.Height ); // Convert other pixel formats into 32bpp formats. diff --git a/ClassicalSharp/Physics/BoundingBox.cs b/ClassicalSharp/Physics/BoundingBox.cs index c4569f62c..b6eeac069 100644 --- a/ClassicalSharp/Physics/BoundingBox.cs +++ b/ClassicalSharp/Physics/BoundingBox.cs @@ -22,12 +22,12 @@ namespace ClassicalSharp { return new BoundingBox( Min + amount, Max + amount ); } - public bool Intersects( BoundingBox box ) { - if( Max.X >= box.Min.X && Min.X <= box.Max.X ) { - if( Max.Y < box.Min.Y || Min.Y > box.Max.Y ) { + public bool Intersects( BoundingBox other ) { + if( Max.X >= other.Min.X && Min.X <= other.Max.X ) { + if( Max.Y < other.Min.Y || Min.Y > other.Max.Y ) { return false; } - return Max.Z >= box.Min.Z && Min.Z <= box.Max.Z; + return Max.Z >= other.Min.Z && Min.Z <= other.Max.Z; } return false; } diff --git a/ClassicalSharp/Rendering/PickingRenderer.cs b/ClassicalSharp/Rendering/PickingRenderer.cs index c8872c4f6..938eadbb9 100644 --- a/ClassicalSharp/Rendering/PickingRenderer.cs +++ b/ClassicalSharp/Rendering/PickingRenderer.cs @@ -25,7 +25,7 @@ namespace ClassicalSharp.Renderers { index = 0; Vector3 p1 = pickedPos.Min - new Vector3( offset, offset, offset ); Vector3 p2 = pickedPos.Max + new Vector3( offset, offset, offset ); - col.A = 220; + col.A = 150; graphics.AlphaBlending = true; // bottom face diff --git a/ClassicalSharp/Utils/Camera.cs b/ClassicalSharp/Utils/Camera.cs index 200ad92ca..c10e10e1c 100644 --- a/ClassicalSharp/Utils/Camera.cs +++ b/ClassicalSharp/Utils/Camera.cs @@ -38,7 +38,8 @@ namespace ClassicalSharp { public override Matrix4 GetProjection() { float fovy = (float)Utils.DegreesToRadians( 70 ); float aspectRatio = (float)game.Width / game.Height; - return Matrix4.CreatePerspectiveFieldOfView( fovy, aspectRatio, 0.1f, game.ViewDistance ); + float zNear = game.Graphics.MinZNear; + return Matrix4.CreatePerspectiveFieldOfView( fovy, aspectRatio, zNear, game.ViewDistance ); } public override void GetPickedBlock( PickedPos pos ) { diff --git a/readme.md b/readme.md index c0716a710..62caa672c 100644 --- a/readme.md +++ b/readme.md @@ -20,10 +20,10 @@ It **does not** work with 'modern/premium' Minecraft servers. Initially, you will need to run launcher.exe to download the required assets from minecraft.net. Just click 'OK' to the dialog box that appears when you start the launcher. -##### Singleplayer +**Singleplayer** Run classicalsharp.exe. -##### Multiplayer +**Multiplayer** Run launcher.exe. You can connect to LAN/locally hosted servers, minecraft.net servers, and classicube.net servers through the launcher. ##### Mono specific notes