Implement wheat farming

This includes farmland behavior as well as the growth of wheat crops
This commit is contained in:
Drew DeVault 2015-02-08 17:21:35 -07:00
parent e637b009a1
commit a64c943997
8 changed files with 112 additions and 0 deletions

View File

@ -10,6 +10,7 @@ namespace TrueCraft.API.World
public interface IWorld public interface IWorld
{ {
string Name { get; set; } string Name { get; set; }
IBlockRepository BlockRepository { get; set; }
IChunkProvider ChunkProvider { get; set; } IChunkProvider ChunkProvider { get; set; }
long Time { get; set; } long Time { get; set; }

View File

@ -36,6 +36,7 @@ namespace TrueCraft.Core.Logic
var items = GetDrop(descriptor); var items = GetDrop(descriptor);
foreach (var item in items) foreach (var item in items)
{ {
if (item.Empty) continue;
var entity = new ItemEntity(new Vector3(descriptor.Coordinates) + new Vector3(0.5), item); var entity = new ItemEntity(new Vector3(descriptor.Coordinates) + new Vector3(0.5), item);
entityManager.SpawnEntity(entity); entityManager.SpawnEntity(entity);
} }

View File

@ -1,5 +1,10 @@
using System; using System;
using TrueCraft.API.Logic; using TrueCraft.API.Logic;
using TrueCraft.API;
using TrueCraft.Core.Logic.Items;
using TrueCraft.API.Networking;
using TrueCraft.API.World;
using TrueCraft.API.Server;
namespace TrueCraft.Core.Logic.Blocks namespace TrueCraft.Core.Logic.Blocks
{ {
@ -23,5 +28,42 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
return new Tuple<int, int>(8, 5); return new Tuple<int, int>(8, 5);
} }
protected override ItemStack[] GetDrop(BlockDescriptor descriptor)
{
if (descriptor.Metadata >= 7)
return new[] { new ItemStack(WheatItem.ItemID), new ItemStack(SeedsItem.ItemID, (sbyte)MathHelper.Random.Next(3)) };
else
return new[] { new ItemStack(SeedsItem.ItemID) };
}
private void GrowBlock(IMultiplayerServer server, IWorld world, Coordinates3D coords)
{
if (world.GetBlockID(coords) != BlockID)
return;
var meta = world.GetMetadata(coords);
meta++;
world.SetMetadata(coords, meta);
if (meta < 7)
{
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(MathHelper.Random.Next(30, 60)),
(_server) => GrowBlock(_server, world, coords));
}
}
public override void BlockUpdate(BlockDescriptor descriptor, IMultiplayerServer server, IWorld world)
{
if (world.GetBlockID(descriptor.Coordinates + Coordinates3D.Down) != FarmlandBlock.BlockID)
{
GenerateDropEntity(descriptor, world, server);
world.SetBlockID(descriptor.Coordinates, 0);
}
}
public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{
user.Server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(MathHelper.Random.Next(30, 60)),
(server) => GrowBlock(server, world, descriptor.Coordinates + MathHelper.BlockFaceToCoordinates(face)));
}
} }
} }

View File

