public void FormEntityMemories(List <Entity> entityIntel) { Entity other; Entity othersTarget; Vector3 othersPosition; Vector3 othersHeading; float othersSpeed; float othersCollisionRadius; for (int i = 0; i < entityIntel.Count; i++) { other = entityIntel[i]; if (other != entity) { othersTarget = other.target; othersPosition = other.transform.position; othersHeading = other.velocity; othersSpeed = othersHeading.magnitude; othersCollisionRadius = other.GetComponent <CapsuleCollider>().radius; // Remembering the thing, or not. EntityMemory_Weighted newMemory = new EntityMemory_Weighted(other, othersTarget, othersPosition, othersHeading, othersSpeed, othersCollisionRadius); RememberEntity(other, newMemory); } } }
void RememberEntity(Entity thing, EntityMemory_Weighted memory) { // This simply determines who the memory is about and if it's worth keeping. // It then tells the brain to think about it's important memories and decide what to focus on. if (thing == null || memory == null || friendMemory == null || threatMemory == null) { return; } if (friendlyTo.Contains(thing.entityType)) { if (friendMemory.ContainsKey(thing)) { friendMemory.Remove(thing); } friendMemory.Add(thing, memory); } if (hostileTo.Contains(thing.entityType)) { if (threatMemory.ContainsKey(thing)) { threatMemory.Remove(thing); } threatMemory.Add(thing, memory); } }
float EvaluateMemory(EntityMemory_Weighted memory) { // Turning the entity's type into a base threat. More dangerous thing should have higher threat. float baseThreat = 10; if (memory.subject.entityType == EntityType.Plant || memory.subject.entityType == EntityType.Prey || memory.subject.entityType == EntityType.Civilian) { baseThreat = 20; } if (memory.subject.entityType == EntityType.Predator || memory.subject.entityType == EntityType.Shambler || memory.subject.entityType == EntityType.Soldier) { baseThreat = 30; } // Turning the entity's target into a threat rating to myself and my friends. float threatToMe = 10; float threatToFriend = 10; if (memory.target != null) { if (memory.target.entityType == entity.entityType) { threatToMe = 20; threatToFriend = 30; } if (memory.target == entity) { threatToMe = 30; threatToFriend = 20; } } // Taking the calculated threat ratings and multiplying them against distance and speed to get final threat weight. float distancePlusSpeed = Vector3.Distance(transform.position, memory.position) / memory.speed + 1; float memoryWeight = (baseThreat + threatToMe + threatToFriend * 10) / (distancePlusSpeed * distancePlusSpeed); // Updating the last known position of the memory. if (lastKnownPositions.ContainsKey(memory.subject)) { lastKnownPositions[memory.subject].transform.position = memory.position; } else { GameObject lkpObject = Instantiate(entity.lastKnownPosition, memory.position, Quaternion.Euler(memory.heading)) as GameObject; lkpObject.transform.SetParent(unit.gameObject.transform); LastKnownPosition_Weighted lkp = lkpObject.GetComponent <LastKnownPosition_Weighted>(); lkp.SetSubject(memory.subject); lastKnownPositions.Add(memory.subject, lkp); } return(memoryWeight); }
public void JudgeEntities(List <Entity> lkpIntel) { // This sifts through memories of entities the AI has seen and decides what entities to focus on. // This is the end of the line for sensory input. All other actions are based on what is done in this chain. // This also calculates threat and morale, and posts it to the expression text. // Morale goes up when there are more and better friends around. // Morale goes down when there are more and better threats around. // The importance of a threat goes up the closer it is especially if it's focused on this AI. morale = selfConfidence; EntityMemory_Weighted heaviestTarget = null; EntityMemory_Weighted heaviestFriend = null; float heaviestWeight = 0; float memoryWeight = 0; entity.SetTarget(null); targetCollisionRadius = 0; if (threatMemory != null) { heaviestTarget = null; heaviestWeight = 0; foreach (KeyValuePair <Entity, EntityMemory_Weighted> memory in threatMemory) { memoryWeight = EvaluateMemory(memory.Value); if (memoryWeight > heaviestWeight) { heaviestWeight = memoryWeight; heaviestTarget = memory.Value; } morale -= memoryWeight; } } if (friendMemory != null) { heaviestFriend = null; heaviestWeight = 0; foreach (KeyValuePair <Entity, EntityMemory_Weighted> memory in friendMemory) { memoryWeight = EvaluateMemory(memory.Value); if (memoryWeight > heaviestWeight) { heaviestWeight = memoryWeight; heaviestFriend = memory.Value; } morale += memoryWeight; } } LKPCleanup(visibleLKPs); target = heaviestTarget; friend = heaviestFriend; if (target != null) { entity.SetTarget(target.subject); targetCollisionRadius = target.collisionRadius; } else { entity.SetTarget(null); targetCollisionRadius = 0; } Locomotion(); }
public void AssignInvestigation(Entity other, Entity othersTarget, Vector3 othersPosition, Vector3 othersHeading, float othersSpeed, float othersCollisionRadius) { EntityMemory_Weighted newMemory = new EntityMemory_Weighted(other, othersTarget, othersPosition, othersHeading, othersSpeed, othersCollisionRadius); RememberEntity(other, newMemory); }