Esempio n. 1
0
 public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
 {
     coordinates += MathHelper.BlockFaceToCoordinates(face);
     var descriptor = world.GetBlockData(coordinates);
     LadderDirection direction;
     switch (MathHelper.DirectionByRotationFlat(user.Entity.Yaw))
     {
         case Direction.North:
             direction = LadderDirection.North;
             break;
         case Direction.South:
             direction = LadderDirection.South;
             break;
         case Direction.East:
             direction = LadderDirection.East;
             break;
         default:
             direction = LadderDirection.West;
             break;
     }
     descriptor.Metadata = (byte)direction;
     if (IsSupported(descriptor, user.Server, world))
     {
         world.SetBlockID(descriptor.Coordinates, BlockID);
         world.SetMetadata(descriptor.Coordinates, (byte)direction);
         item.Count--;
         user.Inventory[user.SelectedSlot] = item;
     }
 }
Esempio n. 2
0
        public FurnaceWindow(IEventScheduler scheduler, Coordinates3D coordinates,
            IItemRepository itemRepository, InventoryWindow inventory)
        {
            ItemRepository = itemRepository;
            EventScheduler = scheduler;
            Coordinates = coordinates;

            WindowAreas = new[]
                {
                    new WindowArea(IngredientIndex, 1, 1, 1),
                    new WindowArea(FuelIndex, 1, 1, 1),
                    new WindowArea(OutputIndex, 1, 1, 1),
                    new WindowArea(MainIndex, 27, 9, 3),
                    new WindowArea(HotbarIndex, 9, 9, 1)
                };
            inventory.MainInventory.CopyTo(MainInventory);
            inventory.Hotbar.CopyTo(Hotbar);
            foreach (var area in WindowAreas)
                area.WindowChange += (s, e) => OnWindowChange(new WindowChangeEventArgs(
                    (s as WindowArea).StartIndex + e.SlotIndex, e.Value));
            Copying = false;
            inventory.WindowChange += (sender, e) =>
            {
                if (Copying) return;
                if ((e.SlotIndex >= InventoryWindow.MainIndex && e.SlotIndex < InventoryWindow.MainIndex + inventory.MainInventory.Length)
                    || (e.SlotIndex >= InventoryWindow.HotbarIndex && e.SlotIndex < InventoryWindow.HotbarIndex + inventory.Hotbar.Length))
                {
                    inventory.MainInventory.CopyTo(MainInventory);
                    inventory.Hotbar.CopyTo(Hotbar);
                }
            };
        }
Esempio n. 3
0
        public override bool GenerateAt(IWorld world, IChunk chunk, Coordinates3D location)
        {
            if (!ValidLocation(location))
            {
                return(false);
            }

            var random = new Random(world.Seed);
            var height = random.Next(7, 8);

            GenerateColumn(chunk, location, height, WoodBlock.BlockId, 0x1);
            for (var y = 1; y < height; y++)
            {
                if (y % 2 == 0)
                {
                    GenerateVanillaCircle(chunk, location + new Coordinates3D(0, y + 1, 0), LeafRadius - 1,
                                          LeavesBlock.BlockId, 0x1);
                    continue;
                }

                GenerateVanillaCircle(chunk, location + new Coordinates3D(0, y + 1, 0), LeafRadius, LeavesBlock.BlockId,
                                      0x1);
            }

            GenerateTopper(chunk, location + new Coordinates3D(0, height, 0), 0x1);
            return(true);
        }
Esempio n. 4
0
 public void Update(IMobEntity entity, IEntityManager manager)
 {
     var cast = entity as IEntity;
     if (entity.CurrentPath != null)
         entity.AdvancePath(manager.TimeSinceLastUpdate);
     else
     {
         if (MathHelper.Random.Next(IdleChance) == 0)
         {
             var target = new Coordinates3D(
                 (int)(cast.Position.X + (MathHelper.Random.Next(Distance) - Distance / 2)),
                 0,
                 (int)(cast.Position.Z + (MathHelper.Random.Next(Distance) - Distance / 2))
             );
             IChunk chunk;
             var adjusted = entity.World.FindBlockPosition(target, out chunk, generate: false);
             target.Y = chunk.GetHeight((byte)adjusted.X, (byte)adjusted.Z) + 1;
             Task.Factory.StartNew(() =>
             {
                     entity.CurrentPath = PathFinder.FindPath(entity.World, entity.BoundingBox,
                         (Coordinates3D)cast.Position, target);
             });
         }
     }
 }
Esempio n. 5
0
        public void SetTileEntity(Coordinates3D coordinates, NbtCompound value)
        {
            IChunk chunk;

            coordinates = FindBlockPosition(coordinates, out chunk);
            chunk.SetTileEntity(coordinates, value);
        }
Esempio n. 6
0
        public NbtCompound GetTileEntity(Coordinates3D coordinates)
        {
            IChunk chunk;

            coordinates = FindBlockPosition(coordinates, out chunk);
            return(chunk.GetTileEntity(coordinates));
        }
Esempio n. 7
0
        public BlockDescriptor GetBlockData(Coordinates3D coordinates)
        {
            IChunk chunk;
            var    adjustedCoordinates = FindBlockPosition(coordinates, out chunk);

            return(GetBlockDataFromChunk(adjustedCoordinates, chunk, coordinates));
        }
