public override void Process() { IReadOnlyCollection <DataCreature> creatures = owner.Memory.Creatures.Read(); List <Agent> agentsToFlee = new List <Agent>(); foreach (DataCreature data in creatures) { Agent agent = data.creature?.agentCreature; if (!agent || !agent.gameObject.activeSelf || data.RegistrationDate < Time.time - 5f) { continue; } bool isHostil = GoalEvade.CreatureIsHostil(owner, agent.Creature); if (isHostil) { agentsToFlee.Add(agent); } } owner.Steering.Evades = agentsToFlee; if (agentsToFlee.Count == 0) { status = GoalStatus.Completed; } }
private void PlayFearSound() { bool chased = false; IReadOnlyCollection <DataCreature> creatures = agentCreature.Memory.Creatures.Read(); foreach (DataCreature data in creatures) { Agent agent = data.creature?.agentCreature; if (!agent || !agent.gameObject.activeSelf || data.RegistrationDate < Time.time - 5f) { continue; } bool isHostil = GoalEvade.CreatureIsHostil(agentCreature, agent.Creature); if (isHostil && agent.Steering.Target == agentCreature) { chased = true; break; } } //Ajout de son if (chased) { AudioBox.PlayOneShot(SoundOneShot.CreatureFear); } else { if (UnityEngine.Random.value > 0.2f) { AudioBox.PlayOneShot(SoundOneShot.CreatureAlert); } } }
public override float CalculateDesirability(Agent agent, bool requested) { DesirabilitiesConfig desirabilitiesConfig = GameManager.Instance.DesirabilitiesConfig; IReadOnlyCollection <DataCreature> creatures = agent.Memory.Creatures.Read(); float minDistanceFromHostil = -1; Creature closestHostil = null; foreach (DataCreature data in creatures) { Agent otherAgent = data.creature?.agentCreature; if (!otherAgent || !otherAgent.gameObject.activeSelf || data.RegistrationDate < Time.time - 1f) { continue; } //bool attackSpecies = otherAgent.Steering.Target && otherAgent.Steering.Target.Creature.SpecieID == agent.Creature.SpecieID; //if(!attackSpecies) continue; if (otherAgent.Thinking.ActiveGoal?.GetType() == typeof(GoalTired)) { return(0); } float distance = Vector3.Distance(agent.transform.position, otherAgent.transform.position); bool isHostil = GoalEvade.CreatureIsHostil(agent, otherAgent.Creature); if (!isHostil) { continue; } if (minDistanceFromHostil == -1 || distance < minDistanceFromHostil) { minDistanceFromHostil = distance; closestHostil = otherAgent.Creature; } } if (minDistanceFromHostil == -1) { return(0); } float desirability = desirabilitiesConfig.DefenseDesirabilityByAggressivity.Evaluate(agent.Creature.Traits.Aggressivity.Value) * desirabilitiesConfig.DefenseDesirabilityTwicker * ForceEvaluation.Evaluate(agent, closestHostil); if (agent.Thinking.ActiveGoal?.GetType() == typeof(GoalDefense)) { desirability *= desirabilitiesConfig.DefenseConfirmationBias; } return(desirability); }
public override float CalculateDesirability(Agent agent, bool requested) { //TODO TEMP if (agent.Creature.Traits.Carnivorous >= 0.5f) { return(0); } DesirabilitiesConfig desirabilitiesConfig = GameManager.Instance.DesirabilitiesConfig; IReadOnlyCollection <DataCreature> creatures = agent.Memory.Creatures.Read(); float minDistanceFromHostil = -1; Creature closestHostil = null; foreach (DataCreature data in creatures) { Agent otherAgent = data.creature?.agentCreature; if (!otherAgent || !agent.gameObject.activeSelf || data.RegistrationDate < Time.time - 2f) { continue; } float distance = Vector3.Distance(agent.transform.position, otherAgent.transform.position); bool isHostil = GoalEvade.CreatureIsHostil(agent, otherAgent.Creature); if (isHostil && (minDistanceFromHostil == -1 || distance < minDistanceFromHostil)) { minDistanceFromHostil = distance; closestHostil = otherAgent.Creature; } } if (minDistanceFromHostil == -1) { return(0); } float desirability = desirabilitiesConfig.EvadeDesirabilityByDistance.Evaluate(1 - minDistanceFromHostil / desirabilitiesConfig.EvadeConsiderationMaxDistance) * desirabilitiesConfig.EvadeDesirabilityByAggressivity.Evaluate(agent.Creature.Traits.Aggressivity.Value) * desirabilitiesConfig.EvadeDesirabilityTwicker * (1 / ForceEvaluation.Evaluate(agent, closestHostil)); if (agent.Thinking.ActiveGoal?.GetType() == typeof(GoalEvade)) { desirability *= desirabilitiesConfig.EvadeConfirmationBias; } return(desirability); }
public override void Process() { Goal child = GetActiveGoal(); if (child != null && child.IsInactive) { child.Activate(); } IReadOnlyCollection <DataCreature> creatures = owner.Memory.Creatures.Read(); Agent aggresor = null; float distanceToAggresor = Mathf.Infinity; foreach (DataCreature data in creatures) { if (!data.creature) { continue; } Agent agent = data.creature.agentCreature; if (!agent.gameObject.activeSelf || data.RegistrationDate < Time.time - 1f) { continue; } bool isHostil = GoalEvade.CreatureIsHostil(owner, agent.Creature); if (!isHostil) { continue; } //bool attackSpecies = agent.Steering.Target && agent.Steering.Target.Creature.SpecieID == owner.Creature.SpecieID; //if(!attackSpecies) continue; float distanceToAgent = Vector3.Distance(owner.transform.position, agent.transform.position); if (agent != owner && agent.Creature.SpecieID != owner.Creature.SpecieID && distanceToAgent < distanceToAggresor) { aggresor = agent; distanceToAggresor = distanceToAgent; } } switch (defenseState) { case DefenseState.LookAt: if (aggresor == null) { if (child != null) { child.Abort(); } } else if (owner.Steering.Behavior != eSteeringBehavior.LookAt) { AddSubgoal(new GoalLookAt(owner, aggresor)); } /*else if(aggresor.Steering.Target != owner){ * if(child != null) child.Abort(); * AddSubgoal(new GoalPursuit(owner, aggresor)); * defenseState = DefenseState.Pursuit; * }*/ else if (distanceToAggresor < 2) { if (child != null) { child.Abort(); } AddSubgoal(new GoalAttack(owner, aggresor.Creature)); defenseState = DefenseState.Attack; } else { IReadOnlyCollection <DataCreature> friends = owner.Memory.Creatures.Read(); bool friendAggresed = false; foreach (DataCreature data in friends) { if (!data.creature) { continue; } Agent agent = data.creature.agentCreature; if (!agent.gameObject.activeSelf || data.RegistrationDate < Time.time - 1f) { continue; } if (aggresor.Steering.Target == agent) { friendAggresed = true; break; } } if (friendAggresed) { if (child != null) { child.Abort(); } AddSubgoal(new GoalPursuit(owner, aggresor)); if (Random.value > 0.5f) { owner.Creature.AudioBox.PlayOneShot(SoundOneShot.CreatureDefense); } defenseState = DefenseState.Pursuit; } } break; case DefenseState.Pursuit: if (child.IsComplete) { AddSubgoal(new GoalAttack(owner, owner.Steering.Target.Creature)); defenseState = DefenseState.Attack; } else if (child.HasFailed) { defenseState = DefenseState.LookAt; } else if (aggresor != null) { if ((owner.Steering.Behavior == eSteeringBehavior.Pursuit || owner.Steering.Behavior == eSteeringBehavior.Seek) && aggresor != owner.Steering.Target && Vector3.Distance(owner.transform.position, aggresor.transform.position) + 5f < Vector3.Distance(owner.transform.position, owner.Steering.Destination)) { child.Abort(); AddSubgoal(new GoalPursuit(owner, aggresor)); } } break; case DefenseState.Attack: if (aggresor == null) { break; } if (child.IsComplete || child.HasFailed) { IReadOnlyCollection <DataCreature> friends = owner.Memory.Creatures.Read(); bool friendAggresed = false; foreach (DataCreature data in friends) { if (!data.creature) { continue; } Agent agent = data.creature.agentCreature; if (!agent.gameObject.activeSelf || data.RegistrationDate < Time.time - 1f) { continue; } if (aggresor.Steering.Target == agent) { friendAggresed = true; break; } } if (friendAggresed) { if (child != null) { child.Abort(); } AddSubgoal(new GoalPursuit(owner, aggresor)); defenseState = DefenseState.Pursuit; } else { AddSubgoal(new GoalLookAt(owner, aggresor)); defenseState = DefenseState.LookAt; } /*if(aggresor.Steering.Target != owner){ * AddSubgoal(new GoalPursuit(owner, aggresor)); * defenseState = DefenseState.Pursuit; * }else{ * AddSubgoal(new GoalLookAt(owner, aggresor)); * defenseState = DefenseState.LookAt; * }*/ } break; } base.ProcessSubgoals(); }