Example #1
0
 // Normal constructor
 internal Player( World _world, string _name, Session _session, Position _pos ) {
     world = _world;
     name = _name;
     nick = name;
     session = _session;
     pos = _pos;
     info = world.db.FindPlayerInfo( this );
 }
Example #2
0
 internal static Packet MakeMove( int id, Position pos )
 {
     Packet packet = new Packet( OpCode.Move );
     packet.Data[1] = ( byte )id;
     packet.Data[2] = ( byte )pos.X;
     packet.Data[3] = ( byte )pos.Z;
     packet.Data[4] = ( byte )pos.Y;
     return packet;
 }
Example #3
0
 public void WriteTeleport( byte id, Position pos ) {
     Write( OpCode.Teleport );
     Write( id );
     Write( pos.X );
     Write( pos.Z );
     Write( pos.Y );
     Write( pos.R );
     Write( pos.L );
 }
Example #4
0
 public void WriteAddEntity( byte id, string name, Position pos ) {
     Write( OutputCodes.AddEntity );
     Write( id );
     Write( name );
     Write( (short)pos.x );
     Write( (short)pos.h );
     Write( (short)pos.y );
     Write( pos.r );
     Write( pos.l );
 }
Example #5
0
 // Normal constructor
 internal Player( World world, string name, Session session, Position position ) {
     if( name == null ) throw new ArgumentNullException( "name" );
     if( session == null ) throw new ArgumentNullException( "session" );
     World = world;
     Session = session;
     Position = position;
     Info = PlayerDB.FindOrCreateInfoForPlayer( name, session.IP );
     spamBlockLog = new Queue<DateTime>( Info.Rank.AntiGriefBlocks );
     ResetAllBinds();
 }
Example #6
0
 public Bot(string name, Position Pos)
 {
     this.name = name;
     this.playerID = 1;
     pos = Pos;
     heading = 0;
     pitch = 0;
     time = 0;
     update = true;
 }
Example #7
0
 public static Packet MakeTeleport( sbyte id, Position pos ) {
     Packet packet = new Packet( OpCode.Teleport );
     packet.Bytes[1] = (byte)id;
     ToNetOrder( pos.X, packet.Bytes, 2 );
     ToNetOrder( pos.Z, packet.Bytes, 4 );
     ToNetOrder( pos.Y, packet.Bytes, 6 );
     packet.Bytes[8] = pos.R;
     packet.Bytes[9] = pos.L;
     return packet;
 }
Example #8
0
 internal static Packet MakeMoveRotate( int id, Position pos )
 {
     Packet packet = new Packet( OpCode.MoveRotate );
     packet.Data[1] = ( byte )id;
     packet.Data[2] = ( byte )( pos.X & 0xFF );
     packet.Data[3] = ( byte )( pos.Z & 0xFF );
     packet.Data[4] = ( byte )( pos.Y & 0xFF );
     packet.Data[5] = pos.R;
     packet.Data[6] = pos.L;
     return packet;
 }
Example #9
0
 public void WriteAddEntity( byte id, Player player, Position pos )
 {
     Write( OutputCodes.AddEntity );
     Write( id );
     Write( player.GetListName() );
     Write( (short)pos.x );
     Write( (short)pos.h );
     Write( (short)pos.y );
     Write( pos.r );
     Write( pos.l );
 }
Example #10
0
        /// <summary>
        /// Sets a bot, as well as the bot values. Must be called before any other bot classes.
        /// </summary>
        public void setBot(String botName, String skinName, String modelName, World botWorld, Position pos, sbyte entityID) {
            Name = botName;
            SkinName =  (skinName ?? SkinName);
            Model =  (modelName ?? Model);
            World = botWorld;
            Position = pos;
            ID = entityID;

            World.Bots.Add(this);
            Server.SaveEntity(this);
        }
Example #11
0
        private bool _isMoving; //if the bot can move, can be changed if it is not boxed in ect

        #endregion Fields

        #region Constructors

        public Bot(string name, Position pos, int iD, World world_)
        {
            Name = name; //bots should be gray :P
            Pos = pos;
            ID = iD;
            world = world_;
            _isMoving = false;
            _direction = Direction.South; //start off at south
            StartNewAIMovement(); //start the while loop in a new thread. I want to change the way the thread works
            //its only like this for testing purposes
        }
