<commit_before>﻿using RootMotion;
using RootMotion.FinalIK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using VRGIN.Controls;
using VRGIN.Core;
using VRGIN.Helpers;

namespace PlayClubVR
{
    class ImpersonationHandler : ProtectedBehaviour
    {
        PlayClubActor _Actor;
        Controller.Lock _Lock1 = Controller.Lock.Invalid;
        Controller.Lock _Lock2 = Controller.Lock.Invalid;

        private Dictionary<HumanBodyBones, string> IK_MAPPING = new Dictionary<HumanBodyBones, string>()
        {
            { HumanBodyBones.Head, "Head" },
            { HumanBodyBones.LeftUpperLeg, "LegUp00_L" },
            { HumanBodyBones.LeftLowerLeg, "LegLow01_L"},
            { HumanBodyBones.LeftFoot, "Foot01_L"},
            { HumanBodyBones.RightUpperLeg, "LegUp00_R" },
            { HumanBodyBones.RightLowerLeg, "LegLow01_R"},
            { HumanBodyBones.RightFoot, "Foot01_R"},
            { HumanBodyBones.LeftLowerArm, "ArmLow01_L"},
            { HumanBodyBones.RightLowerArm, "ArmLow01_R"},
            { HumanBodyBones.LeftHand, "Hand_L"},
            { HumanBodyBones.RightHand, "Hand_R"},
            { HumanBodyBones.LeftUpperArm, "ArmUp00_L"},
            { HumanBodyBones.RightUpperArm, "ArmUp00_R"},
            { HumanBodyBones.Hips, "Hips" }
        };

        protected override void OnStart()
        {
            base.OnStart();
            if(!(VR.Settings as PlayClubSettings).FullImpersonation)
            {
                DestroyImmediate(this);
            }
        }
        protected override void OnUpdate()
        {
            base.OnUpdate();

            var impersonatedActor = VR.Interpreter.Actors.FirstOrDefault(a => !a.HasHead) as PlayClubActor;

            if (!_Lock1.IsValid && impersonatedActor != null) {
                _Actor = impersonatedActor;
                AcquireLocks();

                Enter();
            } else if(_Lock1.IsValid && impersonatedActor == null)
            {
                Exit();
            }
        }

        private void Enter()
        {
            VR.Mode.Left.Model.gameObject.SetActive(false);
            VR.Mode.Right.Model.gameObject.SetActive(false);

            foreach(var ik in _Actor.Actor.IKs)
            {
                if(ik.ik)
                {
                    VRLog.Info(ik.ik.name);
                }
            }

            _Actor.Actor.SetupIK();

            //var biped = SetUpSimpleBiped(_Actor.Actor);

            //biped.solvers.leftHand.target = VR.Mode.Left.transform;
            //biped.solvers.rightHand.target = VR.Mode.Right.transform;

            //left.solver.target = VR.Mode.Left.transform;
            //right.solver.target = VR.Mode.Right.transform;
        }

        private void Exit()
        {
            _Lock1.Release();
            _Lock2.Release();

            VR.Mode.Left.Model.gameObject.SetActive(true);
            VR.Mode.Right.Model.gameObject.SetActive(true);

            var scene = GameObject.FindObjectOfType<H_Scene>();
            if(scene)
            {
                scene.UpdateIK(scene.LastAnimeName);
            }
        }

        protected override void OnLateUpdate()
        {
            base.OnLateUpdate();

            if(_Lock1.IsValid)
            {
                // Update hands!
                LeftIK.solver.IKPosition = VR.Mode.Left.transform.position - VR.Mode.Left.transform.forward * 0.1f;
                LeftIK.solver.IKRotation = VR.Mode.Left.transform.rotation * Quaternion.Euler(0, 60, 0);
                RightIK.solver.IKPosition = VR.Mode.Right.transform.position - VR.Mode.Right.transform.forward * 0.1f;
                RightIK.solver.IKRotation = VR.Mode.Right.transform.rotation * Quaternion.Euler(0, -60, 0);
                RightIK.solver.bendModifier = IKSolverLimb.BendModifier.Target;
                RightIK.solver.bendModifierWeight = 0.5f;
                LeftIK.solver.bendModifier = IKSolverLimb.BendModifier.Target;
                LeftIK.solver.bendModifierWeight = 0.5f;
                RightIK.solver.bendGoal = null;
                    
            }
        }

        private LimbIK LeftIK
        {
            get
            {
                return _Actor.Actor.IKs[(int)Human.IK_TYPE.ARM_L].ik;
            }
        }

        private LimbIK RightIK
        {
            get
            {
                return _Actor.Actor.IKs[(int)Human.IK_TYPE.ARM_R].ik;
            }
        }
        
