Exemple #1
0
        public Map.Elements rememberMark; //remember what was at the position when the agent move to place the mark again

        public Agent(int _team, int _xpos, int _ypos, Map _map, Warehouse _myWarehouse)
        {
            team         = _team;
            xpos         = _xpos;
            ypos         = _ypos;
            empty        = true;
            foundWH      = false;
            map          = _map;
            myWarehouse  = _myWarehouse;
            rememberMark = Map.Elements.Nothing; // The position where the agent is was empty
        }
        override public void DoSomething()
        {
            turn += 1;
            List <int> xs;
            List <int> ys;

            if (!this.empty)
            {
                //If my wearhouse is in an accesible position
                if (SearchPos(this.xpos, this.ypos, Map.Elements.Warehouse, out xs, out ys))
                {
                    this.empty                  = true;
                    this.justEmpty              = true;
                    this.justFull               = false;
                    myWarehouse.StoreResources += 1;
                }

                else
                {
                    //If just find and element in the last turn or every 3 turns look for the shortest path to the wearhouse
                    if (justFull || this.turn % 3 == 0)
                    {
                        pathX.Clear();
                        pathY.Clear();
                        this.justFull = false;
                        FindShortestPath(Map.Elements.Warehouse, out pathX, out pathY);
                        step = 0;
                    }

                    //if following the path to the wearhouse move to the next step
                    if (pathX.Count > 0 && step < pathX.Count && (int)map.currentMap[pathX[step], pathY[step]] <= 2)
                    {
                        Map.Elements temp = (Map.Elements)map.currentMap[pathX[step], pathY[step]];
                        map.MoveAgent(this, pathX[step], pathY[step]);

                        rememberMark = temp;
                        this.xpos    = pathX[step];
                        this.ypos    = pathY[step];
                        step        += 1;
                    }

                    //Move to an empty available position
                    else if (SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark2, out xs, out ys) || SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark1, out xs, out ys) ||
                             SearchPosMove(this.xpos, this.ypos, Map.Elements.Nothing, out xs, out ys))
                    {
                        int          selectPos = pos.Next(xs.Count);
                        Map.Elements temp      = (Map.Elements)map.currentMap[xs[selectPos], ys[selectPos]];
                        map.MoveAgent(this, xs[selectPos], ys[selectPos]);

                        rememberMark = temp;
                        this.xpos    = xs[selectPos];
                        this.ypos    = ys[selectPos];
                    }
                }
            }

            //if is empty
            else
            {
                //If an element is in an accesible position
                if (SearchPos(this.xpos, this.ypos, Map.Elements.Resource, out xs, out ys))
                {
                    int selectPos = pos.Next(xs.Count);
                    map.DeleteResorce(xs[selectPos], ys[selectPos]);
                    this.empty     = false;
                    this.justEmpty = false;
                    this.justFull  = true;
                }

                else
                {
                    //If just drop and element in the last turn or every 3 turns look for the shortest path to the nearest resource
                    if (justEmpty || this.turn % 3 == 0)
                    {
                        pathX.Clear();
                        pathY.Clear();
                        justEmpty = false;
                        FindShortestPath(Map.Elements.Resource, out pathX, out pathY);
                        step = 0;
                    }

                    //if following the path to a resorce move to the next step
                    if (pathX.Count > 0 && step < pathX.Count && (int)map.currentMap[pathX[step], pathY[step]] <= 2)
                    {
                        Map.Elements temp = (Map.Elements)map.currentMap[pathX[step], pathY[step]];
                        map.MoveAgent(this, pathX[step], pathY[step]);

                        rememberMark = temp;
                        this.xpos    = pathX[step];
                        this.ypos    = pathY[step];
                        step        += 1;
                    }

                    else if (SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark2, out xs, out ys) || SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark1, out xs, out ys) ||
                             SearchPosMove(this.xpos, this.ypos, Map.Elements.Nothing, out xs, out ys))
                    {
                        int          selectPos = pos.Next(xs.Count);
                        Map.Elements temp      = (Map.Elements)map.currentMap[xs[selectPos], ys[selectPos]];
                        map.MoveAgent(this, xs[selectPos], ys[selectPos]);

                        rememberMark = temp;
                        this.xpos    = xs[selectPos];
                        this.ypos    = ys[selectPos];
                    }
                }
            }
        }
        //Find the shortest path to the nearest resource or the wearhouse
        private void FindShortestPath(Map.Elements element, out List <int> shortestPathX, out List <int> shortestPathY)
        {
            List <int> shortPathx = new List <int>();
            List <int> shortPathy = new List <int>();

            List <int> currentPathx = new List <int>();
            List <int> currentPathy = new List <int>();

            //A copy of the map to modify
            int[,] newMap = new int[map.currentMap.GetLength(0), map.currentMap.GetLength(0)];

            for (int i = 0; i < map.currentMap.GetLength(0); i++)
            {
                for (int j = 0; j < map.currentMap.GetLength(0); j++)
                {
                    newMap[i, j] = (int)map.currentMap[i, j];
                }
            }

            shortestPathX = new List <int>();
            shortestPathY = new List <int>();

            //Look for the element in de map
            for (int i = 0; i < map.currentMap.GetLength(0); i++)
            {
                for (int j = 0; j < map.currentMap.GetLength(0); j++)
                {
                    if (newMap[i, j] == (int)element)
                    {
                        if (element == Map.Elements.Warehouse)
                        {
                            //found the werahouse
                            if (myWarehouse.Xpos == i && myWarehouse.Ypos == j)
                            {
                                FindShortestPath(newMap, this.xpos, this.ypos, i, j, shortPathx, shortPathy, currentPathx, currentPathy);
                                shortestPathX.AddRange(shortPathx);
                                shortestPathY.AddRange(shortPathy);
                                shortPathx.Clear();
                                shortPathy.Clear();
                                //the firts position in the path is where the agent is
                                if (shortestPathX.Count() > 0)
                                {
                                    shortestPathX.RemoveAt(0);
                                    shortestPathY.RemoveAt(0);
                                }
                                return;
                            }
                        }

                        //Looking for a resourse
                        else
                        {
                            FindShortestPath(newMap, this.xpos, this.ypos, i, j, shortPathx, shortPathy, currentPathx, currentPathy);
                            if (shortPathx.Count > 0 && (shortPathx.Count < shortestPathX.Count || shortestPathX.Count == 0))
                            {
                                shortestPathX.AddRange(shortPathx);
                                shortestPathY.AddRange(shortPathy);
                                //the firts position in the path is where the agent is
                                shortestPathX.RemoveAt(0);
                                shortestPathY.RemoveAt(0);
                            }


                            shortPathx.Clear();
                            shortPathy.Clear();
                        }
                    }
                }
            }
        }
