Пример #1
0
        internal Position2D FindNearbyPlayer()
        {
            Position2D op = new Position2D(Position.X, Position.Y);

            op.X -= 1;
            if (FadingWorldsServer.Instance.TheGrid.GetBlockAt(op) != null &&
                FadingWorldsServer.Instance.TheGrid.GetBlockAt(op).Entities.Any(e => e.EntityType == EntityType.Player)) {
                return op;
            }
            op.X += 2;
            if (FadingWorldsServer.Instance.TheGrid.GetBlockAt(op) != null &&
                FadingWorldsServer.Instance.TheGrid.GetBlockAt(op).Entities.Any(e => e.EntityType == EntityType.Player)) {
                return op;
            }
            op.X -= 1;
            op.Y -= 1;
            if (FadingWorldsServer.Instance.TheGrid.GetBlockAt(op) != null &&
                FadingWorldsServer.Instance.TheGrid.GetBlockAt(op).Entities.Any(e => e.EntityType == EntityType.Player)) {
                return op;
            }
            op.Y += 2;
            if (FadingWorldsServer.Instance.TheGrid.GetBlockAt(op) != null &&
                FadingWorldsServer.Instance.TheGrid.GetBlockAt(op).Entities.Any(e => e.EntityType == EntityType.Player)) {
                return op;
            }
            return null;
        }
Пример #2
0
 public LimitedEntity(Textures texturename, Position2D pos, float timeToShow)
     : base(texturename, Helper.RandomString(20))
 {
     TimeLeft = timeToShow;
     Position = pos;
     Location = FadingWorldsGameWindow.Instance.GetVectorByPos(pos);
 }
Пример #3
0
 public MoveResult CanMove(Position2D pos)
 {
     if (FadingWorldsServer.Instance.TheGrid.GetBlockAt(pos).IsBlocking)
         return MoveResult.CannotMoveBlocked;
     if (FadingWorldsServer.Instance.TheGrid.GetBlockAt(pos).Entities.Any()) {
         return MoveResult.CannotMoveLivingEntityInTheWay;
     }
     return MoveResult.Moved;
 }
Пример #4
0
 public Block GetBlockAt(Position2D pos)
 {
     if (pos.X < 0 || pos.Y < 0)
         return null;
     if (pos.X > Matrix.Count - 1)
         return null;
     if (pos.Y > Matrix[pos.X].Count - 1)
         return null;
     return Matrix[pos.X][pos.Y];
 }
Пример #5
0
 public Entity(Textures tex, string id)
 {
     EntityType = EntityType.Object;
     IsBlocking = false;
     Id = id;
     SetTexture(tex);
     Angle = 0f;
     Position = new Position2D(1, 1);
     Location = new Vector2(FadingWorldsGameWindow.Instance.Screenwidth/2f, FadingWorldsGameWindow.Instance.Screenheight/2f);
 }
Пример #6
0
 public Position2D FindRandomEmptyGrassBlock()
 {
     while (true) {
         int col = Helper.Random(1, Width - 1);
         int row = Helper.Random(1, Height - 1);
         Position2D pos = new Position2D(col, row);
         Block b = GetBlockAt(pos);
         if (b != null && !b.HasEntity && b.Type == BlockType.Grass) {
             return pos;
         }
     }
 }
Пример #7
0
 public void Reset()
 {
     Health = 20;
     MaxHealth = 20;
     Mana = 0;
     MaxMana = 0;
     Level = 1;
     RegenSpeed = 0.3f;
     Position = new Position2D(-1, -1);
     ExperienceValue = 2000;
     ExperiencePoints = 0;
     NextLevelAt = 300;
 }
Пример #8
0
 public Player()
 {
     Health = 20;
     MaxHealth = 20;
     Mana = 0;
     MaxMana = 0;
     Level = 1;
     EntityType = EntityType.Player;
     RegenSpeed = 0.3f;
     Position = new Position2D(-1, -1);
     ExperienceValue = 2000;
     Weapon = "D6";
 }
