Пример #1
0
        public void ChunkReceived(IChunkColumn chunkColumn, int x, int z, bool update)
        {
            var c = new ChunkCoordinates(x, z);

            ChunkManager.AddChunk(chunkColumn, c, update);
            //InitiateChunk(chunkColumn as ChunkColumn);
        }
Пример #2
0
        public void SetBlockEntity(int x, int y, int z, BlockEntity blockEntity)
        {
            var coords      = new BlockCoordinates(x, y, z);
            var chunkCoords = new ChunkCoordinates(x >> 4, z >> 4);

            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                var cx = x & 0xf;
                var cy = y & 0xff;
                var cz = z & 0xf;

                var chunkPos   = new BlockCoordinates(cx, cy, cz);
                var blockAtPos = chunk.GetBlockState(cx, cy, cz);

                if (blockAtPos.Block.BlockMaterial == Material.Air)
                {
                    return;
                }

                chunk.RemoveBlockEntity(chunkPos);
                EntityManager.RemoveBlockEntity(coords);

                chunk.AddBlockEntity(chunkPos, blockEntity);
                EntityManager.AddBlockEntity(coords, blockEntity);
            }
        }
Пример #3
0
        //private ThreadSafeList<ChunkCoordinates> _loadedChunks = new ThreadSafeList<ChunkCoordinates>();
        private void UnloadChunks(ChunkCoordinates center, double maxViewDistance)
        {
            //var chunkPublisher = Client.LastChunkPublish;

            //Client.ChunkRadius
            foreach (var chunk in World.ChunkManager.GetAllChunks())
            {
                if (chunk.Key.DistanceTo(center) > maxViewDistance)
                {
                    //_chunkCache.TryRemove(chunkColumn.Key, out var waste);
                    UnloadChunk(chunk.Key);
                }
            }
            //Parallel.ForEach(_loadedChunks.ToArray(), (chunkColumn) =>
            //{

            /*if (chunkPublisher != null)
             * {
             *      if (chunkColumn.DistanceTo(new ChunkCoordinates(new Vector3(chunkPublisher.coordinates.X,
             *                  chunkPublisher.coordinates.Y, chunkPublisher.coordinates.Z))) < chunkPublisher.radius)
             *              return;
             * }*/


            //	});
        }
Пример #4
0
        public Block GetBlock(BlockCoordinates blockCoordinates, ChunkColumn tryChunk = null)
        {
            ChunkColumn chunk = null;

            var chunkCoordinates = new ChunkCoordinates(blockCoordinates.X >> 4, blockCoordinates.Z >> 4);

            if (tryChunk != null && tryChunk.X == chunkCoordinates.X && tryChunk.Z == chunkCoordinates.Z)
            {
                chunk = tryChunk;
            }
            else
            {
                chunk = GetChunk(chunkCoordinates);
            }

            if (chunk == null)
            {
                return((Block)Airstate.Block);
            }

            var block = (Block)chunk.GetBlockState(blockCoordinates.X & 0x0f, blockCoordinates.Y & 0xff, blockCoordinates.Z & 0x0f).Block;

            block.Coordinates = blockCoordinates;

            return(block);
        }
Пример #5
0
        public void SetBlockState(int x, int y, int z, BlockState block, int storage, BlockUpdatePriority priority = BlockUpdatePriority.High | BlockUpdatePriority.Neighbors)
        {
            var chunkCoords = new ChunkCoordinates(x >> 4, z >> 4);

            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                var cx = x & 0xf;
                var cy = y & 0xff;
                var cz = z & 0xf;

                chunk.SetBlockState(cx, cy, cz, block, storage);

                EntityManager.RemoveBlockEntity(new BlockCoordinates(x, y, z));

                var type = ScheduleType.Full;

                if ((priority & BlockUpdatePriority.Neighbors) != 0)
                {
                    UpdateNeighbors(x, y, z);
                    CheckForUpdate(chunkCoords, cx, cz);
                }

                if ((priority & BlockUpdatePriority.NoGraphic) != 0)
                {
                    type |= ScheduleType.LowPriority;
                }

                //chunk.SetDirty();
                //chunk.IsDirty = true;
                ChunkManager.ScheduleChunkUpdate(chunkCoords, type, (priority & BlockUpdatePriority.Priority) != 0);
            }
        }
