Example #1
0
        public void SendPacket(byte[] pak, bool addCharID = true)
        {
            // 4 len 0x00 0x00 0x00 0x00

            // 4 packid 0x00 0x00 0x00 0x00
            // 4 acct id 0x00 0x00 0x00 0x00
            // 4 0's 0x00, 0x00, 0x00, 0x00
            // the char id to send too 0x00, 0x00, 0x00, 0x00
            foreach (Character c in this.Players)
            {
                Account a = ServerGlobals.GetAccountFromCharacter(c);
                if (c != null)
                {
                    //Console.WriteLine("Sending to {0}", c.Name);
                    byte[] acctid  = BitConverter.GetBytes(a.AccountId);
                    byte[] curchar = BitConverter.GetBytes(c.GameID);
                    Array.Copy(acctid, 0, pak, 8, acctid.Length);
                    if (addCharID)
                    {
                        Array.Copy(curchar, 0, pak, 16, curchar.Length);
                    }
                    a.Socket.Send(ref pak);
                }
            }
        }
Example #2
0
        public static byte[] CreateItem(Account acct, ushort itemGraphicId, ushort color, string name = "")
        {
            PacketWriter w = new PacketWriter();

            w.WriteByte(1);
            w.WriteUShort(itemGraphicId);
            uint id = ServerGlobals.GetNextAvailableID();

            w.WriteUInt32(id);
            ServerGlobals.CharacterIDsInUse.Add(id);

            w.WriteUShort(color);
            if (name != "")
            {
                w.WriteByte(1);
                w.WriteString(name);
                w.WriteByte(0x4A);
                w.WriteByte(0xFF);
            }
            else
            {
                w.WriteBytes(new byte[] { 0x00, 0xFF });
            }

            w.WriteShort(0);
            w.WriteByte(1);
            w.WriteByte(0); // not equiped
            w.WriteShort(0);
            w.WriteUInt32(0);
            return(w.ToArray());
        }
Example #3
0
 private bool TryRemove(Account acct)
 {
     try
     {
         ServerGlobals.ClearAccount(acct, true);
         return(true);
     }
     catch (Exception ex) { Console.WriteLine(ex.Message); return(false); }
 }
Example #4
0
 public void Init(short graphicID, bool isEquipable)
 {
     Begin     = 0x01;
     GraphicID = graphicID;
     GameID    = (int)ServerGlobals.GetNextAvailableID();
     Color     = 0x4A;
     // add id ?
     Enchants    = new byte[] { 0x20 };
     EndEnchants = 0xFF;
     IsEquipable = (byte)(isEquipable ? 0x01 : 0x00);
 }
Example #5
0
        public override byte[] Serialize(out uint itemid)
        {
            // we want to turn this item into a game packet from a item
            PacketWriter w = new PacketWriter();

            w.WriteByte(1);
            w.WriteUShort(this.GraphicID);
            itemid = ServerGlobals.GetNextAvailableID();
            // this.SetCharSlotID(itemid);
            //Console.WriteLine("(Serialize):Set item with graphic id of {0} to {1}", this.GraphicID, this.ID);
            w.WriteUInt32(itemid);
            w.WriteUShort(this.Color);
            if (!string.IsNullOrEmpty(this.Name))
            {
                w.WriteByte(1);
                w.WriteString(Name);
                w.WriteByte(0x4A);
                w.WriteByte(0x00);
                w.WriteByte(0xFF);
            }
            else
            {
                w.WriteBytes(new byte[] { 0x00, 0x00, 0xFF });
            }
            w.WriteByte(0x00);

            if (this.Location != null)
            {
                if (this.Location.CurrentRoom.RoomID < 65534)
                {
                    w.WriteByte(0x01);
                    w.WriteShort((short)this.Location.CurrentRoom.RoomID);
                }
                else
                {
                    w.WriteByte(0x02);
                    w.WriteUInt32(this.Location.CurrentRoom.RoomID);
                }
                w.WriteUShort(this.Location.X);
                w.WriteUShort(this.Location.Y);
                w.WriteShort(0x01);
            }
            else
            {
                w.WriteByte(0x00);  // if this has a value, than item is in a room
            }
            w.WriteByte(0x01);
            w.WriteByte((byte)(this.isEquipable ? 1:0)); // not equiped
            w.WriteShort(0);                             // hmm ? stats ?
            w.WriteShort(0);                             // hmm ?
            //w.WriteShort(0);
            return(w.GetRawPacket());
        }
Example #6
0
        protected override void OnClientDisconnected(ClientBase client, bool forced)
        {
            Console.WriteLine("WorldServer::OnClientDisconnected");
            Account acct = null;

            if (ServerGlobals.TryGetAccountByClient(client, out acct))
            {
                Console.WriteLine("Trying to clear acct");
                ServerGlobals.ClearAccount(acct, true);
            }

            base.OnClientDisconnected(client, forced);
        }
Example #7
0
        public void AddEntity(IEntity entity, Account sender)
        {
            if (entity is Mobile)
            {
                Mobile m = entity as Mobile;
                this.Npcs.Add(m);
                this.RoomsIDsInUse.Add(m.GameID);
            }
            else if (entity is BaseGameItem)
            {
                // the room needs to hold it's id
                BaseGameItem i = entity as BaseGameItem;

                if (!this.Items.Contains(i))
                {
                    this.Items.Add(i);
                }
                sender.IDsInUse.Remove(i.GameID);
                this.RoomsIDsInUse.Add(i.GameID);
                Console.WriteLine("({0}) items in room {1}.", this.Items.Count, this.RoomID);
            }
            else if (entity is Character)
            {
                // Make sure we add them to room here, then if it is a character
                // that we add them to everyoneelse and not them, dont want 2
                // of you on the screen
                Character c = entity as Character;

                for (int i = 0; i < this.Players.Count; i++)
                {
                    // If it isnt you, then send a packet to add you to room
                    Account a = ServerGlobals.GetAccountFromCharacter(this.Players[i]);
                    //if (c != a.CurrentCharacter)
                    // {
                    byte[] reply = Packet.CreateCharacterRoomPacket(a, c);
                    a.Socket.Send(ref reply);
                    // }
                }
                if (!this.Players.Contains(c))
                {
                    this.Players.Add(c);
                }
            }
            else
            {
                // We do not know what to do.
                // Log
                Console.WriteLine("Unknown Entity was added to room {0}", this.RoomID);
            }
        }
Example #8
0
        /// <summary>
        /// For NPC Movement
        /// </summary>
        /// <param name="pak"></param>
        public void SendMovePacket(byte[] pak)
        {
            foreach (Character c in this.Players)
            {
                Account a = ServerGlobals.GetAccountFromCharacter(c);
                if (c != null)
                {
                    byte[] acctid = BitConverter.GetBytes(a.AccountId);
                    Array.Copy(acctid, 0, pak, 8, acctid.Length);
                    a.Socket.Send(ref pak);

                    Packet.WritePacketToFile(pak, "SendMovePacket.txt");
                }
            }
        }
Example #9
0
        public void AddDoor(ushort graphicID, ushort atLocX, ushort atLocY, ushort atFace,
                            uint doorExit, ushort exitX, ushort exitY)
        {
            Door d = new Door();

            d.ExitRoom             = doorExit;
            d.Location             = new RoomLocation(this.RoomID, atLocX, atLocY, atFace);
            d.Location.CurrentRoom = this;
            d.ExitWalkToX          = exitX;
            d.ExitWalkToY          = exitY;
            d.GraphicID            = graphicID;
            d.GameID = ServerGlobals.GetNextAvailableID();
            this.RoomsIDsInUse.Add(d.GameID);
            d.IsOpen = false; // default closed
            this.Items.Add(d);
        }