Пример #9
0
 internal override void PostMove(MoveResult mv, Position2D pos)
 {
     base.PostMove(mv, pos);
     if (mv == MoveResult.CannotMoveLivingEntityInTheWay) {
         var targetEntity = FadingWorldsGameWindow.Instance.TheGrid.GetBlockAt(pos).Entities.LivingEntity as LivingEntity;
         if (targetEntity != null && (this is Player)) {
             //FadingWorldsGameWindow.Instance.TheLoader.connectionLoop.SendCommand("at|" + targetEntity.Id + "|" +
             //                                                              targetEntity.Position.X + "|" +
             //                                                              targetEntity.Position.Y);
             FadingWorldsGameWindow.Instance.TheLoader.ConnectionLoop.SendPayload(new NetworkPayload
             {
                 Type = PayloadType.Attack,
                 Params = { targetEntity.Id, ""+targetEntity.Position.X, ""+targetEntity.Position.Y }
             });
         }
     }
 }
Пример #10
0
        public void GenerateWorld()
        {
            Random r = new Random();

            for (int i = 0; i < Width; i++) {
                Matrix.Add(new List<Block>());
                for (int j = 0; j < Height; j++) {
                    var pos = new Position2D(i, j);
                    Block box = new Grass(pos);
                    if (i == 0 || i == Width - 1 || j == 0 || j == Height - 1) {
                        box = new Stone(pos);
                    }
                    else if (r.Next(1000) > 950) {
                        box = new Stone(pos);
                    }
                    Matrix[i].Add(box);
                }
            }
            // done
        }
Пример #11
0
 public Vector2 GetVectorByPos(Position2D pos)
 {
     return new Vector2(pos.X*BlockSize + (BlockSize/2), pos.Y*BlockSize + (BlockSize/2));
 }
Пример #12
0
 internal virtual void PostMove(MoveResult mr, Position2D pos)
 {
 }
Пример #13
0
 public Grass(Position2D pos)
     : base(Textures.Grass, pos)
 {
     Type = BlockType.Grass;
 }
Пример #14
0
 public MoveResult CanMove(Position2D pos)
 {
     if (FadingWorldsGameWindow.Instance.TheGrid.GetBlockAt(pos).IsBlocking)
         return MoveResult.CannotMoveBlocked;
     if (FadingWorldsGameWindow.Instance.TheGrid.GetBlockAt(pos).Entities.LivingEntity != null) {
         return MoveResult.CannotMoveLivingEntityInTheWay;
     }
     return MoveResult.Moved;
 }
Пример #15
0
        public MoveResult MoveTo(Position2D newpos)
        {
            var result = CanMove(newpos);
            if (result == MoveResult.Moved) {
                if ((this is Player)) {
                    //FadingWorldsGameWindow.Instance.TheLoader.connectionLoop.SendCommand("mv|self|" + newpos.X + "|" + newpos.Y);
                    FadingWorldsGameWindow.Instance.TheLoader.ConnectionLoop.SendPayload(new NetworkPayload
                    {
                        Type = PayloadType.Move,
                        Params = new List<string>{ "self", "" + newpos.X, "" + newpos.Y }

                    });
                }
                FadingWorldsGameWindow.Instance.TheGrid.GetBlockAt(Position).MoveEntity(this,
                                                                                 FadingWorldsGameWindow.Instance.TheGrid.GetBlockAt(newpos));
                //FadingWorldsGameWindow.Instance.TheGrid.GetBlockAt(Position).Entities.Remove(this);
                //FadingWorldsGameWindow.Instance.TheGrid.GetBlockAt(newpos).Entities.Add(this);
                Position = newpos;
            }
            PostMove(result, newpos);
            return result;
        }
Пример #16
0
 internal virtual void PostAttack(Position2D pos, LivingEntity mob)
 {
 }
Пример #17
0
 public static Vector2 GetVectorByPos(Position2D pos)
 {
     return new Vector2(pos.X * 32 + (32 / 2), pos.Y * 32 + (32 / 2));
 }
