Ejemplo n.º 1
0
        public Exception AskWorldForAction(ActionAbstract action)
        {
            action.World = this;
            if (action.CheckForLegitimacy() != null)                    //Checking action for legitimacy
            {
                return(action.CheckForLegitimacy());
            }
            if (Actions.Any(act =>                     //Checking action for conflicts
            {
                if (action.GetType() != act.GetType()) //Comparing actions types
                {
                    return(false);
                }
                if (action.CompareTo(act) == 0)                         //Comparing actions for conflicts
                {
                    return(true);
                }
                return(false);
            }))
            {
                return(new ConflictException());
            }
            Actions.Add(action);

            return(null);
        }
Ejemplo n.º 2
0
        }                                                                                            //Dictionary with memories about other agents: <reference to the agent(id), info about agent>.

        #endregion

        public void StoreAction(Agent argAgentSender, ActionAbstract argAction)
        {
            StoredInformation st = new StoredInformation();   //New information to store

            st.StoredInfo = argAction.CharacteristicsOfSubject();

            if (StoredAgents.ContainsKey(argAgentSender))
            {
                if (!(StoredAgents[argAgentSender].Exists(obj =>
                {
                    return(obj.StoredInfo == argAction.CharacteristicsOfSubject());
                })))
                {
                    StoredAgents[argAgentSender].Add(st);
                }
            }
            else
            {
                List <StoredInformation> lst = new List <StoredInformation>();  //New list to store new agent
                lst.Add(st);
                StoredAgents.Add(argAgentSender, lst);
            }
        }
Ejemplo n.º 3
0
        /// <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;
                }
            }
        }