示例#1
0
        private Sack updateSackAndTarget(IList <Thing> listOfThings, Creature c, out int targetRed,
                                         out int targetGreen, out int targetBlue, out int targetYellow,
                                         out int targetMagenta, out int targetWhite)
        {
            // Get information from sack and leaflets.
            Sack sack = null;

            targetRed     = 0;
            targetGreen   = 0;
            targetBlue    = 0;
            targetYellow  = 0;
            targetMagenta = 0;
            targetWhite   = 0;

            // Update leaflets
            int n = 0;

            foreach (Leaflet l in c.getLeaflets())
            {
                targetRed     += l.getRequired("Red");
                targetGreen   += l.getRequired("Green");
                targetBlue    += l.getRequired("Blue");
                targetYellow  += l.getRequired("Yellow");
                targetMagenta += l.getRequired("Magenta");
                targetWhite   += l.getRequired("White");

                if (n == 0)
                {
                    leaflet1Id = l.leafletID.ToString();
                }
                else if (n == 1)
                {
                    leaflet2Id = l.leafletID.ToString();
                }
                else
                {
                    leaflet3Id = l.leafletID.ToString();
                }
                mind.updateLeaflet(n, l);
                n++;
            }

            if (worldServer != null && worldServer.IsConnected)
            {
                sack           = worldServer.SendGetSack("0");
                targetRed     -= sack.red_crystal;
                targetGreen   -= sack.green_crystal;
                targetBlue    -= sack.blue_crystal;
                targetYellow  -= sack.yellow_crystal;
                targetMagenta -= sack.magenta_crystal;
                targetWhite   -= sack.white_crystal;
            }
            return(sack);
        }
示例#2
0
        private void processLeafletControl(String color)
        {
            if (color.Equals("White"))
            {
                if (this.leafletRemainingJewelWhite > 0)
                {
                    leafletRemainingJewelWhite--;
                }
                if (this.leafletRemainingJewelWhite == 0 && isDesiredJewel(color))
                {
                    jewelOutOfScope.Add(color);
                }
            }
            else if (color.Equals("Red"))
            {
                if (leafletRemainingJewelRed > 0)
                {
                    leafletRemainingJewelRed--;
                }
                if (this.leafletRemainingJewelRed == 0 && isDesiredJewel(color))
                {
                    jewelOutOfScope.Add(color);
                }
            }
            else if (color.Equals("Blue"))
            {
                if (leafletRemainingJewelBlue > 0)
                {
                    leafletRemainingJewelBlue--;
                }
                if (this.leafletRemainingJewelBlue == 0 && isDesiredJewel(color))
                {
                    jewelOutOfScope.Add(color);
                }
            }
            else if (color.Equals("Yellow"))
            {
                if (leafletRemainingJewelYellow > 0)
                {
                    leafletRemainingJewelYellow--;
                }
                if (this.leafletRemainingJewelYellow == 0 && isDesiredJewel(color))
                {
                    jewelOutOfScope.Add(color);
                }
            }
            else if (color.Equals("Magenta"))
            {
                if (leafletRemainingJewelMagenta > 0)
                {
                    leafletRemainingJewelMagenta--;
                }
                if (this.leafletRemainingJewelMagenta == 0 && isDesiredJewel(color))
                {
                    jewelOutOfScope.Add(color);
                }
            }
            else if (color.Equals("Green"))
            {
                if (leafletRemainingJewelGreen > 0)
                {
                    leafletRemainingJewelGreen--;
                }
                if (this.leafletRemainingJewelGreen == 0 && isDesiredJewel(color))
                {
                    jewelOutOfScope.Add(color);
                }
            }

            // Update Visual Leaflet data //
            mind.updateLeaflet(color);
            mind.update();
            //// Update Visual Leaflet data //
            //// Prepare leaflet list
            //Leaflet leaflet1 = new Leaflet ();
            //leaflet1.items = new List<LeafletItem> ();
            //leaflet1.items.Add (new LeafletItem ("Red", 1 - leafletRemainingJewelRed, 0));
            //leaflet1.items.Add (new LeafletItem ("Green", 0, 0));
            //leaflet1.items.Add (new LeafletItem ("Blue", 0, 0));
            //leaflet1.items.Add (new LeafletItem ("Yellow", 0, 0));
            //leaflet1.items.Add (new LeafletItem ("Magenta", 0, 0));
            //leaflet1.items.Add (new LeafletItem ("White", 2 - leafletRemainingJewelWhite, 0));
            //Leaflet leaflet2 = new Leaflet ();
            //leaflet2.items = new List<LeafletItem> ();
            //leaflet2.items.Add (new LeafletItem ("Red", 0, 0));
            //leaflet2.items.Add (new LeafletItem ("Green", 0, 0));
            //leaflet2.items.Add (new LeafletItem ("Blue", 2 - leafletRemainingJewelBlue, 0));
            //leaflet2.items.Add (new LeafletItem ("Yellow", 1 - leafletRemainingJewelYellow, 0));
            //leaflet2.items.Add (new LeafletItem ("Magenta", 0, 0));
            //leaflet2.items.Add (new LeafletItem ("White", 0, 0));
            //Leaflet leaflet3 = new Leaflet ();
            //leaflet3.items = new List<LeafletItem> ();
            //leaflet3.items.Add (new LeafletItem ("Red", 0, 0));
            //leaflet3.items.Add (new LeafletItem ("Green", 1 - leafletRemainingJewelGreen, 0));
            //leaflet3.items.Add (new LeafletItem ("Blue", 0, 0));
            //leaflet3.items.Add (new LeafletItem ("Yellow", 0, 0));
            //leaflet3.items.Add (new LeafletItem ("Magenta", 2 - leafletRemainingJewelMagenta, 0));
            //leaflet3.items.Add (new LeafletItem ("White", 0, 0));

            //// Update leaflets to be showed in UI view
            //mind.updateLeaflet (0, leaflet1);
            //mind.updateLeaflet (1, leaflet2);
            //mind.updateLeaflet (2, leaflet3);

            //Console.WriteLine("processLeafletControl: " + color);
        }
