Exemplo n.º 1
0
 public PointWithIntensity GetPointWithIntensity(Position p)
 {
     LIList <PointWithIntensity> .Enumerator e = points.GetEnumerator();
     while (e.MoveNext())
     {
         if (e.Current.Tile.Position == p)
         {
             return(e.Current);
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        public Spider GetNearestSpider(LIList <Spider> spiders)
        {
            int min = Int32.MaxValue;
            int tmp;

            LIList <Spider> .Enumerator e = spiders.GetEnumerator();
            Spider bestSpider             = null;

            while (e.MoveNext())
            {
                if ((tmp = DistanceMeasurer.Taxi(this.Position, e.Current.Position)) < min)
                {
                    bestSpider = e.Current;
                    min        = tmp;
                }
            }
            return(bestSpider);
        }
Exemplo n.º 3
0
        protected Food GetNearestFood(LIList <Food> foods)
        {
            if (foods.Count == 0)
            {
                return(null);
            }

            Food bestFood = null;
            int  min      = Int32.MaxValue;
            int  tmp;

            LIList <Food> .Enumerator e = foods.GetEnumerator();
            while (e.MoveNext())
            {
                if ((tmp = DistanceMeasurer.Taxi(this.Position, e.Current.Position)) < min)
                {
                    bestFood = e.Current;
                    min      = tmp;
                }
            }
            return(bestFood);
        }
Exemplo n.º 4
0
        private Ant FindNearestAnt()
        {
            if (Simulation.simulation.queen == null)
            {
                return(null);
            }
            if (AntHillConfig.antSightRadius >= DistanceMeasurer.Taxi(Simulation.simulation.queen.Position, this.Position))
            {
                return(Simulation.simulation.queen);
            }

            LIList <Ant> ants = Simulation.simulation.GetVisibleAnts(this);

            if (ants == null)
            {
                return(null);
            }
            if (ants.Count == 0)
            {
                return(null);
            }
            int minDistance = DistanceMeasurer.Taxi(ants.First.Value.Position, Position);

            int distance;
            Ant bestAnt = null;

            LIList <Ant> .Enumerator ant = ants.GetEnumerator();
            while (ant.MoveNext())
            {
                if ((distance = DistanceMeasurer.Taxi(this.Position, ant.Current.Position)) < minDistance)
                {
                    bestAnt     = ant.Current;
                    minDistance = distance;
                }
            }
            return(bestAnt);
        }
Exemplo n.º 5
0
        public override bool Maintain(ISimulationWorld isw)
        {
            if (!base.IsAlive())
            {
                return(false);
            }

            SpreadSignal(isw);
            LIList <Food>   food;
            LIList <Spider> spiders;

            spiders = isw.GetVisibleSpiders(this);
            if (spiders.Count != 0)
            {
                Spider s = this.GetNearestSpider(spiders);
                if (s != lastSpider)
                {
                    if (!FindEqualSignal(MessageType.SpiderLocalization, s.Position))
                    {
                        isw.CreateMessage(this.Position, MessageType.SpiderLocalization, s.Position);
                        lastSpider = s;
                    }
                }
            }
            LIList <Message> .Enumerator msg = isw.GetVisibleMessages(this).GetEnumerator();
            while (msg.MoveNext())
            {
                this.AddToSet(msg.Current, msg.Current.GetPointWithIntensity(this.Position).Intensity);
            }

            if (this.TurnsToBecomeHungry <= 0)
            {
                if (this.foodQuantity > 0)
                {
                    foodQuantity--;
                    Eat();
                }
            }

            if (this.foodQuantity == 0) //search for food
            {
                path = null;
                food = isw.GetVisibleFood(this);

                if (food.Count != 0)
                {// idzie do jedzenia
                    Food nearestFood = this.GetNearestFood(food);
                    int  dist        = DistanceMeasurer.Taxi(this.Position, nearestFood.Position);
                    if (dist == 0)
                    {
                        this.FoodQuantity = nearestFood.GetQuantity;
                        isw.DeleteFood(nearestFood);
                    }
                    else
                    {
                        if (nearestFood != lastFood)
                        {
                            if (!FindEqualSignal(MessageType.FoodLocalization, nearestFood.Position))
                            {
                                isw.CreateMessage(this.Position, MessageType.FoodLocalization, nearestFood.Position);
                                lastFood = nearestFood;
                            }
                        }
                        // znajdujemy t¹ krótk¹ œcie¿kê - wyliczane co 'maintain'
                        List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(nearestFood.Position.X, nearestFood.Position.Y), new AstarOtherObject());
                        if (trail.Count >= 2)
                        {
                            MoveOrRotateOrDig(isw, trail[1]);
                            randomDestination.X = -1;
                            return(true);
                        }
                    }
                }
                else
                {// nie widzi
                    Message m = _messages[(int)MessageType.FoodLocalization];
                    if (m != null)
                    {
                        // ma sygnal o najwiekszej intensywnosci
                        List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(m.Position.X, m.Position.Y), new AstarWorkerObject());
                        if (trail.Count >= 2)
                        {
                            MoveOrRotateOrDig(isw, trail[1]);
                            randomDestination.X = -1;
                            return(true);
                        }
                    }
                }
            }
            else
            {
                int dist = DistanceMeasurer.Taxi(this.Position, Simulation.simulation.queen.Position);
                if (dist == 0)
                {
                    isw.FeedQueen(this);
                    path = null;
                }
                else
                {
                    if (path == null || path.Count < 2)
                    {
                        path = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(Simulation.simulation.queen.Position.X, Simulation.simulation.queen.Position.Y), new AstarWorkerObject());
                    }
                    if (path.Count >= 2)
                    {
                        if (MoveOrRotateOrDig(isw, path[1]))
                        {
                            path.RemoveAt(0);
                        }
                        randomDestination.X = -1;
                        return(true);
                    }
                }
            }

            MoveRandomly(isw);

            return(true);
        }
