2024-07-21 19:33:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-07-16 08:54:47 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Dalamud.Game.ClientState.Conditions;
|
|
|
|
|
using Dalamud.Game.ClientState.Objects;
|
|
|
|
|
using Dalamud.Game.ClientState.Objects.Enums;
|
|
|
|
|
using Dalamud.Game.ClientState.Objects.Types;
|
|
|
|
|
using Dalamud.Plugin.Services;
|
2024-07-14 19:31:07 +00:00
|
|
|
|
using FFXIVClientStructs.FFXIV.Client.Game;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
using FFXIVClientStructs.FFXIV.Client.Game.Object;
|
2024-07-27 15:37:08 +00:00
|
|
|
|
using FFXIVClientStructs.FFXIV.Client.Game.UI;
|
2024-07-25 15:36:51 +00:00
|
|
|
|
using FFXIVClientStructs.FFXIV.Common.Math;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Questionable.Controller.CombatModules;
|
2024-07-14 19:31:07 +00:00
|
|
|
|
using Questionable.Controller.Utils;
|
2024-08-05 15:09:49 +00:00
|
|
|
|
using Questionable.Functions;
|
2024-08-02 16:30:21 +00:00
|
|
|
|
using Questionable.Model.Questing;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
|
|
|
|
|
namespace Questionable.Controller;
|
|
|
|
|
|
2024-07-21 19:33:38 +00:00
|
|
|
|
internal sealed class CombatController : IDisposable
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
2024-08-19 09:26:55 +00:00
|
|
|
|
private const float MaxTargetRange = 55f;
|
|
|
|
|
private const float MaxNameplateRange = 50f;
|
|
|
|
|
|
2024-07-14 01:42:30 +00:00
|
|
|
|
private readonly List<ICombatModule> _combatModules;
|
2024-07-27 21:10:08 +00:00
|
|
|
|
private readonly MovementController _movementController;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
private readonly ITargetManager _targetManager;
|
|
|
|
|
private readonly IObjectTable _objectTable;
|
|
|
|
|
private readonly ICondition _condition;
|
|
|
|
|
private readonly IClientState _clientState;
|
2024-08-05 15:09:49 +00:00
|
|
|
|
private readonly QuestFunctions _questFunctions;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
private readonly ILogger<CombatController> _logger;
|
|
|
|
|
|
|
|
|
|
private CurrentFight? _currentFight;
|
2024-07-28 19:50:17 +00:00
|
|
|
|
private bool _wasInCombat;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
|
2024-07-27 21:10:08 +00:00
|
|
|
|
public CombatController(
|
|
|
|
|
IEnumerable<ICombatModule> combatModules,
|
|
|
|
|
MovementController movementController,
|
|
|
|
|
ITargetManager targetManager,
|
|
|
|
|
IObjectTable objectTable,
|
|
|
|
|
ICondition condition,
|
|
|
|
|
IClientState clientState,
|
2024-08-05 15:09:49 +00:00
|
|
|
|
QuestFunctions questFunctions,
|
2024-07-14 19:31:07 +00:00
|
|
|
|
ILogger<CombatController> logger)
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
|
|
|
|
_combatModules = combatModules.ToList();
|
2024-07-27 21:10:08 +00:00
|
|
|
|
_movementController = movementController;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
_targetManager = targetManager;
|
|
|
|
|
_objectTable = objectTable;
|
|
|
|
|
_condition = condition;
|
|
|
|
|
_clientState = clientState;
|
2024-08-05 15:09:49 +00:00
|
|
|
|
_questFunctions = questFunctions;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
_logger = logger;
|
2024-07-21 19:33:38 +00:00
|
|
|
|
|
|
|
|
|
_clientState.TerritoryChanged += TerritoryChanged;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsRunning => _currentFight != null;
|
|
|
|
|
|
|
|
|
|
public bool Start(CombatData combatData)
|
|
|
|
|
{
|
2024-07-28 19:50:17 +00:00
|
|
|
|
Stop("Starting combat");
|
2024-07-14 01:42:30 +00:00
|
|
|
|
|
|
|
|
|
var combatModule = _combatModules.FirstOrDefault(x => x.IsLoaded);
|
|
|
|
|
if (combatModule == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (combatModule.Start())
|
|
|
|
|
{
|
|
|
|
|
_currentFight = new CurrentFight
|
|
|
|
|
{
|
|
|
|
|
Module = combatModule,
|
|
|
|
|
Data = combatData,
|
|
|
|
|
};
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 19:50:17 +00:00
|
|
|
|
public EStatus Update()
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
2024-07-28 19:50:17 +00:00
|
|
|
|
if (_currentFight == null)
|
|
|
|
|
return EStatus.Complete;
|
|
|
|
|
|
2024-08-19 09:26:55 +00:00
|
|
|
|
if (_movementController.IsPathfinding || _movementController.IsPathRunning || _movementController.MovementStartedAt > DateTime.Now.AddSeconds(-1))
|
2024-07-28 19:50:17 +00:00
|
|
|
|
return EStatus.Moving;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
|
|
|
|
|
var target = _targetManager.Target;
|
|
|
|
|
if (target != null)
|
|
|
|
|
{
|
2024-07-27 09:19:18 +00:00
|
|
|
|
int currentTargetPriority = GetKillPriority(target);
|
2024-07-14 01:42:30 +00:00
|
|
|
|
var nextTarget = FindNextTarget();
|
2024-07-27 09:19:18 +00:00
|
|
|
|
int nextTargetPriority = GetKillPriority(target);
|
|
|
|
|
|
2024-07-25 15:36:51 +00:00
|
|
|
|
if (nextTarget != null && nextTarget.Equals(target))
|
|
|
|
|
{
|
|
|
|
|
_currentFight.Module.Update(target);
|
|
|
|
|
}
|
|
|
|
|
else if (nextTarget != null)
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
2024-07-27 09:19:18 +00:00
|
|
|
|
if (nextTargetPriority > currentTargetPriority)
|
2024-08-19 09:26:55 +00:00
|
|
|
|
SetTarget(nextTarget);
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2024-08-19 09:26:55 +00:00
|
|
|
|
SetTarget(null);
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var nextTarget = FindNextTarget();
|
2024-07-27 21:10:08 +00:00
|
|
|
|
if (nextTarget is { IsDead: false })
|
2024-08-19 09:26:55 +00:00
|
|
|
|
SetTarget(nextTarget);
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 19:50:17 +00:00
|
|
|
|
if (_condition[ConditionFlag.InCombat])
|
|
|
|
|
{
|
|
|
|
|
_wasInCombat = true;
|
|
|
|
|
return EStatus.InCombat;
|
|
|
|
|
}
|
|
|
|
|
else if (_wasInCombat)
|
|
|
|
|
return EStatus.Complete;
|
|
|
|
|
else
|
|
|
|
|
return EStatus.InCombat;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:54:47 +00:00
|
|
|
|
[SuppressMessage("ReSharper", "RedundantJumpStatement")]
|
2024-07-14 01:42:30 +00:00
|
|
|
|
private IGameObject? FindNextTarget()
|
|
|
|
|
{
|
2024-07-14 19:31:07 +00:00
|
|
|
|
if (_currentFight == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
// check if any complex combat conditions are fulfilled
|
|
|
|
|
var complexCombatData = _currentFight.Data.ComplexCombatDatas;
|
|
|
|
|
if (complexCombatData.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < complexCombatData.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_currentFight.Data.CompletedComplexDatas.Contains(i))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var condition = complexCombatData[i];
|
|
|
|
|
if (condition.RewardItemId != null && condition.RewardItemCount != null)
|
|
|
|
|
{
|
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
var inventoryManager = InventoryManager.Instance();
|
|
|
|
|
if (inventoryManager->GetInventoryItemCount(condition.RewardItemId.Value) >=
|
|
|
|
|
condition.RewardItemCount.Value)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation(
|
|
|
|
|
"Complex combat condition fulfilled: itemCount({ItemId}) >= {ItemCount}",
|
|
|
|
|
condition.RewardItemId, condition.RewardItemCount);
|
|
|
|
|
_currentFight.Data.CompletedComplexDatas.Add(i);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 21:24:47 +00:00
|
|
|
|
if (QuestWorkUtils.HasCompletionFlags(condition.CompletionQuestVariablesFlags) &&
|
|
|
|
|
_currentFight.Data.ElementId is QuestId questId)
|
2024-07-14 19:31:07 +00:00
|
|
|
|
{
|
2024-08-07 23:49:14 +00:00
|
|
|
|
var questWork = _questFunctions.GetQuestProgressInfo(questId);
|
2024-08-13 21:24:47 +00:00
|
|
|
|
if (questWork != null &&
|
|
|
|
|
QuestWorkUtils.MatchesQuestWork(condition.CompletionQuestVariablesFlags, questWork))
|
2024-07-14 19:31:07 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Complex combat condition fulfilled: QuestWork matches");
|
|
|
|
|
_currentFight.Data.CompletedComplexDatas.Add(i);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 21:10:08 +00:00
|
|
|
|
return _objectTable.Select(x => new
|
|
|
|
|
{
|
|
|
|
|
GameObject = x,
|
|
|
|
|
Priority = GetKillPriority(x),
|
|
|
|
|
Distance = Vector3.Distance(x.Position, _clientState.LocalPlayer!.Position),
|
|
|
|
|
})
|
2024-07-27 09:19:18 +00:00
|
|
|
|
.Where(x => x.Priority > 0)
|
|
|
|
|
.OrderByDescending(x => x.Priority)
|
2024-07-27 21:10:08 +00:00
|
|
|
|
.ThenBy(x => x.Distance)
|
2024-07-25 15:36:51 +00:00
|
|
|
|
.Select(x => x.GameObject)
|
|
|
|
|
.FirstOrDefault();
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 09:26:55 +00:00
|
|
|
|
public unsafe int GetKillPriority(IGameObject gameObject)
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
2024-07-19 19:16:40 +00:00
|
|
|
|
if (gameObject is IBattleNpc battleNpc)
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
2024-09-01 13:14:28 +00:00
|
|
|
|
if (_currentFight != null && !_currentFight.Module.CanAttack(battleNpc))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2024-07-14 19:31:07 +00:00
|
|
|
|
// TODO this works as somewhat of a delay between killing enemies if certain items/flags are checked
|
|
|
|
|
// but also delays killing the next enemy a little
|
2024-09-11 21:17:39 +00:00
|
|
|
|
if (_currentFight == null ||
|
|
|
|
|
_currentFight.Data.SpawnType == EEnemySpawnType.OverworldEnemies ||
|
2024-09-12 17:11:32 +00:00
|
|
|
|
_currentFight.Data.SpawnType == EEnemySpawnType.FateEnemies ||
|
|
|
|
|
_currentFight.Data.KillEnemyDataIds.Count > 0)
|
2024-07-14 19:31:07 +00:00
|
|
|
|
{
|
2024-07-19 19:16:40 +00:00
|
|
|
|
if (battleNpc.IsDead)
|
2024-07-27 09:19:18 +00:00
|
|
|
|
return 0;
|
2024-07-14 19:31:07 +00:00
|
|
|
|
}
|
2024-07-14 01:42:30 +00:00
|
|
|
|
|
2024-07-19 19:16:40 +00:00
|
|
|
|
if (!battleNpc.IsTargetable)
|
2024-07-27 09:19:18 +00:00
|
|
|
|
return 0;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
|
2024-07-14 19:31:07 +00:00
|
|
|
|
if (_currentFight != null)
|
|
|
|
|
{
|
|
|
|
|
var complexCombatData = _currentFight.Data.ComplexCombatDatas;
|
|
|
|
|
if (complexCombatData.Count >= 0)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < complexCombatData.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_currentFight.Data.CompletedComplexDatas.Contains(i))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-07-19 19:16:40 +00:00
|
|
|
|
if (complexCombatData[i].DataId == battleNpc.DataId)
|
2024-07-25 15:36:51 +00:00
|
|
|
|
return 100;
|
2024-07-14 19:31:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-19 19:16:40 +00:00
|
|
|
|
if (_currentFight.Data.KillEnemyDataIds.Contains(battleNpc.DataId))
|
2024-07-25 15:36:51 +00:00
|
|
|
|
return 90;
|
2024-07-14 19:31:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-14 01:42:30 +00:00
|
|
|
|
|
2024-07-25 15:36:51 +00:00
|
|
|
|
// enemies that we have aggro on
|
2024-07-19 19:16:40 +00:00
|
|
|
|
if (battleNpc.BattleNpcKind is BattleNpcSubKind.BattleNpcPart or BattleNpcSubKind.Enemy)
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
|
|
|
|
var gameObjectStruct = (GameObject*)gameObject.Address;
|
2024-07-27 15:37:08 +00:00
|
|
|
|
|
|
|
|
|
// npc that starts a fate or does turn-ins; not sure why they're marked as hostile
|
|
|
|
|
if (gameObjectStruct->NamePlateIconId is 60093 or 60732)
|
2024-07-27 09:19:18 +00:00
|
|
|
|
return 0;
|
2024-07-21 19:33:38 +00:00
|
|
|
|
|
2024-07-27 15:37:08 +00:00
|
|
|
|
var enemyData =
|
|
|
|
|
_currentFight?.Data.ComplexCombatDatas.FirstOrDefault(x => x.DataId == battleNpc.DataId);
|
2024-07-21 19:33:38 +00:00
|
|
|
|
if (enemyData is { IgnoreQuestMarker: true })
|
2024-07-27 15:37:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (battleNpc.StatusFlags.HasFlag(StatusFlags.InCombat))
|
|
|
|
|
return 20;
|
|
|
|
|
}
|
2024-08-13 21:24:47 +00:00
|
|
|
|
else if (enemyData != null)
|
2024-07-27 15:37:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (gameObjectStruct->NamePlateIconId != 0)
|
|
|
|
|
return 30;
|
2024-08-13 21:24:47 +00:00
|
|
|
|
|
|
|
|
|
// for enemies that are very far away, their nameplate doesn't render but they're in the object table
|
|
|
|
|
if (_currentFight?.Data.SpawnType == EEnemySpawnType.OverworldEnemies &&
|
2024-08-19 09:26:55 +00:00
|
|
|
|
Vector3.Distance(_clientState.LocalPlayer?.Position ?? Vector3.Zero, battleNpc.Position) > MaxNameplateRange)
|
2024-08-13 21:24:47 +00:00
|
|
|
|
return 25;
|
2024-07-27 15:37:08 +00:00
|
|
|
|
}
|
2024-08-14 07:46:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// as part of KillEnemyDataIds, not ComplexCombatData
|
|
|
|
|
// TODO maybe remove KillEnemyDataIds, rename ComplexCombatData to CombatData
|
|
|
|
|
if (gameObjectStruct->NamePlateIconId != 0)
|
|
|
|
|
return 29;
|
|
|
|
|
}
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
2024-07-25 15:36:51 +00:00
|
|
|
|
|
|
|
|
|
// stuff trying to kill us
|
|
|
|
|
if (battleNpc.TargetObjectId == _clientState.LocalPlayer?.GameObjectId)
|
2024-07-27 09:19:18 +00:00
|
|
|
|
return 10;
|
2024-07-25 15:36:51 +00:00
|
|
|
|
|
2024-07-27 15:37:08 +00:00
|
|
|
|
// stuff on our enmity list that's not necessarily targeting us
|
|
|
|
|
var haters = UIState.Instance()->Hater;
|
|
|
|
|
for (int i = 0; i < haters.HaterCount; ++i)
|
|
|
|
|
{
|
|
|
|
|
var hater = haters.Haters[i];
|
|
|
|
|
if (hater.EntityId == battleNpc.GameObjectId)
|
|
|
|
|
return 5;
|
|
|
|
|
}
|
2024-07-25 15:36:51 +00:00
|
|
|
|
|
2024-07-27 15:37:08 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 09:26:55 +00:00
|
|
|
|
private void SetTarget(IGameObject? target)
|
|
|
|
|
{
|
|
|
|
|
if (target == null)
|
|
|
|
|
{
|
|
|
|
|
if (_targetManager.Target != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Clearing target");
|
|
|
|
|
_targetManager.Target = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (Vector3.Distance(_clientState.LocalPlayer!.Position, target.Position) > MaxTargetRange)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Moving to target, distance: {Distance:N2}", Vector3.Distance(_clientState.LocalPlayer!.Position, target.Position));
|
|
|
|
|
_currentFight!.Module.MoveToTarget(target);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Setting target to {TargetName} ({TargetId:X8})", target.Name.ToString(), target.GameObjectId);
|
|
|
|
|
_targetManager.Target = target;
|
|
|
|
|
_currentFight!.Module.MoveToTarget(target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 19:50:17 +00:00
|
|
|
|
public void Stop(string label)
|
2024-07-14 01:42:30 +00:00
|
|
|
|
{
|
2024-07-28 19:50:17 +00:00
|
|
|
|
using var scope = _logger.BeginScope(label);
|
2024-07-14 01:42:30 +00:00
|
|
|
|
if (_currentFight != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Stopping current fight");
|
|
|
|
|
_currentFight.Module.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_currentFight = null;
|
2024-07-28 19:50:17 +00:00
|
|
|
|
_wasInCombat = false;
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 19:50:17 +00:00
|
|
|
|
private void TerritoryChanged(ushort territoryId) => Stop("TerritoryChanged");
|
2024-07-21 19:33:38 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_clientState.TerritoryChanged -= TerritoryChanged;
|
2024-07-28 19:50:17 +00:00
|
|
|
|
Stop("Dispose");
|
2024-07-21 19:33:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 01:42:30 +00:00
|
|
|
|
private sealed class CurrentFight
|
|
|
|
|
{
|
|
|
|
|
public required ICombatModule Module { get; init; }
|
|
|
|
|
public required CombatData Data { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class CombatData
|
|
|
|
|
{
|
2024-08-05 15:09:49 +00:00
|
|
|
|
public required ElementId ElementId { get; init; }
|
2024-07-14 19:31:07 +00:00
|
|
|
|
public required EEnemySpawnType SpawnType { get; init; }
|
|
|
|
|
public required List<uint> KillEnemyDataIds { get; init; }
|
|
|
|
|
public required List<ComplexCombatData> ComplexCombatDatas { get; init; }
|
|
|
|
|
|
|
|
|
|
public HashSet<int> CompletedComplexDatas { get; } = new();
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|
2024-07-28 19:50:17 +00:00
|
|
|
|
|
|
|
|
|
public enum EStatus
|
|
|
|
|
{
|
|
|
|
|
InCombat,
|
|
|
|
|
Moving,
|
|
|
|
|
Complete,
|
|
|
|
|
}
|
2024-07-14 01:42:30 +00:00
|
|
|
|
}
|