Пример #18
0
 internal AttackResult Attack(Position2D pos)
 {
     LivingEntity target =
         FadingWorldsServer.Instance.TheGrid.GetBlockAt(pos).Entities.SingleOrDefault() as LivingEntity;
     if (target == null)
         return AttackResult.InvalidAttack;
     return Attack(target);
 }
Пример #19
0
        /// <summary>
        /// Greate the map from mapData
        /// </summary>
        /// <param name="blockWidth"></param>
        /// <param name="blockHeight"></param>
        /// <param name="mapData"></param>
        public void MakeGrid(int blockWidth, int blockHeight, string mapData)
        {
            if (mapData == "")
                return;
            TheGrid = new Grid(blockWidth, blockHeight);
            int row = 0;
            int col = 0;
            TheGrid.Matrix.Add(new List<Block>());
            foreach (char c in mapData) {
                if (TheGrid.Matrix.Count - 1 < col) {
                    TheGrid.Matrix.Add(new List<Block>());
                }

                var pos = new Position2D(col, row);
                Block box = new Grass(pos);
                if (c == 'S') {
                    box = new Stone(pos);
                }
                TheGrid.Matrix[col].Add(box);
                lock (BlockObjects) {
                    BlockObjects.Add(box);
                }
                box.Location = GetVectorByPos(pos);

                row++;
                if (row == blockHeight) {
                    col++;
                    row = 0;
                }
            }
        }
Пример #20
0
 public LimitedEntity( Position2D pos, float timeToShow)
 {
     TimeLeft = timeToShow;
     Position = pos;
 }
Пример #21
0
 public Stone(Position2D pos)
     : base(Textures.Stone, pos)
 {
     IsBlocking = true;
     Type = BlockType.Stone;
 }
Пример #22
0
 public Stone(Position2D pos)
 {
     Init(pos);
     IsBlocking = true;
     Type = BlockType.Stone;
 }
Пример #23
0
        public MoveResult TryMove(Direction d)
        {
            switch (d) {
                case Direction.Up:
                    Position2D newpos1 = new Position2D(Position.X, Position.Y);
                    if (Position.Y == 0) {
                        newpos1.Y = FadingWorldsServer.Instance.TheGrid.Height - 1;
                    }
                    else {
                        newpos1.Y--;
                    }
                    return MoveTo(newpos1);

                case Direction.Down:
                    Position2D newpos2 = new Position2D(Position.X, Position.Y);
                    if (Position.Y >= FadingWorldsServer.Instance.TheGrid.Height - 1) {
                        newpos2.Y = 0;
                    }
                    else {
                        newpos2.Y++;
                    }
                    return MoveTo(newpos2);

                case Direction.Right:
                    Position2D newpos3 = new Position2D(Position.X, Position.Y);
                    if (Position.X >= FadingWorldsServer.Instance.TheGrid.Width - 1) {
                        newpos3.X = 0;
                    }
                    else {
                        newpos3.X++;
                    }
                    return MoveTo(newpos3);
                case Direction.Left:
                    Position2D newpos4 = new Position2D(Position.X, Position.Y);
                    if (Position.X == 0) {
                        newpos4.X = FadingWorldsServer.Instance.TheGrid.Width - 1;
                    }
                    else {
                        newpos4.X--;
                    }
                    return MoveTo(newpos4);
            }
            throw new Exception("!! Unknown move enum");
        }
Пример #24
0
 internal virtual AttackResult TryAttack(Position2D pos)
 {
     LivingEntity targetEntity =
         FadingWorldsServer.Instance.TheGrid.GetBlockAt(pos).Entities.SingleOrDefault() as LivingEntity;
     if (targetEntity != null) {
         return TryAttack(targetEntity);
     }
     return AttackResult.InvalidAttack;
     ;
 }
