コード例 #1
0
ファイル: Agent.cs プロジェクト: Kopachelli/Indigo
        /// <summary>
        /// Calculate the best decision of action to satisfy need
        /// </summary>
        /// <param name="argNeed">need, that must be satisfied</param>
        protected virtual void MakeAction(Need argNeed)
        {
            Exception      worldResponseToAction = new Exception(); //World response if the action is accepted.
            ActionAbstract newAction             = null;            //New action to create

            if (argNeed.SatisfyingActions.Count == 0)
            {
                logger.Error("Number of Action to satisfy need {0} is 0", argNeed);
                return;
            }

            foreach (Type act in argNeed.SatisfyingActions)
            {
                Attribute actionInfo = Attribute.GetCustomAttribute(act, typeof(ActionInfoAttribute));                  // getting attributes for this class
                if (actionInfo == null)
                {
                    logger.Error("Failed to get action info attribute for {0}", act.GetType());
                    return;
                }
                ActionInfoAttribute currentInfo = actionInfo as ActionInfoAttribute;                 //Converting attribute to ActionInfo

                if (currentInfo.RequiresObject)
                {
                    foreach (Agent ag in Inventory.ItemList.Concat(CurrentVision.CurrentViewAgents))
                    {
                        if (!currentInfo.AcceptedObjects.Contains(ag.GetType()))
                        {
                            continue;
                        }
                        if (Distance(this, ag) > Math.Sqrt(2))
                        {
                            actionInfo = Attribute.GetCustomAttribute(typeof(ActionGo), typeof(ActionInfoAttribute));                              // getting attributes for this class
                            if (actionInfo == null)
                            {
                                logger.Error("Failed to get action info attribute for ActionGo");
                                return;
                            }
                            currentInfo           = actionInfo as ActionInfoAttribute;
                            newAction             = ActionsManager.GetActionForCurrentParticipants(typeof(ActionGo), currentInfo, this, null, ag.CurrentLocation.Coords);
                            worldResponseToAction = HomeWorld.AskWorldForAction(newAction);
                            if (worldResponseToAction == null)
                            {
                                break;
                            }
                        }
                        newAction             = ActionsManager.GetActionForCurrentParticipants(act, currentInfo, this, ag);
                        worldResponseToAction = HomeWorld.AskWorldForAction(newAction);
                        if (worldResponseToAction == null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    newAction             = ActionsManager.GetActionForCurrentParticipants(act, currentInfo, this, null);
                    worldResponseToAction = HomeWorld.AskWorldForAction(newAction);
                }

                if (worldResponseToAction == null)
                {
                    logger.Debug("Made action for {0}: {1}", this.Name, newAction.Name);
                    break;
                }
            }
        }