コード例 #1
0
ファイル: Worker.cs プロジェクト: corefan/yad2
        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);
        }