Exemplo n.º 1
0
        private void UpdatePhysics()
        {
            colony.Wander(r);

            if (timeSteps % 5 == 0 && doSpawn)
            {
                colony.ants.Add(new Ant(10, new Vector2(Width / 2, Height / 2),
                                        Misc.VecFromAng(r.NextDouble() * 360f)));
            }

            if (timeSteps % 9 == 0)
            {
                colony.UpdatePheromones();
            }

            colony.pathPheromonesQTree = new QTree(new Vector2(Width / 2, Height / 2), new Vector2(Width, Height), 4);
            colony.pathPheromonesQTree.Fill(colony.pathPheromones.ToList <Point>());

            colony.foodPheromonesQTree = new QTree(new Vector2(Width / 2, Height / 2), new Vector2(Width, Height), 4);
            colony.foodPheromonesQTree.Fill(colony.foodPheromones.ToList <Point>());

            foodQTree = new QTree(new Vector2(Width / 2, Height / 2), new Vector2(Width, Height), 1);
            foodQTree.Fill(food);

            colony.FollowPheromones();
            colony.SeekFood(foodQTree);
            colony.SeekHome(new Vector2(Width / 2, Height / 2));

            colony.AvoidBorders(30, Width, Height);
            colony.BounceFromBorders(Width, Height);
            colony.UpdateLocation();
        }
Exemplo n.º 2
0
        void Subdivide()
        {
            if (!isDivided)
            {
                topleft  = new QTree(new Vector2(loc.X - dim.X / 4, loc.Y - dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);
                topright = new QTree(new Vector2(loc.X + dim.X / 4, loc.Y - dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);
                botleft  = new QTree(new Vector2(loc.X - dim.X / 4, loc.Y + dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);
                botright = new QTree(new Vector2(loc.X + dim.X / 4, loc.Y + dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);

                isDivided = true;
            }
        }
Exemplo n.º 3
0
        //public void UpdatePheromones()
        //{
        //   List<Pheromone> toRemove = new List<Pheromone>();

        //   foreach (var p in pathPheromones)
        //   {
        //      if (p.durationLeft == 0)
        //         toRemove.Add(p);
        //      else
        //         p.UpdateSaturation();
        //   }

        //   pathPheromones.RemoveAll(p => toRemove.Contains(p));

        //   toRemove = new List<Pheromone>();

        //   foreach (var p in foodPheromones)
        //   {
        //      if (p.durationLeft == 0)
        //         toRemove.Add(p);
        //      else
        //         p.UpdateSaturation();
        //   }

        //   foodPheromones.RemoveAll(p => toRemove.Contains(p));

        //   foreach (var ant in ants)
        //   {
        //      if (ant.isCarryingFood)
        //         foodPheromones.Add(new Pheromone(3, ant.loc, 150));
        //      else
        //         pathPheromones.Add(new Pheromone(3, ant.loc, 150));
        //   }
        //}

        public void SeekFood(QTree foodQTree)
        {
            foreach (var ant in ants)
            {
                if (!ant.isCarryingFood)
                {
                    List <Point> neibs = new List <Point>();
                    foodQTree.Quarry(new Point(ant.loc + ant.vel.Normalized() * ant.size * 3.5f), ant.size * 5f, neibs);

                    if (neibs.Count != 0)
                    {
                        Point minNeib = neibs[0];
                        float minDist = Vector2.Distance(neibs[0].loc, ant.loc);

                        foreach (var n in neibs)
                        {
                            float dist = Vector2.Distance(n.loc, ant.loc);
                            if (dist < minDist)
                            {
                                minDist = dist;
                                minNeib = n;
                            }
                        }

                        if (minDist < ant.size)
                        {
                            ant.isCarryingFood        = true;
                            ant.vel                  *= -1;
                            ant.pheromoneDurationLeft = ant.pheromoneDuration;
                        }
                        else
                        {
                            ant.Steer(minNeib.loc, 1f);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void QuarryLimited(Point centralPoint, float widthOfSearch, List <Point> found, int limit)
        {
            Queue <QTree> queue = new Queue <QTree>();
            Queue <int>   layer = new Queue <int>();

            bool doAddNew = true;

            queue.Enqueue(this);
            //layer.Enqueue(1);

            while (queue.Count != 0)
            {
                QTree t = queue.Dequeue();

                foreach (Point other in t.points)
                {
                    if (centralPoint != other && Contains(other.loc, new Vector2(centralPoint.loc.X, centralPoint.loc.Y), new Vector2(widthOfSearch, widthOfSearch)))
                    {
                        found.Add(other.Copy());
                    }
                }

                if (found.Count >= limit)
                {
                    return;

                    doAddNew = false;
                }

                if (doAddNew && t.isDivided)
                {
                    queue.Enqueue(t.topleft);
                    queue.Enqueue(t.topright);
                    queue.Enqueue(t.botleft);
                    queue.Enqueue(t.botright);
                }
            }
        }