Example #12
0
 public void WriteAddEntity( byte id, [NotNull] Player player, Position pos ) {
     if( player == null ) throw new ArgumentNullException( "player" );
     Write( OpCode.AddEntity );
     Write( id );
     Write( player.ListName );
     Write( pos.X );
     Write( pos.Z );
     Write( pos.Y );
     Write( pos.R );
     Write( pos.L );
 }
Example #13
0
        public static Packet MakeAddEntity( sbyte id, [NotNull] string name, Position pos ) {
            if( name == null ) throw new ArgumentNullException( "name" );

            Packet packet = new Packet( OpCode.AddEntity );
            packet.Bytes[1] = (byte)id;
            Encoding.ASCII.GetBytes( name.PadRight( 64 ), 0, 64, packet.Bytes, 2 );
            ToNetOrder( pos.X, packet.Bytes, 66 );
            ToNetOrder( pos.Z, packet.Bytes, 68 );
            ToNetOrder( pos.Y, packet.Bytes, 70 );
            packet.Bytes[72] = pos.R;
            packet.Bytes[73] = pos.L;
            return packet;
        }
Example #14
0
 /// <summary> Creates a new AddEntity (0x07) packet. </summary>
 /// <param name="id"> Entity ID. Negative values refer to "self". </param>
 /// <param name="name"> Entity name. May not be null. </param>
 /// <param name="spawnPosition"> Spawning position for the player. </param>
 /// <exception cref="ArgumentNullException"> name is null </exception>
 public static Packet MakeAddEntity( sbyte id, [NotNull] string name, Position spawnPosition ) {
     if (name == null) throw new ArgumentNullException("name");
     
     Packet packet = new Packet( OpCode.AddEntity );
     //Logger.Log(LogType.Debug, "Send: MakeAddEntity({0}, {1}, {2})", id, name, spawnPosition);
     packet.Bytes[1] = (byte)id;
     Encoding.ASCII.GetBytes( name.PadRight( 64 ), 0, 64, packet.Bytes, 2 );
     ToNetOrder( spawnPosition.X, packet.Bytes, 66 );
     ToNetOrder( spawnPosition.Z, packet.Bytes, 68 );
     ToNetOrder( spawnPosition.Y, packet.Bytes, 70 );
     packet.Bytes[72] = spawnPosition.R;
     packet.Bytes[73] = spawnPosition.L;
     return packet;
 }
Example #15
0
 static void MakeZone( Player player, Position[] marks, object tag ) {
     Zone zone = (Zone)tag;
     zone.xMin = Math.Min( marks[0].x, marks[1].x );
     zone.xMax = Math.Max( marks[0].x, marks[1].x );
     zone.yMin = Math.Min( marks[0].y, marks[1].y );
     zone.yMax = Math.Max( marks[0].y, marks[1].y );
     zone.hMin = Math.Min( marks[0].h, marks[1].h );
     zone.hMax = Math.Max( marks[0].h, marks[1].h );
     player.Message( "Zone \"" + zone.name + "\" created, " + zone.getVolume() + " blocks total." );
     player.world.log.Log( "Player {0} created a new zone \"{1}\" containing {2} blocks.", LogType.UserActivity,
                           player.name,
                           zone.name,
                           zone.getVolume() );
     player.world.map.zones.Add( zone );
 }
Example #16
0
 /// <summary> Resets spawn to the default location.
 /// Sets the spawn point 1 block above the surface of the map (above the first non-air block), right in the center. </summary>
 public void ResetSpawn() {
     if (Blocks == null) {
         // fallback for maps that don't have a block array yet
         Spawn = new Position(Width*16,
                              Length*16,
                              Math.Min(Height*32, Int16.MaxValue));
         return;
     }
     int spawnZ = 0;
     for (int z = Height - 1; z > 0; z--) {
         if (GetBlock(Width/2, Length/2, z) != Block.Air) {
             spawnZ = z + 1;
             break;
         }
     }
     Spawn = new Position(Width*16,
                          Length*16,
                          spawnZ);
 }
