Exemplo n.º 1
0
        public Rangefinder(Agent agent, float length, Ray ray)
        {
            //assign the owning Agent
            m_Agent = agent;

            //assign the length of the rangefinder
            m_Length = length;

            //create the new ray from the given ray
            m_Ray = ray;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="targetObjects"></param>
        /// <param name="radius"></param>
        /// <param name="owner"></param>
        public AgentAdjacencyList(Agent owner, List<GameEntity> targetObjects, float radius)
        {
            //grab the list to track
            m_Entities = targetObjects;

            //get the radius of the list
            m_Radius = radius;

            //get the owner of the list
            m_Owner = owner;

            //init the InRange list
            m_Edges = new List<AgentAdjacencyListEdge>();
        }
Exemplo n.º 3
0
        public Bomb(Agent agentParent, int id)
        {
            m_ID = id;
            this.m_AgentParent = agentParent;

            //to trace bombs parent
            m_AgentIdTag = agentParent.Id;

            m_World = agentParent.World;

            //inherits parents position
            this.m_Position = agentParent.Position;

            //heading is initially 0
            Heading = 0.0f;

            //load model
            loadContent(agentParent.World.Content, "bomb");
            m_Position = agentParent.Position;

            //velocity, acceleration, speed are initially zero
            Velocity = Vector3.Zero;
            Acceleration = Vector3.Zero;
            Speed = 0.0f;

            //init max speed
            //?MaxSpeed = Bann.MAX_SPEED;

            //lifetime initialy set to 3000 ( 3 seconds )
            LifeTime = 1500;

            //sensory radius will be range of bomb
            /*
             *          |
             *          |
             *       ---o---
             *          |
             *          |
             *
            */
            SensoryRadius = (float)Bomb.RADIUS;

            //logic to figure out what gets hit in explosion to be done
            // using all objects in a range and check if they
            //collide with 2 rectangles looks to be the best way
        }
Exemplo n.º 4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="agent"></param>
 public override void Execute(Agent agent)
 {
     if (agent.NearBomb())
     {
         agent.NavPath = null;
         agent.ChangeState(new State_Escape());
     }
     else if (!agent.NearEnemy())
     {
         agent.NavPath = null;
         agent.ChangeState(new State_Navigate());
     }
     else
     {
         agent.DropBomb();
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a Rangefinder of a given length to serve as an agent's sense for feelers
        /// </summary>
        /// <param name="startPosition">Where the rangefinder originates</param>
        /// <param name="length">The length of the feeler</param>
        /// <param name="angle">The angle offset from the heading of the agent</param>
        public Rangefinder(Agent agent, float length, float angle)
        {
            //assign the owning agent
            m_Agent = agent;

            //assign the length of the rangefinder
            m_Length = length;

            //get an angle representing the direction of the rangefinder
            m_Angle = angle;

            //create a rotation matrix for the angle applied to the heading
            Matrix orientationMatrix = Matrix.CreateRotationY(angle + m_Agent.Heading);

            //use the rotation matrix to transform a forward pointing Vector
            //be sure to normalize the direction vector
            Vector3 forward = Vector3.Forward;

            Vector3 direction = Vector3.Normalize(Vector3.Transform(forward, orientationMatrix));

            //create the ray from the given information
            m_Ray = new Ray(m_Agent.Position, direction);
        }
Exemplo n.º 6
0
        public void LoadNewMap(String filename)
        {
            System.IO.StreamReader fin;
            fin = new System.IO.StreamReader(@filename);

            String currentString;
            currentString = fin.ReadLine();
            m_StartPositions.Clear();
            m_Agents.Clear();
            m_AllAgents.Clear();

            bool readingWalls = false;
            bool readingBlocks = false;
            bool readingNodes = false;
            bool readingTarget = false;
            bool readingSpawn = false;

            float x, y, z;

            List<Vector3> wallPositions = new List<Vector3>();
            List<Vector3> blockPositions = new List<Vector3>();
            List<Vector3> nodePositions = new List<Vector3>();
            Vector3 targetPosition = Vector3.Zero;
            Vector3 spawnPosition = Vector3.Zero;

            while (currentString != "[End]")
            {
                //check to see if we're reading walls
                if (currentString == "[Walls]")
                {
                    readingWalls = true;
                    readingBlocks = false;
                    readingNodes = false;
                    readingTarget = false;
                    readingSpawn = false;
                    //get next line
                    currentString = fin.ReadLine();
                }
                else if (currentString == "[Blocks]")
                {
                    readingWalls = false;
                    readingBlocks = true;
                    readingNodes = false;
                    readingTarget = false;
                    readingSpawn = false;
                    //get next line
                    currentString = fin.ReadLine();
                }
                else if (currentString == "[Nodes]")
                {
                    readingWalls = false;
                    readingBlocks = false;
                    readingNodes = true;
                    readingTarget = false;
                    readingSpawn = false;
                    //get next line
                    currentString = fin.ReadLine();
                }
                else if (currentString == "[Target]")
                {
                    readingWalls = false;
                    readingBlocks = false;
                    readingNodes = false;
                    readingTarget = true;
                    readingSpawn = false;
                    //get next line
                    currentString = fin.ReadLine();
                }
                else if (currentString == "[Spawn]")
                {
                    readingWalls = false;
                    readingBlocks = false;
                    readingNodes = false;
                    readingTarget = false;
                    readingSpawn = true;
                    //get next line
                    currentString = fin.ReadLine();
                }
                else if (currentString == "")
                {
                    currentString = fin.ReadLine();
                }
                else
                {

                    //current string should now be 3 float values separated by spaces
                    //create a array of characters the length of the current string
                    char[] c = new char[currentString.Length];
                    string xString = "";
                    string yString = "";
                    string zString = "";
                    int space1 = 0, space2 = 0;

                    //create a string reader and attach it to the current character string
                    System.IO.StringReader sr = new System.IO.StringReader(currentString);

                    //scan x
                    for (int i = 0; i < currentString.Length; i++)
                    {
                        if (currentString[i] == ' ')
                        {
                            space1 = i;
                            break;
                        }
                        xString += currentString[i];
                    }

                    //scan y
                    for (int i = space1 + 1; i < currentString.Length; i++)
                    {
                        if (currentString[i] == ' ')
                        {
                            space2 = i;
                            break;
                        }
                        yString += currentString[i];
                    }

                    //scan z
                    for (int i = space2 + 1; i < currentString.Length; i++)
                    {
                        zString += currentString[i];
                    }

                    x = float.Parse(xString);
                    y = float.Parse(yString);
                    z = float.Parse(zString);

                    Vector3 newPosition = new Vector3(x, y, z);
                    //check for what we're adding to
                    if (readingWalls)
                    {
                        wallPositions.Add(newPosition);
                    }
                    else if (readingBlocks)
                    {
                        blockPositions.Add(newPosition);
                    }
                    else if (readingNodes)
                    {
                        nodePositions.Add(newPosition);
                    }
                    else if (readingSpawn)
                    {
                        spawnPosition = newPosition;
                        m_StartPositions.Add(newPosition);
                    }
                    else if (readingTarget)
                    {
                        targetPosition = newPosition;
                    }

                    //read in next string
                    currentString = fin.ReadLine();
                }

            }

            //currentString == [End]
            //so close file
            fin.Close();

            //wallPositions contains all wall positions
            LoadNewMapWalls(wallPositions);

            //blockPositions contains all block positions
            LoadNewMapBlocks(blockPositions);

            //nodePositions contains all node positions
            LoadNewMapNodes(nodePositions);

            //spawnPosition contains new m_StartAgentLocation
            if (m_StartPositions.Count == 1)
            {
                //create agents for genetic algorithm
                m_NumAgents = Params.POP_SIZE;
                m_Agents.Clear();
                for (int i = 0; i < m_NumAgents; i++)
                {
                    GameEntity newAgent = new Agent(this, m_NextId++, false);
                    ((Agent)newAgent).LoadContent(m_Content, "agent1");
                    m_Agents.Add(newAgent);
                }
                //load spawn positions
                LoadNewMapSpawn(spawnPosition);
                LoadNewMapTarget(targetPosition);
            }
            else
            {
                //create 3 more agents
                for (int i = 0; i < 3; i++)
                {
                    GameEntity newAgent = new Agent(this, m_NextId++, false);
                    ((Agent)newAgent).LoadContent(m_Content, "agent1");

                    //add new agent
                    m_Agents.Add(newAgent);
                }
                LoadNewMapStartPositions();
            }
            //targetPosition contains new m_StartTargetLocation

            //set adjacency lists
            SetAdjacencyListEnemies();
            SetAdjacencyListNodes();

            //reset tick count
            m_NumTicks = 0;
        }
Exemplo n.º 7
0
        public Vector3 Pursuit(Agent targetAgent)
        {
            //get to target vector
            Vector3 toTarget = targetAgent.Position - m_Agent.Position;

            double lookAheadTime = toTarget.Length() / (m_Agent.MaxSpeed + targetAgent.Speed);

            return Seek(targetAgent.Position + targetAgent.Velocity * (float)lookAheadTime);
        }
Exemplo n.º 8
0
 public SteeringBehaviors(Agent agent)
 {
     m_Agent = agent;
 }
Exemplo n.º 9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="agent"></param>
        public override void Execute(Agent agent)
        {
            //agent.MaxSpeed = Agent.PANIC_SPEED;
            agent.TurnSpeed = Agent.PANIC_SPEED;
            if (!agent.NearBomb())
            {
                agent.NavPath = null;
                agent.ChangeState(new State_Navigate());
            }
            else
            {
                //Console.WriteLine("Executing navigate for agent " + agent.ID);
                //return;
                // Give the agent time to move to prevent constant course correction
                if (agent.NavDelay > DateTime.Now && agent.NavPath != null && agent.NextNode != null)
                {
                    //Console.WriteLine("Navigation Delay");
                    agent.NextNode = agent.MoveAlongPath(agent.NavPath, agent.NextNode);
                    return;
                }
                else if (agent.NavDelay < DateTime.Now)
                {
                    //Console.WriteLine("Adding NavDelay");
                    agent.NavDelay = DateTime.Now.AddSeconds(5);
                }

                //find the smallest distance to the escape position
                AgentAdjacencyListEdge escapeEdge = new AgentAdjacencyListEdge();
                GameEntity escapeNode = escapeEdge.Entity;
                escapeEdge.Distance = float.MaxValue;

                //find node closest to agent
                AgentAdjacencyListEdge closestEdge = new AgentAdjacencyListEdge();
                GameEntity closestNode = closestEdge.Entity;
                closestEdge.Distance = float.MaxValue;

                //loop through all the edges in the adjacency list of nodes
                //check all the distances and retain the smallest distance
                //and record that node as the node to traverse to
                for (int i = 0; i < agent.AdjacencyListNodes.Edges.Count; i++)
                {
                    //get distance from node to escape position
                    float distance = Vector3.Distance(agent.StartingLocation, agent.AdjacencyListNodes.Edges[i].Entity.Position);

                    //compare the distances
                    if (distance < escapeEdge.Distance)
                    {
                        //set new escape edge
                        escapeEdge = agent.AdjacencyListNodes.Edges[i];

                        //set the new escape node
                        escapeNode = ((Node)escapeEdge.Entity);
                    }

                    //comparison for closest edge
                    distance = Vector3.Distance(agent.Position, agent.AdjacencyListNodes.Edges[i].Entity.Position);

                    //compare the distances
                    if (distance < closestEdge.Distance)
                    {
                        //set the closest edge
                        closestEdge = agent.AdjacencyListNodes.Edges[i];

                        //set the closest node
                        closestNode = ((Node)closestEdge.Entity);
                    }
                }

                agent.NavPath = agent.FindPath(((Node)closestNode), ((Node)escapeNode));
                agent.NextNode = (Node)closestNode;
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="agent"></param>
 public abstract void Execute(Agent agent);
Exemplo n.º 11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="agent"></param>
        public override void Execute(Agent agent)
        {
            agent.MaxSpeed = Agent.MAX_SPEED;
            agent.TurnSpeed = Agent.MAX_TURN_SPEED;

            // It is assured that by the time this is executed, all the
            // nodes will exist in the world.
            if (agent.EscapeNode == null)
            {
                float tempNodeDist = float.MaxValue;
                foreach (AgentAdjacencyListEdge edge in agent.AdjacencyListNodes.Edges)
                {
                    if (edge.Distance < tempNodeDist)
                    {
                        tempNodeDist = edge.Distance;
                        agent.EscapeNode = (Node)edge.Entity;
                    }
                }
            }

            if (agent.Invuln == true && DateTime.Now > agent.InvulnTimer)
            {
                agent.Invuln = false;
            }

            if (agent.NearBomb())
            {
                agent.NavPath = null;
                agent.ChangeState(new State_Escape());
            }
            else if (agent.NearEnemy())
            {
                agent.ChangeState(new State_Attack());
            }
            else
            {
                //Console.WriteLine("Executing navigate for agent " + agent.ID);
                //return;
                // Give the agent time to move to prevent constant course correction
                if (agent.NavDelay > DateTime.Now && agent.NavPath != null && agent.NextNode != null)
                {
                    //Console.WriteLine("Navigation Delay");
                    agent.NextNode = agent.MoveAlongPath(agent.NavPath, agent.NextNode);
                    return;
                }
                else if (agent.NavDelay < DateTime.Now)
                {
                    //Console.WriteLine("Adding NavDelay");
                    agent.NavDelay = DateTime.Now.AddSeconds(5);
                }

                // Check to see if the next node is under a block and drop bomb if appropriate
                foreach (Block block in agent.World.Blocks)
                {
                    if (agent.NextNode != null && agent.NextNode.Position == block.Position)
                    {
                        agent.DropBomb();
                        return;
                    }
                }

                // First find the closest agent
                //Console.Write("Finding closest agent... ");
                float tempAgentDist = float.MaxValue;
                Agent tempTargetAgent = null;
                foreach (AgentAdjacencyListEdge edge in agent.AdjacencyListEnemies.Edges)
                {
                    if (edge.Distance < tempAgentDist)
                    {
                        tempAgentDist = edge.Distance;
                        tempTargetAgent = (Agent)edge.Entity;
                    }
                }
                //Console.WriteLine("Agent " + tempTargetAgent.ID + " at " + tempTargetAgent.Position);

                // If that agent is different from the current target agent,
                // recalculate navigation, otherwise continue moving.
                if (!tempTargetAgent.Equals(agent.TargetAgent))
                {
                    // Find the node closest to the target enemy, use as end
                    //Console.Write("Finding end node... ");
                    float tempEndNodeDist = float.MaxValue;
                    foreach (AgentAdjacencyListEdge edge in tempTargetAgent.AdjacencyListNodes.Edges)
                    {
                        //Console.WriteLine("Looking at edge.\n");
                        if (edge.Distance < tempEndNodeDist)
                        {
                            tempEndNodeDist = edge.Distance;
                            agent.EndNode = (Node)edge.Entity;
                        }
                    }
                    //if (agent.EndNode == null)
                      //  return;
                    //Console.WriteLine("Node " + agent.EndNode.ID + " at " + agent.EndNode.Position);

                    // Find the node closest to the current agent, use as start
                    //Console.Write("Finding start node... ");
                    float tempStartNodeDist = float.MaxValue;
                    foreach (AgentAdjacencyListEdge edge in agent.AdjacencyListNodes.Edges)
                    {
                        if (edge.Distance < tempStartNodeDist)
                        {
                            tempStartNodeDist = edge.Distance;
                            agent.StartNode = (Node)edge.Entity;
                        }
                    }
                    //if (agent.StartNode == null)
                        //return;
                    //Console.WriteLine("Node " + agent.StartNode.ID + " at " + agent.StartNode.Position);

                    agent.NavPath = agent.FindPath(agent.StartNode, agent.EndNode);
                    agent.NextNode = agent.StartNode;
                }
                else
                {
                    agent.NextNode = agent.MoveAlongPath(agent.NavPath, agent.NextNode);
                }
            }

            return;
        }