Пример #6
0
        public void ChunkUnload(int x, int z)
        {
            var chunkCoordinates = new ChunkCoordinates(x, z);

            ChunkManager.RemoveChunk(chunkCoordinates);

            EntityManager.UnloadEntities(chunkCoordinates);
        }
Пример #7
0
        public ChunkColumn GetChunk(ChunkCoordinates coordinates, bool cacheOnly = false)
        {
            if (ChunkManager.TryGetChunk(coordinates, out var c))
            {
                return((ChunkColumn)c);
            }

            return(null);
        }
Пример #8
0
        public void SetBlockState(int x, int y, int z, BlockState block, int storage, BlockUpdatePriority priority = BlockUpdatePriority.High | BlockUpdatePriority.Neighbors)
        {
            var chunkCoords = new ChunkCoordinates(x >> 4, z >> 4);

            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                var cx = x & 0xf;
                var cy = y & 0xff;
                var cz = z & 0xf;

                //var previousBlock = chunk.GetBlockState(cx, cy, cz, storage);
                //if (block.Block.RequiresUpdate)
                {
                    //block = block.Block.BlockPlaced(this, block, new BlockCoordinates(x,y,z));
                }

                chunk.SetBlockState(cx, cy, cz, block, storage);

                EntityManager.RemoveBlockEntity(new BlockCoordinates(x, y, z));

                var type = ScheduleType.Full;

                if ((priority & BlockUpdatePriority.Neighbors) != 0)
                {
                    UpdateNeighbors(x, y, z);
                }

                if ((priority & BlockUpdatePriority.NoGraphic) != 0)
                {
                    type |= ScheduleType.LowPriority;
                }

                var blockCoords = new BlockCoordinates(x, y, z);

                /*if (block.Block.BlockMaterial.BlocksLight)
                 * {
                 *      SetBlockLight(blockCoords, 0);
                 * }
                 */
                ChunkManager.SkyLightCalculator.Calculate(this, blockCoords);

                ChunkManager.BlockLightCalculations.Enqueue(blockCoords);

                //chunk.SetDirty();
                //chunk.IsDirty = true;
                ChunkManager.ScheduleChunkUpdate(chunkCoords, type, (priority & BlockUpdatePriority.Priority) != 0);

                CheckForUpdate(chunkCoords, cx, cz);
            }
        }
Пример #9
0
        public void ScheduleBlockUpdate(BlockCoordinates coordinates)
        {
            var chunkCoords = new ChunkCoordinates(coordinates.X >> 4, coordinates.Z >> 4);

            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                var cx = coordinates.X & 0xf;
                var cy = coordinates.Y & 0xff;
                var cz = coordinates.Z & 0xf;

                chunk.ScheduleBlockUpdate(cx, cy, cz);
            }
        }
Пример #10
0
        public ChunkColumn GenerateChunkColumn(ChunkCoordinates chunkCoordinates)
        {
            //  IBlockState stone = BlockFactory.GetBlockState("minecraft:stone");
            //  IBlockState grass = BlockFactory.GetBlockState("minecraft:grass");


            ChunkColumn column = new ChunkColumn();

            for (int x = 0; x < 16; x++)
            {
                var rx = (chunkCoordinates.X * 16) + x;
                for (int z = 0; z < 16; z++)
                {
                    var rz = (chunkCoordinates.Z * 16) + z;

                    int height = GetHeight(rx, rz);

                    bool placeWater = false;
                    //if (height <= 3)
                    {
                        //placeWater = SimplexNoise.Evaluate(rz / 8f, rx / 8f) > 0f;
                        //height = 3;
                    }

                    for (int y = 0; y < Height; y++)
                    {
                        if (y > height - 1 && y <= 5)
                        {
                            column.SetBlockState(x, y, z, LiquidBlock);
                        }
                        else if (y < height)
                        {
                            column.SetBlockState(x, y, z, MainBlock);
                        }
                    }
                }
            }

            column.X = chunkCoordinates.X;
            column.Z = chunkCoordinates.Z;

            return(column);
        }