Exemplo n.º 6
0
        public override bool Maintain(ISimulationWorld isw)
        {//TODO malo:)
            if (!base.IsAlive())
            {
                return(false);
            }

            SpreadSignal(isw);
            LIList <Message> .Enumerator msg = isw.GetVisibleMessages(this).GetEnumerator();
            while (msg.MoveNext())
            {
                this.AddToSet(msg.Current, msg.Current.GetPointWithIntensity(this.Position).Intensity);
            }

            LIList <Spider> spiders;

            if ((spiders = isw.GetVisibleSpiders(this)).Count != 0)
            {
                Spider spider = GetNearestSpider(spiders);
                if (spider != lastSpider)
                {
                    if (!FindEqualSignal(MessageType.SpiderLocalization, spider.Position))
                    {
                        isw.CreateMessage(this.Position, MessageType.SpiderLocalization, spider.Position);
                        lastSpider = spider;
                    }
                }
                MoveRotateOrAttack(this, spider, isw);
                randomDestination.X = -1;
                return(true);
            }
            if (MaintainSignals(MessageType.QueenInDanger))
            {
                randomDestination.X = -1;
                return(true);
            }
            if (MaintainSignals(MessageType.SpiderLocalization))
            {
                randomDestination.X = -1;
                return(true);
            }

            // teraz wcinamy

            LIList <Food> foods = isw.GetVisibleFood(this);

            if (foods.Count != 0)
            {
                Food food     = GetNearestFood(foods);
                int  distance = DistanceMeasurer.Taxi(this.Position, food.Position);
                if (food != lastFood)
                {
                    if (!FindEqualSignal(MessageType.FoodLocalization, food.Position))
                    {
                        isw.CreateMessage(this.Position, MessageType.FoodLocalization, food.Position);
                        lastFood = food;
                    }
                }
                if (this.TurnsToBecomeHungry <= 0)
                {
                    if (distance == 0)
                    {
                        food.Maintain(isw);
                        this.Eat();

                        randomDestination.X = -1;
                        return(true);
                    }
                    List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(food.Position.X, food.Position.Y), new AstarOtherObject());
                    if (trail == null)
                    {
                        randomDestination.X = -1;
                        return(true);
                    }
                    if (trail.Count <= 1)
                    {
                        randomDestination.X = -1;
                        return(true);
                    }
                    MoveOrRotate(trail[1]);
                    randomDestination.X = -1;
                    return(true);
                }
            }
            else
            {
                if (MaintainSignals(MessageType.FoodLocalization))
                {
                    randomDestination.X = -1;
                    return(true);
                }
            }

            MoveRandomly(isw);
            return(true);
        }
