Beispiel #1
0
        public ChunkColumn GenerateChunkColumn(ChunkCoordinates chunkCoordinates)
        {
            var sw           = Stopwatch.StartNew();
            var playerCoords = (BlockCoordinates)chunkCoordinates;

            var movePlayerPacket = McpeMovePlayer.CreateObject();

            movePlayerPacket.runtimeEntityId = _client.EntityId;
            movePlayerPacket.x = playerCoords.X;
            movePlayerPacket.y = 255;
            movePlayerPacket.z = playerCoords.Z;
            _client.SendPacket(movePlayerPacket);

            int         count = 0;
            ChunkColumn chunk = null;

            while (count++ < 100 && !_client.Chunks.TryGetValue(chunkCoordinates, out chunk))
            {
                Thread.Sleep(50);
            }

            if (chunk == null)
            {
                Log.Warn($"Failed to locate chunk {chunkCoordinates}. Tried {count} times");
            }
            else
            {
                Log.Debug($"Successful return of chunk {chunkCoordinates} in {sw.ElapsedMilliseconds}ms. Tried {count} times");
            }

            return(chunk);
        }
Beispiel #2
0
        public void SendMovePlayer()
        {
            var package = McpeMovePlayer.CreateObject();

            package.entityId = EntityId;
            package.x        = KnownPosition.X;
            package.y        = KnownPosition.Y + 1.62f;
            package.z        = KnownPosition.Z;
            package.yaw      = KnownPosition.Yaw;
            package.headYaw  = KnownPosition.HeadYaw;
            package.pitch    = KnownPosition.Pitch;
            package.teleport = 0x0;

            SendPackage(package);
        }
Beispiel #3
0
        public void SendMcpeMovePlayer()
        {
            //var movePlayerPacket = McpeMovePlayer.AddReference();
            McpeMovePlayer movePlayerPacket = McpeMovePlayer.CreateObject();

            movePlayerPacket.entityId = 0;
            movePlayerPacket.x        = CurrentLocation.X;
            movePlayerPacket.y        = CurrentLocation.Y;
            movePlayerPacket.z        = CurrentLocation.Z;
            movePlayerPacket.yaw      = 91;
            movePlayerPacket.pitch    = 28;
            movePlayerPacket.headYaw  = 91;

            SendPackage(movePlayerPacket);

            //SendChat("Movin " + CurrentLocation);
        }
Beispiel #4
0
        public void SendMcpeMovePlayer()
        {
            //Log.Debug($"Sending move {EntityId}: {CurrentLocation}");

            McpeMovePlayer movePlayerPacket = McpeMovePlayer.CreateObject();

            movePlayerPacket.runtimeEntityId = EntityId;
            movePlayerPacket.x       = CurrentLocation.X;
            movePlayerPacket.y       = CurrentLocation.Y;
            movePlayerPacket.z       = CurrentLocation.Z;
            movePlayerPacket.yaw     = 91;
            movePlayerPacket.pitch   = 28;
            movePlayerPacket.headYaw = 91;

            _toNodeHandler.WriteBytes(movePlayerPacket.Encode());
            movePlayerPacket.PutPool();
        }
Beispiel #5
0
        public void SetPosition(PlayerLocation position, bool teleport = true)
        {
            KnownPosition   = position;
            LastUpdatedTime = DateTime.UtcNow;

            var package = McpeMovePlayer.CreateObject();

            package.runtimeEntityId = EntityId;
            package.x       = position.X;
            package.y       = position.Y + 1.62f;
            package.z       = position.Z;
            package.yaw     = position.HeadYaw;
            package.headYaw = position.Yaw;
            package.pitch   = position.Pitch;
            package.mode    = (byte)(teleport ? 1 : 0);

            Level.RelayBroadcast(package);
        }
