public static void Process(ReceivablePacket packet)
    {
        // Server information.
        long objectId = packet.ReadLong();
        CharacterDataHolder npcTemplate   = NpcData.GetNpc(packet.ReadInt());
        CharacterDataHolder characterData = new CharacterDataHolder();

        characterData.SetX(packet.ReadFloat());
        characterData.SetY(packet.ReadFloat());
        characterData.SetZ(packet.ReadFloat());
        characterData.SetHeading(packet.ReadFloat());
        characterData.SetCurrentHp(packet.ReadLong());
        // Client information.
        characterData.SetName(npcTemplate.GetName());
        characterData.SetRace(npcTemplate.GetRace());
        characterData.SetHeight(npcTemplate.GetHeight());
        characterData.SetBelly(npcTemplate.GetBelly());;
        characterData.SetHairType(npcTemplate.GetHairType());
        characterData.SetHairColor(npcTemplate.GetHairColor());
        characterData.SetSkinColor(npcTemplate.GetSkinColor());
        characterData.SetEyeColor(npcTemplate.GetEyeColor());
        characterData.SetHeadItem(npcTemplate.GetHeadItem());
        characterData.SetChestItem(npcTemplate.GetChestItem());
        characterData.SetLegsItem(npcTemplate.GetLegsItem());
        characterData.SetHandsItem(npcTemplate.GetHandsItem());
        characterData.SetFeetItem(npcTemplate.GetFeetItem());
        characterData.SetLeftHandItem(npcTemplate.GetLeftHandItem());
        characterData.SetRightHandItem(npcTemplate.GetRightHandItem());
        characterData.SetMaxHp(npcTemplate.GetMaxHp());
        characterData.SetTargetable(npcTemplate.IsTargetable());

        WorldManager.Instance.UpdateObject(objectId, characterData);
    }
