Example #1
0
        public async Task TeleportAsync(PositionF pos)
        {
            this.LastPosition = this.Position;
            this.Position     = pos;
            await this.client.Server.World.ResendBaseChunksAsync(this.client);

            var tid = Globals.Random.Next(0, 999);

            await client.Server.Events.InvokePlayerTeleportedAsync(
                new PlayerTeleportEventArgs
                (
                    this,
                    this.Position,
                    pos
                ));

            await this.client.QueuePacketAsync(new ClientPlayerPositionLook
            {
                Position   = pos,
                Flags      = PositionFlags.None,
                TeleportId = tid
            });

            this.TeleportId = tid;
        }
Example #2
0
        public void MagnitudeTest()
        {
            PositionF p = new PositionF(3, 4);
            var       m = p.Magnitude;

            Assert.AreEqual(5, m);
        }
Example #3
0
        internal virtual async Task UpdateAsync(Server server, PositionF position, bool onGround)
        {
            var newPos  = position * 32 * 64;
            var lastPos = this.LastPosition * 32 * 64;

            short newX = (short)(newPos.X - lastPos.X);
            short newY = (short)(newPos.Y - lastPos.Y);
            short newZ = (short)(newPos.Z - lastPos.Z);

            var isNewLocation = position != this.LastPosition;

            if (isNewLocation)
            {
                server.BroadcastPacketWithoutQueue(new EntityPosition
                {
                    EntityId = this.EntityId,

                    DeltaX = newX,
                    DeltaY = newY,
                    DeltaZ = newZ,

                    OnGround = onGround
                }, this.EntityId);

                this.UpdatePosition(position, onGround);
            }
            await Task.CompletedTask;
        }
Example #4
0
 public static byte[] ToByteArray(this PositionF p)
 {
     byte[] data = new byte[8];
     BitConverter.GetBytes(p.X).CopyTo(data, 0);
     BitConverter.GetBytes(p.Y).CopyTo(data, 4);
     return(data);
 }
Example #5
0
        public async Task TeleportAsync(CommandContext Context, [Remaining] PositionF location)
        {
            var player = (Player)Context.Player;
            await player.SendMessageAsync($"ight homie tryna tp you (and sip dicks) {location.X} {location.Y} {location.Z}");

            await player.TeleportAsync(location);
        }
Example #6
0
        public void FloatInitTest()
        {
            PositionF p1 = new PositionF(3, 4);
            PositionF p2 = new PositionF(3f, 4f);

            Assert.AreEqual(p1, p2);
        }
Example #7
0
        public void DivisionTest()
        {
            PositionF p1 = new PositionF(10, 10);
            PositionF p2 = new PositionF(1, 1);
            float     v  = 10;

            Assert.AreEqual(p2, p1 / v);
        }
Example #8
0
        public void InvertTest()
        {
            PositionF p1 = new PositionF(1, 1);
            PositionF p2 = new PositionF(-1, -1);
            PositionF p3 = new PositionF(3, 3);

            Assert.AreEqual(p1, -p2);
        }
Example #9
0
        public void MinusTest()
        {
            PositionF p1 = new PositionF(1, 1);
            PositionF p2 = new PositionF(2, 2);
            PositionF p3 = new PositionF(3, 3);

            Assert.AreEqual(p1, p3 - p2);
        }
Example #10
0
        public void SumTest()
        {
            PositionF p1 = new PositionF(1, 1);
            PositionF p2 = new PositionF(2, 2);
            PositionF p3 = new PositionF(3, 3);

            Assert.AreEqual(p3, p1 + p2);
        }
Example #11
0
 public void UpdatePosition(PositionF pos, Angle yaw, Angle pitch, bool onGround = true)
 {
     this.CopyPosition(true);
     this.Position = pos;
     this.Yaw      = yaw;
     this.Pitch    = pitch;
     this.OnGround = onGround;
 }
Example #12
0
        public async Task WritePositionFAsync(PositionF value)
        {
            var val = (long)((int)value.X & 0x3FFFFFF) << 38;

            val |= (long)((int)value.Z & 0x3FFFFFF) << 12;
            val |= (long)((int)value.Y & 0xFFF);

            await this.WriteLongAsync(val);
        }