Beispiel #6
0
        public ChunkColumn GenerateChunkColumn(ChunkCoordinates chunkCoordinates)
        {
            var sw           = Stopwatch.StartNew();
            var playerCoords = (BlockCoordinates)chunkCoordinates;

            if (_client.Chunks.TryGetValue(chunkCoordinates, out ChunkColumn chunk))
            {
                Log.Debug($"Successful return of chunk {chunkCoordinates} from cache.");
                _client.Chunks.TryRemove(chunkCoordinates, out _);
                return(chunk);
            }

            _client.Chunks.TryAdd(chunkCoordinates, null);             // register to receive chunks.

            var movePlayerPacket = McpeMovePlayer.CreateObject();

            movePlayerPacket.runtimeEntityId = _client.EntityId;
            movePlayerPacket.x = playerCoords.X;
            movePlayerPacket.y = 255;
            movePlayerPacket.z = playerCoords.Z;
            _client.SendPacket(movePlayerPacket);

            while (sw.ElapsedMilliseconds < 2000)
            {
                _client.Chunks.TryGetValue(chunkCoordinates, out chunk);
                if (chunk != null)
                {
                    break;
                }
                Thread.Sleep(50);
            }

            if (chunk == null)
            {
                Log.Warn($"Failed to locate chunk {chunkCoordinates}. Tried {sw.ElapsedMilliseconds}ms");
            }
            else
            {
                Log.Debug($"Successful return of chunk {chunkCoordinates} in {sw.ElapsedMilliseconds}ms. Have {_client.Chunks.Count} chunks in memory now.");
            }

            return(chunk);
        }
Beispiel #7
0
        public void McpePlayStatus_pooled_encode_perf_test()
        {
            long totalAllocatedBytes = GC.GetTotalMemory(true);
            var  watch = Stopwatch.StartNew();

            byte[] result = new byte[0];
            for (int i = 0; i < _loops; i++)
            {
                var input = McpeMovePlayer.CreateObject();
                result = input.Encode();
                input.PutPool();
            }
            if (result.Length < 0)
            {
                return;
            }
            long watchElapsedMilliseconds = watch.ElapsedMilliseconds;

            Console.WriteLine($"Time: {watchElapsedMilliseconds:N}ms, mem: {(GC.GetTotalMemory(true) - totalAllocatedBytes) / 1024f:N}KB");
        }
Beispiel #8
0
        protected virtual void BroadCastMovement(Player[] players, Entity[] entities)
        {
            DateTime now = DateTime.UtcNow;

            if (players.Length == 0)
            {
                return;
            }

            if (players.Length <= 1 && entities.Length == 0)
            {
                return;
            }

            if (now - _lastBroadcast < TimeSpan.FromMilliseconds(50))
            {
                return;
            }

            DateTime tickTime = _lastSendTime;

            _lastSendTime = DateTime.UtcNow;

            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                int playerMoveCount = 0;
                int entiyMoveCount  = 0;

                foreach (var player in players)
                {
                    if (now - player.LastUpdatedTime <= now - tickTime)
                    {
                        PlayerLocation knownPosition = player.KnownPosition;

                        McpeMovePlayer move = McpeMovePlayer.CreateObject();
                        move.entityId = player.EntityId;
                        move.x        = knownPosition.X;
                        move.y        = knownPosition.Y + 1.62f;
                        move.z        = knownPosition.Z;
                        move.yaw      = knownPosition.Yaw;
                        move.pitch    = knownPosition.Pitch;
                        move.headYaw  = knownPosition.HeadYaw;
                        move.mode     = 0;
                        byte[] bytes = move.Encode();
                        BatchUtils.WriteLength(stream, bytes.Length);
                        stream.Write(bytes, 0, bytes.Length);
                        move.PutPool();
                        playerMoveCount++;
                    }
                }

                foreach (var entity in entities)
                {
                    if (now - entity.LastUpdatedTime <= now - tickTime)
                    {
                        {
                            McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject();
                            moveEntity.entityId    = entity.EntityId;
                            moveEntity.position    = (PlayerLocation)entity.KnownPosition.Clone();
                            moveEntity.position.Y += entity.PositionOffset;
                            byte[] bytes = moveEntity.Encode();
                            BatchUtils.WriteLength(stream, bytes.Length);
                            stream.Write(bytes, 0, bytes.Length);
                            moveEntity.PutPool();
                        }
                        {
                            McpeSetEntityMotion entityMotion = McpeSetEntityMotion.CreateObject();
                            entityMotion.entityId = entity.EntityId;
                            entityMotion.velocity = entity.Velocity;
                            byte[] bytes = entityMotion.Encode();
                            BatchUtils.WriteLength(stream, bytes.Length);
                            stream.Write(bytes, 0, bytes.Length);
                            entityMotion.PutPool();
                        }
                        entiyMoveCount++;
                    }
                }

                if (playerMoveCount == 0 && entiyMoveCount == 0)
                {
                    return;
                }

                if (players.Length == 1 && entiyMoveCount == 0)
                {
                    return;
                }

                McpeBatch batch = BatchUtils.CreateBatchPacket(stream.GetBuffer(), 0, (int)stream.Length, CompressionLevel.Optimal, false);
                batch.AddReferences(players.Length - 1);
                batch.Encode();
                //batch.ValidUntil = now + TimeSpan.FromMilliseconds(50);
                foreach (var player in players)
                {
                    MiNetServer.FastThreadPool.QueueUserWorkItem(() => player.SendPackage(batch));
                }
                _lastBroadcast = DateTime.UtcNow;
            }
        }