        private bool AcquireLocks()
        {
            if(VR.Mode.Left.AcquireFocus(out _Lock1) && VR.Mode.Right.AcquireFocus(out _Lock2))
            {
                return true;
            } else
            {
                if (_Lock1.IsValid) _Lock1.Release();
                if (_Lock2.IsValid) _Lock2.Release();<commit_msg>Change impersonation to use the vive controllers directions as indicatoin where the elbows are<commit_after>﻿using RootMotion;
using RootMotion.FinalIK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using VRGIN.Controls;
using VRGIN.Core;
using VRGIN.Helpers;

namespace PlayClubVR
{
    class ImpersonationHandler : ProtectedBehaviour
    {
        PlayClubActor _Actor;
        Controller.Lock _Lock1 = Controller.Lock.Invalid;
        Controller.Lock _Lock2 = Controller.Lock.Invalid;
        Transform _GoalLeft;
        Transform _GoalRight;


        private Dictionary<HumanBodyBones, string> IK_MAPPING = new Dictionary<HumanBodyBones, string>()
        {
            { HumanBodyBones.Head, "Head" },
            { HumanBodyBones.LeftUpperLeg, "LegUp00_L" },
            { HumanBodyBones.LeftLowerLeg, "LegLow01_L"},
            { HumanBodyBones.LeftFoot, "Foot01_L"},
            { HumanBodyBones.RightUpperLeg, "LegUp00_R" },
            { HumanBodyBones.RightLowerLeg, "LegLow01_R"},
            { HumanBodyBones.RightFoot, "Foot01_R"},
            { HumanBodyBones.LeftLowerArm, "ArmLow01_L"},
            { HumanBodyBones.RightLowerArm, "ArmLow01_R"},
            { HumanBodyBones.LeftHand, "Hand_L"},
            { HumanBodyBones.RightHand, "Hand_R"},
            { HumanBodyBones.LeftUpperArm, "ArmUp00_L"},
            { HumanBodyBones.RightUpperArm, "ArmUp00_R"},
            { HumanBodyBones.Hips, "Hips" }
        };

        protected override void OnStart()
        {
            base.OnStart();
            if(!(VR.Settings as PlayClubSettings).FullImpersonation)
            {
                DestroyImmediate(this);
            }
        }

        protected override void OnAwake()
        {
            base.OnAwake();

            _GoalLeft = new GameObject().transform;
            _GoalRight = new GameObject().transform;

            DontDestroyOnLoad(_GoalLeft);
            DontDestroyOnLoad(_GoalRight);
        }

        protected virtual void OnDestroy()
        {
            Destroy(_GoalLeft.gameObject);
            Destroy(_GoalRight.gameObject);
        }

        protected override void OnUpdate()
        {
            base.OnUpdate();

            var impersonatedActor = VR.Interpreter.Actors.FirstOrDefault(a => !a.HasHead) as PlayClubActor;

            if (!_Lock1.IsValid && impersonatedActor != null) {
                _Actor = impersonatedActor;
                AcquireLocks();

                Enter();
            } else if(_Lock1.IsValid && impersonatedActor == null)
            {
                Exit();
            }
        }

        private void Enter()
        {
            VR.Mode.Left.Model.gameObject.SetActive(false);
            VR.Mode.Right.Model.gameObject.SetActive(false);

            foreach(var ik in _Actor.Actor.IKs)
            {
                if(ik.ik)
                {
                    VRLog.Info(ik.ik.name);
                }
            }

            _Actor.Actor.SetupIK();

            //var biped = SetUpSimpleBiped(_Actor.Actor);

            //biped.solvers.leftHand.target = VR.Mode.Left.transform;
            //biped.solvers.rightHand.target = VR.Mode.Right.transform;

            //left.solver.target = VR.Mode.Left.transform;
            //right.solver.target = VR.Mode.Right.transform;
        }

        private void Exit()
        {
            _Lock1.Release();
            _Lock2.Release();

            VR.Mode.Left.Model.gameObject.SetActive(true);
            VR.Mode.Right.Model.gameObject.SetActive(true);

            var scene = GameObject.FindObjectOfType<H_Scene>();
            if(scene)
            {
                scene.UpdateIK(scene.LastAnimeName);
            }
        }

        protected override void OnLateUpdate()
        {
            base.OnLateUpdate();

            if(_Lock1.IsValid)
            {
                // Update hands!
                LeftIK.solver.IKPosition = VR.Mode.Left.transform.position - VR.Mode.Left.transform.forward * 0.1f;
                LeftIK.solver.IKRotation = VR.Mode.Left.transform.rotation * Quaternion.Euler(0, 60, 0);
                RightIK.solver.IKPosition = VR.Mode.Right.transform.position - VR.Mode.Right.transform.forward * 0.1f;
                RightIK.solver.IKRotation = VR.Mode.Right.transform.rotation * Quaternion.Euler(0, -60, 0);
                RightIK.solver.bendGoal = _GoalRight;
                RightIK.solver.bendModifier = IKSolverLimb.BendModifier.Goal;
                RightIK.solver.bendModifierWeight = 1f;
                LeftIK.solver.bendModifier = IKSolverLimb.BendModifier.Goal;
                LeftIK.solver.bendGoal = _GoalLeft;
                LeftIK.solver.bendModifierWeight = 1f;

                _GoalLeft.position = VR.Mode.Left.transform.position - VR.Mode.Left.transform.forward;
                _GoalRight.position = VR.Mode.Right.transform.position - VR.Mode.Right.transform.forward;

                _GoalLeft.rotation = VR.Mode.Left.transform.rotation;
                _GoalRight.rotation = VR.Mode.Right.transform.rotation;

            }
        }

        private LimbIK LeftIK
        {
            get
            {
                return _Actor.Actor.IKs[(int)Human.IK_TYPE.ARM_L].ik;
            }
        }

        private LimbIK RightIK
        {
            get
            {
                return _Actor.Actor.IKs[(int)Human.IK_TYPE.ARM_R].ik;
            }
        }
        
        private bool AcquireLocks()
        {
            if(VR.Mode.Left.AcquireFocus(out _Lock1) && VR.Mode.Right.AcquireFocus(out _Lock2))
            {
                return true;
            } else
            {
                if (_Lock1.IsValid) _Lock1.Release();
                if (_Lock2.IsValid) _Lock2.Release();
{'fraction_non_alphanumeric': 0.10321122516244467, 'fraction_numerical': 0.009040399284301724, 'mean_word_length': 1.6450809464508094, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '503107', 'n_tokens_mistral': 3302, 'n_tokens_neox': 3044, 'n_words': 447}