コード例 #1
0
        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)));
        }
コード例 #2
0
        //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)));
        }
コード例 #3
0
ファイル: StateFlee.cs プロジェクト: Deltamat/AI-Projekt
        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)));
        }
コード例 #4
0
ファイル: StateFeed.cs プロジェクト: Deltamat/AI-Projekt
        //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));
            }
        }
コード例 #5
0
ファイル: StateFollow.cs プロジェクト: Deltamat/AI-Projekt
        public override IAction Execute(Agent0047 agent)
        {
            AIVector moveVector = agent.agentToFollow.Position - agent.Position;

            return(new Move(moveVector));
        }
コード例 #6
0
ファイル: State.cs プロジェクト: Deltamat/AI-Projekt
 public abstract IAction Execute(Agent0047 agent);
コード例 #7
0
        public override IAction GetNextAction(List <IEntity> otherEntities)
        {
            deltaTime         = DateTime.Now.TimeOfDay.TotalMilliseconds - prevTime;
            totalTimeElapsed += deltaTime;

            List <Agent> agents = otherEntities.FindAll(a => a is Agent).ConvertAll <Agent>(a => (Agent)a);

            plants = otherEntities.FindAll(a => a is Plant);
            plants.Sort((x, y) => AIVector.Distance(Position, x.Position).CompareTo(AIVector.Distance(Position, y.Position)));
            //foreach (var plant in plants) // add seen plants to list
            //{
            //    if (seenPlants.Count == 0)
            //    {
            //        seenPlants.Add(new CachedPlant(plant));
            //    }
            //    if (seenPlants.Any(a => a.CachedPos != plant.Position))
            //    {
            //        seenPlants.Add(new CachedPlant(plant));
            //    }
            //}

            //Checks if any non-allied agents are nearby and puts them in a list
            closeEnemyAgents = agents.FindAll(a => !(a is Agent0047));
            closeEnemyAgents.Sort((x, y) => AIVector.Distance(Position, x.Position).CompareTo(AIVector.Distance(Position, y.Position)));
            //Checks if allied agents are nearby and puts them in a list
            alliedAgents = agents.FindAll(a => a is Agent0047 && a != this);
            alliedAgents.Sort((x, y) => AIVector.Distance(Position, x.Position).CompareTo(AIVector.Distance(Position, y.Position)));

            delay++;

            //foreach (Agent enemy in closeEnemyAgents)
            //{
            //    if (AIVector.Distance(Position, enemy.Position) <= AIModifiers.maxMeleeAttackRange * 2)
            //    {
            //        underAttack = true;
            //        currentState = new StateFlee();
            //        currentState.Execute(this);
            //    }
            //}

            //if (underAttack && closeEnemyAgents.Count == 0)
            //{
            //    moveX = rnd.Next(-1, 2);
            //    moveY = rnd.Next(-1, 2);
            //    delay = 0;
            //    underAttack = false;
            //}

            // first priorty is procreate, always do if able
            if (ProcreationCountDown <= 0)
            {
                foreach (var ally in alliedAgents)
                {
                    if (ally.ProcreationCountDown <= 0 && AIVector.Distance(Position, ally.Position) <= AIModifiers.maxProcreateRange)
                    {
                        return(new Procreate(ally));
                    }
                    else if (ally.ProcreationCountDown <= 0)
                    {
                        return(new StateProcreate().Execute(this));
                    }
                }
            }

            //if (Hunger < 40 && alliedAgents.Count > 0)
            //{
            //    agentToFollow = (Agent0047)alliedAgents[0];
            //    currentState = new StateFollow();
            //}
            if (ProcreationCountDown == 0 && alliedAgents.Count == 0)
            {
                currentState = new StateMoveToCenter();
            }
            else if (ProcreationCountDown <= 0)
            {
                currentState = new StateProcreate();
            }

            //Stops the agents from moving out to the edges, unless they are hungry and there is food there
            if ((Position.X < Eyesight - 10 ||
                 Position.X + Eyesight - 10 > window.Width ||
                 Position.Y < Eyesight ||
                 Position.Y + Eyesight - 10 > window.Height - Eyesight - 10) &&
                delay > 60 && plants.Count == 0)
            {
                currentState = new StateReverseMove();
            }
            else if (Hunger > 20f)
            {
                currentState = new StateFeed();
            }
            else if (plants.Count == 0 && Hunger > 20f && delay > 300) //choose new direction if there are no nearby plants
            {
                moveX = rnd.Next(-1, 2);
                moveY = rnd.Next(-1, 2);

                delay = 0;
            }

            //If either in melee attack range of an enemy agent or hunger below 100 while it sees an enemy agent, the agent will attack/move closer.
            if (closeEnemyAgents.Count > 0 && !closeEnemyAgents[0].Defending && (closeEnemyAgents[0].Strength < Strength && Hunger < 100 ||
                                                                                 AIVector.Distance(this.Position, closeEnemyAgents[0].Position) <= AIModifiers.maxMeleeAttackRange))
            {
                currentState = new StateAttack();
            }

            //Stops agents from standing still
            if (moveX == 0 && moveY == 0)
            {
                moveX = rnd.Next(-100, 101) * 0.01f;
                moveY = rnd.Next(-100, 101) * 0.01f;
            }
            //only if there are no enemies nearby yourself, then go help your ally
            if (closeEnemyAgents.Count == 0 && Hunger < 100)
            {
                foreach (Agent allied in alliedAgents)
                {
                    Agent0047 agent = (Agent0047)allied;
                    if (agent.currentState is StateAttack)
                    {
                        // move towards the friendly and assists with the fight
                        return(new Move(agent.Position - Position));
                    }
                }
            }



            //used to determine how much time has passed since last update
            prevTime = DateTime.Now.TimeOfDay.TotalMilliseconds;

            return(currentState.Execute(this));
        }
