2021-10-15 21:06:51 +01:00
using HarmonyLib ;
using OWML.Common ;
2021-04-29 18:30:45 +01:00
using QSB.Animation.NPC.WorldObjects ;
using QSB.ConversationSync ;
2021-04-26 14:30:21 +01:00
using QSB.Events ;
using QSB.Patches ;
using QSB.Player ;
using QSB.Utility ;
using QSB.WorldSync ;
2021-04-29 18:30:45 +01:00
using System.Linq ;
using System.Reflection ;
2021-04-26 14:30:21 +01:00
using UnityEngine ;
2021-04-29 18:30:45 +01:00
namespace QSB.Animation.NPC.Patches
2021-04-26 14:30:21 +01:00
{
2021-10-15 21:06:51 +01:00
[HarmonyPatch]
2021-04-26 14:30:21 +01:00
public class CharacterAnimationPatches : QSBPatch
{
public override QSBPatchTypes Type = > QSBPatchTypes . OnClientConnect ;
2021-10-15 21:06:51 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterAnimController), nameof(CharacterAnimController.OnAnimatorIK))]
public static bool AnimatorIKReplacement (
2021-04-26 14:30:21 +01:00
CharacterAnimController __instance ,
float ___headTrackingWeight ,
bool ___lookOnlyWhenTalking ,
bool ____inConversation ,
ref float ____currentLookWeight ,
ref Vector3 ____currentLookTarget ,
DampedSpring3D ___lookSpring ,
Animator ____animator ,
CharacterDialogueTree ____dialogueTree )
{
2021-09-15 17:10:09 +01:00
if ( ! WorldObjectManager . AllReady | | ConversationManager . Instance = = null )
2021-05-02 08:54:00 +01:00
{
return false ;
}
2021-04-26 14:30:21 +01:00
var playerId = ConversationManager . Instance . GetPlayerTalkingToTree ( ____dialogueTree ) ;
2021-04-29 20:59:25 +01:00
var player = QSBPlayerManager . GetPlayer ( playerId ) ;
2021-08-22 16:40:50 +01:00
var qsbObj = QSBWorldSync . GetWorldFromUnity < QSBCharacterAnimController , CharacterAnimController > ( __instance ) ; // OPTIMIZE : maybe cache this somewhere... or assess how slow this is
2021-04-26 14:30:21 +01:00
2021-06-10 23:21:19 +01:00
PlayerInfo playerToUse = null ;
2021-04-26 14:30:21 +01:00
if ( ____inConversation )
{
if ( playerId = = uint . MaxValue )
{
2021-06-23 21:46:52 +01:00
DebugLog . ToConsole ( $"Error - {__instance.name} is in conversation with a null player! Defaulting to active camera." , MessageType . Error ) ;
2021-04-29 20:59:25 +01:00
playerToUse = QSBPlayerManager . LocalPlayer ;
2021-04-26 14:30:21 +01:00
}
else
{
2021-04-29 20:59:25 +01:00
playerToUse = player . CameraBody = = null
? QSBPlayerManager . LocalPlayer
: player ;
2021-04-26 14:30:21 +01:00
}
}
2021-08-22 16:40:50 +01:00
else if ( ! ___lookOnlyWhenTalking & & qsbObj . GetPlayersInHeadZone ( ) . Count ! = 0 ) // IDEA : maybe this would be more fun if characters looked between players at random times? :P
2021-04-26 14:30:21 +01:00
{
2021-04-29 20:59:25 +01:00
playerToUse = QSBPlayerManager . GetClosestPlayerToWorldPoint ( qsbObj . GetPlayersInHeadZone ( ) , __instance . transform . position ) ;
2021-04-26 14:30:21 +01:00
}
2021-06-10 23:21:19 +01:00
else if ( QSBPlayerManager . PlayerList . Count ! = 0 )
2021-04-26 14:30:21 +01:00
{
2021-04-29 20:59:25 +01:00
playerToUse = QSBPlayerManager . GetClosestPlayerToWorldPoint ( __instance . transform . position , true ) ;
2021-04-26 14:30:21 +01:00
}
2021-06-19 11:26:05 +01:00
var localPosition = playerToUse ! = null
? ____animator . transform . InverseTransformPoint ( playerToUse . CameraBody . transform . position )
2021-06-10 23:21:19 +01:00
: Vector3 . zero ;
2021-04-26 14:30:21 +01:00
var targetWeight = ___headTrackingWeight ;
if ( ___lookOnlyWhenTalking )
{
2021-06-19 11:26:05 +01:00
if ( ! ____inConversation
| | qsbObj . GetPlayersInHeadZone ( ) . Count = = 0
2021-04-29 20:59:25 +01:00
| | ! qsbObj . GetPlayersInHeadZone ( ) . Contains ( playerToUse ) )
2021-04-26 14:30:21 +01:00
{
targetWeight * = 0 ;
}
}
else
{
2021-06-19 11:26:05 +01:00
if ( qsbObj . GetPlayersInHeadZone ( ) . Count = = 0
2021-04-29 20:59:25 +01:00
| | ! qsbObj . GetPlayersInHeadZone ( ) . Contains ( playerToUse ) )
2021-04-26 14:30:21 +01:00
{
targetWeight * = 0 ;
}
}
____currentLookWeight = Mathf . Lerp ( ____currentLookWeight , targetWeight , Time . deltaTime * 2f ) ;
____currentLookTarget = ___lookSpring . Update ( ____currentLookTarget , localPosition , Time . deltaTime ) ;
____animator . SetLookAtPosition ( ____animator . transform . TransformPoint ( ____currentLookTarget ) ) ;
____animator . SetLookAtWeight ( ____currentLookWeight ) ;
return false ;
}
2021-10-28 16:18:22 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(SolanumAnimController), nameof(SolanumAnimController.LateUpdate))]
public static bool SolanumLateUpdateReplacement ( SolanumAnimController __instance )
{
if ( __instance . _animatorStateEvents = = null )
{
__instance . _animatorStateEvents = __instance . _animator . GetBehaviour < AnimatorStateEvents > ( ) ;
__instance . _animatorStateEvents . OnEnterState + = __instance . OnEnterAnimatorState ;
}
var qsbObj = QSBWorldSync . GetWorldFromUnity < QSBSolanumAnimController , SolanumAnimController > ( __instance ) ;
var playersInHeadZone = qsbObj . GetPlayersInHeadZone ( ) ;
Transform targetCamera = playersInHeadZone = = null | | playersInHeadZone . Count = = 0
? __instance . _playerCameraTransform
: QSBPlayerManager . GetClosestPlayerToWorldPoint ( playersInHeadZone , __instance . transform . position ) . CameraBody . transform ;
var targetValue = Quaternion . LookRotation ( targetCamera . position - __instance . _headBoneTransform . position , __instance . transform . up ) ;
__instance . _currentLookRotation = __instance . _lookSpring . Update ( __instance . _currentLookRotation , targetValue , Time . deltaTime ) ;
var position = __instance . _headBoneTransform . position + ( __instance . _currentLookRotation * Vector3 . forward ) ;
__instance . _localLookPosition = __instance . transform . InverseTransformPoint ( position ) ;
return false ;
}
2021-10-15 21:06:51 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterAnimController), nameof(CharacterAnimController.OnZoneExit))]
public static bool HeadZoneExit ( CharacterAnimController __instance )
2021-04-26 14:30:21 +01:00
{
var qsbObj = QSBWorldSync . GetWorldFromUnity < QSBCharacterAnimController , CharacterAnimController > ( __instance ) ;
2021-10-28 14:28:57 +01:00
QSBEventManager . FireEvent ( EventNames . QSBExitNonNomaiHeadZone , qsbObj . ObjectId ) ;
2021-04-26 14:30:21 +01:00
return false ;
}
2021-10-15 21:06:51 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterAnimController), nameof(CharacterAnimController.OnZoneEntry))]
public static bool HeadZoneEntry ( CharacterAnimController __instance )
2021-04-26 14:30:21 +01:00
{
var qsbObj = QSBWorldSync . GetWorldFromUnity < QSBCharacterAnimController , CharacterAnimController > ( __instance ) ;
2021-10-28 14:28:57 +01:00
QSBEventManager . FireEvent ( EventNames . QSBEnterNonNomaiHeadZone , qsbObj . ObjectId ) ;
2021-04-26 14:30:21 +01:00
return false ;
}
2021-04-29 18:30:45 +01:00
2021-10-28 22:27:48 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(NomaiConversationManager), nameof(NomaiConversationManager.Update))]
public static bool ReplacementUpdate ( NomaiConversationManager __instance )
{
var qsbObj = QSBWorldSync . GetWorldFromUnity < QSBSolanumAnimController , SolanumAnimController > ( __instance . _solanumAnimController ) ;
__instance . _playerInWatchVolume = qsbObj . GetPlayersInHeadZone ( ) . Any ( ) ;
return true ;
}
2021-10-28 16:18:22 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(NomaiConversationManager), nameof(NomaiConversationManager.OnEnterWatchVolume))]
public static bool EnterWatchZone ( NomaiConversationManager __instance )
{
var qsbObj = QSBWorldSync . GetWorldFromUnity < QSBSolanumAnimController , SolanumAnimController > ( __instance . _solanumAnimController ) ;
QSBEventManager . FireEvent ( EventNames . QSBEnterNomaiHeadZone , qsbObj . ObjectId ) ;
return false ;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(NomaiConversationManager), nameof(NomaiConversationManager.OnExitWatchVolume))]
public static bool ExitWatchZone ( NomaiConversationManager __instance )
{
var qsbObj = QSBWorldSync . GetWorldFromUnity < QSBSolanumAnimController , SolanumAnimController > ( __instance . _solanumAnimController ) ;
QSBEventManager . FireEvent ( EventNames . QSBExitNomaiHeadZone , qsbObj . ObjectId ) ;
return false ;
}
2021-10-15 21:06:51 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(FacePlayerWhenTalking), nameof(FacePlayerWhenTalking.OnStartConversation))]
2021-10-28 14:29:53 +01:00
public static bool OnStartConversation ( FacePlayerWhenTalking __instance )
2021-04-29 18:30:45 +01:00
{
2021-10-28 14:29:53 +01:00
var playerId = ConversationManager . Instance . GetPlayerTalkingToTree ( __instance . _dialogueTree ) ;
2021-04-29 18:30:45 +01:00
if ( playerId = = uint . MaxValue )
{
2021-10-28 14:29:53 +01:00
DebugLog . ToConsole ( $"Error - No player talking to {__instance._dialogueTree.name}!" , MessageType . Error ) ;
2021-04-29 18:30:45 +01:00
return false ;
}
2021-06-18 22:38:32 +01:00
2021-04-29 18:30:45 +01:00
var player = QSBPlayerManager . GetPlayer ( playerId ) ;
var distance = player . Body . transform . position - __instance . transform . position ;
var vector2 = distance - Vector3 . Project ( distance , __instance . transform . up ) ;
var angle = Vector3 . Angle ( __instance . transform . forward , vector2 ) * Mathf . Sign ( Vector3 . Dot ( vector2 , __instance . transform . right ) ) ;
var axis = __instance . transform . parent . InverseTransformDirection ( __instance . transform . up ) ;
var lhs = Quaternion . AngleAxis ( angle , axis ) ;
2021-10-28 14:29:53 +01:00
__instance . FaceLocalRotation ( lhs * __instance . transform . localRotation ) ;
2021-04-29 18:30:45 +01:00
return false ;
}
2021-10-15 21:06:51 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.StartConversation))]
public static bool StartConversation ( CharacterDialogueTree __instance )
2021-04-29 18:30:45 +01:00
{
var allNpcAnimControllers = QSBWorldSync . GetWorldObjects < INpcAnimController > ( ) ;
var ownerOfThis = allNpcAnimControllers . FirstOrDefault ( x = > x . GetDialogueTree ( ) = = __instance ) ;
if ( ownerOfThis = = default )
{
return true ;
}
2021-06-18 22:38:32 +01:00
2021-04-29 18:30:45 +01:00
var id = QSBWorldSync . GetIdFromTypeSubset ( ownerOfThis ) ;
QSBEventManager . FireEvent ( EventNames . QSBNpcAnimEvent , AnimationEvent . StartConversation , id ) ;
return true ;
}
2021-10-15 21:06:51 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.EndConversation))]
public static bool EndConversation ( CharacterDialogueTree __instance )
2021-04-29 18:30:45 +01:00
{
var allNpcAnimControllers = QSBWorldSync . GetWorldObjects < INpcAnimController > ( ) ;
var ownerOfThis = allNpcAnimControllers . FirstOrDefault ( x = > x . GetDialogueTree ( ) = = __instance ) ;
if ( ownerOfThis = = default )
{
return true ;
}
2021-06-18 22:38:32 +01:00
2021-04-29 18:30:45 +01:00
var id = QSBWorldSync . GetIdFromTypeSubset ( ownerOfThis ) ;
QSBEventManager . FireEvent ( EventNames . QSBNpcAnimEvent , AnimationEvent . EndConversation , id ) ;
return true ;
}
2021-04-29 20:59:25 +01:00
2021-10-15 21:06:51 +01:00
[HarmonyPrefix]
[HarmonyPatch(typeof(KidRockController), nameof(KidRockController.Update))]
2021-10-28 14:29:53 +01:00
public static bool UpdateReplacement ( KidRockController __instance )
2021-04-29 20:59:25 +01:00
{
2021-05-02 08:54:00 +01:00
if ( ! WorldObjectManager . AllReady )
{
return true ;
}
2021-06-18 22:38:32 +01:00
2021-10-28 14:29:53 +01:00
var qsbObj = QSBWorldSync . GetWorldObjects < QSBCharacterAnimController > ( ) . First ( x = > x . GetDialogueTree ( ) = = __instance . _dialogueTree ) ;
2021-04-29 20:59:25 +01:00
2021-10-28 14:29:53 +01:00
if ( ! __instance . _throwingRock & & ! qsbObj . InConversation ( ) & & Time . time > __instance . _nextThrowTime )
2021-04-29 20:59:25 +01:00
{
2021-10-28 14:29:53 +01:00
__instance . StartRockThrow ( ) ;
2021-04-29 20:59:25 +01:00
}
2021-06-18 22:38:32 +01:00
2021-04-29 20:59:25 +01:00
return false ;
}
2021-04-26 14:30:21 +01:00
}
}