68 lines
2.7 KiB
C#
68 lines
2.7 KiB
C#
using Logic.AI.Director;
|
|
using Logic.CrashSight;
|
|
using RuntimeData;
|
|
using System.Collections.Generic;
|
|
using TH1_Logic.Action;
|
|
|
|
namespace Logic.AI
|
|
{
|
|
public sealed class DirectorAIKernel : IAIKernel
|
|
{
|
|
private readonly AIDirectorLogic _director = new();
|
|
private readonly HashSet<string> _executedActionKeysThisTurn = new();
|
|
private readonly HashSet<string> _executedIntentKeysThisTurn = new();
|
|
private MapData _mapData;
|
|
private PlayerData _playerData;
|
|
|
|
public AIKernelType KernelType => AIKernelType.Director;
|
|
|
|
public void Initialize(AILogicContext context)
|
|
{
|
|
}
|
|
|
|
public void StartTurn(MapData mapData, PlayerData playerData)
|
|
{
|
|
_mapData = mapData;
|
|
_playerData = playerData;
|
|
_executedActionKeysThisTurn.Clear();
|
|
_executedIntentKeysThisTurn.Clear();
|
|
#if TH1_AI_DIRECTOR_DIAGNOSTICS || UNITY_EDITOR
|
|
AIDirectorDiagnostics.RecordTurnStart(_mapData, _playerData);
|
|
#endif
|
|
}
|
|
|
|
public AIKernelUpdate Update()
|
|
{
|
|
if (_mapData == null || _playerData == null) return AIKernelUpdate.Finished;
|
|
var decision = _director.Decide(_mapData, _playerData, null, _executedActionKeysThisTurn, _executedIntentKeysThisTurn, _executedActionKeysThisTurn.Count);
|
|
#if TH1_AI_DIRECTOR_DIAGNOSTICS || UNITY_EDITOR
|
|
AIDirectorDiagnostics.RecordDecision(_mapData, _playerData, decision);
|
|
#endif
|
|
if (!decision.HasAction) return AIKernelUpdate.Finished;
|
|
var candidate = decision.Candidate;
|
|
var action = candidate.AIAction;
|
|
if (action?.Param == null || action.ActionLogic == null) return AIKernelUpdate.Finished;
|
|
action.Param.MapData = _mapData;
|
|
action.Param.RefreshParams();
|
|
action.CheckIsActionInPlayerSight();
|
|
if (action.IsInSight) action.ActionLogic.CameraControl(action.Param);
|
|
if (action.ActionLogic.ActionId.PlayerActionType == PlayerActionType.OfferAlly)
|
|
LogSystem.LogInfo("AI 发起结盟");
|
|
_executedActionKeysThisTurn.Add(AIDirectorActionIndex.StableActionKey(action));
|
|
if (!string.IsNullOrEmpty(candidate.IntentKey)) _executedIntentKeysThisTurn.Add(candidate.IntentKey);
|
|
return AIKernelUpdate.ActionReady(action);
|
|
}
|
|
|
|
public void FinishTurn()
|
|
{
|
|
#if TH1_AI_DIRECTOR_DIAGNOSTICS || UNITY_EDITOR
|
|
AIDirectorDiagnostics.RecordTurnEnd(_mapData, _playerData);
|
|
#endif
|
|
_mapData = null;
|
|
_playerData = null;
|
|
_executedActionKeysThisTurn.Clear();
|
|
_executedIntentKeysThisTurn.Clear();
|
|
}
|
|
}
|
|
}
|