Пример #11
0
        private void CheckForUpdate(ChunkCoordinates chunkCoords, int cx, int cz)
        {
            if (cx == 0)
            {
                ChunkManager.ScheduleChunkUpdate(chunkCoords - new ChunkCoordinates(1, 0), ScheduleType.Border | ScheduleType.Lighting, true);
            }
            else if (cx == 0xf)
            {
                ChunkManager.ScheduleChunkUpdate(chunkCoords + new ChunkCoordinates(1, 0), ScheduleType.Border | ScheduleType.Lighting, true);
            }

            if (cz == 0)
            {
                ChunkManager.ScheduleChunkUpdate(chunkCoords - new ChunkCoordinates(0, 1), ScheduleType.Border | ScheduleType.Lighting, true);
            }
            else if (cz == 0xf)
            {
                ChunkManager.ScheduleChunkUpdate(chunkCoords + new ChunkCoordinates(0, 1), ScheduleType.Border | ScheduleType.Lighting, true);
            }
        }
Пример #12
0
        public void SetBlock(Block block, bool broadcast = true, bool applyPhysics = true, bool calculateLight = true,
                             ChunkColumn possibleChunk   = null)
        {
            ChunkColumn chunk;
            var         chunkCoordinates = new ChunkCoordinates(block.Coordinates.X >> 4, block.Coordinates.Z >> 4);

            chunk = possibleChunk != null && possibleChunk.X == chunkCoordinates.X && possibleChunk.Z == chunkCoordinates.Z ? possibleChunk : GetChunk(chunkCoordinates);

            chunk.SetBlockState(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0xff, block.Coordinates.Z & 0x0f, block.BlockState);

            if (calculateLight && chunk.GetHeight(block.Coordinates.X & 0x0f, block.Coordinates.Z & 0x0f) <= block.Coordinates.Y + 1)
            {
                chunk.RecalculateHeight(block.Coordinates.X & 0x0f, block.Coordinates.Z & 0x0f, Options.VideoOptions.ClientSideLighting);
            }

            if (calculateLight)
            {
                //new SkyLightCalculations().Calculate(this, block.Coordinates);
            }
        }
Пример #13
0
        public void SetBlockState(int x, int y, int z, IBlockState block)
        {
            var chunkCoords = new ChunkCoordinates(x >> 4, z >> 4);

            IChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                var cx = x & 0xf;
                var cy = y & 0xff;
                var cz = z & 0xf;

                chunk.SetBlockState(cx, cy, cz, block);

                UpdateNeighbors(x, y, z);
                CheckForUpdate(chunkCoords, cx, cz);

                ChunkManager.ScheduleChunkUpdate(chunkCoords, ScheduleType.Scheduled | ScheduleType.Lighting, true);
            }
        }
Пример #14
0
        public void SetSkyLight(BlockCoordinates coordinates, byte p1)
        {
            var         chunkCoords = new ChunkCoordinates(coordinates);
            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                if (chunk.SetSkyLight(coordinates.X & 0xf, coordinates.Y & 0xff, coordinates.Z & 0xf, p1))
                {
                    // if ((chunk.Scheduled & ScheduleType.Lighting) != ScheduleType.Lighting)
                    {
                        // ChunkManager.ScheduleChunkUpdate(chunkCoords, ScheduleType.Lighting);
                    }
                    // else
                    {
                        // chunk.Scheduled = chunk.Scheduled | ScheduleType.Lighting;
                    }
                }
            }
        }
Пример #15
0
        private void ScheduleLightingUpdate(BlockCoordinates coordinates, bool blockLight = false)
        {
            var chunkCoords = new ChunkCoordinates(coordinates.X >> 4, coordinates.Z >> 4);

            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                var cx = coordinates.X & 0xf;
                var cy = coordinates.Y & 0xff;
                var cz = coordinates.Z & 0xf;

                if (blockLight)
                {
                    chunk.ScheduleBlocklightUpdate(cx, cy, cz);
                }
                else
                {
                    chunk.ScheduleSkylightUpdate(cx, cy, cz);
                }
            }
        }
Пример #16
0
        public void SetBlock(Block block)
        {
            var x = block.Coordinates.X;
            var y = block.Coordinates.Y;
            var z = block.Coordinates.Z;

            var chunkCoords = new ChunkCoordinates(x >> 4, z >> 4);

            IChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                var cx = x & 0xf;
                var cy = y & 0xff;
                var cz = z & 0xf;

                chunk.SetBlock(cx, cy, cz, block);
                ChunkManager.ScheduleChunkUpdate(new ChunkCoordinates(x >> 4, z >> 4), ScheduleType.Scheduled | ScheduleType.Lighting, true);

                UpdateNeighbors(x, y, z);
                CheckForUpdate(chunkCoords, cx, cz);
            }
        }
