Esempio n. 1
0
        /// <summary>
        /// Update NPC oral text.
        /// </summary>
        private void UpdateOralText()
        {
            if (_npc.NpcData == null)
            {
                return;
            }

            if (_npc.Timers.LastSpeakTime <= Time.TimeInSeconds())
            {
                if (_npc.NpcData.HasDialog && !string.IsNullOrEmpty(_npc.NpcData.Dialog.OralText))
                {
                    IEnumerable <IPlayerEntity> playersArount = from x in _npc.Object.Entities
                                                                where x.Object.Position.IsInCircle(_npc.Object.Position, OralTextRadius) &&
                                                                x is IPlayerEntity
                                                                select x as IPlayerEntity;

                    foreach (IPlayerEntity player in playersArount)
                    {
                        string text = _npc.NpcData.Dialog.OralText.Replace(DialogVariables.PlayerNameText, player.Object.Name);

                        _chatPacketFactory.SendChatTo(_npc, player, text);
                    }

                    _npc.Timers.LastSpeakTime = Time.TimeInSeconds() + RandomHelper.Random(10, 15);
                }
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public void OpenNpcDialog(IPlayerEntity player, uint npcObjectId, string dialogKey, int questId = 0)
        {
            var npc = player.FindEntity <INpcEntity>(npcObjectId);

            if (npc == null)
            {
                _logger.LogError($"Cannot find NPC with id: {npcObjectId}.");
                return;
            }

            _logger.LogTrace($"{npc.Object.Name}: '{dialogKey}'/{questId}");

            if (string.IsNullOrEmpty(dialogKey))
            {
                if (npc.Quests.Any())
                {
                    IEnumerable <IQuestScript> availableQuests = npc.Quests.Where(x => _questSystem.CanStartQuest(player, npc, x));

                    if (availableQuests.Count() == 1)
                    {
                        IQuestScript firstQuest = availableQuests.First();
                        var          questState = _questSystem.CanStartQuest(player, npc, firstQuest) ? QuestStateType.Suggest : QuestStateType.End;

                        _questSystem.ProcessQuest(player, npc, firstQuest, questState);
                        return;
                    }
                }

                IEnumerable <IQuestScript> questsToBeFinished = player.QuestDiary.ActiveQuests.Where(x => npc.Object.Name.Equals(x.Script.EndCharacter, StringComparison.OrdinalIgnoreCase)).Select(x => x.Script);

                if (questsToBeFinished.Any())
                {
                    _questSystem.ProcessQuest(player, npc, questsToBeFinished.First(), QuestStateType.End);
                    return;
                }

                if (!npc.NpcData.HasDialog)
                {
                    _logger.LogError($"NPC '{npc.Object.Name}' doesn't have a dialog.");
                    return;
                }

                SendNpcDialog(player, npc, npc.NpcData.Dialog.IntroText, npc.NpcData.Dialog.Links);
            }
            else
            {
                if (questId != 0)
                {
                    // Check if the quest exists for the current NPC
                    IQuestScript quest = npc.Quests.FirstOrDefault(x => x.Id == questId);

                    if (quest == null)
                    {
                        // If not, check if the npc is the end character of player's active quest
                        quest = player.QuestDiary.ActiveQuests.Where(x => x.QuestId == questId && npc.Object.Name.Equals(x.Script.EndCharacter, StringComparison.OrdinalIgnoreCase)).Select(x => x.Script).FirstOrDefault();

                        if (quest == null)
                        {
                            _logger.LogError($"Cannot find quest with id '{questId}' at npc '{npc}' for player '{player}'.");
                            return;
                        }
                    }

                    _questSystem.ProcessQuest(player, npc, quest, dialogKey.ToEnum <QuestStateType>());
                }
                else
                {
                    if (!npc.NpcData.HasDialog)
                    {
                        _logger.LogError($"NPC '{npc.Object.Name}' doesn't have a dialog.");
                        return;
                    }

                    if (dialogKey == DialogConstants.Bye)
                    {
                        _chatPacketFactory.SendChatTo(npc, player, npc.NpcData.Dialog.ByeText);
                        _npcDialogPacketFactory.SendCloseDialog(player);
                        return;
                    }
                    else
                    {
                        DialogLink dialogLink = npc.NpcData.Dialog.Links?.FirstOrDefault(x => x.Id == dialogKey);

                        if (dialogLink == null)
                        {
                            _logger.LogError($"Cannot find dialog key: '{dialogKey}' for NPC '{npc.Object.Name}'.");
                            return;
                        }

                        SendNpcDialog(player, npc, dialogLink.Texts, npc.NpcData.Dialog.Links);
                    }
                }
            }
        }