Пример #25
0
        internal MoveResult MoveTo(Position2D newpos)
        {
            var result = CanMove(newpos);
            if (result == MoveResult.Moved) {
                FadingWorldsServer.Instance.TheGrid.GetBlockAt(Position).Entities.Remove(this);
                FadingWorldsServer.Instance.TheGrid.GetBlockAt(newpos).Entities.Add(this);
                Position = newpos;
                FadingWorldsServer.Instance.TCPPool.SendPayloadToAllButUser(Id,
                    new NetworkPayload {Type = PayloadType.Move, Params = {Id, "" + newpos.X, "" + newpos.Y}});
                //FadingWorldsServer.Instance.TCPPool.SendMessageToAllButUser(Id, "mv|" + Id + "|" + newpos.X + "|" + newpos.Y);
            }

            PostMove(result, newpos);
            return result;
        }
Пример #26
0
 public Block(Textures tex, Position2D pos)
     : base(tex, Helper.RandomString(20))
 {
     Position = pos;
     Entities = new EntityCollection();
 }
Пример #27
0
 internal virtual void PostMove(MoveResult mv, Position2D pos)
 {
     if (mv == MoveResult.CannotMoveLivingEntityInTheWay) {
         TryAttack(pos);
     }
 }
Пример #28
0
 public Entity()
 {
     Position = new Position2D(1, 1);
     Id = Helper.RandomString(10);
 }
Пример #29
0
 public Grass(Position2D pos)
 {
     Init(pos);
     Type = BlockType.Grass;
 }