Exemplo n.º 7
0
        private void openGLControl_Paint(object sender, PaintEventArgs ea)
        {
            counter.FrameTick();

            //Gl.glClearColor(0, 0, 0, 0);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

            if (!cbVisualize.Checked)
            {
                return;
            }

            Gl.glLoadIdentity();
            Glu.gluLookAt(0, AntHillConfig.curMagnitude * 10.0f, AntHillConfig.curMagnitude * -20.0f,
                          0, 0, 0,
                          0, -1, 0);  //0.5f * Math.Sqrt(2), 0.5f * Math.Sqrt(2));
            Gl.glRotated(lookAtAngleX, 1.0d, 0.0d, 0.0d);
            Gl.glRotated(lookAtAngleY, 0.0d, 1.0d, 0.0d);
            if (Simulation.simulation == null)
            {
                return;
            }

            Map map = Simulation.simulation.Map;

            int signal;

            Gl.glColor4f(1, 1, 1, 1);
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    //if (ShouldOmitDrawing(x, y)) continue;
                    DrawElement(x, y, map.GetTile(x, y).GetTexture(), Dir.N, offsetX, offsetY, 1, 1, 0.0f);
                }
            }

            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    //if (ShouldOmitDrawing(x, y)) continue;

                    if ((signal = map.MsgCount[x, y].GetCount(MessageType.FoodLocalization)) > 0)
                    {
                        Gl.glColor4f(1, 1, 1, (float)(signal + AntHillConfig.signalInitialAlpha) / AntHillConfig.signalHighestDensity);
                        DrawElement(x, y, (int)AHGraphics.Texture.MessageFoodLocation, Dir.N, offsetX, offsetY, 1, 1, 0.01f);
                    }
                    if ((signal = map.MsgCount[x, y].GetCount(MessageType.QueenInDanger)) > 0)
                    {
                        Gl.glColor4f(1, 1, 1, (float)(signal + AntHillConfig.signalInitialAlpha) / AntHillConfig.signalHighestDensity);
                        DrawElement(x, y, (int)AHGraphics.Texture.MessageQueenInDanger, Dir.N, offsetX, offsetY, 1, 1, 0.01f);
                    }
                    if ((signal = map.MsgCount[x, y].GetCount(MessageType.QueenIsHungry)) > 0)
                    {
                        Gl.glColor4f(1, 1, 1, (float)(signal + AntHillConfig.signalInitialAlpha) / AntHillConfig.signalHighestDensity);
                        DrawElement(x, y, (int)AHGraphics.Texture.MessageQueenIsHungry, Dir.N, offsetX, offsetY, 1, 1, 0.01f);
                    }
                    if ((signal = map.MsgCount[x, y].GetCount(MessageType.SpiderLocalization)) > 0)
                    {
                        Gl.glColor4f(1, 1, 1, (float)(signal + AntHillConfig.signalInitialAlpha) / AntHillConfig.signalHighestDensity);
                        DrawElement(x, y, (int)AHGraphics.Texture.MessageSpiderLocation, Dir.N, offsetX, offsetY, 1, 1, 0.01f);
                    }
                }
            }

            Gl.glColor4f(1, 1, 1, 1);
            Creature e;
            Food     f;

            LIList <Ant> .Enumerator enumerator = Simulation.simulation.ants.GetEnumerator();
            while (enumerator.MoveNext())
            {
                e = enumerator.Current;
                //if (ShouldOmitDrawing(e.Position.X, e.Position.Y)) continue;
                DrawElement(e.Position.X, e.Position.Y, e.GetTexture(), e.Direction, offsetX, offsetY, 1, 1, 0.02f);
            }
            LIList <Spider> .Enumerator enumeratorSpider = Simulation.simulation.spiders.GetEnumerator();
            while (enumeratorSpider.MoveNext())
            {
                e = enumeratorSpider.Current;
                //if (ShouldOmitDrawing(e.Position.X, e.Position.Y)) continue;
                DrawElement(e.Position.X, e.Position.Y, e.GetTexture(), e.Direction, offsetX, offsetY, 1, 1, 0.02f);
            }
            LIList <Food> .Enumerator enumeratorFood = Simulation.simulation.food.GetEnumerator();
            while (enumeratorFood.MoveNext())
            {
                f = enumeratorFood.Current;
                //if (ShouldOmitDrawing(f.Position.X, f.Position.Y)) continue;
                DrawElement(f.Position.X, f.Position.Y, f.GetTexture(), Dir.N, offsetX, offsetY, 1, 1, 0.015f);
            }

            e = Simulation.simulation.queen;
            if (e != null)// && !ShouldOmitDrawing(e.Position.X, e.Position.Y))
            {
                DrawElement(e.Position.X, e.Position.Y, e.GetTexture(), e.Direction, offsetX, offsetY, 1, 1, 0.025f);
            }

            //deszcz
            Rain rain = Simulation.simulation.rain;

            if (rain != null)
            {
                DrawElement(rain.Position.X, rain.Position.Y, rain.GetTexture(), Dir.N, offsetX, offsetY, AntHillConfig.rainWidth, AntHillConfig.rainWidth, 1.0f);
            }
        }