Esempio n. 8
0
        public static bool NeighboursBlock(IChunk chunk, Coordinates3D location, byte block, byte meta = 0x0)
        {
            var surrounding = new[]
            {
                location + Coordinates3D.Left,
                location + Coordinates3D.Right,
                location + Coordinates3D.Forwards,
                location + Coordinates3D.Backwards
            };

            for (var i = 0; i < surrounding.Length; i++)
            {
                var toCheck = surrounding[i];
                if (toCheck.X < 0 || toCheck.X >= Chunk.Width || toCheck.Z < 0 || toCheck.Z >= Chunk.Depth ||
                    toCheck.Y < 0 || toCheck.Y >= Chunk.Height)
                {
                    return(false);
                }
                if (chunk.GetBlockID(toCheck).Equals(block))
                {
                    if (meta != 0x0 && chunk.GetMetadata(toCheck) != meta)
                    {
                        return(false);
                    }
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 9
0
        private void UpdateHeightMap(Coordinates3D coords)
        {
            IChunk chunk;
            var    adjusted = World.FindBlockPosition(coords, out chunk, generate: false);

            if (!HeightMaps.ContainsKey(chunk.Coordinates))
            {
                return;
            }
            var           map = HeightMaps[chunk.Coordinates];
            byte          x = (byte)adjusted.X; byte z = (byte)adjusted.Z;
            Coordinates3D _;

            for (byte y = (byte)(chunk.GetHeight(x, z) + 2); y > 0; y--)
            {
                if (y >= Chunk.Height)
                {
                    continue;
                }
                _.X = x; _.Y = y - 1; _.Z = z;
                var id = chunk.GetBlockID(_);
                if (id == 0)
                {
                    continue;
                }
                var provider = BlockRepository.GetBlockProvider(id);
                if (provider.LightOpacity != 0)
                {
                    map[x, z] = y;
                    break;
                }
            }
        }
Esempio n. 10
0
 public static void SetBlockInfo(this World world, Coordinates3D coordinates, BlockInfo info)
 {
     world.SetBlockId(coordinates, info.BlockId);
     world.SetMetadata(coordinates, info.Metadata);
     world.SetSkyLight(coordinates, info.SkyLight);
     world.SetBlockLight(coordinates, info.BlockLight);
 }
Esempio n. 11
0
        protected static void GenerateSphere(IChunk chunk, Coordinates3D location, int radius, byte block,
                                             byte meta = 0x0)
        {
            for (var i = -radius; i <= radius; i = i + 1)
            {
                for (var j = -radius; j <= radius; j = j + 1)
                {
                    for (var k = -radius; k <= radius; k = k + 1)
                    {
                        var max = (int)Math.Sqrt(i * i + j * j + k * k);
                        if (max <= radius)
                        {
                            var x = location.X + i;
                            var y = location.Y + k;
                            var z = location.Z + j;

                            if (x < 0 || x >= Chunk.Width || z < 0 || z >= Chunk.Depth || y < 0 || y >= Chunk.Height)
                            {
                                continue;
                            }

                            var currentBlock = new Coordinates3D(x, y, z);
                            if (chunk.GetBlockID(currentBlock).Equals(0))
                            {
                                chunk.SetBlockID(currentBlock, block);
                                chunk.SetMetadata(currentBlock, meta);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 12
0
        public void DestroyCactus(BlockDescriptor descriptor, IMultiplayerServer server, IWorld world)
        {
            var toDrop = 0;

            // Search upwards
            for (int y = descriptor.Coordinates.Y; y < 127; y++)
            {
                var coordinates = new Coordinates3D(descriptor.Coordinates.X, y, descriptor.Coordinates.Z);
                if (world.GetBlockID(coordinates) == CactusBlock.BlockID)
                {
                    world.SetBlockID(coordinates, AirBlock.BlockID);
                    toDrop++;
                }
            }

            // Search downwards.
            for (int y = descriptor.Coordinates.Y - 1; y > 0; y--)
            {
                var coordinates = new Coordinates3D(descriptor.Coordinates.X, y, descriptor.Coordinates.Z);
                if (world.GetBlockID(coordinates) == CactusBlock.BlockID)
                {
                    world.SetBlockID(coordinates, AirBlock.BlockID);
                    toDrop++;
                }
            }

            var manager = server.GetEntityManagerForWorld(world);

            manager.SpawnEntity(
                new ItemEntity(descriptor.Coordinates + Coordinates3D.Up,
                               new ItemStack(CactusBlock.BlockID, (sbyte)toDrop)));
        }
Esempio n. 13
0
        public bool ValidCactusPosition(BlockDescriptor descriptor, IBlockRepository repository, IWorld world, bool checkNeighbor = true, bool checkSupport = true)
        {
            if (checkNeighbor)
            {
                var adjacent = new Coordinates3D[]
                {
                    descriptor.Coordinates + Coordinates3D.North,
                    descriptor.Coordinates + Coordinates3D.East,
                    descriptor.Coordinates + Coordinates3D.South,
                    descriptor.Coordinates + Coordinates3D.West,
                };

                foreach (var coords in adjacent)
                {
                    if (world.GetBlockID(coords) != AirBlock.BlockID)
                    {
                        return(false);
                    }
                }
            }

            if (checkSupport)
            {
                var supportingBlock = repository.GetBlockProvider(world.GetBlockID(descriptor.Coordinates + Coordinates3D.Down));
                if ((supportingBlock.ID != CactusBlock.BlockID) && (supportingBlock.ID != SandBlock.BlockID))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 14
0
            public BoundingBox?GetBoundingBox(World world, Coordinates3D coordinates)
            {
                // TODO: Consider passing block info to a handler to get a fancier bounding box
                var info = world.GetBlockInfo(coordinates);

                return(Block.GetBoundingBox(info));
            }
Esempio n. 15
0
 private static void DefaultBlockMinedHandler(World world, Coordinates3D coordinates, BlockInfo info)
 {
     world.SetBlockId(coordinates, 0);
     world.SetMetadata(coordinates, 0);
     world.OnSpawnEntityRequested(new ItemEntity((Vector3)coordinates + new Vector3(0.5),
                                                 new ItemStack(info.BlockId, 1, info.Metadata)));
 }
Esempio n. 16
0
 private void TryGrowth(IMultiplayerServer server, Coordinates3D coords, IWorld world)
 {
     if (world.GetBlockID(coords) != BlockID)
         return;
     // Find current height of stalk
     int height = 0;
     for (int y = -MaxGrowHeight; y <= MaxGrowHeight; y++)
     {
         if (world.GetBlockID(coords + (Coordinates3D.Down * y)) == BlockID)
             height++;
     }
     if (height < MaxGrowHeight)
     {
         var meta = world.GetMetadata(coords);
         meta++;
         world.SetMetadata(coords, meta);
         var chunk = world.FindChunk(coords);
         if (meta == 15)
         {
             if (world.GetBlockID(coords + Coordinates3D.Up) == 0)
             {
                 world.SetBlockID(coords + Coordinates3D.Up, BlockID);
                 server.Scheduler.ScheduleEvent("cactus", chunk,
                     TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
                     (_server) => TryGrowth(_server, coords + Coordinates3D.Up, world));
             }
         }
         else
         {
             server.Scheduler.ScheduleEvent("cactus", chunk,
                 TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
                 (_server) => TryGrowth(_server, coords, world));
         }
     }
 }
Esempio n. 17
0
        public bool ValidCactusPosition(BlockDescriptor descriptor, IBlockRepository repository, IWorld world, bool checkNeighbor = true, bool checkSupport = true)
        {
            if (checkNeighbor)
            {
                var adjacent = new Coordinates3D[]
                {
                    descriptor.Coordinates + Coordinates3D.North,
                    descriptor.Coordinates + Coordinates3D.East,
                    descriptor.Coordinates + Coordinates3D.South,
                    descriptor.Coordinates + Coordinates3D.West,
                };

                foreach (var coords in adjacent)
                    if (world.GetBlockID(coords) != AirBlock.BlockID)
                        return false;
            }

            if (checkSupport)
            {
                var supportingBlock = repository.GetBlockProvider(world.GetBlockID(descriptor.Coordinates + Coordinates3D.Down));
                if ((supportingBlock.ID != CactusBlock.BlockID) && (supportingBlock.ID != SandBlock.BlockID))
                    return false;
            }

            return true;
        }
Esempio n. 18
0
 public static void BlockChange(MinecraftClient client, IPacket _packet)
 {
   var packet = (BlockChangePacket)_packet;
   var position = new Coordinates3D(packet.X, packet.Y, packet.Z);
   client.World.SetBlockId(position, (short)packet.BlockType);
   client.World.SetMetadata(position, packet.BlockMetadata);
 }
Esempio n. 19
0
 public static bool IsCuboidCorner(Coordinates2D location, Coordinates3D start, Vector3 size)
 {
     return location.X.Equals(start.X) && location.Z.Equals(start.Z)
         || location.X.Equals(start.X) && location.Z.Equals(start.Z + (int)size.Z - 1)
         || location.X.Equals(start.X + (int)size.X - 1) && location.Z.Equals(start.Z)
         || location.X.Equals(start.X + (int)size.X - 1) && location.Z.Equals(start.Z + (int)size.Z - 1);
 }
Esempio n. 20
0
        public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
        {
            coordinates += MathHelper.BlockFaceToCoordinates(face);
            var old = world.GetBlockData(coordinates);

            byte[] overwritable =
            {
                AirBlock.BlockID,
                WaterBlock.BlockID,
                StationaryWaterBlock.BlockID,
                LavaBlock.BlockID,
                StationaryLavaBlock.BlockID
            };
            if (overwritable.Any(b => b == old.ID))
            {
                var data = world.GetBlockData(coordinates);
                data.ID       = ID;
                data.Metadata = (byte)item.Metadata;

                BlockPlaced(data, face, world, user);

                if (!IsSupported(world.GetBlockData(coordinates), user.Server, world))
                {
                    world.SetBlockData(coordinates, old);
                }
                else
                {
                    item.Count--;
                    user.Inventory[user.SelectedSlot] = item;
                }
            }
        }
Esempio n. 21
0
        /*
         * Cuboid Modes
         * 0x0 - Solid cuboid of the specified block
         * 0x1 - Hollow cuboid of the specified block
         * 0x2 - Outlines the area of the cuboid using the specified block
         */
        public static void GenerateCuboid(IChunk chunk, Coordinates3D location, Vector3 size, byte block, byte meta = 0x0, byte mode = 0x0)
        {
            //If mode is 0x2 offset the size by 2 and change mode to 0x1
            if (mode.Equals(0x2))
            {
                size += new Vector3(2, 2, 2);
                mode = 0x1;
            }

            for (int w = location.X; w < location.X + size.X; w++)
            {
                for (int l = location.Z; l < location.Z + size.Z; l++)
                {
                    for (int h = location.Y; h < location.Y + size.Y; h++)
                    {

                        if (w < 0 || w >= Chunk.Width || l < 0 || l >= Chunk.Depth || h < 0 || h >= Chunk.Height)
                            continue;
                        Coordinates3D BlockLocation = new Coordinates3D(w, h, l);
                        if (!h.Equals(location.Y) && !h.Equals(location.Y + (int)size.Y - 1)
                            && !IsCuboidWall(new Coordinates2D(w, l), location, size)
                            && !IsCuboidCorner(new Coordinates2D(w, l), location, size))
                            continue;

                        chunk.SetBlockID(BlockLocation, block);
                        if (meta != 0x0)
                            chunk.SetMetadata(BlockLocation, meta);
                    }
                }
            }
        }
Esempio n. 22
0
 private void DrawGrid(PathResult path, IWorld world)
 {
     for (int z = -8; z < 8; z++)
     {
         for (int x = -8; x < 8; x++)
         {
             var coords = new Coordinates3D(x, 4, z);
             if (path.Waypoints.Contains(coords))
             {
                 Console.Write("o");
             }
             else
             {
                 var id = world.GetBlockID(coords);
                 if (id != 0)
                 {
                     Console.Write("x");
                 }
                 else
                 {
                     Console.Write("_");
                 }
             }
         }
         Console.WriteLine();
     }
 }
Esempio n. 23
0
 protected void GenerateVanillaCircle(IChunk chunk, Coordinates3D location, int radius, byte block,
                                      byte meta = 0x0, double corner = 0)
 {
     for (var i = -radius; i <= radius; i = i + 1)
     {
         for (var j = -radius; j <= radius; j = j + 1)
         {
             var max = (int)Math.Sqrt(i * i + j * j);
             if (max <= radius)
             {
                 if (i.Equals(-radius) && j.Equals(-radius) ||
                     i.Equals(-radius) && j.Equals(radius) ||
                     i.Equals(radius) && j.Equals(-radius) ||
                     i.Equals(radius) && j.Equals(radius))
                 {
                     if (corner + radius * 0.2 < 0.4 || corner + radius * 0.2 > 0.7 || corner.Equals(0))
                     {
                         continue;
                     }
                 }
                 var x            = location.X + i;
                 var z            = location.Z + j;
                 var currentBlock = new Coordinates3D(x, location.Y, z);
                 if (chunk.GetBlockID(currentBlock).Equals(0))
                 {
                     chunk.SetBlockID(currentBlock, block);
                     chunk.SetMetadata(currentBlock, meta);
                 }
             }
         }
     }
 }
Esempio n. 24
0
        /// <summary>
        /// Finds a chunk that contains the specified block coordinates.
        /// </summary>
        public IChunk FindChunk(Coordinates3D coordinates, bool generate = true)
        {
            IChunk chunk;

            FindBlockPosition(coordinates, out chunk, generate);
            return(chunk);
        }
Esempio n. 25
0
 public static bool IsCuboidCorner(Coordinates2D location, Coordinates3D start, Vector3 size)
 {
     return(location.X.Equals(start.X) && location.Z.Equals(start.Z) ||
            location.X.Equals(start.X) && location.Z.Equals(start.Z + (int)size.Z - 1) ||
            location.X.Equals(start.X + (int)size.X - 1) && location.Z.Equals(start.Z) ||
            location.X.Equals(start.X + (int)size.X - 1) && location.Z.Equals(start.Z + (int)size.Z - 1));
 }
Esempio n. 26
0
        public byte GetBlockLight(Coordinates3D coordinates)
        {
            IChunk chunk;

            coordinates = FindBlockPosition(coordinates, out chunk);
            return(chunk.GetBlockLight(coordinates));
        }
Esempio n. 27
0
 private void UpdateHeightMap(Coordinates3D coords)
 {
     IChunk chunk;
     var adjusted = World.FindBlockPosition(coords, out chunk, generate: false);
     if (!HeightMaps.ContainsKey(chunk.Coordinates))
         return;
     var map = HeightMaps[chunk.Coordinates];
     byte x = (byte)adjusted.X; byte z = (byte)adjusted.Z;
     Coordinates3D _;
     for (byte y = (byte)(chunk.GetHeight(x, z) + 2); y > 0; y--)
     {
         if (y >= Chunk.Height)
             continue;
         _.X = x; _.Y = y - 1; _.Z = z;
         var id = chunk.GetBlockID(_);
         if (id == 0)
             continue;
         var provider = BlockRepository.GetBlockProvider(id);
         if (provider.LightOpacity != 0)
         {
             map[x, z] = y;
             break;
         }
     }
 }
    public override void CollectObservations()
    {
        List <GameObject> neighbors = gameState.getNeighbors(x, y, sight);

        foreach (GameObject neighbor in neighbors)
        {
            GameState.HexTypes type = neighbor.GetComponent <Hex>().getType();
            AddVectorObs((int)type, 9);

            GameObject other = neighbor.GetComponent <Hex>().unitInHex;

            if (other == null)
            {
                AddVectorObs(NoUnit, 3);
            }
            else if (other.GetComponent <UnitAgent>().team == team)
            {
                AddVectorObs(Friendly, 3);
            }
            else
            {
                AddVectorObs(Enemy, 3);
            }
        }

        Coordinates3D coordDist = Coordinates3D.AddCoordinates(
            new Coordinates3D(homeX, homeY), new Coordinates3D(-x, -y));

        AddVectorObs(Sigmoid(coordDist.x));
        AddVectorObs(Sigmoid(coordDist.y));
        AddVectorObs(Sigmoid(coordDist.z));
        AddVectorObs((Sigmoid(attack) - 0.5f) * 2);
    }
Esempio n. 29
0
        public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
        {
            if (face == BlockFace.PositiveY || face == BlockFace.NegativeY)
            {
                // Trapdoors are not placed when the user clicks on the top or bottom of a block
                return;
            }

            // NOTE: These directions are rotated by 90 degrees so that the hinge of the trapdoor is placed
            // where the user had their cursor.
            switch (face)
            {
                case BlockFace.NegativeZ:
                    item.Metadata = (byte)TrapdoorDirection.West;
                    break;
                case BlockFace.PositiveZ:
                    item.Metadata = (byte)TrapdoorDirection.East;
                    break;
                case BlockFace.NegativeX:
                    item.Metadata = (byte)TrapdoorDirection.South;
                    break;
                case BlockFace.PositiveX:
                    item.Metadata = (byte)TrapdoorDirection.North;
                    break;
                default:
                    return;
            }

            base.ItemUsedOnBlock(coordinates, item, face, world, user);
        }
Esempio n. 30
0
        public short GetBlockId(Coordinates3D coordinates)
        {
            Chunk chunk;

            coordinates = FindBlockPosition(coordinates, out chunk);
            return(chunk.GetBlockId(coordinates));
        }
Esempio n. 31
0
 public override void BlockLoadedFromChunk(Coordinates3D coords, IMultiplayerServer server, IWorld world)
 {
     var chunk = world.FindChunk(coords);
     server.Scheduler.ScheduleEvent("cactus", chunk,
         TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
         s => TryGrowth(s, coords, world));
 }
Esempio n. 32
0
        /// <summary>
        /// Sets the block light at specific coordinates relative to this chunk.
        /// Warning: The parent world's BlockChanged event handler does not get called.
        /// </summary>
        public void SetBlockLight(Coordinates3D coordinates, byte value)
        {
            IsModified = true;
            int index = coordinates.Y + (coordinates.Z * Height) + (coordinates.X * Height * Width);

            BlockLight[index] = value;
        }
Esempio n. 33
0
 public override void BlockLoadedFromChunk(Coordinates3D coords, IMultiplayerServer server, IWorld world)
 {
     var chunk = world.FindChunk(coords);
     server.Scheduler.ScheduleEvent("farmland", chunk,
         TimeSpan.FromSeconds(UpdateIntervalSeconds),
         s => HydrationCheckEvent(s, coords, world));
 }
Esempio n. 34
0
 void HydrationCheckEvent(IMultiplayerServer server, Coordinates3D coords, IWorld world)
 {
     if (world.GetBlockID(coords) != BlockID)
         return;
     if (MathHelper.Random.Next(3) == 0)
     {
         var meta = world.GetMetadata(coords);
         if (IsHydrated(coords, world) && meta != 15)
             meta++;
         else
         {
             meta--;
             if (meta == 0)
             {
                 world.SetBlockID(coords, BlockID);
                 return;
             }
         }
         world.SetMetadata(coords, meta);
     }
     var chunk = world.FindChunk(coords);
     server.Scheduler.ScheduleEvent("farmland", chunk,
         TimeSpan.FromSeconds(UpdateIntervalSeconds),
         _server => HydrationCheckEvent(_server, coords, world));
 }
Esempio n. 35
0
 public static void SetBlockInfo(this World world, Coordinates3D coordinates, BlockInfo info)
 {
     world.SetBlockId(coordinates, info.BlockId);
     world.SetMetadata(coordinates, info.Metadata);
     world.SetSkyLight(coordinates, info.SkyLight);
     world.SetBlockLight(coordinates, info.BlockLight);
 }
Esempio n. 36
0
        public override bool GenerateAt(IWorld world, IChunk chunk, Coordinates3D location)
        {
            if (!ValidLocation(location))
            {
                return(false);
            }

            var random = new Random(world.Seed);

            //Generate room
            GenerateCuboid(chunk, location, Size, CobblestoneBlock.BlockID, 0x0, 0x2);

            //Randomly add mossy cobblestone to floor
            MossFloor(chunk, location, random);

            //Place Spawner
            chunk.SetBlockID(new Coordinates3D((int)(location.X + ((Size.X + 1) / 2)), (int)((location + Coordinates3D.Up).Y), (int)(location.Z + ((Size.Z + 1) / 2))), MonsterSpawnerBlock.BlockID);

            //Create entrances
            CreateEntraces(chunk, location, random);

            //Place Chests
            PlaceChests(chunk, location, random);

            return(true);
        }
Esempio n. 37
0
 public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
 {
     var bottom = coordinates + MathHelper.BlockFaceToCoordinates(face);
     var top = bottom + Coordinates3D.Up;
     if (world.GetBlockID(top) != 0 || world.GetBlockID(bottom) != 0)
         return;
     DoorFlags direction;
     switch (MathHelper.DirectionByRotationFlat(user.Entity.Yaw))
     {
         case Direction.North:
             direction = DoorFlags.Northwest;
             break;
         case Direction.South:
             direction = DoorFlags.Southeast;
             break;
         case Direction.East:
             direction = DoorFlags.Northeast;
             break;
         default: // Direction.West:
             direction = DoorFlags.Southwest;
             break;
     }
     user.Server.BlockUpdatesEnabled = false;
     world.SetBlockID(bottom, BlockID);
     world.SetMetadata(bottom, (byte)direction);
     world.SetBlockID(top, BlockID);
     world.SetMetadata(top, (byte)(direction | DoorFlags.Upper));
     user.Server.BlockUpdatesEnabled = true;
     item.Count--;
     user.Inventory[user.SelectedSlot] = item;
 }
Esempio n. 38
0
        public void SetBlockId(Coordinates3D coordinates, short value)
        {
            int index = coordinates.X + (coordinates.Z * Width) + (coordinates.Y * Height * Width);

            if (value == 0)
            {
                if (Blocks[index] != 0)
                {
                    nonAirCount--;
                }
            }
            else
            {
                if (Blocks[index] == 0)
                {
                    nonAirCount++;
                }
            }
            Blocks[index] = (byte)value;
            if ((value & ~0xFF) != 0)
            {
                if (Add == null)
                {
                    Add = new NibbleArray(Width * Height * Depth);
                }
                Add[index] = (byte)((ushort)value >> 8);
            }
        }
Esempio n. 39
0
 public override void BlockLoadedFromChunk(Coordinates3D coords, IMultiplayerServer server, IWorld world)
 {
     var chunk = world.FindChunk(coords);
     server.Scheduler.ScheduleEvent("crops", chunk,
         TimeSpan.FromSeconds(MathHelper.Random.Next(30, 60)),
         (s) => GrowBlock(s, world, coords));
 }
Esempio n. 40
0
 public void Decorate(IWorld world, IChunk chunk, IBiomeRepository biomes)
 {
     var noise = new Perlin();
     noise.Seed = world.Seed;
     var chanceNoise = new ClampNoise(noise);
     chanceNoise.MaxValue = 2;
     for (int x = 0; x < 16; x++)
     {
         for (int z = 0; z < 16; z++)
         {
             var biome = biomes.GetBiome(chunk.Biomes[x * Chunk.Width + z]);
             var blockX = MathHelper.ChunkToBlockX(x, chunk.Coordinates.X);
             var blockZ = MathHelper.ChunkToBlockZ(z, chunk.Coordinates.Z);
             var height = chunk.HeightMap[x * Chunk.Width + z];
             if (biome.Plants.Contains(PlantSpecies.Cactus) && chanceNoise.Value2D(blockX, blockZ) > 1.7)
             {
                 var blockLocation = new Coordinates3D(x, height, z);
                 var cactiPosition = blockLocation + Coordinates3D.Up;
                 if (chunk.GetBlockID(blockLocation).Equals(SandBlock.BlockID))
                 {
                     var HeightChance = chanceNoise.Value2D(blockX, blockZ);
                     var CactusHeight = (HeightChance < 1.4) ? 2 : 3;
                     Decoration.GenerateColumn(chunk, cactiPosition, CactusHeight, CactusBlock.BlockID);
                 }
             }
         }
     }
 }
Esempio n. 41
0
        private void PlaceChests(IChunk chunk, Coordinates3D location, Random random)
        {
            var above  = location + Coordinates3D.Up;
            var chests = random.Next(0, 2);

            for (int i = 0; i < chests; i++)
            {
                for (int attempts = 0; attempts < 10; attempts++)
                {
                    var x = random.Next(location.X, location.X + (int)Size.X);
                    var z = random.Next(location.Z, location.Z + (int)Size.Z);
                    if (!IsCuboidWall(new Coordinates2D(x, z), location, Size) && !IsCuboidCorner(new Coordinates2D(x, z), location, Size))
                    {
                        if (NeighboursBlock(chunk, new Coordinates3D(x, above.Y, z), CobblestoneBlock.BlockID))
                        {
                            if (x < 0 || x >= Chunk.Width ||
                                z < 0 || z >= Chunk.Depth ||
                                above.Y < 0 || above.Y >= Chunk.Height)
                            {
                                continue;
                            }
                            chunk.SetBlockID(new Coordinates3D(x, above.Y, z), ChestBlock.BlockID);
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 42
0
        public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world,
                                             IRemoteClient user)
        {
            var adjacent = 0;
            var coords   = coordinates + MathHelper.BlockFaceToCoordinates(face);
            var _        = Coordinates3D.Down;

            // Check for adjacent chests. We can only allow one adjacent check block.
            for (var i = 0; i < AdjacentBlocks.Length; i++)
            {
                if (world.GetBlockId(coords + AdjacentBlocks[i]) == BlockId)
                {
                    _ = coords + AdjacentBlocks[i];
                    adjacent++;
                }
            }

            if (adjacent <= 1)
            {
                if (_ != Coordinates3D.Down)
                {
                    for (var i = 0; i < AdjacentBlocks.Length; i++)
                    {
                        if (world.GetBlockId(_ + AdjacentBlocks[i]) == BlockId)
                        {
                            adjacent++;
                        }
                    }
                }
                if (adjacent <= 1)
                {
                    base.ItemUsedOnBlock(coordinates, item, face, world, user);
                }
            }
        }
Esempio n. 43
0
        public void Decorate(IWorld world, IChunk chunk, IBiomeRepository biomes)
        {
            var noise       = new Perlin(world.Seed);
            var chanceNoise = new ClampNoise(noise);

            chanceNoise.MaxValue = 2;
            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    var biome  = biomes.GetBiome(chunk.Biomes[x * Chunk.Width + z]);
                    var blockX = MathHelper.ChunkToBlockX(x, chunk.Coordinates.X);
                    var blockZ = MathHelper.ChunkToBlockZ(z, chunk.Coordinates.Z);
                    var height = chunk.HeightMap[x * Chunk.Width + z];
                    if (biome.Plants.Contains(PlantSpecies.Cactus) && chanceNoise.Value2D(blockX, blockZ) > 1.7)
                    {
                        var blockLocation = new Coordinates3D(x, height, z);
                        var cactiPosition = blockLocation + Coordinates3D.Up;
                        if (chunk.GetBlockID(blockLocation).Equals(SandBlock.BlockID))
                        {
                            var HeightChance = chanceNoise.Value2D(blockX, blockZ);
                            var CactusHeight = (HeightChance < 1.4) ? 2 : 3;
                            Decoration.GenerateColumn(chunk, cactiPosition, CactusHeight, CactusBlock.BlockID);
                        }
                    }
                }
            }
        }
Esempio n. 44
0
        public byte GetMetadata(Coordinates3D coordinates)
        {
            Chunk chunk;

            coordinates = FindBlockPosition(coordinates, out chunk);
            return(chunk.GetMetadata(coordinates));
        }
Esempio n. 45
0
        public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
        {
            if (face == BlockFace.PositiveY || face == BlockFace.NegativeY)
            {
                // Trapdoors are not placed when the user clicks on the top or bottom of a block
                return;
            }

            // NOTE: These directions are rotated by 90 degrees so that the hinge of the trapdoor is placed
            // where the user had their cursor.
            switch (face)
            {
            case BlockFace.NegativeZ:
                item.Metadata = (byte)TrapdoorDirection.West;
                break;

            case BlockFace.PositiveZ:
                item.Metadata = (byte)TrapdoorDirection.East;
                break;

            case BlockFace.NegativeX:
                item.Metadata = (byte)TrapdoorDirection.South;
                break;

            case BlockFace.PositiveX:
                item.Metadata = (byte)TrapdoorDirection.North;
                break;

            default:
                return;
            }

            base.ItemUsedOnBlock(coordinates, item, face, world, user);
        }
Esempio n. 46
0
        private void ItemUsedOnBlock(World world, Coordinates3D coordinates, BlockFace face, Coordinates3D cursor, ItemInfo item)
        {
            var info = world.GetBlockInfo(coordinates);
            if (Block.GetIsSolidOnFace(info, face) == false)
                return;
            
            coordinates += MathHelper.BlockFaceToCoordinates(face);

            switch (face)
            {
                case BlockFace.NegativeZ:
                    world.SetBlockId(coordinates, item.ItemId);
                    world.SetMetadata(coordinates, (byte)Orientation.FacingNorth);
                    break;
                case BlockFace.PositiveZ:
                    world.SetBlockId(coordinates, item.ItemId);
                    world.SetMetadata(coordinates, (byte)Orientation.FacingSouth);
                    break;
                case BlockFace.NegativeX:
                    world.SetBlockId(coordinates, item.ItemId);
                    world.SetMetadata(coordinates, (byte)Orientation.FacingWest);
                    break;
                case BlockFace.PositiveX:
                    world.SetBlockId(coordinates, item.ItemId);
                    world.SetMetadata(coordinates, (byte)Orientation.FacingEast);
                    break;
                default:
                    // Ladders can't be placed lying flat.
                    break;
            }
        }
Esempio n. 47
0
        private void CreateEntraces(IChunk chunk, Coordinates3D location, Random random)
        {
            int entrances = 0;
            var above     = location + Coordinates3D.Up;

            for (int X = location.X; X < location.X + Size.X; X++)
            {
                if (entrances >= MaxEntrances)
                {
                    break;
                }
                for (int Z = location.Z; Z < location.Z + Size.Z; Z++)
                {
                    if (entrances >= MaxEntrances)
                    {
                        break;
                    }
                    if (random.Next(0, 3) == 0 && IsCuboidWall(new Coordinates2D(X, Z), location, Size) &&
                        !IsCuboidCorner(new Coordinates2D(X, Z), location, Size))
                    {
                        var blockLocation = new Coordinates3D(X, above.Y, Z);
                        chunk.SetBlockID(blockLocation, AirBlock.BlockID);
                        chunk.SetBlockID(blockLocation + Coordinates3D.Up, AirBlock.BlockID);
                        entrances++;
                    }
                }
            }
        }
Esempio n. 48
0
 public override void BlockLoadedFromChunk(Coordinates3D coords, IMultiplayerServer server, IWorld world)
 {
     var chunk = world.FindChunk(coords);
     server.Scheduler.ScheduleEvent(chunk,
         DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthTime, MaxGrowthTime)),
         s => TrySpread(coords, world, server));
 }
Esempio n. 49
0
        /// <summary>
        /// Sets the metadata at specific coordinates relative to this chunk.
        /// Warning: The parent world's BlockChanged event handler does not get called.
        /// </summary>
        public void SetMetadata(Coordinates3D coordinates, byte value)
        {
            IsModified = true;
            int index = coordinates.Y + (coordinates.Z * Height) + (coordinates.X * Height * Width);

            Metadata[index] = value;
        }
Esempio n. 50
0
 public short GetBlockId(Coordinates3D coordinates)
 {
     int index = coordinates.X + (coordinates.Z * Width) + (coordinates.Y * Height * Width);
     short value = Blocks[index];
     if (Add != null)
         value |= (short)(Add[index] << 8);
     return value;
 }
Esempio n. 51
0
 public static void DefaultBlockMinedHandler(BlockDescriptor block, World world, Coordinates3D destroyedBlock, ItemDescriptor? tool)
 {
     var drops = Block.GetBlockDrop(block, world, destroyedBlock);
     world.SetBlockId(destroyedBlock, 0);
     world.SetMetadata(destroyedBlock, 0);
     foreach (var drop in drops)
         world.OnSpawnEntityRequested(new ItemEntity((Vector3)destroyedBlock + new Vector3(0.5), drop));
 }
Esempio n. 52
0
 public BoundingBox? GetBoundingBox(IWorld world, Coordinates3D coordinates)
 {
     // TODO: Block-specific bounding boxes
     var id = world.GetBlockID(coordinates);
     if (id == 0) return null;
     var provider = BlockProviders[id];
     return provider.BoundingBox;
 }
Esempio n. 53
0
 public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
 {
     coordinates += MathHelper.BlockFaceToCoordinates(face);
     if (world.GetBlockID(coordinates) == AirBlock.BlockID)
     {
         world.SetBlockID(coordinates, FireBlock.BlockID);
     }
 }
Esempio n. 54
0
 public override bool ValidLocation(Coordinates3D location)
 {
     var OffsetSize = Size + new Vector3(1, 1, 1);
     if (location.X + (int)OffsetSize.X >= Chunk.Width
         || location.Z + (int)OffsetSize.Z >= Chunk.Depth
         || location.Y + (int)OffsetSize.Y >= Chunk.Height)
         return false;
     return true;
 }
Esempio n. 55
0
 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);
     }
 }
Esempio n. 56
0
 public override bool ValidLocation(Coordinates3D location)
 {
     if (location.X - LeafRadius < 0
         || location.X + LeafRadius >= Chunk.Width
         || location.Z - LeafRadius < 0
         || location.Z + LeafRadius >= Chunk.Depth)
         return false;
     return true;
 }
Esempio n. 57
0
 public void ScheduleNextEvent(Coordinates3D coords, IWorld world, IMultiplayerServer server)
 {
     if (world.GetBlockID(coords) == StillID)
         return;
     var chunk = world.FindChunk(coords);
     server.Scheduler.ScheduleEvent("fluid", chunk,
         TimeSpan.FromSeconds(SecondsBetweenUpdates), (_server) =>
         AutomataUpdate(_server, world, coords));
 }
Esempio n. 58
0
 private void ScheduledUpdate(IWorld world, Coordinates3D coords)
 {
     if (world.IsValidPosition(coords + Coordinates3D.Up))
     {
         var id = world.GetBlockID(coords + Coordinates3D.Up);
         var provider = world.BlockRepository.GetBlockProvider(id);
         if (provider.Opaque)
             world.SetBlockID(coords, DirtBlock.BlockID);
     }
 }
Esempio n. 59
0
 /*
  * Generates the top of the pine/conifer trees.
  * Type:
  * 0x0 - two level topper
  * 0x1 - three level topper
  */
 protected void GenerateTopper(IChunk chunk, Coordinates3D location, byte type = 0x0)
 {
     const int sectionRadius = 1;
     GenerateCircle(chunk, location, sectionRadius, LeavesBlock.BlockID, 0x1);
     var top = location + Coordinates3D.Up;
     chunk.SetBlockID(top, LeavesBlock.BlockID);
     chunk.SetMetadata(top, 0x1);
     if (type == 0x1 && (top + Coordinates3D.Up).Y < Chunk.Height)
         GenerateVanillaCircle(chunk, top + Coordinates3D.Up, sectionRadius, LeavesBlock.BlockID, 0x1); 
 }
Esempio n. 60
0
 public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
 {
     var id = world.GetBlockID(coordinates);
     if (id == DirtBlock.BlockID || id == GrassBlock.BlockID)
     {
         world.SetBlockID(coordinates, FarmlandBlock.BlockID);
         user.Server.BlockRepository.GetBlockProvider(FarmlandBlock.BlockID).BlockPlaced(
             new BlockDescriptor { Coordinates = coordinates }, face, world, user);
     }
 }