Example #10
0
        public override byte[] Serialize(out uint mobileID)
        {
            PacketWriter w = new PacketWriter();

            w.WriteByte(0x01);
            w.WriteUShort(this.GraphicID);
            mobileID    = ServerGlobals.GetNextAvailableID();
            this.GameID = mobileID;
            w.WriteUInt32(this.GameID);
            // Spells
            w.WriteUShort(0x00);
            w.WriteByte(0xFF);
            w.WriteBytes(new byte[] { 0x00, 0x0C, 0x08 });
            w.WriteByte(this.Girth);
            w.WriteByte(this.Height);
            w.WriteByte(0x01);
            w.WriteShort(0x00);
            // 3 more bytes here
            w.WriteBytes(new byte[3]);
            w.WriteInt32(this.CurrentHitPoints);
            w.WriteInt32(this.MaxHitPoints);
            // w.WriteBytes(new byte[3]); not here
            if (this.Location.CurrentRoom.RoomID > 65534)
            {
                w.WriteByte(0x01);
                w.WriteShort((short)this.Location.CurrentRoom.RoomID);
            }
            else
            {
                w.WriteByte(0x02);
                w.WriteUInt32(this.Location.CurrentRoom.RoomID);
            }
            w.WriteUShort(this.Location.X);
            w.WriteUShort(this.Location.Y);
            w.WriteUShort(this.Location.Facing); // always south
                                                 // now inventory
                                                 // w.WriteUShort(0x00);
                                                 // skin color ?
                                                 //w.WriteByte(0x00);
                                                 // w.WriteBytes(new byte[7]);
                                                 // w.WriteByte(0xFF);
            w.WriteUShort(0x02);
            w.WriteUInt32(0x00);
            w.WriteShort(0x00);
            w.WriteByte(0x00);
            return(w.GetRawPacket());
        }
Example #11
0
        public static Door FromCode(ushort graphicID, uint roomNumber, ushort LocX, ushort LocY,
                                    uint exitRoomNumber, ushort exitX, ushort exitY, Room inRoom, bool isOpen = false)
        {
            Door d = new Door();

            d.GraphicID   = graphicID;
            d.GameID      = ServerGlobals.GetNextAvailableID();
            d.IsContainer = isOpen;
            d.Location    = new RoomLocation(roomNumber, LocX, LocY, 0);
            inRoom.RoomsIDsInUse.Add(d.GameID);
            d.Location.CurrentRoom = inRoom;
            d.ExitRoom             = exitRoomNumber;
            d.ExitWalkToX          = exitX;
            d.ExitWalkToY          = exitY;
            d.LookAt = "You see a door.";
            return(d);
        }
Example #12
0
        public void WaitForCommands()
        {
            string input = null;

            this.Start();
            ServerGlobals.Init();
            while (!IsConsoleShuttingDown)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write("\n(Router Server) ");
                Console.ResetColor();
                input = Console.ReadLine();
                string[] args = input.Split(' ');



                switch (args[0].ToLower())
                {
                case "start":
                {
                    this.Start();
                }
                break;

                case "ip":
                {
                    IPAddress add = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
                    Console.WriteLine(add);
                }
                break;

                case "stop":
                    ThreadMgr.Stop();
                    srv.Stop();
                    break;

                case "exit":
                    this.Stop();
                    break;

                default:
                    Console.WriteLine("No Such Command: " + input + "\r\n");
                    break;
                }
            }
        }
Example #13
0
 public virtual void Dispose()
 {
     // return our id to pool
     ServerGlobals.RemoveID(this.RoomID);
     foreach (BaseGameItem i in this.Items)
     {
         ServerGlobals.RemoveID(i.GameID);
     }
     // any npcs ?
     foreach (Entity e in this.Npcs)
     {
         ServerGlobals.RemoveID(e.GameID);
     }
     // remove us from importedrooms
     ScriptResolver.ImportedRooms.Remove(this);
     //clean us up
 }
Example #14
0
 public void DropItemInRoom(Account acct, uint itemID)
 {
     foreach (Character c in this.Players)
     {
         Account      a      = ServerGlobals.GetAccountFromCharacter(c);
         PacketWriter writer = new PacketWriter(42);
         writer.WriteUInt32(a.AccountId);
         writer.WriteUInt32(0);
         writer.WriteUInt32(a.CurrentCharacter.GameID);
         writer.WriteUInt32(this.RoomID);
         writer.WriteByte(0x06);
         writer.WriteUInt32(a.CurrentCharacter.GameID);
         writer.WriteUInt32(itemID);
         writer.WriteBytes(new byte[] { 0x21, 0xFF, 0x00 });
         byte[] r = writer.ToArray();
         a.Socket.Send(ref r);
     }
 }
Example #15
0
        public void MoveEntity(IEntity entity, Account sender, byte[] movePacket = null)
        {
            if (entity is Mobile)
            {
                Mobile m = entity as Mobile;
                // make the mobile create its own move packet
            }
            else if (entity is Item)
            {
                // Items dont walk
            }
            else if (entity is Character)
            {
                // Make sure we add them to room here, then if it is a character
                // that we add them to everyoneelse and not them, dont want 2
                // of you on the screen
                Character c = entity as Character;

                for (int i = 0; i < this.Players.Count; i++)
                {
                    if (this.Players[i] != c)
                    {
                        Account a = ServerGlobals.GetAccountFromCharacter(this.Players[i]);
                        // Send it
                        PacketWriter w = new PacketWriter(0x2A);
                        w.WriteUInt32(a.AccountId);
                        w.WriteUInt32(0);
                        w.WriteUInt32(c.GameID);
                        w.WriteUInt32(this.RoomID);
                        w.WriteBytes(movePacket);
                        byte[] reply = w.ToArray();
                        // Console.WriteLine("Sent {0} to {1}",
                        //    BitConverter.ToString(movePacket), a.CurrentCharacter.Name);
                        a.Socket.Send(ref reply);
                    }
                }
            }
            else
            {
                // We do not know what to do.
                // Log
                Console.WriteLine("Attempted to force Unknown Entity to move in room {0}", this.RoomID);
            }
        }
Example #16
0
        public void UnequipItemInRoom(Account sender, uint itemID)
        {
            // Get the item
            BaseGameItem item = sender.CurrentCharacter.GetInventoryItem(itemID);//Managers.MySqlManager.GetItem(sender.CurrentCharacter.SqlCharId, itemID);

            if (item != null)
            {
                for (int i = 0; i < this.Players.Count; i++)
                {
                    Account a = ServerGlobals.GetAccountFromCharacter(this.Players[i]);
                    // Need to send the to everyone in room
                    PacketWriter w42 = new PacketWriter(0x2A);
                    w42.WriteUInt32(a.AccountId);
                    w42.WriteInt32(0);
                    w42.WriteUInt32(sender.CurrentCharacter.GameID);
                    w42.WriteUInt32(this.RoomID);
                    w42.WriteByte(0x08);
                    w42.WriteUInt32(sender.CurrentCharacter.GameID);
                    w42.WriteUInt32(item.GameID);
                    w42.WriteByte(0x2F);
                    w42.WriteUInt32(sender.CurrentCharacter.GameID);
                    w42.WriteUInt32(sender.CurrentCharacter.GameID);

                    // These are item stats  ? figure them out
                    //w42.WriteBytes(new byte[] { 0x2D, 0x00, 0x00, 0x00, 0x21, 0xFF });
                    // sets max hp, use the chars current if no change
                    w42.WriteUInt32((uint)sender.CurrentCharacter.TotalHP);

                    w42.WriteBytes(new byte[] { 0x21, 0xFF });
                    w42.WriteBytes(new byte[] { 0x00, 0x00 }); // not sure
                    // w42.WriteBytes(new byte[] { 0x00, 0x6A });
                    //w42.WriteBytes(new byte[4]);
                    byte[] p42 = w42.ToArray();
                    a.Socket.Send(ref p42);
                    item.Equipped = false;
                }
                //  bool un = Managers.MySqlManager.EquipItem(sender.CurrentCharacter.SqlCharId, item.CurrentGameID, true);
                // if (!un) Console.WriteLine("Failed to unequip");
            }
            else
            {
                Console.WriteLine("Item was null.");
            }
        }