Example #17
0
        public static bool IsInRangeOfSpawnpoint( World world, Position PlayerPos )
        {
            try {
                int Xdistance = ( world.Map.Spawn.X / 32 ) - ( PlayerPos.X / 32 );
                int Ydistance = ( world.Map.Spawn.Y / 32 ) - ( PlayerPos.Y / 32 );
                int Zdistance = ( world.Map.Spawn.Z / 32 ) - ( PlayerPos.Z / 32 );

                if ( Xdistance <= 20 && Xdistance >= -20 ) {
                    if ( Ydistance <= 20 && Ydistance >= -20 ) {
                        if ( Zdistance <= 20 && Zdistance >= -20 ) {
                            return true;
                        }
                    }
                }
            } catch ( Exception ex ) {
                Logger.Log( LogType.Error, "GuildHandler.IsInRangeOfSpawnpoint: " + ex );
            }

            return false;
        }
Example #18
0
 public void JoinWorld( [NotNull] World newWorld, WorldChangeReason reason ) {
     if( newWorld == null ) throw new ArgumentNullException( "newWorld" );
     lock( joinWorldLock ) {
         useWorldSpawn = true;
         postJoinPosition = Position.Zero;
         forcedWorldToJoin = newWorld;
         worldChangeReason = reason;
     }
 }
Example #19
0
        void ProcessMovementPacket() {
            BytesReceived += 10;
            reader.ReadByte();
            Position newPos = new Position {
                X = IPAddress.NetworkToHostOrder( reader.ReadInt16() ),
                Z = IPAddress.NetworkToHostOrder( reader.ReadInt16() ),
                Y = IPAddress.NetworkToHostOrder( reader.ReadInt16() ),
                R = reader.ReadByte(),
                L = reader.ReadByte()
            };

            Position oldPos = Position;

            // calculate difference between old and new positions
            Position delta = new Position {
                X = (short)(newPos.X - oldPos.X),
                Y = (short)(newPos.Y - oldPos.Y),
                Z = (short)(newPos.Z - oldPos.Z),
                R = (byte)Math.Abs( newPos.R - oldPos.R ),
                L = (byte)Math.Abs( newPos.L - oldPos.L )
            };

            // skip everything if player hasn't moved
            if( delta.IsZero ) return;

            bool rotChanged = (delta.R != 0) || (delta.L != 0);

            // only reset the timer if player rotated
            // if player is just pushed around, rotation does not change (and timer should not reset)
            if( rotChanged ) ResetIdleTimer();

            if( Info.IsFrozen ) {
                // special handling for frozen players
                if( delta.X * delta.X + delta.Y * delta.Y > AntiSpeedMaxDistanceSquared ||
                    Math.Abs( delta.Z ) > 40 ) {
                    SendNow( PacketWriter.MakeSelfTeleport( Position ) );
                }
                newPos.X = Position.X;
                newPos.Y = Position.Y;
                newPos.Z = Position.Z;

                // recalculate deltas
                delta.X = 0;
                delta.Y = 0;
                delta.Z = 0;

            } else if( !Can( Permission.UseSpeedHack ) ) {
                int distSquared = delta.X * delta.X + delta.Y * delta.Y + delta.Z * delta.Z;
                // speedhack detection
                if( DetectMovementPacketSpam() ) {
                    return;

                } else if( (distSquared - delta.Z * delta.Z > AntiSpeedMaxDistanceSquared || delta.Z > AntiSpeedMaxJumpDelta) &&
                           speedHackDetectionCounter >= 0 ) {

                    if( speedHackDetectionCounter == 0 ) {
                        lastValidPosition = Position;
                    } else if( speedHackDetectionCounter > 1 ) {
                        DenyMovement();
                        speedHackDetectionCounter = 0;
                        return;
                    }
                    speedHackDetectionCounter++;

                } else {
                    speedHackDetectionCounter = 0;
                }
            }

            if( RaisePlayerMovingEvent( this, newPos ) ) {
                DenyMovement();
                return;
            }

            Position = newPos;
            RaisePlayerMovedEvent( this, oldPos );
        }
Example #20
0
 public VisibleEntity( Position newPos, sbyte newId, Rank newRank ) {
     Id = newId;
     LastKnownPosition = newPos;
     MarkedForRetention = true;
     Hidden = true;
     LastKnownRank = newRank;
 }
