public void Execute(StateBasedAgent obj) { if (obj.Hunger < (AIModifiers.maxHungerBeforeHitpointsDamage - AIModifiers.maxHungerBeforeHitpointsDamage * 0.2)) { obj.Fsm.ChangeState(Mature.Instance); return; } List <IEntity> inRange = obj.ViewedEntities.FindAll(x => x is Plant); inRange.Sort((x, y) => (int)(AIVector.Distance(obj.Position, x.Position) - AIVector.Distance(obj.Position, y.Position))); if (inRange.Count > 0) { if (AIVector.Distance(obj.Position, inRange[0].Position) < AIModifiers.maxFeedingRange) { obj.NextAction = new Feed((Plant)inRange[0]); return; } else { obj.NextAction = new Move((inRange[0].Position - obj.Position)); return; } } //Default obj.Fsm.ChangeState(new Exploring(ExploringGoal.Food)); }
public override IAction Execute(Agent0047 agent) { lover = null; foreach (Agent0047 allied in agent.alliedAgents) { if (allied.ProcreationCountDown == 0) { lover = allied; } } if (lover != null) { if (agent.alliedAgents.Count() > 1 && agent.alliedAgents[0].ProcreationCountDown == 0 && AIVector.Distance(agent.Position, agent.alliedAgents[0].Position) < AIModifiers.maxProcreateRange) { return(new Procreate(agent.alliedAgents[0])); } else if (agent.alliedAgents.Count() > 1 && agent.alliedAgents[0].ProcreationCountDown == 0 && AIVector.Distance(agent.Position, agent.alliedAgents[0].Position) > AIModifiers.maxProcreateRange) { AIVector vector = new AIVector(agent.alliedAgents[0].Position.X - agent.Position.X, agent.alliedAgents[0].Position.Y - agent.Position.Y); agent.moveX = vector.X; agent.moveY = vector.Y; return(new Move(new AIVector(agent.moveX, agent.moveY))); } } return(new Move(new AIVector(agent.moveX, agent.moveY))); }
public void Execute(Speedy obj) { List <IEntity> proCoward = obj.ViewedEntities.FindAll(x => x is Speedy && x != obj && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxProcreateRange); //Mate Now Multiply MORE if (proCoward.Count > 0 && obj.ProcreationCountDown <= 0) { foreach (Speedy coward in proCoward) { if (coward.ProcreationCountDown <= 0) { obj.NextAction = new Procreate(coward);//multiply break; } } } switch (obj.cowardAgentType) { case cowardType.coward: obj.FSM.ChangeState(Flee.Instance); break; case cowardType.nonCoward: obj.FSM.ChangeState(DMG.Instance); break; default: obj.FSM.ChangeState(Flee.Instance); break; } }
public void Execute(Speedy obj) { List <IEntity> nearEnemies = obj.ViewedEntities.FindAll(x => x.GetType() != typeof(Speedy) && x is Agent && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange + obj.enemyBufferDistance); //Flee you fools if (nearEnemies.Count > 0) { foreach (Agent enemy in nearEnemies) { if (!(enemy.Strength <= 5)) { int xy = rnd.Next(2); obj.direction = new AIVector((enemy.Position.X - obj.Position.X), (enemy.Position.Y - obj.Position.Y)).Normalize() * -1; if ((obj.lastDirectionAction + obj.directionChangeDelay <= obj.action)) { if (xy == 0) //X Randomizer { obj.direction = obj.direction + new AIVector(0, rnd.Next(-1, 1)); //flee a bit up or down } else//Y Randomizer { obj.direction = obj.direction + new AIVector(rnd.Next(-1, 1), 0);//fle a bit left or right } obj.lastDirectionAction = obj.action; } obj.NextAction = new Move(obj.direction);//Flee break; } } } obj.FSM.ChangeState(Eat.Instance); }
public override IAction GetNextAction(List <IEntity> otherEntities) { List <IEntity> NearbyPlants = otherEntities.FindAll(x => x.GetType() == typeof(Plant) && AIVector.Distance(Position, x.Position) < Eyesight); if (Hunger >= 100) { List <float> plantDistances = new List <float>(); foreach (Plant plant in NearbyPlants) { plantDistances.Add(AIVector.Distance(Position, plant.Position)); } float lowestValue = plantDistances.Min(); foreach (Plant item in NearbyPlants) { if (lowestValue == AIVector.Distance(Position, item.Position)) { if (lowestValue <= AIModifiers.maxFeedingRange) { return(new Feed(item)); } else { return(new Move(new AIVector(this.Position.X - item.Position.X, this.Position.Y - item.Position.Y))); } } } } }
private AIVector RotateVector(AIVector vector, double angle) { angle = -angle * (Math.PI / 180); double cos = Math.Cos(angle); double sin = Math.Sin(angle); return(new AIVector((float)cos * vector.X - (float)sin * vector.Y, (float)sin * vector.X + (float)cos * vector.Y)); }
public void Enter(StateBasedAgent obj) { //Calculate new seed based upon hash of id and the current time int seed = obj.Id.GetHashCode() + (int)DateTime.Now.ToBinary(); rnd = new Random(seed); currentDirection = new AIVector(rnd.Next(-1, 2), rnd.Next(-1, 2)); startTime = DateTime.Now; }
public void Execute(Speedy obj) { List <IEntity> feedPlants = obj.ViewedEntities.FindAll(x => x is Plant && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxFeedingRange); //Eat if posible if (feedPlants.Count > 0) { obj.NextAction = new Feed((Plant)feedPlants[rnd.Next(feedPlants.Count)]);//eat } obj.FSM.ChangeState(FindMate.Instance); }
public override GoalStatus Process() { status = GoalStatus.Active; if (AIVector.Distance(entity.Position, target) <= slack) { status = GoalStatus.Completed; } else { entity.NextAction = new Move(target - entity.Position); } return(status); }
public override void _Ready() { AddToGroup(HitGroups.Bullet); if (this.TargetHitgroups == null) { this.TargetHitgroups = new List <string>(); } movement = new AIVector(this, direction, speed); this.AddChild(movement); this.Connect("body_entered", this, "OnCollide"); }
public void Execute(StateBasedAgent obj) { if (obj.Hunger >= AIModifiers.maxHungerBeforeHitpointsDamage) { obj.Fsm.ChangeState(Hungry.Instance); return; } // No other agent with range < AIModifiers.maxMeleeAttackRange List <IEntity> inRange = obj.ViewedEntities.FindAll(x => x.GetType() != typeof(StateBasedAgent) && x is Agent && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange); if (inRange.Count == 0) { obj.Fsm.ChangeState(Mature.Instance); return; } //If speed is higher than any agent with AIModifiers.maxMeleeAttackRange bool speedIsHigher = true; float xFearCenter = 0; float yFearCenter = 0; foreach (Agent x in inRange) { if (x.MovementSpeed > obj.MovementSpeed) { speedIsHigher = false; break; } xFearCenter += x.Position.X; yFearCenter += x.Position.Y; } if (speedIsHigher) { AIVector fear = new AIVector(xFearCenter / inRange.Count, yFearCenter / inRange.Count); obj.NextAction = new Move((obj.Position - fear)); return; } else { int i = 0; i++; } //Default obj.NextAction = new Defend(); }
private Agent GetClosestAgent() { List <IEntity> agents = entity.ViewedEntities.FindAll(x => x is Agent && !(x is GoalBasedAgent)); Agent closestAgent = null; foreach (Agent a in agents) { if (closestAgent == null || AIVector.Distance(entity.Position, a.Position) < AIVector.Distance(entity.Position, closestAgent.Position)) { closestAgent = a; } } return(closestAgent); }
private void RememberPosition() { if (rememberPositionCount >= 10) { if (LastPos != this.Position) { AIVector tmp = new AIVector(Position.X, Position.Y); LastPos = tmp; rememberPositionCount = 0; } } rememberPositionCount++; }
private Plant GetClosestPlant() { List <IEntity> plants = entity.ViewedEntities.FindAll(x => x is Plant); Plant closestPlant = null; foreach (Plant p in plants) { if (closestPlant == null || AIVector.Distance(entity.Position, p.Position) < AIVector.Distance(entity.Position, closestPlant.Position)) { closestPlant = p; } } return(closestPlant); }
//Random rnd = new Random(); public override IAction Execute(Agent0047 agent) { Random rnd = new Random(); AIVector vector = new AIVector(Agent0047.window.Width * 0.5f - agent.Position.X, Agent0047.window.Height * 0.5f - agent.Position.Y); agent.moveX = vector.X; agent.moveY = vector.Y; if (AIVector.Distance(agent.Position, vector) <= 5) { agent.moveX = rnd.Next(-1, 2); agent.moveY = rnd.Next(-1, 2); } return(new Move(new AIVector(agent.moveX, agent.moveY))); }
public override IAction Execute(Agent0047 agent) { //Resets variables longestSpace = 0f; halfwayDistance = null; if (agent.closeEnemyAgents.Count == 1) //Checks if there is only one enemy, then reverses direction { AIVector vector = agent.Position - agent.closeEnemyAgents[0].Position; vector.Normalize(); AIVector rotatedVector = RotateVector(vector, rnd.Next(2, 6)); //agent.moveX = vector.X; //agent.moveY = vector.Y; agent.moveX = rotatedVector.X; agent.moveY = rotatedVector.Y; //return new Move(vector); return(new Move(rotatedVector)); } else { //Goes through all nearby enemies foreach (Agent enemyAgent in agent.closeEnemyAgents) { foreach (Agent otherEnemyAgent in agent.closeEnemyAgents) { //Finds the longest distance between two enemies, from all nearby enemies if (AIVector.Distance(enemyAgent.Position, otherEnemyAgent.Position) > longestSpace && otherEnemyAgent != enemyAgent) { longestSpace = AIVector.Distance(enemyAgent.Position, otherEnemyAgent.Position); //Finds the distance halfwayDistance = (enemyAgent.Position - otherEnemyAgent.Position) * 0.5f; //Finds the halfway point between the enemies furthest away from each other } } } } if (longestSpace < 20) { AIVector vector = agent.Position - agent.closeEnemyAgents[0].Position; return(new Move(vector)); } if (halfwayDistance != null) { //Returns a direction vector from the agents position and the halfway point found above return(new Move(new AIVector(halfwayDistance.X - agent.Position.X, halfwayDistance.Y - agent.Position.Y))); } return(new Move(new AIVector(agent.moveX, agent.moveY))); }
public void Execute(Speedy obj) { List <IEntity> nearEnemies = obj.ViewedEntities.FindAll(x => x.GetType() != typeof(Speedy) && x is Agent && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange); //kill if (nearEnemies.Count > 0) { foreach (Agent enemy in nearEnemies) { obj.NextAction = new Attack(enemy); break; } } obj.FSM.ChangeState(Eat.Instance); }
public override IAction GetNextAction(List <IEntity> otherEntities) { //Find all enemies witin Eyesight range List <IEntity> nearEnemies = otherEntities.FindAll(x => x.GetType() != typeof(FishAgent) && x is Agent && AIVector.Distance(Position, x.Position) < AIModifiers.maxMeleeAttackRange); if (nearEnemies.Count > 0) { return(new Attack((Agent)nearEnemies[0])); } //Find all food withing eyesight range List <IEntity> plants = otherEntities.FindAll(x => x is Plant && AIVector.Distance(Position, x.Position) < AIModifiers.maxFeedingRange); if (plants.Count > 0) { return(new Feed((Plant)plants[0])); } return(new Move(new AIVector(-1, 0))); }
public Chad(IPropertyStorage propertyStorage, int v) : base(propertyStorage) { rnd = new Random(); MovementSpeed = 174; Strength = 14; Health = 13; Eyesight = 49; Endurance = 0; Dodge = 0; moveX = rnd.Next(-1, 2); moveY = rnd.Next(-1, 2); direction = new AIVector(moveX, moveY); string ddd = this.GetType().FullName; }
public void Movement() { rnd = new Random(); aiMovement = (byte)rnd.Next(1, 500); if (lastFrameX == Position.X || lastFrameY == Position.Y) { caughtOnWall = true; wallTimer = new TimeSpan(0, 0, 3); } if (lastMovement == null) { aiMovement = 1; } switch (aiMovement) { case 1: iMove = new AIVector(rnd.Next(-1, 2), rnd.Next(-1, 2)); break; case 2: iMove = new AIVector(rnd.Next(-1, 2), rnd.Next(-1, 2)); break; case 3: iMove = new AIVector(rnd.Next(-1, 2), rnd.Next(-1, 2)); break; case 4: iMove = new AIVector(rnd.Next(-1, 2), rnd.Next(-1, 2)); break; default: iMove = lastMovement; break; } lastMovement = iMove; }
//Random rnd; public override IAction Execute(Agent0047 agent) { // rnd = new Random(); if (agent.plants.Count > 0) { agent.targetPlant = (Plant)agent.plants[0]; } else { agent.targetPlant = null; return(new Move(new AIVector(agent.moveX, agent.moveY))); } if (agent.alliedAgents.Count > 0) { foreach (Agent allied in agent.alliedAgents) { Agent0047 alliedAgent = (Agent0047)allied; if (allied.Hunger > agent.Hunger || (allied.Health < agent.Health && allied.Hunger > allied.Endurance)) { return(new Move(new AIVector(agent.moveX, agent.moveY))); } } } if (agent.targetPlant != null && AIVector.Distance(agent.Position, agent.targetPlant.Position) > AIModifiers.maxFeedingRange) //if agent is too far away from a plant to feed, move closer to it { AIVector vector = new AIVector(agent.targetPlant.Position.X - agent.Position.X, agent.targetPlant.Position.Y - agent.Position.Y); agent.moveX = vector.X; agent.moveY = vector.Y; return(new Move(new AIVector(agent.moveX, agent.moveY))); } else //eat focused plant { return(new Feed(agent.targetPlant)); } }
public override GoalStatus Process() { plant = GetClosestPlant(); base.Process(); if (status == GoalStatus.Completed) { if (plant == null) { subGoals.Push(new ExploreGoal(entity)); status = GoalStatus.Active; } else if (AIVector.Distance(entity.Position, plant.Position) < AIModifiers.maxFeedingRange) { entity.NextAction = new Feed(plant); } else if (AIVector.Distance(entity.Position, plant.Position) >= AIModifiers.maxFeedingRange) { subGoals.Push(new MoveGoal(entity, plant.Position, AIModifiers.maxFeedingRange - 1)); status = GoalStatus.Active; } } return(status); }
public override GoalStatus Process() { if (target == null || target.Hitpoints < 1) { target = GetClosestAgent(); } base.Process(); if (status == GoalStatus.Completed) { if (target != null && AIVector.Distance(entity.Position, target.Position) < AIModifiers.maxMeleeAttackRange) { entity.NextAction = new Attack(target); } else if (target != null && AIVector.Distance(entity.Position, target.Position) >= AIModifiers.maxMeleeAttackRange) { subGoals.Push(new MoveGoal(entity, target.Position, AIModifiers.maxMeleeAttackRange - 1)); status = GoalStatus.Active; } } return(status); }
public void Execute(StateBasedAgent obj) { List <IEntity> inRange; switch (goalType) { case ExploringGoal.Enemy: inRange = obj.ViewedEntities.FindAll(x => x.GetType() != typeof(StateBasedAgent) && x is Agent && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange); if (inRange.Count > 0) { obj.Fsm.ChangeState(Mature.Instance); return; } break; case ExploringGoal.Friend: inRange = obj.ViewedEntities.FindAll(x => x.GetType() == obj.GetType() && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange); if (inRange.Count > 0) { obj.Fsm.ChangeState(Mature.Instance); return; } break; case ExploringGoal.Food: inRange = obj.ViewedEntities.FindAll(x => x.GetType() == typeof(Plant) && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxFeedingRange); if (inRange.Count > 0) { obj.Fsm.ChangeState(Hungry.Instance); return; } break; default: break; } if ((DateTime.Now - startTime).TotalSeconds > 5) { obj.Fsm.ChangeState(Mature.Instance); return; } obj.NextAction = new Move(currentDirection); }
public void Execute(ModelBasedAgent obj) { //ProcreationCountDown > 0 if (obj.ProcreationCountDown > 0) { obj.Fsm.ChangeState(NotReadyToMate.Instance); return; } //Range to another agent < AIModfiers.maxMeleeAttackRange and other agent Strength + Health > Strength + Health List <IEntity> inRange = obj.Model.FindAll(x => x is Agent && x.EntityType != typeof(ModelBasedAgent).Name && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange && (((Agent)x).Strength + ((Agent)x).Health) > (obj.Strength + obj.Health)); if (inRange.Count > 0) { obj.Fsm.ChangeState(Scared.Instance); return; } //Another agent of same type with range < AIModifiers.maxProcreateRange and other agent ProcreationCountDown <= 0 inRange = obj.ViewedEntities.FindAll(x => x is Agent && x.EntityType == typeof(ModelBasedAgent).Name && ((Agent)x).ProcreationCountDown <= 0 && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxProcreateRange); if (inRange.Count > 0) { obj.NextAction = new Procreate((Agent)inRange[0]); return; } //Default obj.NextAction = new Move(new AIVector(rnd.Next(-1, 2), rnd.Next(-1, 2))); }
private bool DoAction(Agent agent, TimeSpan gameTime) { PropertyProvider p = pproviders[agent.Id.ToString()]; p.Defending = false; bool status = false; List <IEntity> sightEntities = entities.FindAll(e => AIVector.Distance(p.Position, e.Position) <= agent.Eyesight); IAction action = agent.GetNextAction(sightEntities); switch (action.GetType().Name) { case "Attack": { Attack att = (Attack)action; if (att.Defender != null) { PropertyProvider pDefender = pproviders[att.Defender.Id.ToString()]; if (AIVector.Distance(p.Position, pDefender.Position) <= AIModifiers.maxMeleeAttackRange) //If { Random rnd = new Random(); int currentSuccessChance = AIModifiers.baseChanceOfAttackSuccess - (pDefender.Defending ? pDefender.Dodge : 0); if (currentSuccessChance < AIModifiers.minChanceOfAttackSuccess) { currentSuccessChance = AIModifiers.minChanceOfAttackSuccess; } if (rnd.Next(100) < currentSuccessChance) //Attack was an success { pDefender.Hitpoints -= p.Strength * (float)gameTime.TotalSeconds; status = true; } } } } break; case "Move": { Move m = (Move)action; AIVector v = m.Direction.Normalize(); if (float.IsNaN(v.X) || float.IsNaN(v.Y)) { v.X = 0; v.Y = 0; } v = v * p.MovementSpeed * (float)gameTime.TotalSeconds; p.Position.X += v.X; p.Position.Y += v.Y; status = true; if (p.Position.X < 0) { status = false; p.Position.X = 0; } if (p.Position.X > (int)surfaceWidth) { status = false; p.Position.X = (int)surfaceWidth; } if (p.Position.Y < 0) { status = false; p.Position.Y = 0; } if (p.Position.Y > (int)surfaceHeight) { status = false; p.Position.Y = (int)surfaceHeight; } } break; case "Procreate": { Procreate procreation = (Procreate)action; if (procreation.Mate != null) { PropertyProvider pMate = pproviders[procreation.Mate.Id.ToString()]; //Both agents should be ready to procreate and within distance if (agent != procreation.Mate && agent.GetType() == procreation.Mate.GetType() && p.ProcreationCountDown <= 0 && pMate.ProcreationCountDown <= 0 && AIVector.Distance(p.Position, pMate.Position) <= AIModifiers.maxProcreateRange) { //A random parent factory is chosen Random rnd = new Random(); string randomParentType = rnd.Next(2) == 0 ? agent.EntityType : procreation.Mate.EntityType; foreach (AgentFactory factory in factories) { if (factory.ProvidedAgentTypeName == randomParentType) { p.ProcreationCountDown = 100; pMate.ProcreationCountDown = 100; PropertyProvider pNew = new PropertyProvider(); pproviders.Add(pNew.Id.ToString(), pNew); Agent newAgent = factory.CreateAgent(agent, procreation.Mate, pNew); if (newAgent.Invarient()) { pNew.Hitpoints = newAgent.Health; pNew.Hunger = 0; pNew.ProcreationCountDown = AIModifiers.initialProcreationCount; pNew.Position = new AIVector(agent.Position.X, agent.Position.Y); toBeAdded.Add(newAgent); status = true; } else { //throw new Exception("Invarient exception"); } break; } } } } } break; case "Feed": { Feed f = (Feed)action; if (f.FoodSource != null) { if (AIVector.Distance(agent.Position, f.FoodSource.Position) <= AIModifiers.maxFeedingRange) { int removed = entities.RemoveAll(x => x.Id.ToString() == f.FoodSource.Id.ToString()); if (removed > 0) { p.Hunger -= AIModifiers.hungerReductionPerFeeding; if (p.Hunger < 0) { p.Hunger = 0; } status = true; } } } } break; case "Defend": p.Defending = true; status = true; break; default: break; } if (status) { effects.Add(new VisualEffect(agent, action)); } return(status); }
public MoveGoal(GoalBasedAgent agent, AIVector target, float slack) : this(agent, target) { this.slack = slack; }
public MoveGoal(GoalBasedAgent agent, AIVector target) : base(agent) { this.target = target; }
public void Execute(ModelBasedAgent obj) { // No other agent with range < AIModifiers.maxMeleeAttackRange List <IEntity> inRange = obj.Model.FindAll(x => x is Agent && x.EntityType != typeof(ModelBasedAgent).Name && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange); if (inRange.Count == 0) { obj.Fsm.ChangeState(Mature.Instance); return; } //If speed is higher than any agent with AIModifiers.maxMeleeAttackRange bool speedIsHigher = true; foreach (Agent x in inRange) { if (x.MovementSpeed > obj.MovementSpeed) { speedIsHigher = false; break; } } if (speedIsHigher) { obj.NextAction = new Move((obj.Position - inRange[0].Position)); return; } //Default obj.NextAction = new Defend(); }
public void Execute(StateBasedAgent obj) { if (obj.Hunger >= AIModifiers.maxHungerBeforeHitpointsDamage) { obj.Fsm.ChangeState(Hungry.Instance); return; } //ProcreationCountDown <=0 if (obj.ProcreationCountDown <= 0) { obj.Fsm.ChangeState(Mature.Instance); return; } //Range to another agent < AIModfiers.maxMeleeAttackRange and other agent Strength + Health > Strength + Health List <IEntity> inRange = obj.ViewedEntities.FindAll(x => x.GetType() != typeof(StateBasedAgent) && x is Agent && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange && (((Agent)x).Strength + ((Agent)x).Health) > (obj.Strength + obj.Health)); if (inRange.Count > 0) { obj.Fsm.ChangeState(Scared.Instance); return; } //A agent of another type with range < AIModifiers.maxMeleeAttackRange and other agent Strength + Health < Strength + Health inRange = obj.ViewedEntities.FindAll(x => x.GetType() != typeof(StateBasedAgent) && x is Agent && AIVector.Distance(obj.Position, x.Position) < AIModifiers.maxMeleeAttackRange && (((Agent)x).Strength + ((Agent)x).Health) < (obj.Strength + obj.Health)); if (inRange.Count > 0) { obj.NextAction = new Attack((Agent)inRange[0]); return; } obj.Fsm.ChangeState(new Exploring(ExploringGoal.Random)); }