Example #17
0
        public static byte[] WrapCloud(Account a, short graphicid, out uint id)
        {
            PacketWriter wrap = new PacketWriter();

            wrap.WriteByte(0x3F); // wrapped animation

            #region Object
            PacketWriter ob = new PacketWriter();
            // now the object
            ob.WriteByte(0x01);
            ob.WriteShort(graphicid);
            uint newid = ServerGlobals.GetNextAvailableID();
            id = newid;
            ob.WriteUInt32(id);
            ob.WriteBytes(new byte[3]);
            ob.WriteShort(0xFF);
            // Now room stuff
            if (a.CurrentCharacter.Location.CurrentRoom.RoomID < 65534)
            {
                ob.WriteByte(0x01);
                ob.WriteShort((short)a.CurrentCharacter.Location.CurrentRoom.RoomID);
            }
            else
            {
                ob.WriteByte(0x02);
                ob.WriteUInt32(a.CurrentCharacter.Location.CurrentRoom.RoomID);
            }
            ob.WriteUShort(a.CurrentCharacter.Location.X);
            ob.WriteUShort(a.CurrentCharacter.Location.Y);
            ob.WriteUShort(a.CurrentCharacter.Location.Facing);
            ob.WriteBytes(new byte[5]);
            ob.WriteByte(0xFF);
            #endregion
            byte[] obp = ob.GetRawPacket();
            wrap.WriteShort((short)obp.Length);
            wrap.WriteUInt32(0x00);
            wrap.WriteBytes(obp);
            return(wrap.GetRawPacket());
        }
Example #18
0
 public void Start()
 {
     // if (cmd.Length == 3)
     // {
     srv.TcpIP     = IPAddress.Any;
     srv.TcpPort   = 8001;
     srv.EnableTCP = true;
     ServerGlobals.Init();
     ThreadMgr.Start();
     srv.Start();
     // Turn on and test our mysql
     try
     {
         bool mysql = MySqlManager.TestConnection;
         Console.WriteLine("MySql Initialize {0}", mysql ? "Successful" : "Unsuccessful");
     }
     catch (Exception ex)
     {
         Console.WriteLine("Unable to continue, error is {0}\nPress any key to close!", ex.Message);
         Console.Read();
         this.IsConsoleShuttingDown = true;
     }
 }
Example #19
0
 //Sends packet containg all data except acct is and 0 spacer
 public void SendColorUpdate(Account sender, uint item, byte color)
 {
     foreach (Character c in this.Players)
     {
         Account a = ServerGlobals.GetAccountFromCharacter(c);
         if (a != null)
         {
             PacketWriter w = new PacketWriter(0x2A);
             w.WriteUInt32(a.AccountId);
             w.WriteUInt32(0);
             w.WriteUInt32(a.CurrentCharacter.GameID);
             w.WriteUInt32(this.RoomID);
             w.WriteByte(0x41);
             w.WriteUInt32(a.CurrentCharacter.GameID);
             // the item
             w.WriteUInt32(item);
             w.WriteByte(color);// (8546); // the color ?
             w.WriteByte(0x21);
             w.WriteByte(0xFF);
             byte[] dye = w.ToArray();
             a.Socket.Send(ref dye);
         }
     }
 }
Example #20
0
        public virtual byte[] GetRoomPacket(uint forAccountID)
        {
            PacketWriter writer = new PacketWriter(0x21);

            writer.WriteUInt32(forAccountID);
            writer.WriteInt32(0x00);
            writer.WriteByte(0x01);
            writer.WriteString(this.RoomName);
            writer.WriteBytes(new byte[] { 0x00, 0x17 }); // still dunno
            writer.WriteUInt32(this.RoomID);
            writer.WriteUShort((ushort)this.Background);
            writer.WriteByte(0);
            writer.WriteByte((byte)this.BlockedRoomExits);
            writer.WriteByte(0x00);
            int cast = this.AllowCasting ? 0 : 7;

            writer.WriteByte((byte)cast); // casting no = 7, yes = 0
            // How many decorations
            writer.WriteByte((byte)this.Decorations.Count);
            //writer.WriteUShort((ushort)this.Decorations.Count);
            foreach (RoomDecoration dec in this.Decorations)
            {
                writer.WriteBytes(dec.Serialize());
            }
            // How many items in room ?
            writer.WriteUShort((ushort)(this.Items.Count));
            Console.WriteLine("Loading {0} items.", this.Items.Count);
            foreach (BaseGameItem item in this.Items)
            {
                ItemType type = BaseItem.GetItemType(item.GraphicID);
                if (type == ItemType.Container)
                {
                    Container c = Container.CastFromBaseGameItem(item);
                    writer.WriteBytes(c.Serialize());
                }
                else if (item is Door)
                {
                    Door d = item as Door;
                    writer.WriteBytes(d.Serialize());
                }
                else
                {
                    Item i = Item.CastFromBaseItem(item);
                    writer.WriteBytes(i.Serialize());
                }
            }


            // Now all mobs, players, and npc's
            // Ok so we dont want to list us here or it adds a extra slot to room for npc / player but has no data afterwards
            // so if we are the only player we do not want to list us, we only want to list all other players
            ushort totalMobiles = (ushort)(this.Players.Count - 1 + this.Npcs.Count); // -1 for not you

            writer.WriteUShort(totalMobiles);                                         // everyone but you.
                                                                                      // npcs first

            Console.WriteLine("{0} Players.", this.Players.Count - 1);
            foreach (Character c in this.Players)
            {
                Account a = ServerGlobals.GetAccountFromCharacter(c);
                if (a.AccountId != forAccountID)
                {
                    writer.WriteBytes(c.AddToRoom());                              // dont add yourself twice
                }
            }
            Console.WriteLine("Loading {0} npcs and {1} players.", this.Npcs.Count, this.Players.Count - 1);

            foreach (Mobile m in this.Npcs)
            {
                MobileType type = MobileType.None;
                string     name = BaseMobile.MobNameFromID((short)m.GraphicID, out type);

                if (m is NPC)
                {
                    NPC     n = m as NPC;
                    NPCFace f = n.Face;
                    if (n != null)
                    {
                        writer.WriteBytes(n.AppearInRoom());
                        if (!string.IsNullOrEmpty(n.Name))
                        {
                            name = n.Name;
                        }

                        Console.WriteLine("Added Npc {0} to room {1} at LocX:{2} LocY:{3} Facing:{4}.", name, this.RoomID, m.Location.X, m.Location.Y, m.Location.Facing);
                    }
                }
                if (m is Mob)
                {
                    Mob n = m as Mob;
                    // NPCFace f = n.Face;
                    if (n != null)
                    {
                        writer.WriteBytes(n.Serialize());
                        if (!string.IsNullOrEmpty(n.Name))
                        {
                            name = n.Name;
                        }
                        Console.WriteLine("Added Mob {0} to room {1} at LocX:{2} LocY:{3} Facing:{4}.", name, this.RoomID, m.Location.X, m.Location.Y, m.Location.Facing);
                    }
                }
            }
            writer.WriteBytes(new byte[7]);
            byte[] room         = writer.ToArray();
            string fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "loadroom.txt");

            File.WriteAllText(fileLocation, BitConverter.ToString(room));
            return(room);
        }