Exemple #2
0
    public IEnumerator CustomizeCharacterAppearance(CharacterDataHolder characterData, DynamicCharacterAvatar newAvatar)
    {
        // Hide avatar until delay ends.
        newAvatar.gameObject.SetActive(false);

        // Unfortunately UMA needs a small delay to initialize.
        yield return(new WaitForSeconds(0.25f));

        // Delay ended. Show avatar.
        newAvatar.gameObject.SetActive(true);

        // Customize character.
        int hairType = characterData.GetHairType();

        if (characterData.GetRace() == 0)
        {
            newAvatar.ChangeRace("HumanMaleDCS");
            if (hairType != 0)
            {
                newAvatar.SetSlot("Hair", _hairModelsMale[characterData.GetHairType()]);
            }
        }
        if (characterData.GetRace() == 1)
        {
            newAvatar.ChangeRace("HumanFemaleDCS");
            if (hairType != 0)
            {
                newAvatar.SetSlot("Hair", _hairModelsFemale[characterData.GetHairType()]);
            }
        }

        // Set colors.
        newAvatar.SetColor("Hair", Util.IntToColor(characterData.GetHairColor()));
        newAvatar.SetColor("Skin", Util.IntToColor(characterData.GetSkinColor()));
        newAvatar.SetColor("Eyes", Util.IntToColor(characterData.GetEyeColor()));
        newAvatar.UpdateColors(true);

        Dictionary <string, DnaSetter> dna = newAvatar.GetDNA();

        dna["height"].Set(characterData.GetHeight());
        dna["belly"].Set(characterData.GetBelly());
        newAvatar.BuildCharacter(false);

        // Set visible equipable armor items.
        EquipItem(newAvatar, characterData.GetHeadItem());
        EquipItem(newAvatar, characterData.GetChestItem());
        EquipItem(newAvatar, characterData.GetLegsItem());
        EquipItem(newAvatar, characterData.GetHandsItem());
        EquipItem(newAvatar, characterData.GetFeetItem());

        // Without this delay, sometimes, we cannot not see mounted weapons.
        yield return(new WaitForSeconds(0.25f));

        // Set visible equipable left and right hand items.
        EquipItem(newAvatar, characterData.GetLeftHandItem());
        EquipItem(newAvatar, characterData.GetRightHandItem());
    }
    private static readonly string VISIBLE_ITEMS_QUERY = "SELECT * FROM character_items WHERE owner=@owner AND slot_id>'0' AND slot_id<'8' ORDER BY slot_id ASC"; // Visible equipment slots are 1 to 7.

    public CharacterSelectionInfoResult(string accountName)
    {
        // Local data.
        List <CharacterDataHolder> characterList = new List <CharacterDataHolder>();

        // Get data from database.
        try
        {
            MySqlConnection con = DatabaseManager.GetConnection();
            MySqlCommand    cmd = new MySqlCommand(CHARACTER_QUERY, con);
            cmd.Parameters.AddWithValue("account", accountName);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                CharacterDataHolder characterData = new CharacterDataHolder();
                string name = reader.GetString("name");
                characterData.SetName(name);
                characterData.SetSlot((byte)reader.GetInt16("slot")); // TODO: Remove cast?
                characterData.SetSelected(reader.GetBoolean("selected"));
                characterData.SetRace((byte)reader.GetInt16("race")); // TODO: Remove cast?
                characterData.SetHeight(reader.GetFloat("height"));
                characterData.SetBelly(reader.GetFloat("belly"));
                characterData.SetHairType((byte)reader.GetInt16("hair_type")); // TODO: Remove cast?
                characterData.SetHairColor(reader.GetInt32("hair_color"));
                characterData.SetSkinColor(reader.GetInt32("skin_color"));
                characterData.SetEyeColor(reader.GetInt32("eye_color"));
                characterData.SetX(reader.GetFloat("x"));
                characterData.SetY(reader.GetFloat("y"));
                characterData.SetZ(reader.GetFloat("z"));
                characterData.SetHeading(reader.GetFloat("heading"));
                characterData.SetExperience(reader.GetInt64("experience"));
                characterData.SetHp(reader.GetInt64("hp"));
                characterData.SetMp(reader.GetInt64("mp"));
                characterData.SetAccessLevel((byte)reader.GetInt16("access_level")); // TODO: Remove cast?

                // Also get items for this character.
                try
                {
                    MySqlConnection con2 = DatabaseManager.GetConnection();
                    MySqlCommand    cmd2 = new MySqlCommand(VISIBLE_ITEMS_QUERY, con2);
                    cmd2.Parameters.AddWithValue("owner", name);
                    MySqlDataReader reader2 = cmd2.ExecuteReader();
                    while (reader2.Read())
                    {
                        int slotId = reader2.GetInt32("slot_id");
                        int itemId = reader2.GetInt32("item_id");
                        switch (slotId)
                        {
                        case 1:
                            characterData.SetHeadItem(itemId);
                            break;

                        case 2:
                            characterData.SetChestItem(itemId);
                            break;

                        case 3:
                            characterData.SetLegsItem(itemId);
                            break;

                        case 4:
                            characterData.SetHandsItem(itemId);
                            break;

                        case 5:
                            characterData.SetFeetItem(itemId);
                            break;

                        case 6:
                            characterData.SetLeftHandItem(itemId);
                            break;

                        case 7:
                            characterData.SetRightHandItem(itemId);
                            break;
                        }
                    }
                    con2.Close();
                }
                catch (Exception e)
                {
                    LogManager.Log(e.ToString());
                }

                // Add gathered data to character list.
                characterList.Add(characterData);
            }
            con.Close();
        }
        catch (Exception e)
        {
            LogManager.Log(e.ToString());
        }

        // Send the data.
        WriteShort(2); // Packet id.
        WriteByte(characterList.Count);
        foreach (CharacterDataHolder characterData in characterList)
        {
            WriteString(characterData.GetName());
            WriteByte(characterData.GetSlot());
            WriteByte(characterData.IsSelected() ? 1 : 0);
            WriteByte(characterData.GetRace());
            WriteFloat(characterData.GetHeight());
            WriteFloat(characterData.GetBelly());
            WriteByte(characterData.GetHairType());
            WriteInt(characterData.GetHairColor());
            WriteInt(characterData.GetSkinColor());
            WriteInt(characterData.GetEyeColor());
            WriteInt(characterData.GetHeadItem());
            WriteInt(characterData.GetChestItem());
            WriteInt(characterData.GetLegsItem());
            WriteInt(characterData.GetHandsItem());
            WriteInt(characterData.GetFeetItem());
            WriteInt(characterData.GetLeftHandItem());
            WriteInt(characterData.GetRightHandItem());
            WriteFloat(characterData.GetX());
            WriteFloat(characterData.GetY());
            WriteFloat(characterData.GetZ());
            WriteFloat(characterData.GetHeading());
            WriteLong(characterData.GetExperience());
            WriteLong(characterData.GetHp());
            WriteLong(characterData.GetMp());
            WriteByte(characterData.GetAccessLevel());
        }
    }
    private void Update()
    {
        if (exitingWorld)
        {
            return;
        }
        if (kickFromWorld)
        {
            ExitWorld();
            MainManager.Instance.LoadScene(MainManager.LOGIN_SCENE);
            return;
        }

        lock (updateMethodLock)
        {
            // Distance check.
            foreach (GameObject obj in gameObjects.Values)
            {
                if (obj != null && CalculateDistance(obj.transform.position) > VISIBILITY_RADIUS)
                {
                    WorldObject worldObject = obj.GetComponent <WorldObject>();
                    if (worldObject != null && !deleteQueue.Contains(worldObject.objectId))
                    {
                        deleteQueue.Add(worldObject.objectId);
                    }
                }
            }

            // Delete pending objects.
            foreach (long objectId in deleteQueue)
            {
                if (gameObjects.ContainsKey(objectId))
                {
                    GameObject obj = gameObjects[objectId];
                    if (obj != null)
                    {
                        // Disable.
                        obj.GetComponent <WorldObjectText>().nameMesh.gameObject.SetActive(false);
                        obj.SetActive(false);

                        // Remove from objects list.
                        ((IDictionary <long, GameObject>)gameObjects).Remove(obj.GetComponent <WorldObject>().objectId);

                        // Delete game object from world with a delay.
                        StartCoroutine(DelayedDestroy(obj));
                    }
                }

                // If object was current target, unselect it.
                if (targetWorldObject != null && targetWorldObject.objectId == objectId)
                {
                    SetTarget(null);
                }
            }
            if (deleteQueue.Count > 0)
            {
                deleteQueue.Clear();
            }

            // Move pending objects.
            foreach (KeyValuePair <long, MovementHolder> entry in moveQueue)
            {
                Vector3 position = new Vector3(entry.Value.posX, entry.Value.posY, entry.Value.posZ);
                if (gameObjects.ContainsKey(entry.Key))
                {
                    GameObject obj = gameObjects[entry.Key];
                    if (obj != null)
                    {
                        WorldObject worldObject = obj.GetComponent <WorldObject>();
                        if (worldObject != null)
                        {
                            if (CalculateDistance(position) > VISIBILITY_RADIUS) // Moved out of sight.
                            {
                                // Broadcast self position, object out of sight.
                                NetworkManager.ChannelSend(new LocationUpdateRequest(MovementController.storedPosition.x, MovementController.storedPosition.y, MovementController.storedPosition.z, MovementController.storedRotation));
                                deleteQueue.Add(worldObject.objectId);
                            }
                            else
                            {
                                worldObject.MoveObject(position, entry.Value.heading);
                            }
                        }
                    }
                }
                // Check self teleporting.
                else if (entry.Key == 0)
                {
                    activeCharacter.transform.localPosition             = position;
                    activeCharacter.GetComponent <Rigidbody>().position = position;
                }
                // Request unknown object info from server.
                else if (CalculateDistance(position) <= VISIBILITY_RADIUS)
                {
                    NetworkManager.ChannelSend(new ObjectInfoRequest(entry.Key));
                    // Broadcast self position, in case player is not moving.
                    NetworkManager.ChannelSend(new LocationUpdateRequest(MovementController.storedPosition.x, MovementController.storedPosition.y, MovementController.storedPosition.z, MovementController.storedRotation));
                }

                ((IDictionary <long, MovementHolder>)moveQueue).Remove(entry.Key);
            }

            // Animate pending objects.
            foreach (KeyValuePair <long, AnimationHolder> entry in animationQueue)
            {
                if (gameObjects.ContainsKey(entry.Key))
                {
                    GameObject obj = gameObjects[entry.Key];
                    if (obj != null)
                    {
                        WorldObject worldObject = obj.GetComponent <WorldObject>();
                        if (worldObject != null)
                        {
                            if (worldObject.GetDistance() <= VISIBILITY_RADIUS) // Object is in sight radius.
                            {
                                worldObject.AnimateObject(entry.Value.velocityX, entry.Value.velocityZ, entry.Value.triggerJump, entry.Value.isInWater, entry.Value.isGrounded);
                            }
                        }
                    }
                }

                ((IDictionary <long, AnimationHolder>)animationQueue).Remove(entry.Key);
            }

            // Update pending characters.
            foreach (KeyValuePair <long, CharacterDataHolder> entry in characterUpdateQueue)
            {
                if (gameObjects.ContainsKey(entry.Key))
                {
                    GameObject obj = gameObjects[entry.Key];
                    if (obj != null)
                    {
                        WorldObject worldObject = obj.GetComponent <WorldObject>();
                        if (worldObject != null)
                        {
                            if (worldObject.GetDistance() <= VISIBILITY_RADIUS) // Object is in sight radius.
                            {
                                DynamicCharacterAvatar avatar = obj.GetComponent <DynamicCharacterAvatar>();
                                if (avatar != null)
                                {
                                    // TODO: Manage more things than just item updates.
                                    CharacterDataHolder oldData = worldObject.characterData;
                                    CharacterDataHolder newData = entry.Value;

                                    int headItem = newData.GetHeadItem();
                                    if (headItem != oldData.GetHeadItem())
                                    {
                                        if (headItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.HEAD);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, headItem);
                                        }
                                    }

                                    int chestItem = newData.GetChestItem();
                                    if (chestItem != oldData.GetChestItem())
                                    {
                                        if (chestItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.CHEST);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, chestItem);
                                        }
                                    }

                                    int legsItem = newData.GetLegsItem();
                                    if (legsItem != oldData.GetLegsItem())
                                    {
                                        if (legsItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.LEGS);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, legsItem);
                                        }
                                    }

                                    int handsItem = newData.GetHandsItem();
                                    if (handsItem != oldData.GetHandsItem())
                                    {
                                        if (handsItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.HANDS);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, handsItem);
                                        }
                                    }

                                    int feetItem = newData.GetFeetItem();
                                    if (feetItem != oldData.GetFeetItem())
                                    {
                                        if (feetItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.FEET);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, feetItem);
                                        }
                                    }

                                    int leftHandItem = newData.GetLeftHandItem();
                                    if (leftHandItem != oldData.GetLeftHandItem())
                                    {
                                        if (leftHandItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.LEFT_HAND);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, leftHandItem);
                                        }
                                    }

                                    int rightHandItem = newData.GetRightHandItem();
                                    if (rightHandItem != oldData.GetRightHandItem())
                                    {
                                        if (rightHandItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.RIGHT_HAND);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, rightHandItem);
                                        }
                                    }

                                    // Update world object with new data.
                                    worldObject.characterData = newData;
                                }
                            }
                        }
                    }
                }

                ((IDictionary <long, CharacterDataHolder>)characterUpdateQueue).Remove(entry.Key);
            }
        }
    }