Пример #30
0
        private bool HandlePayload(NetworkPayload payload)
        {
            switch (payload.Type)
            {
                case PayloadType.Auth:
                    switch (payload.Command)
                    {
                        case PayloadCommand.AuthPlease:
                            Log("Starting auth against server with version " + payload.Params[0]);
                            TheLoader.ThreadSafeSet(payload.Params[0], TheLoader.txtServerVersion);
                            if (_loggedInUser == null)
                            {
                                SendPayload(new NetworkPayload
                                {
                                    Type = PayloadType.Auth,
                                    Command = PayloadCommand.Login,
                                    Params = {_username, _password}
                                });
                            }
                            else
                            {
                                Log("OOOPS - server wants us to authenticate, but we already are??? :(");
                            }
                            break;
                        case PayloadCommand.OldVersion:
                            Log("OOOPS - server says we are outdated");
                            return false;

                        case PayloadCommand.Fail:
                            Log("Login failed!");
                            return false;

                        case PayloadCommand.Success:
                            if (payload.Params.Count == 1)
                            {
                                var connectionId = payload.Params[0];
                                Log("Our connectionid is " + connectionId);
                                _loggedInUser = _username;

                                TheLoader.State = GameState.WaitingForMap;

                                TheLoader.SetLoggedIn(true);
                                TheLoader.SpawnGame(0, 0, "");
                                //TheLoader.SetVisible(false);

                                while (TheLoader.TheGame == null || !TheLoader.TheGame.IsLoaded)
                                {
                                    Thread.Sleep(100);
                                }
                                SendPayload(new NetworkPayload
                                {
                                    Type = PayloadType.Data,
                                    Command = PayloadCommand.GetMap
                                });
                                return true;
                            }

                            Log("OOOPS - server did not send connectionid");
                            return false;

                        default:
                            throw new Exception("Unknown auth (subCode): '" + payload.Command + "'");
                    }

                    break;
                case PayloadType.Data:
                    switch (payload.Command)
                    {
                        case PayloadCommand.Map:

                            var blockWidth = int.Parse(payload.Params[0]);
                            var blockHeight = int.Parse(payload.Params[1]);
                            var mapData = payload.Params[2];
                            //TheLoader.SetLoggedIn(true);
                            //TheLoader.SetVisible(false);

                            //while (TheLoader.TheGame == null || !TheLoader.TheGame.IsLoaded) {
                            //  Thread.Sleep(100);
                            //}
                            TheLoader.SetMap(blockWidth, blockHeight, mapData);

                            SendPayload(new NetworkPayload
                            {
                                Type = PayloadType.Data,
                                Command = PayloadCommand.MapLoaded
                            });

                            return true;

                        case PayloadCommand.PleaseLoadGame:
                            while (TheLoader.TheGame == null || !TheLoader.TheGame.IsLoaded)
                            {
                                Thread.Sleep(100);
                            }
                            SendPayload(new NetworkPayload
                            {
                                Type = PayloadType.Data,
                                Command = PayloadCommand.GameLoaded
                            });

                            return true;
                        case PayloadCommand.InitPlayer:
                            var confSet = Helper.ConvertDataStringToDictionary(payload.Params[0]);
                            foreach (var keyValuePair in confSet)
                            {
                                var attr = keyValuePair.Key;
                                var val = keyValuePair.Value;

                                switch (attr)
                                {
                                    case "type":
                                        //TheLoader.TheGame.ThePlayer.Health = int.Parse(val);
                                        break;
                                    case "id":
                                        //TheLoader.TheGame.ThePlayer.Health = int.Parse(val);
                                        break;
                                    case "dmg":
                                        TheLoader.TheGame.ThePlayer.Weapon = val;
                                        break;
                                    case "hp":
                                        TheLoader.TheGame.ThePlayer.Health = int.Parse(val);
                                        break;
                                    case "maxhp":
                                        TheLoader.TheGame.ThePlayer.MaxHealth = int.Parse(val);
                                        break;
                                    case "ap":
                                        TheLoader.TheGame.ThePlayer.AttackPower = int.Parse(val);
                                        break;
                                    case "ac":
                                        TheLoader.TheGame.ThePlayer.ArmorClass = int.Parse(val);
                                        break;
                                    case "maxmana":
                                        TheLoader.TheGame.ThePlayer.MaxMana = int.Parse(val);
                                        break;
                                    case "mana":
                                        TheLoader.TheGame.ThePlayer.Mana = int.Parse(val);
                                        break;
                                    case "xp":
                                        TheLoader.TheGame.ThePlayer.ExperiencePoints = int.Parse(val);
                                        break;
                                    case "level":
                                        TheLoader.TheGame.ThePlayer.Level = int.Parse(val);
                                        break;
                                    case "nextlevel":
                                        TheLoader.TheGame.ThePlayer.NextLevelAt = int.Parse(val);
                                        break;

                                    default:
                                        Log("Unknown attr for player: " + attr + " -> " + val + "");
                                        break;
                                }
                            }
                            if (confSet["x"] != null)
                            {
                                var pos = new Position2D(int.Parse(confSet["x"]), int.Parse(confSet["y"]));
                                TheLoader.TheGame.ThePlayer.Position = pos;
                                TheLoader.TheGame.TheGrid.GetBlockAt(pos).Entities.Add(TheLoader.TheGame.ThePlayer);
                                lock (TheLoader.TheGame.GameObjects)
                                {
                                    TheLoader.TheGame.GameObjects.Add(TheLoader.TheGame.ThePlayer);
                                }

                            }
                            else
                            {
                                Log("Did not get user coords for player!!");
                            }
                            return true;
                        case PayloadCommand.InitEntity:
                            var confSet2 = Helper.ConvertDataStringToDictionary(payload.Params[0]);
                            LivingEntity mob2;
                            if (confSet2["type"].Contains("Skeleton"))
                            {
                                mob2 = new Skeleton(confSet2["id"]);
                            }
                            else if (confSet2["type"].Contains("Ghost"))
                            {
                                mob2 = new Ghost(confSet2["id"]);
                            }
                            else
                            {
                                return true;
                            }
                            mob2.Position = new Position2D(int.Parse(confSet2["x"]), int.Parse(confSet2["y"]));
                            mob2.Health = int.Parse(confSet2["hp"]);

                            TheLoader.TheGame.TheGrid.GetBlockAt(new Position2D(int.Parse(confSet2["x"]),
                                int.Parse(confSet2["y"]))).
                                Entities.Add(mob2);
                            lock (TheLoader.TheGame.GameObjects)
                            {
                                TheLoader.TheGame.GameObjects.Add(mob2);
                            }
                            return true;

                        default:
                            throw new Exception("Unknown auth (subCode): '" + payload.Command + "'");
                    }
                    //}
                    break;
                case PayloadType.Message:
                    if (payload.Params.Count > 1)
                    {
                        var from = payload.Params[0];
                        var message = payload.Params[1];
                        if (from.ToUpper().Equals("SYSTEM"))
                            Log("" + from + " " + message + "");
                        if (from.Equals(_loggedInUser))
                            Log("" + from + " " + message + "");
                        else
                            Log("" + from + " " + message + "");
                    }
                    else
                    {
                        // error from server, to few command
                        throw new Exception("unknown ms syntax ");
                    }
                    break;

                case PayloadType.User:
                    //if (parm.Length > 1) {
                    //    string subCode = parm[0];
                    switch (payload.Command)
                    {
                        case PayloadCommand.Login:
                            var confSet = Helper.ConvertDataStringToDictionary(payload.Params[0]);

                            if (confSet["id"] == _username)
                            {
                                return true;
                            }
                            var op = TheLoader.TheGame.GameObjects.GetById(confSet["id"]) as OtherPlayer;
                            if (op == null)
                            {
                                var otherplayer = new OtherPlayer(Textures.Hero, confSet["id"]);
                                var playerPos = new Position2D(int.Parse(confSet["x"]), int.Parse(confSet["y"]));
                                otherplayer.Desc = confSet["id"];
                                otherplayer.Position = playerPos;
                                //	otherplayer.Disconnected = true;
                                otherplayer.Health = int.Parse(confSet["hp"]);
                                otherplayer.MaxHealth = int.Parse(confSet["maxhp"]);
                                otherplayer.Level = int.Parse(confSet["level"]);
                                otherplayer.ExperiencePoints = int.Parse(confSet["xp"]);
                                otherplayer.Mana = int.Parse(confSet["mana"]);

                                TheLoader.TheGame.TheGrid.GetBlockAt(playerPos).Entities.Add(otherplayer);
                                lock (TheLoader.TheGame.GameObjects)
                                {
                                    TheLoader.TheGame.GameObjects.Add(otherplayer);
                                }
                            }
                            break;
                        case PayloadCommand.Logout:
                            var username = payload.Params[0];
                            var otherplayer2 = TheLoader.TheGame.GameObjects.GetById(username) as OtherPlayer;
                            if (otherplayer2 != null)
                            {
                                otherplayer2.OnDeath();
                                TheLoader.TheGame.TheGrid.GetBlockAt(otherplayer2.Position)
                                    .Entities.Remove(otherplayer2);
                                lock (TheLoader.TheGame.GameObjects)
                                {
                                    TheLoader.TheGame.GameObjects.Remove(username);
                                }
                            }
                            break;
                        default:
                            throw new Exception("unknown us subCode " + payload.Command);
                    }
                    //}
                    //else {
                    //    // error from server, to few command
                    //    throw new Exception("unknown us syntax ");
                    //}
                    break;

                case PayloadType.UserList:
                    while (TheLoader.TheGame == null || TheLoader.TheGame.GameObjects == null)
                    {
                        Thread.Sleep(100);
                    }

                    foreach (string t in payload.Params)
                    {
                        string[] configBlocks = t.Split('#');
                        foreach (string configBlock in configBlocks)
                        {
                            if (configBlock.Trim().Length > 0)
                            {
                                Dictionary<string, string> confSet = Helper.ConvertDataStringToDictionary(configBlock);
                                if (confSet["id"] == _username)
                                {
                                    continue;
                                }

                                var op = TheLoader.TheGame.GameObjects.GetById(confSet["id"]) as OtherPlayer;
                                if (op == null)
                                {
                                    op = new OtherPlayer(Textures.Hero, confSet["id"]);
                                    op.Desc = confSet["id"];
                                    var playerPos = new Position2D(int.Parse(confSet["x"]), int.Parse(confSet["y"]));
                                    op.Position = playerPos;
                                    TheLoader.TheGame.TheGrid.GetBlockAt(playerPos).Entities.Add(op);
                                    op.Health = int.Parse(confSet["hp"]);
                                    op.MaxHealth = int.Parse(confSet["maxhp"]);
                                    op.Level = int.Parse(confSet["level"]);
                                    op.ExperiencePoints = int.Parse(confSet["xp"]);
                                    op.Mana = int.Parse(confSet["mana"]);

                                    lock (TheLoader.TheGame.GameObjects)
                                    {
                                        TheLoader.TheGame.GameObjects.Add(op);
                                    }
                                }
                                else
                                {
                                    op.Health = int.Parse(confSet["hp"]);
                                    op.MaxHealth = int.Parse(confSet["maxhp"]);
                                    op.Level = int.Parse(confSet["level"]);
                                    op.ExperiencePoints = int.Parse(confSet["xp"]);
                                    op.Mana = int.Parse(confSet["mana"]);
                                    op.MoveTo(new Position2D(int.Parse(confSet["x"]), int.Parse(confSet["y"])));
                                }
                            }
                        }
                        //string username = userdata[0];
                        //int column = int.Parse(userdata[1]);
                        //int row = int.Parse(userdata[2]);

                        //OtherPlayer op = TheLoader.TheGame.GameObjects.GetById(username) as OtherPlayer;
                        //if (op == null) {
                        //  OtherPlayer o = new OtherPlayer(Textures.Hero, username);
                        //  o.Desc = username;
                        //  Position2D playerPos = new Position2D(column, row);
                        //  o.Position = playerPos;
                        //  TheLoader.TheGame.TheGrid.GetBlockAt(playerPos).Entities.Add(o);
                        //  lock (TheLoader.TheGame.GameObjects) {
                        //    TheLoader.TheGame.GameObjects.Add(o);
                        //  }
                        //}
                        //else {
                        //  op.MoveTo(new Position2D(column, row));
                        //}
                    }
                    break;
                case PayloadType.System:
                    //try
                    //{
                    switch (payload.Command)
                    {
                            //case "start-reconnect":
                            //    Log(DateTime.Now.ToLongTimeString() + ": Starting reconnect sequence");
                            //    TheLoader.StartReconnectSequence();
                            //    return true;
                            //case "whois":
                            //    if (parm.Length >= 3)
                            //        Log("(whois " + parm[1] + ")? " + parm[2]);
                            //    return true;
                        case PayloadCommand.LoggedInDifferentLocation:
                            TheLoader.TheGame.Exit();
                            return true;
                        case PayloadCommand.Ping:
                            SendPayload(new NetworkPayload
                            {
                                Type = PayloadType.System,
                                Command = PayloadCommand.Pong
                            });

                            return true;
                        default:
                            if (payload.Params.Count > 0)
                            {
                                Log("SYSTEM: " + payload.Params[0]);

                            }
                            return true;
                    }
                    //}
                    return true;
                    //}
                    //catch (ThreadAbortException)
                    //{
                    //    throw;
                    //}
                    //catch (Exception ex)
                    //{
                    //    Log(ex.ToString());
                    //    return true;
                    //}
                case PayloadType.Move:
                    string mobId = payload.Params[0];
                    int mobCol = int.Parse(payload.Params[1]);
                    int mobRow = int.Parse(payload.Params[2]);
                    LivingEntity mob = TheLoader.TheGame.GameObjects.GetById(mobId) as LivingEntity;
                    if (mob != null)
                    {
                        mob.MoveTo(new Position2D(mobCol, mobRow));
                        //Log(DateTime.Now.ToLongTimeString() + "MOVEMENT: " + mobId + " moved to " + mobCol + "x" +
                        //    mobRow);
                    }
                    else
                    {
                        Log(DateTime.Now.ToLongTimeString() + "MOVEMENT: " + mobId + " NOT FOUND!!");
                    }

                    return true;
                case PayloadType.EntityChange:

                    string entityId = payload.Params[0];
                    string attribute = payload.Params[1];
                    string deltaValue = payload.Params[2];
                    string newValue = payload.Params[3];

                    var entity = TheLoader.TheGame.GameObjects.GetById(entityId) as LivingEntity;
                    if (entity != null)
                    {
                        switch (attribute)
                        {
                            case "die":
                                Log(DateTime.Now.ToLongTimeString() + " DIED CHANGE");
                                lock (TheLoader.TheGame.GameObjects)
                                {
                                    TheLoader.TheGame.GameObjects.RemoveById(entity.Id);
                                }
                                TheLoader.TheGame.TheGrid.GetBlockAt(entity.Position).Entities.RemoveById(entity.Id);
                                return true;
                            case "hp":
                                Log(DateTime.Now.ToLongTimeString() + " HP CHANGE: " + entityId + " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.Health += int.Parse(deltaValue);
                                if (entity.Health != int.Parse(newValue))
                                {
                                    //throw new Exception("server and client hp out of sync: server=" + newValue + "/client=" + entity.Health);
                                    Log(DateTime.Now.ToLongTimeString() +
                                        "Health server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.Health);
                                }
                                if (int.Parse(deltaValue) < 0)
                                {
                                    FadingWorldsGameWindow.Instance.TemporaryObjects.Add(new LimitedEntity(Textures.Explosion,
                                        entity.Position, 0.12f));
                                }

                                break;

                            case "maxhp":
                                Log(DateTime.Now.ToLongTimeString() + " MaxHealth CHANGE: " + entityId + " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.MaxHealth += int.Parse(deltaValue);
                                if (entity.MaxHealth != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " MaxHealth server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.MaxHealth);
                                }
                                break;
                            case "mana":
                                Log(DateTime.Now.ToLongTimeString() + " Mana CHANGE: " + entityId + " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.Mana += int.Parse(deltaValue);
                                if (entity.Mana != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " Mana server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.Mana);
                                }
                                break;
                            case "maxmana":
                                Log(DateTime.Now.ToLongTimeString() + " MaxMana CHANGE: " + entityId + " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.MaxMana += int.Parse(deltaValue);
                                if (entity.MaxMana != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " MaxMana server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.MaxMana);
                                }
                                break;
                            case "xp":
                                Log(DateTime.Now.ToLongTimeString() + " ExperiencePoints CHANGE: " + entityId +
                                    " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.ExperiencePoints += int.Parse(deltaValue);
                                if (entity.ExperiencePoints != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " ExperiencePoints server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.ExperiencePoints);
                                }
                                break;
                            case "ac":
                                Log(DateTime.Now.ToLongTimeString() + " ArmorClass CHANGE: " + entityId + " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.ArmorClass += int.Parse(deltaValue);
                                if (entity.ArmorClass != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " ArmorClass server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.ArmorClass);
                                }
                                break;
                            case "ap":
                                Log(DateTime.Now.ToLongTimeString() + " AttackPower CHANGE: " + entityId +
                                    " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.AttackPower += int.Parse(deltaValue);
                                if (entity.AttackPower != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " AttackPower server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.AttackPower);
                                }
                                break;
                            case "level":
                                Log(DateTime.Now.ToLongTimeString() + " Level CHANGE: " + entityId + " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.Level += int.Parse(deltaValue);
                                if (entity.Level != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " Level server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.Level);
                                }
                                break;
                            case "nextlevel":
                                Log(DateTime.Now.ToLongTimeString() + "NextLevelAt CHANGE: " + entityId + " changed to " +
                                    newValue +
                                    " -- delta " + deltaValue);
                                entity.NextLevelAt += int.Parse(deltaValue);
                                if (entity.NextLevelAt != int.Parse(newValue))
                                {
                                    Log(DateTime.Now.ToLongTimeString() +
                                        " NextLevelAt server and client hp out of sync: server=" +
                                        newValue +
                                        "/client=" + entity.NextLevelAt);
                                }
                                break;

                            default:
                                Log(DateTime.Now.ToLongTimeString() + "UNKNOWN CHANGE: " + entityId + " changed " +
                                    attribute +
                                    " to " + newValue + " -- delta " + deltaValue);

                                break;
                        }
                    }
                    else
                    {
                        Log(DateTime.Now.ToLongTimeString() + "ENTITYCHANGE: " + entityId + " NOT FOUND!!");
                    }
                    //}

                    return true;
                default:
                    throw new Exception("Unknown (opCode): '" + payload.Type + "'");
            }

            // Default to true
            return true;
        }