示例#3
0
        /// <summary>
        /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand
        /// </summary>
        /// <param name="sensorialInformation">The information that came from server</param>
        /// <returns>The perceived information</returns>
        private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings)
        {
            // New sensory information
            SensoryInformation si = World.NewSensoryInformation(CurrentAgent);

            // Detect if we have jewel ahead
            IEnumerable <Thing> jewelsAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.DistanceToCreature <= CLOSE_DISTANCE));
            Boolean             jewelAhead  = jewelsAhead.Any();

            if (jewelAhead)
            {
                jewelName = jewelsAhead.First().Name;
            }

            // Detect if we have food ahead
            IEnumerable <Thing> foods     = listOfThings.Where(item => ((item.CategoryId == Thing.CATEGORY_FOOD || item.CategoryId == Thing.CATEGORY_NPFOOD || item.CategoryId == Thing.CATEGORY_PFOOD) && item.DistanceToCreature <= CLOSE_DISTANCE));
            Boolean             foodAhead = foods.Any();

            if (foodAhead)
            {
                foodName = foods.First().Name;
            }

            // save the closest food just in case the creature needs it
            IEnumerable <Thing> distantFoods = listOfThings.Where(item => ((item.CategoryId == Thing.CATEGORY_NPFOOD || item.CategoryId == Thing.CATEGORY_PFOOD) && item.DistanceToCreature > CLOSE_DISTANCE));

            if (distantFoods.Count() > 0)
            {
                closestFood = distantFoods.OrderBy(item => item.DistanceToCreature).First();
            }

            // Detect if we have a wall ahead
            Boolean wallAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_BRICK && item.DistanceToCreature <= CLOSE_DISTANCE)).Any();

            closestJewel = null;

            //Console.WriteLine(sensorialInformation);
            Creature c       = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First();
            int      n       = 0;
            bool     deliver = false;

            foreach (Leaflet l in c.getLeaflets())
            {
                // Detect if some leaflet is complete
                //l.situation
                mind.updateLeaflet(n, l);
                // check if we have all jewels from the leaflet (situation = true)
                if (l.situation)
                {
                    // avoid delivering it twice
                    if (!checkDelivered(l.leafletID.ToString()))
                    {
                        leafletId = l.leafletID.ToString();
                        deliver   = true;
                    }
                }
                else
                {
                    // lookup the closest jewel that we need to fill a leaflet
                    IEnumerable <Thing> distantJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.DistanceToCreature > CLOSE_DISTANCE && leafletNeedsJewel(l, item)));
                    if (distantJewels.Count() > 0)
                    {
                        closestJewel = distantJewels.OrderBy(item => item.DistanceToCreature).First();
                    }
                }
                n++;
            }

            // Detect if we have food and we need it
            bool needAndHaveFood = (closestFood != null) && (c.Fuel < 400);

            // Add sensorial input with a rule-based prioritization

            if (stopped)
            {
                // Success, don't do anything else
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION);
            }
            else if (checkThreeLeafletsReady())
            {
                // Time to deliver
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MAX_ACTIVATION);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION);
            }
            else if (jewelAhead || foodAhead)
            {
                // Prioritize actions that don't require move: eat food, get jewel or deliver leaflet
                double jewelAheadActivationValue = jewelAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION;
                double foodAheadActivationValue  = foodAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION;
                double deliverActivationValue    = deliver ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION;
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputJewelAhead, jewelAheadActivationValue);
                si.Add(inputFoodAhead, foodAheadActivationValue);
                si.Add(inputDeliverLeaflet, deliverActivationValue);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION);
            }
            else if (wallAhead)
            {
                // Avoid obstacle
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MAX_ACTIVATION);
                si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION);
            }
            else if (needAndHaveFood)
            {
                // go for food if the creature needs it
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantFood, CurrentAgent.Parameters.MAX_ACTIVATION);
                si.Add(inputDistantJewel, CurrentAgent.Parameters.MIN_ACTIVATION);
            }
            else if (closestJewel != null)
            {
                // go for the closest jewel needed for a leaflet
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputWallAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputJewelAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputFoodAhead, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDeliverLeaflet, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantFood, CurrentAgent.Parameters.MIN_ACTIVATION);
                si.Add(inputDistantJewel, CurrentAgent.Parameters.MAX_ACTIVATION);
            }
            return(si);
        }