Beispiel #9
0
        public static Action <Task, PlayerLocation> DoMoveTo(MiNetClient client)
        {
            Action <Task, PlayerLocation> doMoveTo = (t, loc) =>
            {
                Vector3 originalPosition = client.CurrentLocation.ToVector3();
                Vector3 targetPosition   = loc.ToVector3();

                PlayerLocation lookAtPos = LookAt(originalPosition + new Vector3(0, 1.62f, 0), targetPosition);

                {
                    // First just rotate towards target pos
                    McpeMovePlayer movePlayerPacket = McpeMovePlayer.CreateObject();
                    movePlayerPacket.runtimeEntityId = client.EntityId;
                    movePlayerPacket.x       = client.CurrentLocation.X;
                    movePlayerPacket.y       = client.CurrentLocation.Y;
                    movePlayerPacket.z       = client.CurrentLocation.Z;
                    movePlayerPacket.yaw     = lookAtPos.Yaw;
                    movePlayerPacket.pitch   = lookAtPos.Pitch;
                    movePlayerPacket.headYaw = lookAtPos.HeadYaw;
                }
                float lenght = Math.Abs((originalPosition - targetPosition).Length());

                float stepLen = 0.5f;
                float weight;

                while (true)
                {
                    if (Math.Abs((targetPosition - client.CurrentLocation.ToVector3()).Length()) > stepLen)
                    {
                        float lenLeft = Math.Abs((client.CurrentLocation.ToVector3() - targetPosition).Length());
                        weight = Math.Abs((float)((lenLeft - stepLen) / lenght));

                        client.CurrentLocation = new PlayerLocation(Vector3.Lerp(originalPosition, targetPosition, 1 - weight));

                        McpeMovePlayer movePlayerPacket = McpeMovePlayer.CreateObject();
                        movePlayerPacket.runtimeEntityId = client.EntityId;
                        movePlayerPacket.x       = client.CurrentLocation.X;
                        movePlayerPacket.y       = client.CurrentLocation.Y;
                        movePlayerPacket.z       = client.CurrentLocation.Z;
                        movePlayerPacket.yaw     = lookAtPos.Yaw;
                        movePlayerPacket.pitch   = lookAtPos.Pitch;
                        movePlayerPacket.headYaw = lookAtPos.HeadYaw;

                        client.SendPackage(movePlayerPacket);

                        Thread.Sleep(50);
                        continue;
                    }
                    {
                        client.CurrentLocation = new PlayerLocation(targetPosition);

                        McpeMovePlayer movePlayerPacket = McpeMovePlayer.CreateObject();
                        movePlayerPacket.runtimeEntityId = client.EntityId;
                        movePlayerPacket.x       = client.CurrentLocation.X;
                        movePlayerPacket.y       = client.CurrentLocation.Y;
                        movePlayerPacket.z       = client.CurrentLocation.Z;
                        movePlayerPacket.yaw     = lookAtPos.Yaw;
                        movePlayerPacket.pitch   = lookAtPos.Pitch;
                        movePlayerPacket.headYaw = lookAtPos.HeadYaw;

                        client.SendPackage(movePlayerPacket);
                    }
                    break;
                }
            };

            return(doMoveTo);
        }