Example #13
0
        public void NotEqualTest()
        {
            PositionF p1 = new PositionF(1, 1);
            PositionF p2 = new PositionF(1, 1);
            PositionF p3 = new PositionF(1, 2);

            Assert.False(p1 != p2);
            Assert.True(p1 != p3);
        }
Example #14
0
        public void MultiplicationTest()
        {
            PositionF p1 = new PositionF(10, 10);
            PositionF p2 = new PositionF(1, 1);
            float     v  = 10;

            Assert.AreEqual(p1, p2 * v);
            Assert.AreEqual(p1, v * p2);
        }
Example #15
0
        public void WritePositionF(PositionF value)
        {
            var val = (long)((int)value.X & 0x3FFFFFF) << 38;

            val |= (long)((int)value.Z & 0x3FFFFFF) << 12;
            val |= (long)((int)value.Y & 0xFFF);

            WriteLong(val);
        }
Example #16
0
        internal virtual async Task UpdateAsync(Server server, PositionF position, Angle yaw, Angle pitch, bool onGround)
        {
            var newPos  = position * 32 * 64;
            var lastPos = this.LastPosition * 32 * 64;

            short newX = (short)(newPos.X - lastPos.X);
            short newY = (short)(newPos.Y - lastPos.Y);
            short newZ = (short)(newPos.Z - lastPos.Z);

            var isNewLocation = position != this.LastPosition;

            var isNewRotation = yaw != this.LastYaw || pitch != this.LastPitch;


            if (isNewLocation)
            {
                if (isNewRotation)
                {
                    server.BroadcastPacketWithoutQueue(new EntityPositionAndRotation
                    {
                        EntityId = this.EntityId,

                        DeltaX = newX,
                        DeltaY = newY,
                        DeltaZ = newZ,

                        Yaw = yaw,

                        Pitch = pitch,

                        OnGround = onGround
                    }, this.EntityId);

                    this.CopyLook();
                }
                else
                {
                    server.BroadcastPacketWithoutQueue(new EntityPosition
                    {
                        EntityId = this.EntityId,

                        DeltaX = newX,
                        DeltaY = newY,
                        DeltaZ = newZ,

                        OnGround = onGround
                    }, this.EntityId);
                }

                this.UpdatePosition(position, yaw, pitch, onGround);
            }
            await Task.CompletedTask;
        }
Example #17
0
        public void PositionFSerializationTest()
        {
            var       data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
            PositionF p    = new PositionF {
                X = 0, Y = 0
            };

            var array = p.ToByteArray();

            Assert.AreEqual(data.Length, array.Length);
            Assert.AreEqual(data, array);
        }
Example #18
0
        public void MoveTowardsTest()
        {
            PositionF p1 = new PositionF(3, 3);
            PositionF p2 = new PositionF(4, 4);
            float     maxDistanceDelta = 2;

            PositionF p3 = PositionF.MoveTowards(p1, p2, maxDistanceDelta);
            PositionF p4 = PositionF.MoveTowards(p1, p2, 0);
            PositionF p5 = PositionF.MoveTowards(p2, p2, maxDistanceDelta);

            Assert.AreEqual(p2, p3);
            Assert.AreEqual(p1, p4);
            Assert.AreEqual(p2, p5);
        }
Example #19
0
        public void PositionFDeserializationTest()
        {
            PositionF p1 = new PositionF {
                X = 10, Y = 125
            };

            var array = p1.ToByteArray();

            var p2 = new PositionF();

            p2 = p2.FromBytes(array);

            Assert.AreEqual(p1.X, p2.X);
            Assert.AreEqual(p1.Y, p2.Y);
        }
Example #20
0
        public void Update(float deltaTime)
        {
            if (Unit.PositionF == _targetPosition && Unit.Position == Unit.TargetPosition)
            {
                State = States.Stay;
                Path  = null;
            }

            if (State == States.Stay)
            {
                return;
            }

            Unit.PositionF = PositionF.MoveTowards(Unit.PositionF, _targetPosition, Speed * deltaTime);
        }
