Adding CustomBlocks example to demonstrate registering new blocks

This commit is contained in:
Justin Aquadro 2011-10-09 16:58:14 -04:00
parent 14657bf4a9
commit b1d4201679
4 changed files with 187 additions and 0 deletions

View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B01AD681-31A7-43A0-A84B-F839FBCB2F01}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CustomBlocks</RootNamespace>
<AssemblyName>CustomBlocks</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Substrate">
<HintPath>..\..\bin\Release\Substrate.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -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 <world>");
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.
}
}
}

View file

@ -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")]

View file

@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PurgeEntities", "PurgeEntit
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoodyChest", "GoodyChest\GoodyChest.csproj", "{6A998912-C939-4029-9F1D-D5C2A5E9C804}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoodyChest", "GoodyChest\GoodyChest.csproj", "{6A998912-C939-4029-9F1D-D5C2A5E9C804}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomBlocks", "CustomBlocks\CustomBlocks.csproj", "{B01AD681-31A7-43A0-A84B-F839FBCB2F01}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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|Mixed Platforms.Build.0 = Release|x86
{6A998912-C939-4029-9F1D-D5C2A5E9C804}.Release|x86.ActiveCfg = Release|x86 {6A998912-C939-4029-9F1D-D5C2A5E9C804}.Release|x86.ActiveCfg = Release|x86
{6A998912-C939-4029-9F1D-D5C2A5E9C804}.Release|x86.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE