From b1d4201679a3e96cd890844ab5b75c3180f687b1 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 9 Oct 2011 16:58:14 -0400 Subject: [PATCH] Adding CustomBlocks example to demonstrate registering new blocks --- .../Examples/CustomBlocks/CustomBlocks.csproj | 55 ++++++++++++ SubstrateCS/Examples/CustomBlocks/Program.cs | 84 +++++++++++++++++++ .../CustomBlocks/Properties/AssemblyInfo.cs | 36 ++++++++ SubstrateCS/Examples/Examples.sln | 12 +++ 4 files changed, 187 insertions(+) create mode 100644 SubstrateCS/Examples/CustomBlocks/CustomBlocks.csproj create mode 100644 SubstrateCS/Examples/CustomBlocks/Program.cs create mode 100644 SubstrateCS/Examples/CustomBlocks/Properties/AssemblyInfo.cs diff --git a/SubstrateCS/Examples/CustomBlocks/CustomBlocks.csproj b/SubstrateCS/Examples/CustomBlocks/CustomBlocks.csproj new file mode 100644 index 0000000..f960b3b --- /dev/null +++ b/SubstrateCS/Examples/CustomBlocks/CustomBlocks.csproj @@ -0,0 +1,55 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01} + Exe + Properties + CustomBlocks + CustomBlocks + v2.0 + + + 512 + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\bin\Release\Substrate.dll + + + + + + + + + + \ No newline at end of file diff --git a/SubstrateCS/Examples/CustomBlocks/Program.cs b/SubstrateCS/Examples/CustomBlocks/Program.cs new file mode 100644 index 0000000..640eb9a --- /dev/null +++ b/SubstrateCS/Examples/CustomBlocks/Program.cs @@ -0,0 +1,84 @@ +using System; +using Substrate; +using Substrate.Core; + +// This example demonstrates adding explicit recognization of custom block types +// to your program (in this case, the LightSensor block form Risugami). This +// program will scan a world and print out each instance of a LightSensor block +// that is found. + +// The real usefulness in explicitly registering custom blocks that appear in +// your world is that you can properly support algorithms that depend on block's +// innate properties such as state of matter, opacity, and luminance. This data +// lets the Substrate lighting and fluid calculations behave as expected. + +namespace CustomBlocks +{ + class Program + { + static void Main (string[] args) + { + if (args.Length != 1) { + Console.WriteLine("Usage: CustomBlock "); + return; + } + + string dest = args[0]; + + // Open our world + BetaWorld world = BetaWorld.Open(dest); + + // The chunk manager is more efficient than the block manager for + // this purpose, since we'll inspect every block + BetaChunkManager cm = world.GetChunkManager(); + + foreach (ChunkRef chunk in cm) { + // You could hardcode your dimensions, but maybe some day they + // won't always be 16. Also the CLR is a bit stupid and has + // trouble optimizing repeated calls to Chunk.Blocks.xx, so we + // cache them in locals + int xdim = chunk.Blocks.XDim; + int ydim = chunk.Blocks.YDim; + int zdim = chunk.Blocks.ZDim; + + chunk.Blocks.AutoFluid = true; + + // x, z, y is the most efficient order to scan blocks (not that + // you should care about internal detail) + for (int x = 0; x < xdim; x++) { + for (int z = 0; z < zdim; z++) { + for (int y = 0; y < ydim; y++) { + BlockInfo info = chunk.Blocks.GetInfo(x, y, z); + if (info.ID == BlockInfoM.LightSensor.ID) { + Console.WriteLine("Found custom block '{0}' at {1}", info.Name, new BlockKey(x, y, z)); + } + } + } + } + } + } + } + + // Convenience class -- like the BlockType class, it's not required that you define this + static class BlockTypeM + { + public static int LIGHT_SENSOR = 131; + } + + // A place to store a global instance of a LightSensor block, for our convenience. + static class BlockInfoM + { + public static BlockInfo LightSensor; + + static BlockInfoM () + { + // Creating a BlockInfo (or BlockInfoEx) will also automatically register the + // block ID, lighting, and opacity data with internal tables in the BlockInfo + // static class, making them available to GetInfo() calls. + LightSensor = new BlockInfo(BlockTypeM.LIGHT_SENSOR, "Light Sensor").SetOpacity(0).SetState(BlockState.NONSOLID); + + // You can redefine already-registered blocks at any time by creating a new + // BlockInfo object with the given ID. + } + } +} diff --git a/SubstrateCS/Examples/CustomBlocks/Properties/AssemblyInfo.cs b/SubstrateCS/Examples/CustomBlocks/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..51efa14 --- /dev/null +++ b/SubstrateCS/Examples/CustomBlocks/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CustomBlocks")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("CustomBlocks")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("5b6300c0-2930-47df-9281-31cf7885044e")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SubstrateCS/Examples/Examples.sln b/SubstrateCS/Examples/Examples.sln index 64a7d8c..da154b0 100644 --- a/SubstrateCS/Examples/Examples.sln +++ b/SubstrateCS/Examples/Examples.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PurgeEntities", "PurgeEntit EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoodyChest", "GoodyChest\GoodyChest.csproj", "{6A998912-C939-4029-9F1D-D5C2A5E9C804}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomBlocks", "CustomBlocks\CustomBlocks.csproj", "{B01AD681-31A7-43A0-A84B-F839FBCB2F01}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -119,6 +121,16 @@ Global {6A998912-C939-4029-9F1D-D5C2A5E9C804}.Release|Mixed Platforms.Build.0 = Release|x86 {6A998912-C939-4029-9F1D-D5C2A5E9C804}.Release|x86.ActiveCfg = Release|x86 {6A998912-C939-4029-9F1D-D5C2A5E9C804}.Release|x86.Build.0 = Release|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Debug|Any CPU.ActiveCfg = Debug|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Debug|Mixed Platforms.Build.0 = Debug|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Debug|x86.ActiveCfg = Debug|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Debug|x86.Build.0 = Debug|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Release|Any CPU.ActiveCfg = Release|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Release|Mixed Platforms.ActiveCfg = Release|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Release|Mixed Platforms.Build.0 = Release|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Release|x86.ActiveCfg = Release|x86 + {B01AD681-31A7-43A0-A84B-F839FBCB2F01}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE