Files
zzz_REBIRTH__Utils/Scripts/EAI/NPCs/EAIMoveToTargetCompanion.cs
2025-06-04 16:44:53 +09:30

328 lines
11 KiB
C#

using GamePath;
using UnityEngine.Scripting;
[Preserve]
public class EAIMoveToTargetCompanion : EAITarget
{
public override void Init(EntityAlive _theEntity)
{
base.Init(_theEntity, 25f, true);
this.MutexBits = 1;
}
public override void Start()
{
this.relocateTicks = 0;
this.pathCounter = 0;
base.Start();
}
public bool NPCEAICanProceed()
{
bool result = true;
this.OwnerID = (int)this.theEntity.Buffs.GetCustomVar("$Leader");
if (this.OwnerID == 0)
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed 0");
return false;
}
if (this.theEntity.Buffs.GetCustomVar("onMission") == 1f ||
this.theEntity.Buffs.GetCustomVar("$FR_NPC_Respawn") == 1f ||
this.theEntity.Buffs.GetCustomVar("$FR_NPC_Hidden") == 1f
)
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed 1");
return false;
}
EntityUtilities.CheckDistanceToLeader(this.theEntity, this.targetPlayer);
this.entityTarget = this.theEntity.GetAttackTarget();
if (this.entityTarget == null)
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed this.entityTarget == null");
return false;
}
if (this.targetPlayer != null && !this.targetPlayer.CanEntityBeSeen(this.entityTarget) && !this.theEntity.CanEntityBeSeen(this.entityTarget))
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed 2b");
return false;
}
if (this.theEntity.CanEntityBeSeen(this.entityTarget))
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed 2c");
return false;
}
if (this.theEntity.emodel.IsRagdollActive)
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed 3");
return false;
}
if (this.theEntity.sleepingOrWakingUp || this.theEntity.bodyDamage.CurrentStun != EnumEntityStunType.None || (this.theEntity.Jumping && !this.theEntity.isSwimming))
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed 4");
return false;
}
if (this.theEntity.Buffs.GetCustomVar("CurrentOrder") != (int)EntityUtilities.Orders.Follow &&
this.theEntity.Buffs.GetCustomVar("CurrentOrder") != (int)EntityUtilities.Orders.Guard)
{
//Log.Out("EAIMoveToTargetCompanion-CanExecute IS NOT FOLLOWING");
return false;
}
bool stopAttacking = this.theEntity.Buffs.HasBuff("FuriousRamsayBuffPauseAttack") ||
this.theEntity.Buffs.HasBuff("buffNPCModStopAttacking") ||
this.theEntity.Buffs.HasBuff("FuriousRamsayStandStill");
if (stopAttacking)
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed stopAttacking");
this.theEntity.SetRevengeTarget((EntityAlive) null);
this.theEntity.attackTarget = (EntityAlive) null;
return false;
}
if (this.theEntity.sleepingOrWakingUp || this.theEntity.bodyDamage.CurrentStun != EnumEntityStunType.None)
{
//Log.Out("EAIMoveToTargetCompanion-NPCEAICanProceed 6");
return false;
}
return result;
}
public override bool CanExecute()
{
//Log.Out("EAIMoveToTargetCompanion-CanExecute START");
if (!NPCEAICanProceed())
{
return false;
}
if (this.targetPlayer == null)
{
//Log.Out("EAIMoveToTargetCompanion-CanExecute 2");
this.OwnerID = (int)this.theEntity.Buffs.GetCustomVar("$Leader");
if (this.OwnerID > 0)
{
//Log.Out("EAIMoveToTargetCompanion-CanExecute this.OwnerID: " + this.OwnerID);
EntityPlayer entityPlayer = (EntityPlayer)this.theEntity.world.GetEntity(this.OwnerID);
if (entityPlayer != null)
{
this.targetPlayer = entityPlayer;
return true;
}
}
return false;
}
//Log.Out("EAIMoveToTargetCompanion-CanExecute this.theEntity.GetAttackTarget: " + this.theEntity.GetAttackTarget().EntityClass.entityClassName);
return false;
}
public override bool Continue()
{
//Log.Out("EAIMoveToTargetCompanion-Continue START");
if (!NPCEAICanProceed())
{
return false;
}
if (pathCounter == 0)
{
return false;
}
return true;
}
public override void Update()
{
//Log.Out("EAIMoveToTargetCompanion-Update START");
if (!NPCEAICanProceed())
{
return;
}
if (this.targetPlayer == null)
{
return;
}
/*if (this.OwnerID == 0)
{
theEntity.moveHelper.CalcIfUnreachablePos();
//Log.Out($"Is unreachable: {theEntity.moveHelper.IsUnreachableSide}");
if (!theEntity.moveHelper.IsUnreachableSide)
EntityUtilities.CheckForClosedDoor(theEntity);
}*/
if (this.relocateTicks > 0)
{
//Log.Out("EAIMoveToTargetCompanion-Update 2");
if (!this.theEntity.navigator.noPathAndNotPlanningOne())
{
//Log.Out($"EAIMoveToTargetCompanion-Update moving to entityTarget.position: {entityTarget.position}");
this.relocateTicks--;
this.theEntity.moveHelper.SetFocusPos(this.entityTarget.position);
return;
}
this.relocateTicks = 0;
}
Vector3 vector2 = this.entityTarget.position;
Vector3 a = theEntity.position - vector2;
//Log.Out("EAIMoveToTargetCompanion-Update entityTargetPosition: " + vector2);
//Log.Out("EAIMoveToTargetCompanion-Update a: " + a);
//Log.Out("EAIMoveToTargetCompanion-Update a.sqrMagnitude: " + a.sqrMagnitude);
//Log.Out("EAIMoveToTargetCompanion-Update this.OwnerID: " + this.theEntity.Buffs.GetCustomVar("$Leader"));
//Log.Out("EAIMoveToTargetCompanion-Update SetLookPosition");
theEntity.SetLookPosition(vector2);
theEntity.RotateTo(vector2.x, vector2.y + 2, vector2.z, 8f, 8f);
if (a.sqrMagnitude < 1f)
{
//Log.Out("EAIMoveToTargetCompanion-Update 6");
this.entityPlayerVel = this.entityPlayerVel * 0.7f + a * 0.3f;
}
this.entityPlayerPos = vector2;
this.theEntity.moveHelper.CalcIfUnreachablePos();
float num2;
float num3;
num2 = 1.095f;
num3 = Utils.FastMax(0.7f, num2 - 0.35f);
float num4 = num3 * num3;
float num5 = 4f;
if (this.theEntity.IsFeral)
{
num5 = 8f;
}
num5 = base.RandomFloat * num5;
float targetXZDistanceSq = this.GetTargetXZDistanceSq(num5);
float num6 = vector2.y - this.theEntity.position.y;
float num7 = Utils.FastAbs(num6);
bool flag = targetXZDistanceSq <= num4 && num7 < 1f;
if (!flag)
{
//Log.Out("EAIMoveToTargetCompanion-Update 9");
if (num7 < 3f && !PathFinderThread.Instance.IsCalculatingPath(this.theEntity.entityId))
{
//Log.Out("EAIMoveToTargetCompanion-Update 10");
PathEntity path = this.theEntity.navigator.getPath();
if (path != null && path.NodeCountRemaining() <= 2)
{
//Log.Out("EAIMoveToTargetCompanion-Update 11");
this.pathCounter = 0;
}
}
int num = this.pathCounter - 1;
this.pathCounter = num;
if (num <= 0 && this.theEntity.CanNavigatePath() && !PathFinderThread.Instance.IsCalculatingPath(this.theEntity.entityId))
{
//Log.Out("EAIMoveToTargetCompanion-Update 12");
this.pathCounter = 6 + base.GetRandom(10);
Vector3 moveToLocation = EntityUtilities.GetMoveToLocation(this.theEntity, this.entityTarget.position, 5);
if (moveToLocation.y - this.theEntity.position.y < -8f)
{
//Log.Out("EAIMoveToTargetCompanion-Update 13");
this.pathCounter += 40;
if (base.RandomFloat < 0.2f)
{
//Log.Out("EAIMoveToTargetCompanion-Update 14");
this.seekPosOffset.x = this.seekPosOffset.x + (base.RandomFloat * 0.6f - 0.3f);
this.seekPosOffset.y = this.seekPosOffset.y + (base.RandomFloat * 0.6f - 0.3f);
}
moveToLocation.x += this.seekPosOffset.x;
moveToLocation.z += this.seekPosOffset.y;
}
else
{
//Log.Out("EAIMoveToTargetCompanion-Update 15");
float num8 = (moveToLocation - this.theEntity.position).magnitude - 5f;
if (num8 > 0f)
{
//Log.Out("EAIMoveToTargetCompanion-Update 16");
if (num8 > 60f)
{
//Log.Out("EAIMoveToTargetCompanion-Update 17");
num8 = 60f;
}
this.pathCounter += (int)(num8 / 20f * 20f);
}
}
//Log.Out($"MoveToTargetCompanion FindPath moveToLocation: {moveToLocation}");
this.theEntity.FindPath(moveToLocation, this.theEntity.GetMoveSpeedAggro(), true, this);
}
}
if (this.theEntity.Climbing)
{
//Log.Out("EAIMoveToTargetCompanion-Update 18");
return;
}
if (!flag)
{
//Log.Out("EAIMoveToTargetCompanion-Update 20");
if (this.theEntity.navigator.noPathAndNotPlanningOne() && this.pathCounter > 0 && num6 < 2.1f)
{
//Log.Out("EAIMoveToTargetCompanion-Update 21");
Vector3 moveToLocation2 = EntityUtilities.GetMoveToLocation(this.theEntity, this.entityTarget.position, 5);
//Log.Out($"MoveToTargetCompanion SetMoveTo moveToLocation2: {moveToLocation2}");
this.theEntity.moveHelper.SetMoveTo(moveToLocation2, true);
}
}
else
{
//Log.Out("EAIMoveToTargetCompanion-Update 22");
this.theEntity.moveHelper.Stop();
this.pathCounter = 0;
}
}
private float GetTargetXZDistanceSq(float estimatedTicks)
{
Vector3 vector = this.entityTarget.position;
vector += this.entityPlayerVel * estimatedTicks;
Vector3 vector2 = this.theEntity.position + this.theEntity.motion * estimatedTicks - vector;
vector2.y = 0f;
return vector2.sqrMagnitude;
}
public override void Reset()
{
this.entityTarget = null;
this.targetPlayer = null;
}
private EntityPlayer targetPlayer;
private EntityAlive entityTarget;
private Vector3 entityPlayerVel;
private Vector3 entityPlayerPos;
private int relocateTicks;
private int OwnerID = 0;
private int pathCounter;
private Vector2 seekPosOffset;
}