Example #21
0
        bool Collides(Axis axis, double offsetX = 0, double offsetY = 0)
        {
            PositionF position = PositionF + new PositionF(offsetX, offsetY);

            if (position[axis] < 0 || position[axis] > Level.Size[axis] - Size[axis])
            {
                return(true);
            }

            var  toRemove  = new HashSet <IDisposable>();
            bool shouldDie = false;

            foreach (Entity entity in Level.Entities.Where(entity => entity != this))
            {
                if (position < entity.Position + entity.Size && position + Size > entity.Position)
                {
                    if (entity is Spike && !(isOnGround && axis == Axis.Vertical))
                    {
                        shouldDie = true;
                    }
                    else if (entity.IsCollidable)
                    {
                        return(true);
                    }
                    else if (entity is Coin coin)
                    {
                        ++scoreboard.Coins;
                        toRemove.Add(coin);
                    }
                }
            }

            if (shouldDie)
            {
                Die();
            }

            foreach (IDisposable entity in toRemove)
            {
                entity.Dispose();
            }

            return(false);
        }
Example #22
0
        internal override async Task UpdateAsync(Server server, PositionF position, Angle yaw, Angle pitch, bool onGround)
        {
            await base.UpdateAsync(server, position, yaw, pitch, onGround);

            this.HeadY = position.Y + 1.62f;

            foreach (var entity in this.World.GetEntitiesNear(this.Position, 0.8f))
            {
                if (entity is ItemEntity item)
                {
                    if (!item.CanPickup)
                    {
                        continue;
                    }

                    server.BroadcastPacketWithoutQueue(new CollectItem
                    {
                        CollectedEntityId = item.EntityId,
                        CollectorEntityId = this.EntityId,
                        PickupItemCount   = item.Count
                    });

                    var slot = this.Inventory.AddItem(new ItemStack(Registry.GetItem(item.Id).Type, item.Count, item.ItemMeta)
                    {
                        Present = true
                    });

                    this.client.SendPacket(new SetSlot
                    {
                        Slot = (short)slot,

                        WindowId = 0,

                        SlotData = this.Inventory.GetItem(slot)
                    });

                    await item.RemoveAsync();
                }
            }
        }