示例#4
0
        /// <summary>
        /// Make the agent perception. In other words, translate the information that came from sensors to a new type that the agent can understand
        /// </summary>
        /// <param name="sensorialInformation">The information that came from server</param>
        /// <returns>The perceived information</returns>
        private SensoryInformation prepareSensoryInformation(IList <Thing> listOfThings)
        {
            // New sensory information
            SensoryInformation si = World.NewSensoryInformation(CurrentAgent);

            // Detect if we have a wall ahead
            Boolean wallAhead = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_BRICK && item.DistanceToCreature <= 21)).Any();
            // double wallAheadActivationValue = wallAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION;
            double wallAheadActivationValue = wallAhead ? CurrentAgent.Parameters.MAX_ACTIVATION : CurrentAgent.Parameters.MIN_ACTIVATION;

            si.Add(inputWallAhead, wallAheadActivationValue);

            // Detect if we have food close by
            IEnumerable <Thing> foods = listOfThings.Where(item => ((item.CategoryId == Thing.CATEGORY_NPFOOD || item.CategoryId == Thing.CATEGORY_PFOOD) && item.DistanceToCreature <= 61));
            double foodAheadActivationValue;

            if (foods.Any())
            {
                foodFound = foods.First().Name;
                foodAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION;
            }
            else
            {
                foodFound = String.Empty;
                foodAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION;
            }
            si.Add(inputFoodAhead, foodAheadActivationValue);


            // Detect if we have jewel close by
            IEnumerable <Thing> closeJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.DistanceToCreature <= 61));
            double jewelAheadActivationValue;

            if (closeJewels.Any())
            {
                jewelFound = closeJewels.First().Name;
                jewelAheadActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION;
            }
            else
            {
                jewelFound = String.Empty;
                jewelAheadActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION;
            }
            si.Add(inputJewelAhead, jewelAheadActivationValue);



            //Console.WriteLine(sensorialInformation);
            Creature c = (Creature)listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_CREATURE)).First();
            int      n = 0;

            foreach (Leaflet l in c.getLeaflets())
            {
                mind.updateLeaflet(n, l);
                n++;
            }
            mind.updateTotLeaflet();

            // Detect if has a required leaflet and prepare its position
            IEnumerable <Thing> jewels    = listOfThings.Where(item => false);
            IEnumerable <Thing> redJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Red"));
            double goToJewelActivationValue;

            if (redJewels.Any() && ((mind.totLeaflet [0] - mind.red) > 0))
            {
                jewels = jewels.Concat(redJewels);
            }
            IEnumerable <Thing> greenJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Green"));

            if (greenJewels.Any() && ((mind.totLeaflet [1] - mind.green) > 0))
            {
                jewels = jewels.Concat(greenJewels);
            }
            IEnumerable <Thing> blueJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Blue"));

            if (blueJewels.Any() && ((mind.totLeaflet [2] - mind.blue) > 0))
            {
                jewels = jewels.Concat(blueJewels);
            }
            IEnumerable <Thing> yellowJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Yellow"));

            if (yellowJewels.Any() && ((mind.totLeaflet [3] - mind.yellow) > 0))
            {
                jewels = jewels.Concat(yellowJewels);
            }
            IEnumerable <Thing> magentaJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "Magenta"));

            if (magentaJewels.Any() && ((mind.totLeaflet[4] - mind.magenta) > 0))
            {
                jewels = jewels.Concat(magentaJewels);
            }
            IEnumerable <Thing> whiteJewels = listOfThings.Where(item => (item.CategoryId == Thing.CATEGORY_JEWEL && item.Material.Color == "White"));

            if (whiteJewels.Any() && ((mind.totLeaflet[5] - mind.white) > 0))
            {
                jewels = jewels.Concat(whiteJewels);
            }

            if (jewels.Any())
            {
                jewels = jewels.OrderBy(item => item.DistanceToCreature);
                x_go   = jewels.First().X1;
                y_go   = jewels.First().Y1;
                goToJewelActivationValue = CurrentAgent.Parameters.MAX_ACTIVATION;
            }
            else
            {
                goToJewelActivationValue = CurrentAgent.Parameters.MIN_ACTIVATION;
            }
            si.Add(inputNeedJewel, goToJewelActivationValue);

            return(si);
        }