public void handleMessage(CommMessage msg)
 {
     //for foraging
     if (msg.text == "resource_request")
     {
         if (untouchedResources.Count > 0)
         {
             location = untouchedResources[0];
             robotResourceMap[msg.senderId] = location;
             satellite.directMessage(msg.senderId, "resource_location\t" + "" + location);
             aboutToBeTouchedResources.Add(location);
             untouchedResources.Remove(location);
         }
         else
         {
             return_to_base = robotPositions[msg.senderId];
             satellite.directMessage(msg.senderId, "go_to_base\t" + "" + return_to_base);
         }
     }
     if (msg.text == "resource_home_request")
     {
         RH_location = resourceDestenation();
         satellite.directMessage(msg.senderId, "resource_home\t" + "" + RH_location);
     }
     if (msg.text == "resource_delivered")
     {
         Vector2 resourceLocation = robotResourceMap[msg.senderId];
         //remove resource from pending
         aboutToBeTouchedResources.Remove(resourceLocation);
         //add resource to touchedResources
         touchedResources.Add(resourceLocation);
         if (untouchedResources.Count > 0)
         {
             location = untouchedResources[0];
             robotResourceMap[msg.senderId] = location;
             satellite.directMessage(msg.senderId, "resource_location\t" + "" + location);
             aboutToBeTouchedResources.Add(location);
             untouchedResources.Remove(location);
         }
         else
         {
             return_to_base = robotPositions[msg.senderId];
             satellite.directMessage(msg.senderId, "go_to_base\t" + "" + return_to_base);
         }
     }
     // when untouchedResources and aboutToBeTouchedResources are empty, satellite will exit the foraging state
     if (untouchedResources == null && aboutToBeTouchedResources == null)
     {
         foraging = false;
     }
 }
        public bool handleMessage(CommMessage msg)
        {
            bool handled = false;

            if (msg.text.StartsWith("construction"))
            {
                string[] lines = msg.text.Split('\n');
                if (lines[1] == "request_task")
                {
                    handled = true;

                    if (structureLayers.Count > 0 && resourceOrigins.Count > 0)
                    {
                        List <Vector2> constructionPerimeter = structureLayers.Peek().constructionPerimeter;
                        Vector2        resourceOrigin        = resourceOrigins.Dequeue();
                        Vector2        resourcePlacement     = structureLayers.Peek().resourcePlacements.Dequeue();

                        --totalRequiredResources;

                        if (structureLayers.Peek().resourcePlacements.Count == 0)
                        {
                            structureLayers.Dequeue();
                        }

                        bool lastTask = structureLayers.Count == 0 || resourceOrigins.Count == 0 || totalRequiredResources < mainScript.getNumRobots();
                        satellite.directMessage(msg.senderId,
                                                new MessageConstructionTask(resourceOrigin,
                                                                            constructionPerimeter,
                                                                            resourcePlacement,
                                                                            lastTask).ToString());
                    }
                    else
                    {
                        satellite.directMessage(msg.senderId, "construction/finished");
                    }
                }
            }

            return(handled);
        }
Esempio n. 3
0
        public bool handleMessage(CommMessage msg)
        {
            bool handled = false;

            if (msg.text.StartsWith("build"))
            {
                string[] lines = msg.text.Split('\n');
                if (lines[1] == "request_task")
                {
                    handled = true;

                    if (totalRequiredResources > 0)
                    {
                        if (resourceOrigins.Count > 0)
                        {
                            --totalRequiredResources;
                            ++robotsInResourceCache;

                            bool lastTask = totalRequiredResources < mainScript.getNumRobots();
                            satellite.directMessage(msg.senderId,
                                                    new MessageBuildTask(resourceOrigins.Dequeue(),
                                                                         resourcePlacements.Dequeue(),
                                                                         lastTask).ToString());
                        }
                        else // must generate more resources
                        {
                            Log.w(LogTag.SATELLITE, "adding to pending requests");
                            pendingRequests.Enqueue(msg);
                        }
                    }
                    else
                    {
                        satellite.directMessage(msg.senderId, "build/finished");
                    }
                }
                else if (lines[1] == "left_cache")
                {
                    --robotsInResourceCache;

                    if (pendingRequests.Count > 0 && robotsInResourceCache == 0)
                    {
                        // place more resources in cache and refresh resourceOrigins
                        resourceOrigins = new Queue <Vector2>(mainScript.refillResourceCache());

                        while (pendingRequests.Count > 0)
                        {
                            --totalRequiredResources;
                            ++robotsInResourceCache;

                            CommMessage requestMessage = pendingRequests.Dequeue();
                            bool        lastTask       = totalRequiredResources < mainScript.getNumRobots();
                            satellite.directMessage(requestMessage.senderId,
                                                    new MessageBuildTask(resourceOrigins.Dequeue(),
                                                                         resourcePlacements.Dequeue(),
                                                                         lastTask).ToString());
                        }
                    }
                }
            }

            return(handled);
        }