Esempio n. 1
0
        public void CreateGroup(Agent agent, Sequence sequence, Door startingDoor)
        {
            Door targetDoor = SimulationController.Instance
                              .BuildingManager.GetFinishingDoorByTargetBuilding(
                startingDoor,
                sequence.TargetBuildingId
                );

            List <Agent> availableAgents = new List <Agent>();

            foreach (var sequenceGroupingAgent in sequence.GroupingAgents)
            {
                if (IsAgentAvailableForGrouping(agent, sequence, sequenceGroupingAgent))
                {
                    availableAgents.Add(SimulationController.Instance.AgentManager.GetAgentById(sequenceGroupingAgent));
                }
            }
            availableAgents.Add(agent);

            tempSet.Clear();
            sameBuildingStarters.Clear();
            var meetingPosition = CalculateMeetingPosition(startingDoor, targetDoor, availableAgents);
            var groupSequence   = new GroupSequence(meetingPosition, targetDoor.transform.position, targetDoor);

            var subgroupList = new List <int>();

            //Find agents that will leave the building at the same time, create a subgroup for them
            for (int i = 0; i < availableAgents.Count; i++)
            {
                for (int j = i + 1; j < availableAgents.Count; j++)
                {
                    if (tempSet.Contains(i) || tempSet.Contains(j))
                    {
                        continue;
                    }

                    if (availableAgents[i].GetNextSequence().StartingBuildingId == availableAgents[j].GetNextSequence().StartingBuildingId)
                    {
                        tempSet.Add(j);
                        if (!sameBuildingStarters.Contains(availableAgents[i]))
                        {
                            sameBuildingStarters.Add(availableAgents[i]);
                        }

                        if (!sameBuildingStarters.Contains(availableAgents[j]))
                        {
                            sameBuildingStarters.Add(availableAgents[j]);
                        }
                    }
                }
                if (sameBuildingStarters.Count > 1)
                {
                    subgroupList.Add(sameBuildingStarters.Count);
                    Debug.Log("Same building group!");
                    tempSet.Add(i);
                    var leaveTogetherGroup = new GroupSequence();
                    leaveTogetherGroup.agentCount = sameBuildingStarters.Count;
                    leaveTogetherGroup.SetParentGroupSequence(groupSequence);
                    foreach (var sameBuildingStarter in sameBuildingStarters)
                    {
                        Debug.Log($"Same building agent ID: {sameBuildingStarter.GetAgentId()}", sameBuildingStarter);
                        leaveTogetherGroup.AddAgent(sameBuildingStarter);
                        activeGroups[sameBuildingStarter.GetAgentId()] = leaveTogetherGroup;
                    }
                }
                sameBuildingStarters.Clear();
            }

            //Create the main group
            for (var index = 0; index < availableAgents.Count; index++)
            {
                var availableAgent = availableAgents[index];

                if (!tempSet.Contains(index))
                {
                    activeGroups[availableAgent.GetAgentId()] = groupSequence;
                    subgroupList.Add(1);
                }
                groupSequence.AddAgent(availableAgent);

                groupSequence.debugText += $" - {availableAgent.GetAgentId()} - ";
            }
            groupSequence.agentCount = availableAgents.Count;

            OverallData.Instance.AddParentGroup(groupSequence.agentCount);
            OverallData.Instance.AddSubgroupSize(groupSequence.agentCount, subgroupList);
            Debug.Log("A group is created!");
        }