Beispiel #10
0
        protected virtual void BroadCastMovement(Player[] players, Entity[] entities)
        {
            if (players.Length == 0)
            {
                return;
            }

            DateTime tickTime = _lastSendTime;

            _lastSendTime = DateTime.UtcNow;
            DateTime now = DateTime.UtcNow;

            MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream();

            int            count = 0;
            McpeMovePlayer move  = McpeMovePlayer.CreateObject();

            foreach (var player in players)
            {
                if (((now - player.LastUpdatedTime) <= now - tickTime))
                {
                    PlayerLocation knownPosition = player.KnownPosition;

                    move.entityId = player.EntityId;
                    move.x        = knownPosition.X;
                    move.y        = knownPosition.Y + 1.62f;
                    move.z        = knownPosition.Z;
                    move.yaw      = knownPosition.Yaw;
                    move.pitch    = knownPosition.Pitch;
                    move.headYaw  = knownPosition.HeadYaw;
                    move.mode     = 0;
                    byte[] bytes = move.Encode();
                    stream.Write(BitConverter.GetBytes(Endian.SwapInt32(bytes.Length)), 0, 4);
                    stream.Write(bytes, 0, bytes.Length);
                    move.Reset();
                    count++;
                }
            }
            move.PutPool();

            McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject();

            moveEntity.entities = new EntityLocations();

            McpeSetEntityMotion entityMotion = McpeSetEntityMotion.CreateObject();

            entityMotion.entities = new EntityMotions();

            foreach (var entity in entities)
            {
                if (((now - entity.LastUpdatedTime) <= now - tickTime))
                {
                    moveEntity.entities.Add(entity.EntityId, entity.KnownPosition);
                    entityMotion.entities.Add(entity.EntityId, entity.Velocity);
                    count++;
                }
            }

            if (moveEntity.entities.Count > 0)
            {
                byte[] bytes = moveEntity.Encode();
                stream.Write(BitConverter.GetBytes(Endian.SwapInt32(bytes.Length)), 0, 4);
                stream.Write(bytes, 0, bytes.Length);
            }
            moveEntity.PutPool();

            if (moveEntity.entities.Count > 0)
            {
                byte[] bytes = entityMotion.Encode();
                stream.Write(BitConverter.GetBytes(Endian.SwapInt32(bytes.Length)), 0, 4);
                stream.Write(bytes, 0, bytes.Length);
            }
            entityMotion.PutPool();

            if (count == 0)
            {
                return;
            }

            McpeBatch batch = McpeBatch.CreateObject(players.Length);

            byte[] buffer = Player.CompressBytes(stream.ToArray(), CompressionLevel.Optimal);
            batch.payloadSize = buffer.Length;
            batch.payload     = buffer;
            batch.Encode(true);

            foreach (var player in players)
            {
                Task sendTask = new Task(obj => ((Player)obj).SendMoveList(batch, now), player);
                sendTask.Start();
            }
        }