Пример #17
0
        public void SetBlockLight(BlockCoordinates coordinates, byte value)
        {
            var         chunkCoords = new ChunkCoordinates(coordinates);
            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                if (chunk.SetBlocklight(coordinates.X & 0xf, coordinates.Y & 0xff, coordinates.Z & 0xf, value))
                {
                    var x = coordinates.X;
                    var y = coordinates.Y;
                    var z = coordinates.Z;

                    ScheduleLightingUpdate(new BlockCoordinates(x + 1, y, z), true);
                    ScheduleLightingUpdate(new BlockCoordinates(x + -1, y, z), true);

                    ScheduleLightingUpdate(new BlockCoordinates(x, y, z + 1), true);
                    ScheduleLightingUpdate(new BlockCoordinates(x, y, z + -1), true);

                    ScheduleLightingUpdate(new BlockCoordinates(x, y + 1, z), true);
                    ScheduleLightingUpdate(new BlockCoordinates(x, y + -1, z), true);
                }
            }
        }
Пример #18
0
        public void SetSkyLight(BlockCoordinates coordinates, byte p1)
        {
            var         chunkCoords = new ChunkCoordinates(coordinates);
            ChunkColumn chunk;

            if (ChunkManager.TryGetChunk(chunkCoords, out chunk))
            {
                if (chunk.SetSkyLight(coordinates.X & 0xf, coordinates.Y & 0xff, coordinates.Z & 0xf, p1))
                {
                    var x = coordinates.X;
                    var y = coordinates.Y;
                    var z = coordinates.Z;

                    ScheduleLightingUpdate(new BlockCoordinates(x + 1, y, z));
                    ScheduleLightingUpdate(new BlockCoordinates(x - 1, y, z));

                    ScheduleLightingUpdate(new BlockCoordinates(x, y, z + 1));
                    ScheduleLightingUpdate(new BlockCoordinates(x, y, z - 1));

                    ScheduleLightingUpdate(new BlockCoordinates(x, y + 1, z));
                    ScheduleLightingUpdate(new BlockCoordinates(x, y - 1, z));
                }
            }
        }
Пример #19
0
 public void UnloadChunk(ChunkCoordinates coordinates)
 {
     World.UnloadChunk(coordinates);
 }