Example #21
0
        public void RemoveEntity(IEntity entity, Account sender)
        {
            if (entity is Mobile)
            {
                Mobile m = entity as Mobile;
                // dunno yet
                this.Npcs.Remove(m);
                this.RoomsIDsInUse.Remove(m.GameID);
            }
            else if (entity is BaseGameItem)
            {
                BaseGameItem item = entity as BaseGameItem;
                this.Items.Remove(item);
                sender.IDsInUse.Add(item.GameID);
                this.RoomsIDsInUse.Remove(item.GameID);
                sender.CurrentCharacter.AddInventoryItem(item);

                /*
                 * for (int i = 0; i < this.Players.Count; i++)
                 * {
                 *  Account a = ServerGlobals.GetAccountFromCharacter(this.Players[i]);
                 *  if (a != null)
                 *  {
                 *      PacketWriter w = new PacketWriter(0x2D);
                 *      w.WriteUInt32(a.AccountId);
                 *      w.WriteInt32(0);
                 *      w.WriteUInt32(this.RoomID); // Room to remove from
                 *
                 *      w.WriteUInt32(item.CurrentGameID); // obj to remove
                 *
                 *      w.WriteBytes(new byte[] { 0x00, 0x15, 0x00, 0x69 });
                 *      byte[] rem = w.ToArray();
                 *      a.Socket.Send(ref rem);
                 *  }
                 * }
                 */
                Console.WriteLine("({0}) items in room {1}.", this.Items.Count, this.RoomID);
            }
            else if (entity is Character)
            {
                Character c = entity as Character;
                this.Players.Remove(c);
                for (int i = 0; i < this.Players.Count; i++)
                {
                    Account a = ServerGlobals.GetAccountFromCharacter(this.Players[i]);
                    if (a != null && a != sender) // do not send remove packet to remove yourself
                    {
                        PacketWriter w = new PacketWriter(0x2D);
                        w.WriteUInt32(a.AccountId);
                        w.WriteInt32(0);
                        w.WriteUInt32(this.RoomID); // Room to remove from

                        w.WriteUInt32(c.GameID);    // obj to remove

                        w.WriteBytes(new byte[] { 0x00, 0x15, 0x00, 0x69 });
                        byte[] rem = w.ToArray();
                        a.Socket.Send(ref rem);
                    }
                }
            }
            else
            {
                // We do not know what to do.
                // Log
                Console.WriteLine("Attempted to remove Unknown Entity from room {0}", this.RoomID);
            }
        }
Example #22
0
File: Item.cs Project: 0xefface/tro
        public override byte[] Serialize(out uint itemid)
        {
            // we want to turn this item into a game packet from a item
            PacketWriter w = new PacketWriter();

            w.WriteByte(1);
            w.WriteUShort(this.GraphicID);
            itemid = ServerGlobals.GetNextAvailableID();
            this.SetCharSlotID(itemid);
            //Console.WriteLine("(Serialize):Set item with graphic id of {0} to {1}", this.GraphicID, this.ID);
            w.WriteUInt32(itemid);
            w.WriteUShort(this.Color);
            if (!string.IsNullOrEmpty(this.Name))
            {
                w.WriteByte(1);
                w.WriteString(this.Name);
            }
            if (this.Enchantments.Count == 0)
            {
                w.WriteUShort(0x00);
            }
            else
            {
                w.WriteByte(0x00);
                foreach (byte b in this.Enchantments)
                {
                    w.WriteByte(b);
                }
            }
            w.WriteByte(0xFF);
            if (this.IsGoldOrMana)
            {
                w.WriteByte(0x02);
                w.WriteInt32(this.Value);
            }

            else
            {
                w.WriteByte(0x00);
            }
            if (this.IsInRoom)
            {
                if (this.Location.CurrentRoom.RoomID < 65534)
                {
                    w.WriteByte(0x01);
                    w.WriteShort((short)this.Location.CurrentRoom.RoomID);
                }
                else
                {
                    w.WriteByte(0x02);
                    w.WriteUInt32(this.Location.CurrentRoom.RoomID);
                }
                w.WriteUShort(this.Location.X);
                w.WriteUShort(this.Location.Y);
                w.WriteShort(0x01);
            }
            else
            {
                w.WriteByte(0x00);  // if this has a value, than item is in a room
            }
            int i = this.IsInventoryItem ? 1 : 0;

            w.WriteByte((byte)i);
            int e = this.Equipped ? 1 : 0;

            w.WriteByte((byte)e); // not equiped
            w.WriteShort(0);      // hmm ? stats ?
            w.WriteShort(0);      // hmm ?
            //w.WriteShort(0);
            return(w.GetRawPacket());
        }
