Abstract BlockInfo, use DefaultPlugin.Network namespace instead of ClassicalSharp in various classes in the default plugin.

This commit is contained in:
UnknownShadow200 2015-06-27 15:05:20 +10:00
parent 9b742f9d71
commit 2db1414370
17 changed files with 234 additions and 168 deletions

View file

@ -145,7 +145,7 @@ namespace ClassicalSharp {
void RecreateBlockTextures() {
int blocksCount = 0;
for( int i = 0; i < BlockInfo.BlocksCount; i++ ) {
for( int i = 0; i < Window.BlockInfo.BlocksCount; i++ ) {
if( Window.CanPlace[i] || Window.CanDelete[i] ) {
blocksCount++;
}
@ -158,7 +158,7 @@ namespace ClassicalSharp {
blocksTable = new BlockDrawInfo[blocksCount];
int tableIndex = 0;
for( int tile = 1; tile < BlockInfo.BlocksCount; tile++ ) {
for( int tile = 1; tile < Window.BlockInfo.BlocksCount; tile++ ) {
if( Window.CanPlace[tile] || Window.CanDelete[tile] ) {
Block block = (Block)tile;
int texId = Window.BlockInfo.GetOptimTextureLoc( (byte)block, TileSide.Left );

View file

@ -2,149 +2,55 @@
namespace ClassicalSharp {
public partial class BlockInfo {
bool[] isTransparent = new bool[BlocksCount];
bool[] isTranslucent = new bool[BlocksCount];
bool[] isOpaque = new bool[BlocksCount];
bool[] isSprite = new bool[BlocksCount];
bool[] isLiquid = new bool[BlocksCount];
float[] heights = new float[BlocksCount];
bool[] blocksLight = new bool[BlocksCount];
public const byte MaxDefinedBlock = (byte)Block.StoneBrick;
public const byte BlocksCount = MaxDefinedBlock + 1;
public abstract class BlockInfo {
public void Init() {
for( int tile = 1; tile < BlocksCount; tile++ ) {
heights[tile] = 1f;
blocksLight[tile] = true;
isOpaque[tile] = true;
}
SetupOptimTextures();
SetIsTranslucent( Block.StillWater, Block.Water );
SetIsTransparent( Block.Glass, Block.Leaves, Block.Sapling,
Block.RedMushroom, Block.BrownMushroom, Block.Rose,
Block.Dandelion, Block.Slab );
SetIsSprite( Block.Rose, Block.Sapling, Block.Dandelion,
Block.BrownMushroom, Block.RedMushroom );
SetBlockHeight( 8 / 16f, Block.Slab );
SetBlocksLight( false, Block.Glass, Block.Leaves, Block.Sapling,
Block.RedMushroom, Block.BrownMushroom, Block.Rose,
Block.Dandelion );
SetIsLiquid( Block.StillWater, Block.Water, Block.StillLava, Block.Lava );
SetIsTransparent( Block.Snow, Block.Fire, Block.Rope, Block.CobblestoneSlab );
SetBlocksLight( false, Block.Fire, Block.Rope );
SetIsTranslucent( Block.Ice );
SetIsSprite( Block.Rope, Block.Fire );
SetBlockHeight( 8 / 16f, Block.CobblestoneSlab );
SetBlockHeight( 2 / 16f, Block.Snow );
SetupCullingCache();
public BlockInfo( Game window ) {
}
public void SetDefaultBlockPermissions( bool[] canPlace, bool[] canDelete ) {
for( int tile = (int)Block.Stone; tile <= (int)Block.Obsidian; tile++ ) {
canPlace[tile] = true;
canDelete[tile] = true;
}
canPlace[(int)Block.Grass] = false;
canPlace[(int)Block.Lava] = false;
canPlace[(int)Block.Water] = false;
canPlace[(int)Block.StillLava] = false;
canPlace[(int)Block.StillWater] = false;
canPlace[(int)Block.Bedrock] = false;
canDelete[(int)Block.Bedrock] = false;
canDelete[(int)Block.Lava] = false;
canDelete[(int)Block.Water] = false;
canDelete[(int)Block.StillWater] = false;
canDelete[(int)Block.StillLava] = false;
}
public abstract byte BlocksCount { get; }
void SetIsTransparent( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isTransparent[(int)ids[i]] = true;
isOpaque[(int)ids[i]] = false;
}
}
public abstract void Init();
void SetIsSprite( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isSprite[(int)ids[i]] = true;
}
}
void SetIsTranslucent( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isTranslucent[(int)ids[i]] = true;
isOpaque[(int)ids[i]] = false;
}
}
void SetIsLiquid( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isLiquid[(int)ids[i]] = true;
}
}
void SetBlockHeight( float height, params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
heights[(int)ids[i]] = height;
}
}
void SetBlocksLight( bool blocks, params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
blocksLight[(int)ids[i]] = blocks;
}
}
public abstract void SetDefaultBlockPermissions( bool[] canPlace, bool[] canDelete );
/// <summary> Gets whether the given block id is opaque/not see through. </summary>
public bool IsOpaque( byte id ) {
return isOpaque[id];
}
public abstract bool IsOpaque( byte id );
/// <summary> Gets whether the given block id is opaque/not see through,
/// and occupies a full block. </summary>
public bool IsFullAndOpaque( byte id ) {
return isOpaque[id] && heights[id] == 1;
}
public abstract bool IsFullAndOpaque( byte id );
/// <summary> Gets whether the given block id is transparent/fully see through. </summary>
/// <remarks> Note that these blocks's alpha values are treated as either 'fully see through'
/// or 'fully solid'. </remarks>
public bool IsTransparent( byte id ) {
return isTransparent[id];
}
public abstract bool IsTransparent( byte id );
/// <summary> Gets the tile height of the given block id. </summary>
public float BlockHeight( byte id ) {
return heights[id];
}
public abstract float BlockHeight( byte id );
/// <summary> Gets whether the given block id is translucent/partially see through. </summary>
/// <remarks> Note that these blocks's colour values are blended into both
/// the transparent and opaque blocks behind them. </remarks>
public bool IsTranslucent( byte id ) {
return isTranslucent[id];
}
public abstract bool IsTranslucent( byte id );
/// <summary> Gets whether the given block blocks sunlight. </summary>
public bool BlocksLight( byte id ) {
return blocksLight[id];
}
public abstract bool BlocksLight( byte id );
/// <summary> Gets whether the given block id is a sprite. <br/>
/// (flowers, saplings, fire, etc) </summary>
public bool IsSprite( byte id ) {
return isSprite[id];
}
public abstract bool IsSprite( byte id );
/// <summary> Gets whether the given block id is a liquid. <br/>
/// (water or lava) </summary>
public bool IsLiquid( byte id ) {
return isLiquid[id];
}
public abstract bool IsLiquid( byte id );
public abstract bool IsFaceHidden( byte tile, byte block, int tileSide );
/// <summary> Gets the index in the ***optimised*** 2D terrain atlas for the
/// texture of the face of the given block. </summary>
/// <param name="block"> Block ID. </param>
/// <param name="face">Face of the given block, see TileSide constants. </param>
/// <returns>The index of the texture within the terrain atlas.</returns>
public abstract int GetOptimTextureLoc( byte block, int face );
}
}

View file

@ -107,8 +107,6 @@
<Compile Include="2D\Widgets\Widget.cs" />
<Compile Include="Blocks\Block.cs" />
<Compile Include="Blocks\BlockInfo.cs" />
<Compile Include="Blocks\BlockInfo.Culling.cs" />
<Compile Include="Blocks\BlockInfo.Optimised.cs" />
<Compile Include="Entities\Entity.cs" />
<Compile Include="Entities\LocalPlayer.cs" />
<Compile Include="Entities\NetPlayer.cs" />

View file

@ -1,14 +1,15 @@
using System;
using ClassicalSharp;
namespace ClassicalSharp {
namespace DefaultPlugin {
public partial class BlockInfo {
public partial class ClassicBlockInfo : BlockInfo {
bool[] hidden = new bool[BlocksCount * BlocksCount * 6];
bool[] hidden = new bool[blocksCount * blocksCount * 6];
void SetupCullingCache() {
for( byte tile = 1; tile < BlocksCount; tile++ ) {
for( byte neighbour = 1; neighbour < BlocksCount; neighbour++ ) {
for( byte tile = 1; tile < blocksCount; tile++ ) {
for( byte neighbour = 1; neighbour < blocksCount; neighbour++ ) {
bool hidden = IsHidden( tile, neighbour );
if( hidden ) {
SetHidden( tile, neighbour, TileSide.Left, true );
@ -29,11 +30,11 @@ namespace ClassicalSharp {
}
void SetHidden( byte tile, byte block, int tileSide, bool value ) {
hidden[( tile * BlocksCount + block ) * 6 + tileSide] = value;
hidden[( tile * blocksCount + block ) * 6 + tileSide] = value;
}
public bool IsFaceHidden( byte tile, byte block, int tileSide ) {
return hidden[( tile * BlocksCount + block ) * 6 + tileSide];
public override bool IsFaceHidden( byte tile, byte block, int tileSide ) {
return hidden[( tile * blocksCount + block ) * 6 + tileSide];
}
}
}

View file

@ -1,13 +1,14 @@
using System;
using ClassicalSharp;
namespace ClassicalSharp {
namespace DefaultPlugin {
public partial class BlockInfo {
public partial class ClassicBlockInfo : BlockInfo {
// The designation used is as follows:
// 0 - left 1 - right 2 - front
// 3 - back 4 - bottom 5 - top
int[] optimTextures = new int[BlocksCount * 6];
int[] optimTextures = new int[blocksCount * 6];
const int Row1 = 0, Row2 = 16, Row3 = 32, Row4 = 48,
Row5 = 64, Row6 = 80, Row7 = 96, Row8 = 112, Row9 = 128;
@ -128,7 +129,7 @@ namespace ClassicalSharp {
/// <param name="block"> Block ID. </param>
/// <param name="face">Face of the given block, see TileSide constants. </param>
/// <returns>The index of the texture within the terrain atlas.</returns>
public int GetOptimTextureLoc( byte block, int face ) {
public override int GetOptimTextureLoc( byte block, int face ) {
return optimTextures[block * 6 + face];
}
}

View file

@ -0,0 +1,157 @@
using System;
using ClassicalSharp;
namespace DefaultPlugin {
public partial class ClassicBlockInfo : BlockInfo {
public ClassicBlockInfo( Game window ) : base( window ) {
}
bool[] isTransparent = new bool[blocksCount];
bool[] isTranslucent = new bool[blocksCount];
bool[] isOpaque = new bool[blocksCount];
bool[] isSprite = new bool[blocksCount];
bool[] isLiquid = new bool[blocksCount];
float[] heights = new float[blocksCount];
bool[] blocksLight = new bool[blocksCount];
const byte blocksCount = (byte)Block.StoneBrick + 1;
public override byte BlocksCount {
get { return blocksCount; }
}
public override void Init() {
for( int tile = 1; tile < blocksCount; tile++ ) {
heights[tile] = 1f;
blocksLight[tile] = true;
isOpaque[tile] = true;
}
SetupOptimTextures();
SetIsTranslucent( Block.StillWater, Block.Water );
SetIsTransparent( Block.Glass, Block.Leaves, Block.Sapling,
Block.RedMushroom, Block.BrownMushroom, Block.Rose,
Block.Dandelion, Block.Slab );
SetIsSprite( Block.Rose, Block.Sapling, Block.Dandelion,
Block.BrownMushroom, Block.RedMushroom );
SetBlockHeight( 8 / 16f, Block.Slab );
SetBlocksLight( false, Block.Glass, Block.Leaves, Block.Sapling,
Block.RedMushroom, Block.BrownMushroom, Block.Rose,
Block.Dandelion );
SetIsLiquid( Block.StillWater, Block.Water, Block.StillLava, Block.Lava );
SetIsTransparent( Block.Snow, Block.Fire, Block.Rope, Block.CobblestoneSlab );
SetBlocksLight( false, Block.Fire, Block.Rope );
SetIsTranslucent( Block.Ice );
SetIsSprite( Block.Rope, Block.Fire );
SetBlockHeight( 8 / 16f, Block.CobblestoneSlab );
SetBlockHeight( 2 / 16f, Block.Snow );
SetupCullingCache();
}
public override void SetDefaultBlockPermissions( bool[] canPlace, bool[] canDelete ) {
for( int tile = (int)Block.Stone; tile <= (int)Block.Obsidian; tile++ ) {
canPlace[tile] = true;
canDelete[tile] = true;
}
canPlace[(int)Block.Grass] = false;
canPlace[(int)Block.Lava] = false;
canPlace[(int)Block.Water] = false;
canPlace[(int)Block.StillLava] = false;
canPlace[(int)Block.StillWater] = false;
canPlace[(int)Block.Bedrock] = false;
canDelete[(int)Block.Bedrock] = false;
canDelete[(int)Block.Lava] = false;
canDelete[(int)Block.Water] = false;
canDelete[(int)Block.StillWater] = false;
canDelete[(int)Block.StillLava] = false;
}
void SetIsTransparent( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isTransparent[(int)ids[i]] = true;
isOpaque[(int)ids[i]] = false;
}
}
void SetIsSprite( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isSprite[(int)ids[i]] = true;
}
}
void SetIsTranslucent( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isTranslucent[(int)ids[i]] = true;
isOpaque[(int)ids[i]] = false;
}
}
void SetIsLiquid( params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
isLiquid[(int)ids[i]] = true;
}
}
void SetBlockHeight( float height, params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
heights[(int)ids[i]] = height;
}
}
void SetBlocksLight( bool blocks, params Block[] ids ) {
for( int i = 0; i < ids.Length; i++ ) {
blocksLight[(int)ids[i]] = blocks;
}
}
/// <summary> Gets whether the given block id is opaque/not see through. </summary>
public override bool IsOpaque( byte id ) {
return isOpaque[id];
}
/// <summary> Gets whether the given block id is opaque/not see through,
/// and occupies a full block. </summary>
public override bool IsFullAndOpaque( byte id ) {
return isOpaque[id] && heights[id] == 1;
}
/// <summary> Gets whether the given block id is transparent/fully see through. </summary>
/// <remarks> Note that these blocks's alpha values are treated as either 'fully see through'
/// or 'fully solid'. </remarks>
public override bool IsTransparent( byte id ) {
return isTransparent[id];
}
/// <summary> Gets the tile height of the given block id. </summary>
public override float BlockHeight( byte id ) {
return heights[id];
}
/// <summary> Gets whether the given block id is translucent/partially see through. </summary>
/// <remarks> Note that these blocks's colour values are blended into both
/// the transparent and opaque blocks behind them. </remarks>
public override bool IsTranslucent( byte id ) {
return isTranslucent[id];
}
/// <summary> Gets whether the given block blocks sunlight. </summary>
public override bool BlocksLight( byte id ) {
return blocksLight[id];
}
/// <summary> Gets whether the given block id is a sprite. <br/>
/// (flowers, saplings, fire, etc) </summary>
public override bool IsSprite( byte id ) {
return isSprite[id];
}
/// <summary> Gets whether the given block id is a liquid. <br/>
/// (water or lava) </summary>
public override bool IsLiquid( byte id ) {
return isLiquid[id];
}
}
}

View file

@ -50,6 +50,7 @@ namespace DefaultPlugin {
new PluginModule( PluginModuleType.PostProcessingShader, typeof( GrayscaleFilter ) ),
new PluginModule( PluginModuleType.NetworkProcessor, typeof( ClassicNetworkProcessor ) ),
new PluginModule( PluginModuleType.BlockInfo, typeof( ClassicBlockInfo ) ),
};
}
}

View file

@ -47,6 +47,9 @@
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<Compile Include="Blocks\BlockInfo.cs" />
<Compile Include="Blocks\BlockInfo.Culling.cs" />
<Compile Include="Blocks\BlockInfo.Optimised.cs" />
<Compile Include="Network\ClassicNetwork.Writing.cs" />
<Compile Include="Network\ClassicNetwork.Reading.cs" />
<Compile Include="Network\Enums.cs" />
@ -78,6 +81,7 @@
<ItemGroup>
<Folder Include="Models" />
<Folder Include="Network" />
<Folder Include="Blocks" />
<Folder Include="Shaders" />
<Folder Include="Renderers" />
</ItemGroup>

View file

@ -1,6 +1,6 @@
using System;
namespace ClassicalSharp {
namespace DefaultPlugin.Network {
public enum PacketId {
ServerIdentification = 0,
@ -39,15 +39,4 @@ namespace ClassicalSharp {
CpeHackControl = 32,
CpeExtAddEntity2 = 33,
}
public enum CpeMessageType {
Normal = 0,
Status1 = 1,
Status2 = 2,
Status3 = 3,
BottomRight1 = 11,
BottomRight2 = 12,
BottomRight3 = 13,
Announcement = 100,
}
}

View file

@ -2,7 +2,7 @@
using System.Net.Sockets;
using System.Text;
namespace ClassicalSharp {
namespace DefaultPlugin.Network {
// Basically a much faster version of List<byte>( capacity )
internal class FastNetReader {

View file

@ -1,7 +1,7 @@
using System;
using System.IO;
namespace ClassicalSharp {
namespace DefaultPlugin.Network {
internal class FixedBufferStream : Stream {

View file

@ -1,7 +1,7 @@
using System;
using System.IO;
namespace ClassicalSharp {
namespace DefaultPlugin.Network {
internal class GZipHeaderReader {

View file

@ -1,7 +1,8 @@
using System;
using ClassicalSharp;
using ClassicalSharp.GraphicsAPI;
namespace ClassicalSharp {
namespace DefaultPlugin {
public class StandardChunkMeshBuilder : ChunkMeshBuilder {

View file

@ -29,6 +29,7 @@ namespace ClassicalSharp {
public Camera Camera;
Camera firstPersonCam, thirdPersonCam;
public BlockInfo BlockInfo;
public List<Type> BlockInfoTypes = new List<Type>();
public double accumulator;
public TerrainAtlas2D TerrainAtlas;
public SkinType DefaultPlayerSkinType;
@ -127,23 +128,6 @@ namespace ClassicalSharp {
Bitmap terrainBmp = new Bitmap( "terrain.png" );
TerrainAtlas = new TerrainAtlas2D( Graphics );
LoadAtlas( terrainBmp );
BlockInfo = new BlockInfo();
BlockInfo.Init();
BlockInfo.SetDefaultBlockPermissions( CanPlace, CanDelete );
Map = new Map( this );
LocalPlayer = new LocalPlayer( 255, this );
width = Width;
height = Height;
firstPersonCam = new FirstPersonCamera( this );
thirdPersonCam = new ThirdPersonCamera( this );
Camera = firstPersonCam;
CommandManager = new CommandManager();
CommandManager.Init( this );
SelectionManager = new SelectionManager( this );
SelectionManager.Init();
ParticleManager = new ParticleManager( this );
ParticleManager.Init();
Picking = new PickingRenderer( this );
VSync = VSyncMode.On;
Graphics.DepthTest = true;
@ -155,6 +139,13 @@ namespace ClassicalSharp {
fpsScreen = new FpsScreen( this );
fpsScreen.Init();
Culling = new FrustumCulling();
width = Width;
height = Height;
firstPersonCam = new FirstPersonCamera( this );
thirdPersonCam = new ThirdPersonCamera( this );
Camera = firstPersonCam;
CommandManager = new CommandManager();
CommandManager.Init( this );
string defaultPlugin = Path.Combine( "plugins", "defaultplugin.dll" );
if( !Directory.Exists( "plugins" ) || !File.Exists( defaultPlugin ) ) {
@ -171,6 +162,17 @@ namespace ClassicalSharp {
}
}
BlockInfo = Utils.New<BlockInfo>( BlockInfoTypes[0], this );
BlockInfo.Init();
BlockInfo.SetDefaultBlockPermissions( CanPlace, CanDelete );
Map = new Map( this );
LocalPlayer = new LocalPlayer( 255, this );
SelectionManager = new SelectionManager( this );
SelectionManager.Init();
ParticleManager = new ParticleManager( this );
ParticleManager.Init();
Picking = new PickingRenderer( this );
MapBordersRenderer = Utils.New<MapBordersRenderer>( MapBordersRendererTypes[0], this );
MapBordersRenderer.Init();
EnvRenderer = Utils.New<EnvRenderer>( EnvRendererTypes[0], this );

View file

@ -30,7 +30,7 @@ namespace ClassicalSharp.Model {
byte blockId;
if( Byte.TryParse( modelName, out blockId ) ) {
modelName = "block";
if( blockId == 0 || blockId > BlockInfo.MaxDefinedBlock )
if( blockId == 0 || blockId >= window.BlockInfo.BlocksCount )
return cache["humanoid"];
}

View file

@ -69,6 +69,10 @@ namespace ClassicalSharp.Plugins {
case PluginModuleType.NetworkProcessor:
game.NetworkProcessorTypes.Add( module.Type );
break;
case PluginModuleType.BlockInfo:
game.BlockInfoTypes.Add( module.Type );
break;
}
}
}
@ -82,6 +86,7 @@ namespace ClassicalSharp.Plugins {
MapRenderer,
PostProcessingShader,
NetworkProcessor,
BlockInfo,
}
public class PluginModule {

View file

@ -29,12 +29,10 @@ namespace ClassicalSharp {
public abstract class PerspectiveCamera : Camera {
protected Player player;
IInputDriver driver;
public PerspectiveCamera( Game window ) {
Window = window;
driver = window.InputDriver;
player = Window.LocalPlayer;
}
public override Matrix4 GetProjection() {
@ -44,6 +42,7 @@ namespace ClassicalSharp {
}
public override void GetPickedBlock( PickedPos pos ) {
Player player = Window.LocalPlayer;
Vector3 dir = Utils.GetDirectionVector( player.YawRadians, player.PitchRadians + Math.PI );
Vector3 eyePos = player.EyePosition;
float reach = Window.LocalPlayer.ReachDistance;
@ -107,6 +106,7 @@ namespace ClassicalSharp {
}
public override Matrix4 GetView() {
Player player = Window.LocalPlayer;
Vector3 eyePos = player.EyePosition;
Vector3 cameraPos = eyePos - Utils.GetDirectionVector( player.YawRadians, player.PitchRadians + Math.PI ) * distance;
return Matrix4.LookAt( cameraPos, eyePos, Vector3.UnitY );
@ -123,6 +123,7 @@ namespace ClassicalSharp {
}
public override Matrix4 GetView() {
Player player = Window.LocalPlayer;
Vector3 eyePos = player.EyePosition;
Vector3 cameraDir = Utils.GetDirectionVector( player.YawRadians, player.PitchRadians + Math.PI );
return Matrix4.LookAt( eyePos, eyePos + cameraDir, Vector3.UnitY );