コード例 #8
0
ファイル: StateAttack.cs プロジェクト: Deltamat/AI-Projekt
        public override IAction Execute(Agent0047 agent)
        {
            // if no enemies just EAT
            if (agent.closeEnemyAgents.Count == 0)
            {
                return(new StateFeed().Execute(agent));
            }

            // if in range of enemy: attack
            if (AIVector.Distance(agent.Position, agent.closeEnemyAgents[0].Position) <= AIModifiers.maxMeleeAttackRange && !agent.closeEnemyAgents[0].Defending)
            {
                return(new Attack(agent.closeEnemyAgents[0]));
            }

            float enemyDPS            = 0;
            float ownDPS              = agent.Strength * 0.5f;
            int   combinedEnemyHealth = 0;
            int   combinedAllyHealth  = agent.Health;

            foreach (var enemy in agent.closeEnemyAgents)
            {
                combinedEnemyHealth += enemy.Health;
                enemyDPS            += enemy.Strength * 0.5f;
            }
            foreach (var ally in agent.alliedAgents)
            {
                combinedAllyHealth += ally.Health;
                ownDPS             += ally.Strength * 0.5f;
            }
            float timeToKillEnemy  = combinedEnemyHealth / ownDPS;
            float timeToKillAllied = combinedAllyHealth / enemyDPS;

            if (timeToKillAllied > timeToKillEnemy)
            {
                Agent targetEnemy = agent.closeEnemyAgents[0];
                foreach (var enemy in agent.closeEnemyAgents)
                {
                    if (targetEnemy.Health > enemy.Health && !enemy.Defending)
                    {
                        targetEnemy = enemy;
                    }
                }

                if (agent.closeEnemyAgents.Count > 0 && AIVector.Distance(agent.Position, targetEnemy.Position) <= AIModifiers.maxMeleeAttackRange)
                {
                    //attack
                    return(new Attack(agent.closeEnemyAgents[0]));
                }
                else if (agent.closeEnemyAgents.Count > 0)
                {
                    //move closer if out of range
                    AIVector vectorToEnemyAgentPosition = targetEnemy.Position - agent.Position;
                    return(new Move(vectorToEnemyAgentPosition));
                }
            }
            else if (AIVector.Distance(agent.Position, agent.closeEnemyAgents[0].Position) < agent.Eyesight * 0.5f)
            {
                AIVector moveVector = agent.closeEnemyAgents[0].Position - agent.Position;
                agent.moveX = moveVector.X;
                agent.moveY = moveVector.Y;
                return(new StateFlee().Execute(agent));
            }
            return(new StateFeed().Execute(agent));
            //return new StateFlee().Execute(agent);

            //if outnumbered flee

            /*if(agent.closeEnemyAgents.Count > agent.alliedAgents.Count + 1)
             * {
             *  return new StateFlee().Execute(agent);
             * }
             * else */
            //if (agent.closeEnemyAgents.Count > 0 && AIVector.Distance(agent.Position, agent.closeEnemyAgents[0].Position) <= AIModifiers.maxMeleeAttackRange)
            //{
            //    //attack
            //    return new Attack(agent.closeEnemyAgents[0]);
            //}
            //else if (agent.closeEnemyAgents.Count > 0)
            //{
            //    //move closer if out of range
            //    AIVector vectorToEnemyAgentPosition = agent.closeEnemyAgents[0].Position - agent.Position;
            //    return new Move(vectorToEnemyAgentPosition);
            //}
            //else
            //{
            //    //if there are no closeEnemyAgents they are either dead/gone somewhere out of vision range.
            //    return new StateMoveToCenter().Execute(agent);
            //}
        }