Example #23
0
        public void WaitForCommands()
        {
            string input = null;

            this.Start();
            LoadScripts();
            InitGameData();
            while (!IsConsoleShuttingDown)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write("\n[{Realm World Server}] ");
                Console.ResetColor();
                input = Console.ReadLine();
                string[] args = input.Split(' ');



                switch (args[0].ToLower())
                {
                case "pass":
                {
                    // we gonna hash passwords here man !!
                    string password = string.Empty;

                    if (args.Length >= 2)
                    {
                        // get the pass
                        password = args[1];
                        string hash = RealmOffline.Accounts.PasswordHash.CreateHash(password);
                        bool   back = RealmOffline.Accounts.PasswordHash.ValidatePassword(password, hash);
                        Console.WriteLine("We got password ({0}) and hash ({1}), they {2} match.", password, hash,
                                          back ? "do":" do not");
                    }
                }
                break;

                case "/mob":
                {
                    if (args.Length >= 2)
                    {
                        short id = 0;
                        if (short.TryParse(args[1], out id))
                        {
                            MobileType type = MobileType.None;
                            string     name = BaseMobile.MobNameFromID(id, out type);
                            Console.WriteLine("We found {0} for MobID {1}, its type is {2}",
                                              name, id, type);
                        }
                    }
                }
                break;

                case "/sm":
                {
                    if (args.Length >= 3)
                    {
                        //1 = topic 2 on = message
                        string topic = input.Split('|').First();
                        string msg   = input.Split('|').Last();
                        Console.WriteLine("Topic ({0}) Message ({1})", topic, msg);
                    }
                }
                break;

                case "terd":
                {
                    BaseMobile.Init();
                    string[] m = BaseMobile.NonHumanoid.Select(x => x.Value).ToArray();
                    foreach (string c in m)
                    {
                        Console.WriteLine(c);
                    }
                }
                break;

                case "fight":
                {
                    // We want stats 2 stats 2 skills for attack
                    //1 = str 2 = dex 3 = CS 4 = wepskill
                    if (args.Length >= 5)
                    {
                        int str = 0;
                        int dex = 0;
                        int cs  = 0;
                        int wep = 0;
                        if (int.TryParse(args[1], out str) &&
                            int.TryParse(args[2], out dex) &&
                            int.TryParse(args[3], out cs) &&
                            int.TryParse(args[4], out wep))
                        {
                            Console.WriteLine("We got STR:({0}) DEX:({1}) CS:({2}) Wep:({3})",
                                              str, dex, cs, wep);

                            // Lets formulate a chance
                            int[] attacker = { str, dex, cs, wep };
                            int[] oppn     = { 12, 12, 5, 5 };

                            // int hitpoints = 3000; // both have 3k hp
                            int aper = attacker.Sum();
                            int oper = oppn.Sum();

                            Console.WriteLine("We start with {0}% for Attacker and {1}% for opponent.", aper, oper);
                        }
                    }
                }
                break;

                case "id":
                {
                    if (args.Length == 2)
                    {
                        short id = 0;
                        if (short.TryParse(args[1], out id))
                        {
                            bool vali = Mob.IsValidID(id);
                            Console.WriteLine("{0} {1} a valid id.", id, vali ? "is" : "is not");
                        }
                    }
                }
                break;

                case "ts":
                {
                    byte[] t = new byte[] { 0x21, 0xFF, 0x00, 0x9C };
                    Array.Reverse(t);
                    double   d  = (double)BitConverter.ToInt32(t, 0);
                    DateTime dt = MagicMail.UnixTimeStampToDateTime(d);
                    Console.WriteLine(dt);
                }
                break;

                    #region Account Creation and Deletion
                case "account":
                {
                    string useage = "[Create an Account]\naccount create username password emailaddress secretword 3 \n" +
                                    "Last value is User Priv's\n" +
                                    "5 = Player, 4 = EventHost 3 = Guide 2 = Moderator 1 = Admin 0 = Owner \n" +
                                    "\n" +
                                    "[Delete an Account]\n" +
                                    "account delete username";
                    string createUseage = "[Create an Account]\naccount create username password emailaddress secretword 3 \n" +
                                          "Last value is User Priv's\n" +
                                          "5 = Player, 4 = EventHost 3 = Guide 2 = Moderator 1 = Admin 0 = Owner \n";
                    string deleteUsage = "[Delete an Account]\n" +
                                         "account delete username";

                    // create or delete
                    if (args.Length > 2)
                    {
                        if (args[1].ToLower() == "create")
                        {
                            if (args[2].ToLower() == "help" || args[2] == string.Empty)
                            {
                                Console.WriteLine(createUseage);
                            }
                            else
                            {
                                string error = string.Empty;
                                if (!MySqlManager.ValidateAccount(args, out error))
                                {
                                    if (error.Trim() != string.Empty)
                                    {
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.WriteLine(error);
                                        Console.ResetColor();
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("Account {0} Created.", args[2]);
                                }
                            }
                        }
                        else if (args[1].ToLower() == "delete")
                        {
                            if (args.Length != 3)
                            {
                                Console.WriteLine(deleteUsage);
                            }
                            else
                            {
                                bool delete = MySqlManager.DeleteUserAccount(args[2].ToLower());
                                if (delete)
                                {
                                    Console.WriteLine("Removed Account {0}.", args[2].ToLower());
                                }
                                else
                                {
                                    Console.WriteLine("Account {0} not found.", args[2].ToLower());
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("create or delete are the only valid options.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(useage);
                    }
                    // Ok this will be the command to create a new account

                    /*
                     * We need
                     * 2: Username
                     * 3: Password
                     * 4: email address
                     * 5: secret word
                     * 6: Priv Level
                     * string string string string int
                     */
                }
                    #endregion
                    break;

                case "spell":
                {
                    List <Account> accts = ServerGlobals.GetAccountsInRoom(1310);
                    Console.WriteLine("Total in room 1310 {0}", accts.Count);
                }
                break;

                case "online":
                {
                    int online = 0;
                    if (ServerGlobals.LoggedInAccounts != null)
                    {
                        online = ServerGlobals.LoggedInAccounts.Count;
                    }
                    Console.WriteLine("Currently {0} accounts online.", online);
                }
                break;

                case "clear":
                    Console.Clear();
                    break;

                case "mem":
                {
                    // Returns the current memory usage
                    Console.WriteLine(MemoryUseDisplay);
                }
                break;

                case "q":
                {
                    TestObject t       = new TestObject();
                    string     fileloc = Path.Combine(ServerGlobals.BaseDirectory, "TestSerialize.xml");

                    SerializedObject.Serialize(t, fileloc);

                    TestObject t1 = SerializedObject.Deserialize <TestObject>(fileloc);
                    Console.WriteLine(t1.ID);
                }
                break;

                case "touint16":
                {
                    if (args.Length == 2)
                    {
                        // should be 4 letters / numbers
                        string toParse = args[1].Trim();
                        // split string in half
                        if (toParse.Length == 4)
                        {
                            string      b1    = toParse.Substring(0, 2);
                            string      b2    = toParse.Substring(2);
                            List <byte> blist = new List <byte>();

                            try
                            {
                                byte a;
                                byte b;
                                a = Byte.Parse(b1, NumberStyles.HexNumber);
                                b = Byte.Parse(b2, NumberStyles.HexNumber);
                                blist.Add(a);
                                blist.Add(b);
                                byte[] f1     = blist.ToArray();
                                ushort result = BitConverter.ToUInt16(f1, 0);
                                Console.WriteLine("UInt16 result of bytes [{0}] [{1}] = {2}",
                                                  BitConverter.ToString(new byte[] { a }), BitConverter.ToString(new byte[] { b }), result);
                            }
                            catch { Console.WriteLine("Bad Parse"); }
                        }
                    }
                }
                break;

                case "toint32":
                {
                    if (args.Length == 2)
                    {
                        // should be 8 letters / numbers
                        string toParse = args[1].Trim();
                        // split string in half
                        if (toParse.Length == 8)
                        {
                            string      b1    = toParse.Substring(0, 2);
                            string      b2    = toParse.Substring(2, 2);
                            string      b3    = toParse.Substring(4, 2);
                            string      b4    = toParse.Substring(6);
                            List <byte> blist = new List <byte>();

                            try
                            {
                                byte a;
                                byte b;
                                byte c;
                                byte d;
                                a = Byte.Parse(b1, NumberStyles.HexNumber);
                                b = Byte.Parse(b2, NumberStyles.HexNumber);
                                c = Byte.Parse(b3, NumberStyles.HexNumber);
                                d = Byte.Parse(b4, NumberStyles.HexNumber);
                                blist.Add(a);
                                blist.Add(b);
                                blist.Add(c);
                                blist.Add(d);
                                byte[] f1 = blist.ToArray();
                                f1.Reverse();
                                Int32 result = BitConverter.ToInt32(f1, 0);
                                Console.WriteLine("Int32 result of bytes [{0}] [{1}] [{2}] [{3}]= {4}",
                                                  BitConverter.ToString(new byte[] { a }), BitConverter.ToString(new byte[] { b }),
                                                  BitConverter.ToString(new byte[] { c }), BitConverter.ToString(new byte[] { d }),
                                                  result);
                            }
                            catch { Console.WriteLine("Bad Parse"); }
                        }
                    }
                }
                break;

                case "touint32":
                {
                    if (args.Length == 2)
                    {
                        // should be 8 letters / numbers
                        string toParse = args[1].Trim();
                        // split string in half
                        if (toParse.Length == 8)
                        {
                            string      b1    = toParse.Substring(0, 2);
                            string      b2    = toParse.Substring(2, 2);
                            string      b3    = toParse.Substring(4, 2);
                            string      b4    = toParse.Substring(6);
                            List <byte> blist = new List <byte>();

                            try
                            {
                                byte a;
                                byte b;
                                byte c;
                                byte d;
                                a = Byte.Parse(b1, NumberStyles.HexNumber);
                                b = Byte.Parse(b2, NumberStyles.HexNumber);
                                c = Byte.Parse(b3, NumberStyles.HexNumber);
                                d = Byte.Parse(b4, NumberStyles.HexNumber);
                                blist.Add(a);
                                blist.Add(b);
                                blist.Add(c);
                                blist.Add(d);
                                byte[] f1 = blist.ToArray();
                                f1.Reverse();
                                UInt32 result = BitConverter.ToUInt32(f1, 0);
                                Console.WriteLine("Int32 result of bytes [{0}] [{1}] [{2}] [{3}]= {4}",
                                                  BitConverter.ToString(new byte[] { a }), BitConverter.ToString(new byte[] { b }),
                                                  BitConverter.ToString(new byte[] { c }), BitConverter.ToString(new byte[] { d }),
                                                  result);
                            }
                            catch { Console.WriteLine("Bad Parse"); }
                        }
                    }
                }
                break;

                case "tobytes32":
                {
                    // the next value is a number
                    if (args.Length == 2)
                    {
                        int value = 0;
                        if (int.TryParse(args[1], out value))
                        {
                            byte[] conv = BitConverter.GetBytes(value);
                            Console.WriteLine(BitConverter.ToString(conv));
                        }
                    }
                }
                break;

                case "m2ushort":
                {
                    // we have 2 bytes space 2 bytes 0 1 2 args or 3 total

                    if (args.Length == 3)
                    {
                        try
                        {
                            string firstushort  = args[1].Trim();
                            string secondushort = args[2].Trim();

                            string      b1    = firstushort.Substring(0, 2);
                            string      b2    = firstushort.Substring(2, 2);
                            string      b3    = secondushort.Substring(0, 2);
                            string      b4    = secondushort.Substring(2, 2);
                            List <byte> blist = new List <byte>();


                            byte a;
                            byte b;
                            byte c;
                            byte d;
                            a = Byte.Parse(b1, NumberStyles.HexNumber);
                            b = Byte.Parse(b2, NumberStyles.HexNumber);
                            c = Byte.Parse(b3, NumberStyles.HexNumber);
                            d = Byte.Parse(b4, NumberStyles.HexNumber);
                            byte[] fArg = { a, b };
                            byte[] sArg = { c, d };
                            ushort vone = BitConverter.ToUInt16(fArg, 0);
                            ushort vtwo = BitConverter.ToUInt16(sArg, 0);
                            Console.WriteLine("We got {0} and {1} for a difference of {2} or {3}",
                                              vone, vtwo, vone - vtwo, vtwo - vone);
                        }
                        catch { Console.WriteLine("Bad Parse"); }
                    }
                }
                break;

                case "bc":
                {
                    if (args.Length > 2)
                    {
                        // get the rest of this string
                        StringBuilder build = new StringBuilder();
                        foreach (string s in args)
                        {
                            build.Append(s + " ");
                        }
                        string   first  = build.ToString().Remove(0, 3);
                        string[] parse2 = first.Split(' ');
                        // whats the color ?
                        int color = -1;
                        if (int.TryParse(parse2[0], out color))
                        {
                            // Fix the last string again then
                            build.Clear();
                            foreach (string s in parse2)
                            {
                                build.Append(s + " ");
                            }
                            string message       = build.ToString().Remove(0, parse2[0].Length + 1);
                            byte[] messagePacket = RealmPacketIO.BuildGossipPacket(color, message, "SERVER");
                            ServerGlobals.SendToAllClients(messagePacket);
                            byte[] messagePacket2 = Packet.ChatPacket(color, 1, message, "SERVER");
                            ServerGlobals.SendToAllClients(messagePacket2);
                            //Console.WriteLine(message);
                        }
                        else
                        {
                            Console.WriteLine("Bad color argument of {0}", parse2[0]);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Usage: bc 10 I want to say hi !!");
                    }
                }
                break;

                case "sta":
                {
                    //string fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ServerPacket24Message.txt");
                    byte[] fake0 = RealmPacketIO.ServerMessageType1(
                        "A Topic Test", "A Very Nice Message, we couldnt have much nicer, so dont be a dick!");        //RealmPacketIO.GetByteArrayFromFile(fileLocation);

                    if (args.Length == 2)
                    {
                        if (args[1].ToLower() == "len")
                        {
                            Console.WriteLine("Message Packet Length {0}",
                                              BitConverter.ToString(BitConverter.GetBytes(fake0.Length - 8)));
                        }
                    }
                    else
                    {
                        //ServerPacket24Message.txt

                        ServerGlobals.SendToAllClients(fake0);
                    }
                }
                break;

                case "sta2":
                {
                    // ServerMessagePacket81.txt
                    string fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ServerMessagePacket81.txt");
                    byte[] fake0        = RealmPacketIO.ServerMessageType2(
                        "F**k you and da horse you roded in on foo!");        //RealmPacketIO.GetByteArrayFromFile(fileLocation);
                    if (args.Length == 2)
                    {
                        if (args[1].ToLower() == "len")
                        {
                            Console.WriteLine("Message Packet Length {0}",
                                              BitConverter.ToString(BitConverter.GetBytes(fake0.Length - 8)));
                        }
                    }
                    else
                    {
                        // ServerMessagePacket81.txt

                        ServerGlobals.SendToAllClients(fake0);
                    }
                }
                break;

                case "switch":
                {
                    if (ServerGlobals.LoadFromSql)
                    {
                        ServerGlobals.LoadFromSql = false; Console.WriteLine("Loading from CharData.txt File.");
                    }
                    else
                    {
                        ServerGlobals.LoadFromSql = true; Console.WriteLine("Loading from MySql.");
                    }
                }
                break;

                case "croom":
                {
                    // Sends a packet derived from a filename in our debug dir
                    // args[1] is the filename
                    string fileLocation = string.Empty;
                    if (args.Length != 2)
                    {
                        Console.WriteLine("Please supply a filename with extention.");
                    }
                    else
                    {
                        if (args[1].Trim() == "off")
                        {
                            RealmPacketIO.CustomRoomLoad = false;
                            RealmPacketIO.CustomRoomFile = string.Empty;
                        }
                        else
                        {
                            fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), args[1].Trim());
                            if (!File.Exists(fileLocation))
                            {
                                Console.WriteLine("Unable to find file {0}", fileLocation);
                            }
                            else
                            {
                                RealmPacketIO.CustomRoomLoad = true;
                                RealmPacketIO.CustomRoomFile = args[1].Trim();
                                Console.WriteLine("CustomFile Atrrib = {0} for file {1}",
                                                  RealmPacketIO.CustomRoomLoad, RealmPacketIO.CustomRoomFile);
                            }
                        }
                    }
                }
                break;

                case "to":
                {
                }
                break;

                case "itempacket":
                {
                    for (int f = 0; f < 2001; f++)
                    {
                        Item i = new Item();
                        i.GraphicID = 1444;
                        i.Color     = 0x69;
                        //uint id = 0;
                        // byte[] r = i.Ser();
                        //byte[] item = i.Serialize(out id);
                        Console.Write("\rItem:{0}.          ", f);
                    }
                }
                break;

                case "send":
                {
                    // Sends a packet derived from a filename in our debug dir
                    // args[1] is the filename
                    string fileLocation = string.Empty;
                    if (args.Length != 2)
                    {
                        Console.WriteLine("Please supply a filename with extention.");
                    }
                    else
                    {
                        fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), args[1].Trim());
                        if (!File.Exists(fileLocation))
                        {
                            Console.WriteLine("Unable to find file {0}", fileLocation);
                        }
                        else
                        {
                            byte[] fileToArray = null;
                            try { fileToArray = RealmPacketIO.GetByteArrayFromFile(fileLocation); }
                            catch (Exception ex) { Console.WriteLine(ex.Message); }

                            if (fileToArray != null)
                            {
                                ServerGlobals.SendToAllClients(fileToArray);
                                Console.WriteLine("Sent {0}", fileLocation);
                            }
                        }
                    }
                }
                break;

                case "a":
                {
                }
                break;

                case "x":
                {
                    //byte[] p = RealmPacketIO.Test;
                    // ServerGlobals.SendToAllClients(p);
                }
                break;

                case "w":
                {
                    /*
                     * string fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Lookat.txt");
                     * byte[] fake0 = RealmPacketIO.GetByteArrayFromFile(fileLocation);
                     * byte[] len = BitConverter.GetBytes(fake0.Length);
                     * Console.WriteLine(BitConverter.ToString(len));
                     */
                }
                break;

                case "t":
                {
                    // try to get this room
                    Room found = null;
                    if (ScriptResolver.ImportedRooms.TryGetRoom(213, out found))
                    {
                        Console.WriteLine("Found room {0}", 213);
                        Console.WriteLine(BitConverter.ToString(found.GetRoomPacket(3)));
                    }
                }
                break;

                case "tt":
                {
                    // messin wit commands
                    if (ScriptResolver.ImportedCommands.Count == 0)
                    {
                        Console.WriteLine("WTF ?");
                    }
                    else
                    {
                        Console.WriteLine("{0} commands found.", ScriptResolver.ImportedCommands.Count);
                        foreach (Command cmd in ScriptResolver.ImportedCommands)
                        {
                            Console.WriteLine("Command Name {0}", cmd.CommandName);

                            Command[] cmds = ScriptResolver.ImportedCommands.CommandsByPrefix('/');
                            Console.WriteLine("Commands by Prefix returned {0}", cmds.Length);
                        }
                    }
                }
                break;

                case "lmob":
                {
                    string fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mobtest.txt");
                    byte[] fake0        = RealmPacketIO.GetByteArrayFromFile(fileLocation);
                    byte[] len          = BitConverter.GetBytes(fake0.Length);
                    Console.WriteLine(BitConverter.ToString(len));
                }
                break;

                case "mob":
                {
                    string fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mobtest.txt");
                    byte[] fake0        = RealmPacketIO.GetByteArrayFromFile(fileLocation);
                    Console.WriteLine("We have a packet {0} in length.", fake0.Length);
                    ServerGlobals.SendToAllClients(fake0);
                }
                break;

                case "s":
                {
                    string        fileLocation = Path.Combine(ServerGlobals.BaseDirectory, "Test.log");
                    RLog          log          = new RLog(fileLocation);
                    StringBuilder b            = new StringBuilder();
                    for (int i = 2000; i != 0; i--)
                    {
                        b.AppendLine(i.ToString());
                    }
                    log.LogMessage(b.ToString(), MessageType.Warning);
                }
                break;

                case "start":
                {
                    this.Start();
                }
                break;

                case "stop":
                    ThreadMgr.Stop();
                    srv.Stop();
                    break;

                case "exit":
                    this.Stop();
                    break;

                default:
                    Console.WriteLine("No Such Command: " + input + "\r\n");
                    break;
                }
            }
        }
Example #24
0
        public override byte[] Serialize(out uint itemid)
        {
            itemid      = ServerGlobals.GetNextAvailableID();
            this.GameID = itemid;
            Packets.PacketWriter w = new Packets.PacketWriter();
            w.WriteByte(0x01);
            w.WriteUShort(this.GraphicID);
            w.WriteUInt32(this.GameID);
            w.WriteShort(0x73); // color
            if (string.IsNullOrEmpty(this.Name))
            {
                w.WriteByte(0x00); // 1 here if name follows, then followed by 2 bytes with length of naem
            }
            else
            {
                w.WriteByte(0x01);
                w.WriteString(this.Name); // prefixes the string with a short length
            }
            if (this.Enchantments.Count == 0)
            {
                w.WriteShort(0x00);
                w.WriteByte(0x00);
            }
            else
            {
                int totalenchants = this.Enchantments.Count;
                // must be 2 0x00 and 1 buff, if the item only has 1 buff.
                if (totalenchants == 1)
                {
                    w.WriteShort(0x00);
                    w.WriteByte(this.Enchantments[0]);
                }
                else if (totalenchants == 2)
                {
                    w.WriteByte(0x00);
                    w.WriteByte(this.Enchantments[0]);
                    w.WriteByte(this.Enchantments[1]);
                }
                else
                {
                    foreach (byte b in this.Enchantments)
                    {
                        w.WriteByte(b);
                    }
                }
            }
            w.WriteByte(0xFF);
            w.WriteByte(0x00);
            if (this.Location == null)
            {
                w.WriteByte(0x00);
            }                                                 // 0x01 if in a room with loc
            else
            {
                // Add room location
                if (this.Location.RoomNumber < 65534)
                {
                    w.WriteByte(0x01);
                    w.WriteUShort((ushort)this.Location.RoomNumber);
                }
                else
                {
                    w.WriteByte(0x02);
                    w.WriteUInt32(this.Location.RoomNumber);
                }
                w.WriteUShort(this.Location.X);
                w.WriteUShort(this.Location.Y);
                w.WriteUShort(this.Location.Facing);
            }
            w.WriteByte(0x01); // Not sure most items have this
            w.WriteByte((byte)(this.Equipped ? 1:0));
            // Not sure on all this
            w.WriteByte(0x00); // dunno
            w.WriteShort((short)this.ContainedItems.Count);


            w.WriteByte(0x00);
            // w.WriteUShort(0x00);
            w.WriteUInt32(0x00);
            // w.WriteBytes(new byte[3]);
            //w.WriteByte(0x00); // items in backpack just after this ?
            return(w.GetRawPacket());
        }
Example #25
0
        public virtual MemoryStream ToStream()
        {
            MemoryStream _stream = new MemoryStream();
            BinaryWriter _writer = new BinaryWriter(_stream);

            _writer.Write((byte)0x01);
            _writer.Write(GraphicID);
            _writer.Write(GameID);

            if (this is GameMobile == false)
            {
                _writer.Write(Color);
            }

            // empty byte here ?
            _writer.Write((byte)Name.Length > 0);

            if (this is GameItem)
            {
                // Custom Engraved name ?
                if (!string.IsNullOrEmpty(Name))
                {
                    _writer.Write(LengthPrefixedString(Name));
                }
            }

            if (this is GameCharacter)
            {
                _writer.Write((byte)0x00);
                _writer.Write((byte)0x53);
                _writer.Write((byte)0x54);
            }

            foreach (byte a in affectedStates)
            {
                _writer.Write(a);
            }
            _writer.Write((byte)0xFF);


            if (Value == 0)
            {
                _writer.Write((byte)0x00);             // Money section
            }
            else if (Value < 0xFFFF)
            {
                _writer.Write((byte)0x01);
                _writer.Write((ushort)Value);
            }
            else
            {
                _writer.Write((byte)0x02);
                _writer.Write(Value);
            }

            if (this is GameMobile || this is GameCharacter)
            {
                // Now only NPC's need this data
                // Mobs do not
                _writer.Write(XSpeed); //0C
                _writer.Write(YSpeed); // 08
                _writer.Write(Width);
                _writer.Write(Height);
                _writer.Write(Profession);
                _writer.Write(Race);
                _writer.Write(Gender);
                _writer.Write(LengthPrefixedString(Name));
                _writer.Write(Peaceful);
                _writer.Write(CurrentHealth);
                _writer.Write(MaximumHealth);
            }

            if (Location == null)
            {
                _writer.Write((byte)0x00); // isinroom
            }
            else
            {
                byte roomStatus = 0x00;

                //if (!Location.DoScaler) roomStatus |= 0x10;

                if (Location.RoomNumber <= 0xFFFF)
                {
                    roomStatus |= 0x01;
                }
                else
                {
                    roomStatus |= 0x02;
                }

                _writer.Write(roomStatus);

                if (Location.RoomNumber <= 0xFFFF)
                {
                    _writer.Write((ushort)Location.RoomNumber);
                }
                else
                {
                    _writer.Write(Location.RoomNumber);
                }

                _writer.Write(Location.X);
                _writer.Write(Location.Y);
                _writer.Write(Location.Facing);
            }

            if (this is GameCarryable)
            {
                GameCarryable o = this as GameCarryable;
                _writer.Write(o.isVisible);
            }
            else if (this is GameMobile == false)
            {
                _writer.Write((byte)0x00);
            }

            if (this is GameWeapon)
            {
                GameWeapon o = this as GameWeapon;
                _writer.Write(o.isWorn);
            }

            if (this is GameMobile)
            {
                //  _writer.Write((byte)0x00);
                GameMobile o = this as GameMobile;

                if (o.Inventory != null)
                {
                    _writer.Write((ushort)(o.Inventory.Count + 1));
                    _writer.Write((byte)0x01);
                    _writer.Write((ushort)0x04);
                    _writer.Write(ServerGlobals.GetNextAvailableID());
                    _writer.Write((ushort)0x00);
                    _writer.Write((byte)0x00);
                    _writer.Write((byte)0xFF);
                    _writer.Write((ushort)0x00);
                    _writer.Write((byte)0x00);
                }
                else
                {
                    _writer.Write((ushort)0x00);
                }
            }

            if (Head != null)
            {
                _writer.Write(Head.ToArray());
                //  _writer.Write((byte)0x00); // wont appear on screen unless i do this
            }
            // 6 bytes after head start inventory
            if (this is GameWearable)
            {
                GameWearable o = this as GameWearable;
                _writer.Write(o.isWorn);
            }
            else
            {
                _writer.Write((byte)0x00);
            }

            if (this is GameDescribed)
            {
                GameDescribed o = this as GameDescribed;
                _writer.Write(o.isBook);
            }
            else
            {
                _writer.Write((byte)0x00);
            }

            if (this is GameContainer)
            {
                GameContainer o = this as GameContainer;
                _writer.Write((ushort)o.Items.Count);

                foreach (GameEntity e in o.Items)
                {
                    _writer.Write(e.ToByteArray());
                }
            }

            /*
             * // do we have a inventory ?
             *
             * if (this is GameMobile)
             * {
             *  GameMobile o = this as GameMobile;
             *
             *  if (o.Inventory != null)
             *  {
             *
             *   //   _writer.Write((ushort)0x01);//(o.Inventory.Count)); // works if it has 00 inventory
             *      _writer.Write((byte)0x01);
             *      _writer.Write((ushort)0x04);
             *      _writer.Write(ServerGlobals.GetNextAvailableID());
             *      _writer.Write((ushort)0x00);
             *      _writer.Write((byte)0x00);
             *      _writer.Write((byte)0xFF);
             *      _writer.Write((ushort)0x00);
             *      _writer.Write((byte)0x00);
             *  }
             * }
             */
            if (this is GameOpenable)
            {
                GameOpenable o = this as GameOpenable;
                _writer.Write(o.isOpen);
            }

            if (this is GameSwitch)
            {
                GameSwitch o = this as GameSwitch;
                _writer.Write(o.SwitchState);
            }

            byte lockState = 0;

            _writer.Write(lockState);

            byte sitState = 0;

            _writer.Write(sitState);

            _writer.Write(CanTalk);

            _writer.Write(IsInvisible);

            byte combatState = 0;

            _writer.Write(combatState);

            if (this is GameMobile)
            {
                // _writer.Write((ushort)0x00);
                // _writer.Write((byte)0xFF);
            }
            _stream.Position = 0;
            return(_stream);
        }
Example #26
0
 public override byte[] Serialize(out uint id)
 {
     id          = ServerGlobals.GetNextAvailableID();
     this.GameID = id;
     Packets.PacketWriter w = new Packets.PacketWriter();
     w.WriteByte(0x01);
     w.WriteUShort(this.GraphicID);
     w.WriteUInt32(this.GameID);
     w.WriteUShort(this.Color); // color
     if (string.IsNullOrEmpty(this.Name))
     {
         w.WriteByte(0x00); // 1 here if name follows, then followed by 2 bytes with length of naem
     }
     else
     {
         w.WriteByte(0x01);
         w.WriteString(this.Name); // prefixes the string with a short length
     }
     // right after name, if this item is engraved with a custom name
     // the spell byte is 0x4A
     if (!string.IsNullOrEmpty(this.Name))
     {
         w.WriteByte(0x4A);
     }
     else
     {
         w.WriteByte(0x00);
     }
     // identified, added later ? yes
     // how to handle buffs ?
     if (this.Enchantments.Count == 0)
     {
         w.WriteUShort(0x00);
     }
     else
     {
         foreach (byte b in this.Enchantments)
         {
             w.WriteByte(b);
             //Console.WriteLine("Wrote {0} for {1}.", b.ToString("X2"), this.GameID);
         }
     }
     w.WriteByte(0xFF); // buff end
     w.WriteByte(0x00); // i do not know if this byte holds a value
     // Roomlocation, if in a room
     #region Room and Location bytes
     if (this.Location == null)
     {
         w.WriteByte(0x00);                        // no location
     }
     else
     {
         if (this.Location.CurrentRoom != null)
         {
             if (this.Location.CurrentRoom.RoomID <= 65535)
             {
                 w.WriteByte(0x01);
                 w.WriteUShort((ushort)this.Location.CurrentRoom.RoomID);
             }
             else
             {
                 w.WriteByte(0x02);
                 w.WriteUInt32(this.Location.CurrentRoom.RoomID);
             }
             w.WriteUShort(this.Location.X);
             w.WriteUShort(this.Location.Y);
             w.WriteUShort(this.Location.Facing);
         }
         else
         {
             w.WriteByte(0x00); // we do not know what happened, a locations room should never be null
         }
     }
     // Ok locations done
     #endregion
     int i = this.IsInventoryItem ? 1 : 0;
     w.WriteByte((byte)i); // Not sure most items have this
     int e = this.Equipped ? 1 : 0;
     w.WriteByte((byte)e);
     w.WriteByte(0x00); // Do not know
     // Starts info for containers
     if (this.IsContainer)
     {
         w.WriteShort((short)this.ContainedItems.Count);
         // Followed by items contained
     }
     else
     {
         w.WriteShort(0x00);
     }
     //  w.WriteByte(0x00);
     w.WriteUInt32(0x00);
     return(w.GetRawPacket());
 }
Example #27
0
        public void EquipItemInRoom(Account sender, uint itemID)
        {
            // Get the item
            BaseGameItem item = sender.CurrentCharacter.GetInventoryItem(itemID);//Managers.MySqlManager.GetItem(sender.CurrentCharacter.SqlCharId, itemID);

            // now we pull from and equip the items from memory

            if (item != null)
            {
                // bool un = Managers.MySqlManager.EquipItem(sender.CurrentCharacter.SqlCharId, item.CurrentGameID);
                // if (!un) Console.WriteLine("Failed to equip");
                // else
                // {
                item.Equipped = false;     // dont forget to equip it !!
                for (int i = 0; i < this.Players.Count; i++)
                {
                    if (sender.CurrentCharacter != this.Players[i])
                    {
                        Account a = ServerGlobals.GetAccountFromCharacter(this.Players[i]);
                        // Need to send the to everyone in room
                        PacketWriter toRoom = new PacketWriter(0x2A);
                        toRoom.WriteUInt32(a.AccountId);
                        toRoom.WriteInt32(0);
                        toRoom.WriteUInt32(sender.CurrentCharacter.GameID);
                        toRoom.WriteUInt32(this.RoomID);
                        toRoom.WriteByte(0x3F);

                        PacketWriter it = new PacketWriter();
                        it.WriteBytes(item.Serialize());
                        it.WriteByte(0xFF);
                        byte[] justitem = it.GetRawPacket();
                        toRoom.WriteShort((short)justitem.Length);
                        toRoom.WriteUInt32(sender.CurrentCharacter.GameID);
                        toRoom.WriteBytes(justitem);
                        // toRoom.WriteUShort(0x00);
                        //toRoom.WriteByte(0x00);
                        byte[] p42 = toRoom.ToArray();

                        // Try to actually equip it via sql
                        a.Socket.Send(ref p42);

                        string fileLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Equiproom1.txt");
                        File.WriteAllText(fileLocation, BitConverter.ToString(p42));

                        PacketWriter toRoom2 = new PacketWriter(0x2A);
                        toRoom2.WriteUInt32(a.AccountId);
                        toRoom2.WriteInt32(0);
                        toRoom2.WriteUInt32(sender.CurrentCharacter.GameID);
                        toRoom2.WriteUInt32(this.RoomID);
                        toRoom2.WriteByte(0x07);
                        toRoom2.WriteUInt32(sender.CurrentCharacter.GameID);
                        toRoom2.WriteUInt32(itemID);
                        toRoom2.WriteByte(0x2F);
                        toRoom2.WriteUInt32(sender.CurrentCharacter.GameID);
                        toRoom2.WriteUInt32(sender.CurrentCharacter.GameID);

                        // These are item stats  ? figure them out
                        //w42.WriteBytes(new byte[] { 0x2D, 0x00, 0x00, 0x00, 0x21, 0xFF });
                        // sets max hp, use the chars current if no change
                        toRoom2.WriteUInt32((uint)sender.CurrentCharacter.TotalHP);

                        toRoom2.WriteBytes(new byte[] { 0x21, 0xFF });
                        //w42.WriteBytes(new byte[] { 0x00, 0x12 }); // not sure
                        //w42.WriteBytes(new byte[] { 0xA1, 0xF3 });
                        toRoom2.WriteBytes(new byte[4]);
                        byte[] proom2 = toRoom2.ToArray();
                        a.Socket.Send(ref proom2);
                        item.Equipped = false;
                        string fileLocation2 = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Equiproom2.txt");
                        File.WriteAllText(fileLocation2, BitConverter.ToString(p42));
                    }
                }
                //}
            }
            else
            {
                Console.WriteLine("Item was null.");
            }
        }