From 334ce12773a19c997ec8b589ab6997e1e3a15bb3 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 6 Apr 2011 21:18:48 +0000 Subject: [PATCH] Additional entities. Additional compound schema constructor. --- .../Source/Entities/EntityArrow.cs | 46 ++++++ .../SubstrateCS/Source/Entities/EntityEgg.cs | 46 ++++++ .../SubstrateCS/Source/Entities/EntityItem.cs | 103 ++++++++++++++ .../Source/Entities/EntityPainting.cs | 131 +++++++++++++++++ .../Source/Entities/EntitySnowball.cs | 46 ++++++ .../Source/Entities/EntityThrowable.cs | 133 ++++++++++++++++++ Substrate/SubstrateCS/Source/EntityFactory.cs | 5 + Substrate/SubstrateCS/Source/NBT/NBTSchema.cs | 14 ++ 8 files changed, 524 insertions(+) create mode 100644 Substrate/SubstrateCS/Source/Entities/EntityArrow.cs create mode 100644 Substrate/SubstrateCS/Source/Entities/EntityEgg.cs create mode 100644 Substrate/SubstrateCS/Source/Entities/EntityItem.cs create mode 100644 Substrate/SubstrateCS/Source/Entities/EntityPainting.cs create mode 100644 Substrate/SubstrateCS/Source/Entities/EntitySnowball.cs create mode 100644 Substrate/SubstrateCS/Source/Entities/EntityThrowable.cs diff --git a/Substrate/SubstrateCS/Source/Entities/EntityArrow.cs b/Substrate/SubstrateCS/Source/Entities/EntityArrow.cs new file mode 100644 index 0000000..0f67c1f --- /dev/null +++ b/Substrate/SubstrateCS/Source/Entities/EntityArrow.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate.Map.Entities +{ + using Substrate.Map.NBT; + + public class EntityArrow : EntityThrowable + { + public static readonly NBTCompoundNode ArrowSchema = ThrowableSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Arrow"), + }); + + public EntityArrow () + : base("Arrow") + { + } + + public EntityArrow (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, ArrowSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityArrow(this); + } + + #endregion + } +} diff --git a/Substrate/SubstrateCS/Source/Entities/EntityEgg.cs b/Substrate/SubstrateCS/Source/Entities/EntityEgg.cs new file mode 100644 index 0000000..63baffc --- /dev/null +++ b/Substrate/SubstrateCS/Source/Entities/EntityEgg.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate.Map.Entities +{ + using Substrate.Map.NBT; + + public class EntityEgg : EntityThrowable + { + public static readonly NBTCompoundNode EggSchema = ThrowableSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Egg"), + }); + + public EntityEgg () + : base("Egg") + { + } + + public EntityEgg (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, EggSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityEgg(this); + } + + #endregion + } +} \ No newline at end of file diff --git a/Substrate/SubstrateCS/Source/Entities/EntityItem.cs b/Substrate/SubstrateCS/Source/Entities/EntityItem.cs new file mode 100644 index 0000000..038bafe --- /dev/null +++ b/Substrate/SubstrateCS/Source/Entities/EntityItem.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate.Map.Entities +{ + using Substrate.Map.NBT; + + public class EntityItem : Entity + { + public static readonly NBTCompoundNode ItemSchema = BaseSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Item"), + new NBTScalerNode("Health", NBT_Type.TAG_SHORT), + new NBTScalerNode("Age", NBT_Type.TAG_SHORT), + new NBTCompoundNode("Item", Item.ItemSchema), + }); + + private short _health; + private short _age; + + private Item _item; + + public int Health + { + get { return _health; } + set { _health = (short)value; } + } + + public int Age + { + get { return _age; } + set { _age = (short)value; } + } + + public Item Item + { + get { return _item; } + set { _item = value; } + } + + public EntityItem () + : base("Item") + { + } + + public EntityItem (Entity e) + : base(e) + { + EntityItem e2 = e as EntityItem; + if (e2 != null) { + _health = e2._health; + _age = e2._age; + _item = e2._item.Copy(); + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _health = ctree["Health"].ToNBTShort(); + _age = ctree["Age"].ToNBTShort(); + + _item = new Item().LoadTree(ctree["Item"]); + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["Health"] = new NBT_Short(_health); + tree["Age"] = new NBT_Short(_age); + tree["Item"] = _item.BuildTree(); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, ItemSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityItem(this); + } + + #endregion + } +} diff --git a/Substrate/SubstrateCS/Source/Entities/EntityPainting.cs b/Substrate/SubstrateCS/Source/Entities/EntityPainting.cs new file mode 100644 index 0000000..1de086f --- /dev/null +++ b/Substrate/SubstrateCS/Source/Entities/EntityPainting.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate.Map.Entities +{ + using Substrate.Map.NBT; + + public class EntityPainting : Entity + { + public enum Direction + { + EAST = 0, + NORTH = 1, + WEST = 2, + SOUTH = 3, + } + + public static readonly NBTCompoundNode PaintingSchema = BaseSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Painting"), + new NBTScalerNode("Dir", NBT_Type.TAG_BYTE), + new NBTScalerNode("TileX", NBT_Type.TAG_SHORT), + new NBTScalerNode("TileY", NBT_Type.TAG_SHORT), + new NBTScalerNode("TileZ", NBT_Type.TAG_SHORT), + new NBTScalerNode("Motive", NBT_Type.TAG_STRING), + }); + + private Direction _dir; + private string _motive; + private short _xTile; + private short _yTile; + private short _zTile; + + public Direction Direction + { + get { return _dir; } + set { _dir = value; } + } + + public string Motive + { + get { return _motive; } + set { _motive = value; } + } + + public int TileX + { + get { return _xTile; } + set { _xTile = (short)value; } + } + + public int TileY + { + get { return _yTile; } + set { _yTile = (short)value; } + } + + public int TileZ + { + get { return _zTile; } + set { _zTile = (short)value; } + } + + public EntityPainting () + : base("Painting") + { + } + + public EntityPainting (Entity e) + : base(e) + { + EntityPainting e2 = e as EntityPainting; + if (e2 != null) { + _xTile = e2._xTile; + _yTile = e2._yTile; + _zTile = e2._zTile; + _dir = e2._dir; + _motive = e2._motive; + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _dir = (Direction) ctree["Dir"].ToNBTByte().Data; + _motive = ctree["Motive"].ToNBTString(); + _xTile = ctree["TileX"].ToNBTShort(); + _yTile = ctree["TileY"].ToNBTShort(); + _zTile = ctree["TileZ"].ToNBTShort(); + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["Dir"] = new NBT_Byte((byte)_dir); + tree["Motive"] = new NBT_String(_motive); + tree["TileX"] = new NBT_Short(_xTile); + tree["TileY"] = new NBT_Short(_yTile); + tree["TileZ"] = new NBT_Short(_zTile); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, PaintingSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityPainting(this); + } + + #endregion + } +} diff --git a/Substrate/SubstrateCS/Source/Entities/EntitySnowball.cs b/Substrate/SubstrateCS/Source/Entities/EntitySnowball.cs new file mode 100644 index 0000000..1e78136 --- /dev/null +++ b/Substrate/SubstrateCS/Source/Entities/EntitySnowball.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate.Map.Entities +{ + using Substrate.Map.NBT; + + public class EntitySnowball : EntityThrowable + { + public static readonly NBTCompoundNode SnowballSchema = ThrowableSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Snowball"), + }); + + public EntitySnowball () + : base("Snowball") + { + } + + public EntitySnowball (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, SnowballSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntitySnowball(this); + } + + #endregion + } +} diff --git a/Substrate/SubstrateCS/Source/Entities/EntityThrowable.cs b/Substrate/SubstrateCS/Source/Entities/EntityThrowable.cs new file mode 100644 index 0000000..2a5e136 --- /dev/null +++ b/Substrate/SubstrateCS/Source/Entities/EntityThrowable.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate.Map.Entities +{ + using Substrate.Map.NBT; + + public class EntityThrowable : Entity + { + public static readonly NBTCompoundNode ThrowableSchema = BaseSchema.MergeInto(new NBTCompoundNode("") + { + new NBTScalerNode("xTile", NBT_Type.TAG_SHORT), + new NBTScalerNode("yTile", NBT_Type.TAG_SHORT), + new NBTScalerNode("zTile", NBT_Type.TAG_SHORT), + new NBTScalerNode("inTile", NBT_Type.TAG_BYTE), + new NBTScalerNode("shake", NBT_Type.TAG_BYTE), + new NBTScalerNode("inGround", NBT_Type.TAG_BYTE), + }); + + private short _xTile; + private short _yTile; + private short _zTile; + private byte _inTile; + private byte _shake; + private byte _inGround; + + public int XTile + { + get { return _xTile; } + set { _xTile = (short)value; } + } + + public int YTile + { + get { return _yTile; } + set { _yTile = (short)value; } + } + + public int ZTile + { + get { return _zTile; } + set { _zTile = (short)value; } + } + + public bool IsInTile + { + get { return _inTile == 1; } + set { _inTile = (byte)(value ? 1 : 0); } + } + + public int Shake + { + get { return _shake; } + set { _shake = (byte)value; } + } + + public bool IsInGround + { + get { return _inGround == 1; } + set { _inGround = (byte)(value ? 1 : 0); } + } + + public EntityThrowable (string id) + : base(id) + { + } + + public EntityThrowable (Entity e) + : base(e) + { + EntityThrowable e2 = e as EntityThrowable; + if (e2 != null) { + _xTile = e2._xTile; + _yTile = e2._yTile; + _zTile = e2._zTile; + _inTile = e2._inTile; + _inGround = e2._inGround; + _shake = e2._shake; + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _xTile = ctree["xTile"].ToNBTShort(); + _yTile = ctree["yTile"].ToNBTShort(); + _zTile = ctree["zTile"].ToNBTShort(); + _inTile = ctree["inTile"].ToNBTByte(); + _shake = ctree["shake"].ToNBTByte(); + _inGround = ctree["inGround"].ToNBTByte(); + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["xTile"] = new NBT_Short(_xTile); + tree["yTile"] = new NBT_Short(_yTile); + tree["zTile"] = new NBT_Short(_zTile); + tree["inTile"] = new NBT_Byte(_inTile); + tree["shake"] = new NBT_Byte(_shake); + tree["inGround"] = new NBT_Byte(_inGround); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, ThrowableSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityThrowable(this); + } + + #endregion + } +} diff --git a/Substrate/SubstrateCS/Source/EntityFactory.cs b/Substrate/SubstrateCS/Source/EntityFactory.cs index 47cc52c..797681f 100644 --- a/Substrate/SubstrateCS/Source/EntityFactory.cs +++ b/Substrate/SubstrateCS/Source/EntityFactory.cs @@ -54,18 +54,23 @@ namespace Substrate.Map { _registry = new Dictionary(); + _registry["Arrow"] = typeof(EntityArrow); _registry["Chicken"] = typeof(EntityChicken); _registry["Cow"] = typeof(EntityCow); _registry["Creeper"] = typeof(EntityCreeper); + _registry["Egg"] = typeof(EntityEgg); _registry["Ghast"] = typeof(EntityGhast); _registry["Giant"] = typeof(EntityGiant); + _registry["Item"] = typeof(EntityItem); _registry["Mob"] = typeof(EntityMob); _registry["Monster"] = typeof(EntityMonster); + _registry["Painting"] = typeof(EntityPainting); _registry["Pig"] = typeof(EntityPig); _registry["PigZombie"] = typeof(EntityPigZombie); _registry["Sheep"] = typeof(EntitySheep); _registry["Skeleton"] = typeof(EntitySkeleton); _registry["Slime"] = typeof(EntitySlime); + _registry["Snowball"] = typeof(EntitySnowball); _registry["Spider"] = typeof(EntitySpider); _registry["Wolf"] = typeof(EntityWolf); _registry["Zombie"] = typeof(EntityZombie); diff --git a/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs b/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs index 806bbbf..d80731a 100644 --- a/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs +++ b/Substrate/SubstrateCS/Source/NBT/NBTSchema.cs @@ -269,6 +269,20 @@ namespace Substrate.Map.NBT _subnodes = new List(); } + public NBTCompoundNode (string name, NBTSchemaNode subschema) + : base(name) + { + NBTCompoundNode schema = subschema as NBTCompoundNode; + if (schema == null) { + return; + } + + foreach (NBTSchemaNode node in schema._subnodes) + { + _subnodes.Add(node); + } + } + public NBTCompoundNode MergeInto (NBTCompoundNode tree) { foreach (NBTSchemaNode node in _subnodes) {