@ -1,5 +1,9 @@
using System; using System;
using TrueCraft.API.Logic; using TrueCraft.API.Logic;
using TrueCraft.API.Networking;
using TrueCraft.API.World;
using TrueCraft.API;
using TrueCraft.API.Server;
namespace TrueCraft.Core.Logic.Blocks namespace TrueCraft.Core.Logic.Blocks
{ {
@ -25,5 +29,51 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
return new Tuple<int, int>(7, 5); return new Tuple<int, int>(7, 5);
} }
public bool IsHydrated(Coordinates3D coordinates, IWorld world)
{
var min = new Coordinates3D(-6 + coordinates.X, coordinates.Y, -6 + coordinates.Z);
var max = new Coordinates3D(6 + coordinates.X, coordinates.Y + 1, 6 + coordinates.Z);
for (int x = min.X; x < max.X; x++)
{
for (int y = min.Y; y < max.Y; y++) // TODO: This does not check one above the farmland block for some reason
{
for (int z = min.Z; z < max.Z; z++)
{
var id = world.GetBlockID(new Coordinates3D(x, y, z));
if (id == WaterBlock.BlockID || id == StationaryWaterBlock.BlockID)
return true;
}
}
}
return false;
}
void HydrationCheckEvent(IMultiplayerServer server, Coordinates3D coords, IWorld world)
{
if (world.GetBlockID(coords) != BlockID)
return;
if (MathHelper.Random.Next(3) == 0)
{
if (IsHydrated(coords, world))
{
world.SetMetadata(coords, 15);
}
else
{
world.SetBlockID(coords, DirtBlock.BlockID);
}
}
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(30), (_server) => HydrationCheckEvent(_server, coords, world));
}
public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{
if (IsHydrated(descriptor.Coordinates, world))
{
world.SetMetadata(descriptor.Coordinates, 15);
}
user.Server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(30), (server) => HydrationCheckEvent(server, descriptor.Coordinates, world));
}
} }
} }

View File

@ -67,6 +67,8 @@ namespace TrueCraft.Core.Logic.Items
if (id == DirtBlock.BlockID || id == GrassBlock.BlockID) if (id == DirtBlock.BlockID || id == GrassBlock.BlockID)
{ {
world.SetBlockID(coordinates, FarmlandBlock.BlockID); world.SetBlockID(coordinates, FarmlandBlock.BlockID);
user.Server.BlockRepository.GetBlockProvider(FarmlandBlock.BlockID).BlockPlaced(
new BlockDescriptor { Coordinates = coordinates }, face, world, user);
} }
} }
} }

View File

@ -1,5 +1,9 @@
using System; using System;
using TrueCraft.API.Logic; using TrueCraft.API.Logic;
using TrueCraft.API;
using TrueCraft.API.World;
using TrueCraft.API.Networking;
using TrueCraft.Core.Logic.Blocks;
namespace TrueCraft.Core.Logic.Items namespace TrueCraft.Core.Logic.Items
{ {
@ -10,5 +14,15 @@ namespace TrueCraft.Core.Logic.Items
public override short ID { get { return 0x127; } } public override short ID { get { return 0x127; } }
public override string DisplayName { get { return "Seeds"; } } public override string DisplayName { get { return "Seeds"; } }
public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
{
if (world.GetBlockID(coordinates) == FarmlandBlock.BlockID)
{
world.SetBlockID(coordinates + MathHelper.BlockFaceToCoordinates(face), CropsBlock.BlockID);
world.BlockRepository.GetBlockProvider(CropsBlock.BlockID).BlockPlaced(
new BlockDescriptor { Coordinates = coordinates }, face, world, user);
}
}
} }
} }

View File

@ -17,6 +17,7 @@ namespace TrueCraft.Core.World
public string BaseDirectory { get; internal set; } public string BaseDirectory { get; internal set; }
public IDictionary<Coordinates2D, IRegion> Regions { get; set; } public IDictionary<Coordinates2D, IRegion> Regions { get; set; }
public IChunkProvider ChunkProvider { get; set; } public IChunkProvider ChunkProvider { get; set; }
public IBlockRepository BlockRepository { get; set; }
public DateTime BaseTime { get; set; } public DateTime BaseTime { get; set; }
public long Time public long Time
{ {

View File

@ -107,6 +107,7 @@ namespace TrueCraft
public void AddWorld(IWorld world) public void AddWorld(IWorld world)
{ {
Worlds.Add(world); Worlds.Add(world);
world.BlockRepository = BlockRepository;
world.BlockChanged += HandleBlockChanged; world.BlockChanged += HandleBlockChanged;
var manager = new EntityManager(this, world); var manager = new EntityManager(this, world);
EntityManagers.Add(manager); EntityManagers.Add(manager);