Exemple #4
0
        //Return all availables positions (the 4 where the agent can move)  where is the wanted element
        internal static bool SearchPosMove(int x, int y, Map.Elements element, out List <int> xs, out List <int> ys)
        {
            bool found = false;

            xs = new List <int>();
            ys = new List <int>();

            if (x + 1 < map.currentMap.GetLength(0) && map.currentMap[x + 1, y] == element)
            {
                //If looking for a wearhouse check that is the one of the agent team
                if (element == Map.Elements.Warehouse)
                {
                    if (myWarehouse.Xpos == x + 1 && myWarehouse.Ypos == y)
                    {
                        found = true;
                        xs.Add(x + 1);
                        ys.Add(y);
                    }
                }

                else
                {
                    found = true;
                    xs.Add(x + 1);
                    ys.Add(y);
                }
            }

            if (x - 1 >= 0 && map.currentMap[x - 1, y] == element)
            {
                //If looking for a wearhouse check that is the one of the agent team
                if (element == Map.Elements.Warehouse)
                {
                    if (myWarehouse.Xpos == x - 1 && myWarehouse.Ypos == y)
                    {
                        found = true;
                        xs.Add(x - 1);
                        ys.Add(y);
                    }
                }

                else
                {
                    found = true;
                    xs.Add(x - 1);
                    ys.Add(y);
                }
            }

            if (y + 1 < map.currentMap.GetLength(0) && map.currentMap[x, y + 1] == element)
            {
                //If looking for a wearhouse check that is the one of the agent team
                if (element == Map.Elements.Warehouse)
                {
                    if (myWarehouse.Xpos == x && myWarehouse.Ypos == y + 1)
                    {
                        found = true;
                        xs.Add(x);
                        ys.Add(y + 1);
                    }
                }

                else
                {
                    found = true;
                    xs.Add(x);
                    ys.Add(y + 1);
                }
            }

            if (y - 1 >= 0 && map.currentMap[x, y - 1] == element)
            {
                //If looking for a wearhouse check that is the one of the agent team
                if (element == Map.Elements.Warehouse)
                {
                    if (myWarehouse.Xpos == x && myWarehouse.Ypos == y - 1)
                    {
                        found = true;
                        xs.Add(x);
                        ys.Add(y - 1);
                    }
                }

                else
                {
                    found = true;
                    xs.Add(x);
                    ys.Add(y - 1);
                }
            }

            return(found);
        }
        override public void DoSomething()
        {
            List <int> xs;
            List <int> ys;

            if (this.empty)
            {
                //if is a resource at a reachable position, get one random
                if (SearchPos(this.xpos, this.ypos, Map.Elements.Resource, out xs, out ys))
                {
                    int selectPos = pos.Next(xs.Count);
                    map.DeleteResorce(xs[selectPos], ys[selectPos]);
                    this.empty = false;
                    return;
                }

                //if is an empty position at wich the agent can move, move to one random
                else if (SearchPosMove(this.xpos, this.ypos, Map.Elements.Nothing, out xs, out ys) || SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark1, out xs, out ys) ||
                         SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark2, out xs, out ys))
                {
                    int          selectPos = pos.Next(xs.Count);
                    Map.Elements temp      = (Map.Elements)map.currentMap[xs[selectPos], ys[selectPos]];
                    map.MoveAgent(this, xs[selectPos], ys[selectPos]);

                    //if already found the wearhouse mark the path taken
                    if (foundWH)
                    {
                        map.currentMap[this.xpos, this.ypos] = Map.Elements.Mark2;
                    }

                    rememberMark = temp;
                    this.xpos    = xs[selectPos];
                    this.ypos    = ys[selectPos];
                    return;
                }
            }

            else if (!this.empty)
            {
                //if the agent's wearhouse is at a reachable position, drop the resource
                if (SearchPos(this.xpos, this.ypos, Map.Elements.Warehouse, out xs, out ys))
                {
                    this.empty = true;
                    myWarehouse.StoreResources += 1;
                    foundWH = true;
                    return;
                }

                //if is an empty position at wich the agent can move, move to one random
                else if (SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark2, out xs, out ys) || SearchPosMove(this.xpos, this.ypos, Map.Elements.Mark1, out xs, out ys) ||
                         SearchPosMove(this.xpos, this.ypos, Map.Elements.Nothing, out xs, out ys))
                {
                    int          selectPos = pos.Next(xs.Count);
                    Map.Elements temp      = (Map.Elements)map.currentMap[xs[selectPos], ys[selectPos]];
                    map.MoveAgent(this, xs[selectPos], ys[selectPos]);

                    if (rememberMark == Map.Elements.Mark2)
                    {
                        map.currentMap[this.xpos, this.ypos] = Map.Elements.Mark1;
                    }

                    else if ((rememberMark == Map.Elements.Mark1))
                    {
                        map.currentMap[this.xpos, this.ypos] = Map.Elements.Nothing;
                    }

                    rememberMark = temp;
                    this.xpos    = xs[selectPos];
                    this.ypos    = ys[selectPos];
                    return;
                }
            }
        }