Beispiel #11
0
        public Package Hookshot(McpeUseItem packet, Player player)
        {
            if (!(packet.face == -1 & player.Inventory.GetItemInHand().Id == 346))
            {
                return(packet);
            }
            player.Level.BroadcastMessage("test: arrow", type: MessageType.Raw); // Message typeはtip popup messageが選べる!

            const int force = 1;
            //Arrow arrow = new Arrow(player, player.Level, !(force < 1.0));
            Arrow arrow = new Arrow(player, player.Level);

            arrow.KnownPosition    = (PlayerLocation)player.KnownPosition.Clone();
            arrow.KnownPosition.Y += 1.62f;

            arrow.Velocity            = arrow.KnownPosition.GetHeadDirection() * (force * 2.0f * 1.5f);
            arrow.KnownPosition.Yaw   = (float)arrow.Velocity.GetYaw();
            arrow.KnownPosition.Pitch = (float)arrow.Velocity.GetPitch();
            arrow.BroadcastMovement   = true;
            arrow.DespawnOnImpact     = false;

            //arrow.HitEvent += testary;

            arrow.SpawnEntity();
            var setEntityLinkPk = McpeSetEntityLink.CreateObject();

            setEntityLinkPk.riderId  = 0;
            setEntityLinkPk.riddenId = arrow.EntityId;
            setEntityLinkPk.linkType = 2;

            /*    public static final byte TYPE_REMOVE = 0;
             *       public static final byte TYPE_RIDE = 1;
             *   public static final byte TYPE_PASSENGER = 2;
             */
            new Task(() => player.Level.RelayBroadcast(setEntityLinkPk)).Start();



            Level level  = player.Level;
            Mob   entity = new Mob(EntityType.Zombie, level)
            {
                KnownPosition = player.KnownPosition,
            };

            entity.SpawnEntity();
            entity.IsAngry = true;
            entity.


            Task.Run(() =>
            {
                PlayerLocation pos = null;
                while (arrow.Velocity.Length() > 0)
                {
                    pos = arrow.KnownPosition;

                    Thread.Sleep(100);
                }
                McpeMovePlayer mp = McpeMovePlayer.CreateObject();
                mp.entityId       = 0;
                mp.x       = pos.X;
                mp.y       = pos.Y;
                mp.z       = pos.Z;
                mp.pitch   = player.KnownPosition.Pitch;
                mp.headYaw = player.KnownPosition.HeadYaw;
                mp.yaw     = player.KnownPosition.Yaw;
                new Task(() => player.Level.RelayBroadcast(mp)).Start();

                /*Console.Write("done!!" + pos);
                 * Block block = BlockFactory.GetBlockById((byte)1);
                 * block.Coordinates = new BlockCoordinates((int)arrow.KnownPosition.X, (int)arrow.KnownPosition.Y - 2, (int)arrow.KnownPosition.Z);
                 * //arrow.Level.SetBlock(block);
                 *
                 * Block item = new Block(35);
                 * player.KnownPosition.Y -= 2;*/
            });
            return(packet);
        }
Beispiel #12
0
        protected virtual void BroadCastMovement(Player[] players, Player[] updatedPlayers)
        {
            if (updatedPlayers.Length == 0)
            {
                return;
            }

            List <McpeMovePlayer> moves = new List <McpeMovePlayer>();

            foreach (var player in updatedPlayers)
            {
                Player updatedPlayer = player;
                var    knownPosition = updatedPlayer.KnownPosition;

                {
                    McpeMovePlayer move = McpeMovePlayer.CreateObject(players.Length);
                    move.entityId = updatedPlayer.EntityId;
                    move.x        = knownPosition.X;
                    move.y        = knownPosition.Y + 1.62f;
                    move.z        = knownPosition.Z;
                    move.yaw      = knownPosition.Yaw;
                    move.pitch    = knownPosition.Pitch;
                    move.headYaw  = knownPosition.HeadYaw;
                    move.teleport = 0;
                    move.Encode();                     // Optmized

                    moves.Add(move);
                }
            }

            foreach (var p in players)
            {
                Player player   = p;
                Task   sendTask = new Task(delegate { player.SendMoveList(moves); });
                sendTask.Start();
            }


            //MemoryStream stream = new MemoryStream();

            //int messageCount = 0;
            //foreach (var movePlayer in moves)
            //{
            //	{
            //		messageCount++;
            //		byte[] bytes = movePlayer.Encode();
            //		stream.Write(bytes, 0, bytes.Length);
            //	}

            //	movePlayer.PutPool();
            //}

            //if (messageCount > 0)
            //{
            //	McpeBatch batch = McpeBatch.CreateObject(players.Length);
            //	byte[] buffer = CompressBytes(stream.ToArray());
            //	batch.payloadSize = buffer.Length;
            //	batch.payload = buffer;
            //	batch.Encode();

            //	foreach (var p in players)
            //	{
            //		Player player = p;
            //		Task sendTask = new Task(delegate { player.SendPackage(batch, true); });
            //		sendTask.Start();
            //	}
            //}
        }