Пример #20
0
        public override void Update(IUpdateArgs args)
        {
            if (WaitingOnChunk && Age % 4 == 0)
            {
                NoAi = true;

                if (Level.GetChunk(KnownPosition.GetCoordinates3D(), true) != null)
                {
                    Velocity       = Vector3.Zero;
                    WaitingOnChunk = false;
                    NoAi           = false;
                }
            }

            ChunkCoordinates oldChunkCoordinates = new ChunkCoordinates(base.KnownPosition);
            bool             sprint = IsSprinting;
            bool             sneak  = IsSneaking;

            if (!CanFly && IsFlying)
            {
                IsFlying = false;
            }

            if (IsSprinting && !CanSprint)
            {
                IsSprinting = false;
            }

            Controller.Update(args.GameTime);
            //KnownPosition.HeadYaw = KnownPosition.Yaw;

            if (IsSprinting && !sprint)
            {
                FOVModifier = 10;

                Network.EntityAction((int)EntityId, EntityAction.StartSprinting);
            }
            else if (!IsSprinting && sprint)
            {
                FOVModifier = 0;

                Network.EntityAction((int)EntityId, EntityAction.StopSprinting);
            }

            if (IsSneaking != sneak)
            {
                if (IsSneaking)
                {
                    Network.EntityAction((int)EntityId, EntityAction.StartSneaking);
                    Camera.UpdateOffset(new Vector3(0f, -0.15f, 0.35f));
                }
                else
                {
                    Network.EntityAction((int)EntityId, EntityAction.StopSneaking);
                    Camera.UpdateOffset(Vector3.Zero);
                }
            }

            //	DoHealthAndExhaustion();

            var previousCheckedInput = _prevCheckedInput;

            if ((Controller.CheckInput && Controller.CheckMovementInput))
            {
                _prevCheckedInput = true;
                if (!previousCheckedInput || World.FormManager.IsShowingForm)
                {
                    return;
                }

                UpdateRayTracer();

                //if (Controller.InputManager.IsDown(InputCommand.LeftClick) && DateTime.UtcNow - _lastAnimate >= TimeSpan.FromMilliseconds(500))
                //{
                //	SwingArm(true);
                //}

                var hitEntity = HitEntity;
                if (hitEntity != null && Controller.InputManager.IsPressed(InputCommand.LeftClick) && hitEntity is LivingEntity)
                {
                    if (_destroyingBlock)
                    {
                        StopBreakingBlock(forceCanceled: true);
                    }

                    InteractWithEntity(hitEntity, true);
                }
                else if (hitEntity != null && Controller.InputManager.IsPressed(InputCommand.RightClick) && hitEntity is LivingEntity)
                {
                    if (_destroyingBlock)
                    {
                        StopBreakingBlock(forceCanceled: true);
                    }

                    InteractWithEntity(hitEntity, false);
                }
                else if (hitEntity == null && !_destroyingBlock &&
                         Controller.InputManager.IsPressed(InputCommand.LeftClick) &&
                         !HasRaytraceResult)
                {
                    HandleLeftClick(Inventory[Inventory.SelectedSlot], Inventory.SelectedSlot);
                }
                else if (hitEntity == null && !_destroyingBlock && Controller.InputManager.IsDown(InputCommand.LeftClick) && !IsWorldImmutable && HasRaytraceResult)                 //Destroying block.
                {
                    StartBreakingBlock();
                }
                else if (_destroyingBlock && Controller.InputManager.IsUp(InputCommand.LeftClick))
                {
                    StopBreakingBlock();
                }
                else if (_destroyingBlock && Controller.InputManager.IsDown(InputCommand.LeftClick))
                {
                    if (_destroyingTarget != new BlockCoordinates(Vector3.Floor(Raytraced)))
                    {
                        StopBreakingBlock(true, true);

                        if (Gamemode != Gamemode.Creative)
                        {
                            StartBreakingBlock();
                        }
                    }
                    else
                    {
                        if ((DateTime.UtcNow - _lastAnimate).TotalMilliseconds > 500)
                        {
                            _lastAnimate = DateTime.UtcNow;
                            SwingArm(true);
                        }

                        var timeRan = (DateTime.UtcNow - _destroyingTick).TotalMilliseconds / 50d;
                        if (timeRan >= _destroyTimeNeeded)
                        {
                            StopBreakingBlock(true);
                        }
                    }
                }
                else if (Controller.InputManager.IsPressed(InputCommand.RightClick))
                {
                    bool handledClick = false;
                    var  item         = Inventory[Inventory.SelectedSlot];
                    // Log.Debug($"Right click!");
                    if (item != null)
                    {
                        handledClick = HandleClick(item, Inventory.SelectedSlot);
                    }

                    /*if (!handledClick && Inventory.OffHand != null && !(Inventory.OffHand is ItemAir))
                     * {
                     *      handledClick = HandleRightClick(Inventory.OffHand, 1);
                     * }*/
                }

                if (hitEntity != null && HasCollision)
                {
                    if (IsColliding(hitEntity))
                    {
                        //var distance = DistanceToHorizontal(hitEntity);
                        //	Velocity += (KnownPosition.ToVector3() - hitEntity.KnownPosition.ToVector3());
                    }
                }
            }
            else
            {
                if (_destroyingBlock)
                {
                    StopBreakingBlock();
                }

                _prevCheckedInput     = false;
                _lastTimeWithoutInput = DateTime.UtcNow;
            }

            if (PreviousSlot != Inventory.SelectedSlot)
            {
                var slot = Inventory.SelectedSlot;
                Network?.HeldItemChanged(Inventory[Inventory.SelectedSlot], (short)slot);
                PreviousSlot = slot;
            }

            base.Update(args);
        }
Пример #21
0
 public void UnloadChunk(ChunkCoordinates coordinates)
 {
     ChunkManager.RemoveChunk(coordinates);
     EntityManager.UnloadEntities(coordinates);
 }