Example #23
0
        internal void BroadcastPlayerDig(PlayerDiggingStore store)
        {
            var digging = store.Packet;

            var block = this.World.GetBlock(digging.Position);

            var player = this.OnlinePlayers.GetValueOrDefault(store.Player);

            switch (digging.Status)
            {
            case DiggingStatus.DropItem:
            {
                var droppedItem = player.GetHeldItem();

                if (droppedItem is null || droppedItem.Type == Material.Air)
                {
                    return;
                }

                var loc = new PositionF(player.Position.X, (float)player.HeadY - 0.3f, player.Position.Z);

                var item = new ItemEntity
                {
                    EntityId      = player + this.World.TotalLoadedEntities() + 1,
                    Count         = 1,
                    Id            = droppedItem.GetItem().Id,
                    EntityBitMask = EntityBitMask.Glowing,
                    World         = this.World,
                    Position      = loc
                };

                this.TryAddEntity(player.World, item);

                var f8 = Math.Sin(player.Pitch.Degrees * ((float)Math.PI / 180f));
                var f2 = Math.Cos(player.Pitch.Degrees * ((float)Math.PI / 180f));

                var f3 = Math.Sin(player.Yaw.Degrees * ((float)Math.PI / 180f));
                var f4 = Math.Cos(player.Yaw.Degrees * ((float)Math.PI / 180f));

                var f5 = Globals.Random.NextDouble() * ((float)Math.PI * 2f);
                var f6 = 0.02f * Globals.Random.NextDouble();

                var vel = new Velocity((short)((double)(-f3 * f2 * 0.3F) + Math.Cos((double)f5) * (double)f6),
                                       (short)((double)(-f8 * 0.3F + 0.1F + (Globals.Random.NextDouble() - Globals.Random.NextDouble()) * 0.1F)),
                                       (short)((double)(f4 * f2 * 0.3F) + Math.Sin((double)f5) * (double)f6));

                this.BroadcastPacketWithoutQueue(new SpawnEntity
                    {
                        EntityId = item.EntityId,
                        Uuid     = Guid.NewGuid(),
                        Type     = EntityType.Item,
                        Position = item.Position,
                        Pitch    = 0,
                        Yaw      = 0,
                        Data     = 1,
                        Velocity = vel
                    });
                this.BroadcastPacketWithoutQueue(new EntityMetadata
                    {
                        EntityId = item.EntityId,
                        Entity   = item
                    });

                player.client.SendPacket(new SetSlot
                    {
                        Slot = player.CurrentSlot,

                        WindowId = 0,

                        SlotData = player.Inventory.GetItem(player.CurrentSlot) - 1
                    });

                player.Inventory.RemoveItem(player.CurrentSlot);
                break;
            }

            case DiggingStatus.StartedDigging:
                this.BroadcastPacketWithoutQueue(new AcknowledgePlayerDigging
                {
                    Position   = digging.Position,
                    Block      = block.Id,
                    Status     = digging.Status,
                    Successful = true
                });

                if (player.Gamemode == Gamemode.Creative)
                {
                    this.BroadcastPacketWithoutQueue(new BlockChange(digging.Position, 0));

                    this.World.SetBlock(digging.Position, Block.Air);
                }
                break;

            case DiggingStatus.CancelledDigging:
                this.BroadcastPacketWithoutQueue(new AcknowledgePlayerDigging
                {
                    Position   = digging.Position,
                    Block      = block.Id,
                    Status     = digging.Status,
                    Successful = true
                });
                break;

            case DiggingStatus.FinishedDigging:
            {
                this.BroadcastPacketWithoutQueue(new AcknowledgePlayerDigging
                    {
                        Position   = digging.Position,
                        Block      = block.Id,
                        Status     = digging.Status,
                        Successful = true
                    });
                this.BroadcastPacketWithoutQueue(new BlockBreakAnimation
                    {
                        EntityId     = player,
                        Position     = digging.Position,
                        DestroyStage = -1
                    });

                this.BroadcastPacketWithoutQueue(new BlockChange(digging.Position, 0));

                this.World.SetBlock(digging.Position, Block.Air);

                var itemId = Registry.GetItem(block.Material).Id;

                if (itemId == 0)
                {
                    break;
                }

                var item = new ItemEntity
                {
                    EntityId      = player + this.World.TotalLoadedEntities() + 1,
                    Count         = 1,
                    Id            = itemId,
                    EntityBitMask = EntityBitMask.Glowing,
                    World         = this.World,
                    Position      = digging.Position + new PositionF(
                        (Globals.Random.NextSingle() * 0.5f) + 0.25f,
                        (Globals.Random.NextSingle() * 0.5f) + 0.25f,
                        (Globals.Random.NextSingle() * 0.5f) + 0.25f)
                };

                this.TryAddEntity(player.World, item);

                this.BroadcastPacketWithoutQueue(new SpawnEntity
                    {
                        EntityId = item.EntityId,
                        Uuid     = Guid.NewGuid(),
                        Type     = EntityType.Item,
                        Position = item.Position,
                        Pitch    = 0,
                        Yaw      = 0,
                        Data     = 1,
                        Velocity = Velocity.FromPosition(digging.Position)
                    });

                this.BroadcastPacketWithoutQueue(new EntityMetadata
                    {
                        EntityId = item.EntityId,
                        Entity   = item
                    });
                break;
            }
            }
        }
Example #24
0
 public void UpdatePosition(PositionF pos, bool onGround = true)
 {
     this.CopyPosition();
     this.Position = pos;
     this.OnGround = onGround;
 }
Example #25
0
 public Particle(ParticleType type, PositionF position, int particleCount)
 {
     Type          = type;
     Position      = position;
     ParticleCount = particleCount;
 }
Example #26
0
 public static PositionF FromBytes(this PositionF p, byte[] data)
 {
     p.X = BitConverter.ToSingle(data, 0);
     p.Y = BitConverter.ToSingle(data, 4);
     return(p);
 }
Example #27
0
 public void WriteAbsolutePositionF(PositionF value)
 {
     WriteDouble(value.X);
     WriteDouble(value.Y);
     WriteDouble(value.Z);
 }
Example #28
0
 public PlayerPosition(PositionF pos, bool onground)
 {
     this.Position = pos;
     this.OnGround = onground;
 }
Example #29
0
 public SpawnPosition(PositionF position)
 {
     Position = position;
 }
Example #30
0
 void Die()
 {
     Viewport.Position.X = 0;
     PositionF           = Level.SpawnPosition;
     isOnGround          = true;
 }