Example #21
0
        void MoveEntity( [NotNull] VisibleEntity entity, Position newPos ) {
            if( entity == null ) throw new ArgumentNullException( "entity" );
            Position oldPos = entity.LastKnownPosition;

            // calculate difference between old and new positions
            Position delta = new Position {
                X = (short)(newPos.X - oldPos.X),
                Y = (short)(newPos.Y - oldPos.Y),
                Z = (short)(newPos.Z - oldPos.Z),
                R = (byte)Math.Abs( newPos.R - oldPos.R ),
                L = (byte)Math.Abs( newPos.L - oldPos.L )
            };

            bool posChanged = (delta.X != 0) || (delta.Y != 0) || (delta.Z != 0);
            bool rotChanged = (delta.R != 0) || (delta.L != 0);

            if( skipUpdates ) {
                int distSquared = delta.X * delta.X + delta.Y * delta.Y + delta.Z * delta.Z;
                // movement optimization
                if( distSquared < SkipMovementThresholdSquared &&
                    (delta.R * delta.R + delta.L * delta.L) < SkipRotationThresholdSquared &&
                    !entity.SkippedLastMove ) {

                    entity.SkippedLastMove = true;
                    return;
                }
                entity.SkippedLastMove = false;
            }

            Packet packet;
            // create the movement packet
            if( partialUpdates && delta.FitsIntoMoveRotatePacket && fullUpdateCounter < FullPositionUpdateInterval ) {
                if( posChanged && rotChanged ) {
                    // incremental position + absolute rotation update
                    packet = PacketWriter.MakeMoveRotate( entity.Id, new Position {
                        X = delta.X,
                        Y = delta.Y,
                        Z = delta.Z,
                        R = newPos.R,
                        L = newPos.L
                    } );

                } else if( posChanged ) {
                    // incremental position update
                    packet = PacketWriter.MakeMove( entity.Id, delta );

                } else if( rotChanged ) {
                    // absolute rotation update
                    packet = PacketWriter.MakeRotate( entity.Id, newPos );
                } else {
                    return;
                }

            } else {
                // full (absolute position + absolute rotation) update
                packet = PacketWriter.MakeTeleport( entity.Id, newPos );
            }

            entity.LastKnownPosition = newPos;
            SendNow( packet );
        }
Example #22
0
        void ShowEntity( [NotNull] VisibleEntity entity, Position newPos ) {
            if( entity == null ) throw new ArgumentNullException( "entity" );
#if DEBUG_MOVEMENT
            Logger.Log( LogType.Debug, "ShowEntity: {0} now sees {1}", Name, entity.Id );
#endif
            entity.Hidden = false;
            entity.LastKnownPosition = newPos;
            SendNow( PacketWriter.MakeTeleport( entity.Id, newPos ) );
        }
Example #23
0
        void UpdateVisibleEntities() {
            if( World == null ) PlayerOpException.ThrowNoWorld( this );

            // handle following the spectatee
            if( spectatedPlayer != null ) {
                if( !spectatedPlayer.IsOnline || !CanSee( spectatedPlayer ) ) {
                    Message( "Stopped spectating {0}&S (disconnected)", spectatedPlayer.ClassyName );
                    spectatedPlayer = null;
                } else {
                    Position spectatePos = spectatedPlayer.Position;
                    World spectateWorld = spectatedPlayer.World;
                    if( spectateWorld == null ) {
                        throw new InvalidOperationException( "Trying to spectate player without a world." );
                    }
                    if( spectateWorld != World ) {
                        if( CanJoin( spectateWorld ) ) {
                            postJoinPosition = spectatePos;
                            Message( "Joined {0}&S to continue spectating {1}",
                                     spectateWorld.ClassyName,
                                     spectatedPlayer.ClassyName );
                            JoinWorldNow( spectateWorld, false, WorldChangeReason.SpectateTargetJoined );
                        } else {
                            Message( "Stopped spectating {0}&S (cannot join {1}&S)",
                                     spectatedPlayer.ClassyName,
                                     spectateWorld.ClassyName );
                            spectatedPlayer = null;
                        }
                    } else if( spectatePos != Position ) {
                        SendNow( PacketWriter.MakeSelfTeleport( spectatePos ) );
                    }
                }
            }

            // check every player on the current world
            Player[] worldPlayerList = World.Players;
            Position pos = Position;
            for( int i = 0; i < worldPlayerList.Length; i++ ) {
                Player otherPlayer = worldPlayerList[i];
                if( otherPlayer == this || !CanSee( otherPlayer ) ) continue;

                Position otherPos = otherPlayer.Position;
                int distance = pos.DistanceSquaredTo( otherPos );

                // Fetch or create a VisibleEntity object for the player
                VisibleEntity entity;
                if( entities.TryGetValue( otherPlayer, out entity ) ) {
                    entity.MarkedForRetention = true;
                } else {
                    entity = AddEntity( otherPlayer );
                }

                // Re-add player if their rank changed (to maintain correct name colors/prefix)
                if( entity.LastKnownRank != otherPlayer.Info.Rank ) {
                    ReAddEntity( entity, otherPlayer );
                    entity.LastKnownRank = otherPlayer.Info.Rank;
                }

                if( entity.Hidden ) {
                    if( distance < entityShowingThreshold && CanSeeMoving( otherPlayer ) ) {
                        ShowEntity( entity, otherPos );
                    }

                } else {
                    if( distance > entityHidingThreshold || !CanSeeMoving( otherPlayer ) ) {
                        HideEntity( entity );

                    } else if( entity.LastKnownPosition != otherPos ) {
                        MoveEntity( entity, otherPos );
                    }
                }
            }

            // Find entities to remove (not marked for retention).
            foreach( var pair in entities ) {
                if( pair.Value.MarkedForRetention ) {
                    pair.Value.MarkedForRetention = false;
                } else {
                    playersToRemove.Push( pair.Key );
                }
            }

            // Remove non-retained entities
            while( playersToRemove.Count > 0 ) {
                RemoveEntity( playersToRemove.Pop() );
            }

            fullUpdateCounter++;
            if( fullUpdateCounter >= FullPositionUpdateInterval ) {
                fullUpdateCounter = 0;
            }
        }
Example #24
0
 /// <summary> Resets spawn to the default location (top center of the map). </summary>
 public void ResetSpawn()
 {
     Spawn = new Position( Width * 16,
                           Length * 16,
                           Math.Min( short.MaxValue, Height * 32 ) );
 }
Example #25
0
 internal static Packet MakeSelfTeleport( Position pos )
 {
     return MakeTeleport( 255, pos.GetFixed() );
 }
Example #26
0
 public void JoinWorld( [NotNull] World newWorld, WorldChangeReason reason, Position position ) {
     if( newWorld == null ) throw new ArgumentNullException( "newWorld" );
     if( !Enum.IsDefined( typeof( WorldChangeReason ), reason ) ) {
         throw new ArgumentOutOfRangeException( "reason" );
     }
     lock( joinWorldLock ) {
         useWorldSpawn = false;
         postJoinPosition = position;
         forcedWorldToJoin = newWorld;
         worldChangeReason = reason;
     }
 }
Example #27
0
 internal static Packet MakeRotate( int id, Position pos )
 {
     Packet packet = new Packet( OpCode.Rotate );
     packet.Data[1] = (byte)id;
     packet.Data[2] = pos.R;
     packet.Data[3] = pos.L;
     return packet;
 }
 void ShowEntity( [NotNull] VisibleEntity entity, Position newPos )
 {
     if( entity == null ) throw new ArgumentNullException( "entity" );
     entity.Hidden = false;
     entity.LastKnownPosition = newPos;
     SendNow( PacketWriter.MakeTeleport( entity.Id, newPos ) );
 }
Example #29
0
 internal static Packet MakeTeleport( int id, Position pos )
 {
     Packet packet = new Packet( OpCode.Teleport );
     packet.Data[1] = (byte)id;
     ToNetOrder( pos.X, packet.Data, 2 );
     ToNetOrder( pos.Z, packet.Data, 4 );
     ToNetOrder( pos.Y, packet.Data, 6 );
     packet.Data[8] = pos.R;
     packet.Data[9] = pos.L;
     return packet;
 }
 void AddEntity( [NotNull] Player player, Position newPos )
 {
     if( player == null ) throw new ArgumentNullException( "player" );
     if( freePlayerIDs.Count > 0 ) {
         var pos = new VisibleEntity( newPos, freePlayerIDs.Pop(), player.Info.Rank );
         entities.Add( player, pos );
         SendNow( PacketWriter.MakeAddEntity( pos.Id, player.ListName, newPos ) );
     }
 }