Exemplo n.º 1
0
 public Stimulus(StimulusSource source, int sourceAllegiance, StimulusType type, float radius, Vector2 pos, Object handle)
 {
     source_ = source;
     sourceAllegiance_ = sourceAllegiance;
     type_ = type;
     radius_ = radius;
     position_ = pos;
     handle_ = handle;
     message_ = null;
 }
        /// <summary>
        /// Broadcasts the belief to all agents within the broadcast radius
        /// </summary>
        /// <param name="message">The message to broadcast</param>
        protected void broadcastMessage(Message message)
        {
            isBroadcasting_ = true;
            //communicationRequested_ = false;
            framesSinceLastCommunication_ = 0;

            framesLeftToBroadcast_ = message.TimeToBroadcast();
            broadcastMessage_ = message.ToString();

            WorldState.Audial_.Remove(key_);
            WorldState.Audial_.Add(key_,
                new Stimulus(StimulusSource.CharacterAbstract, AI_.Character_.Allegiance_, StimulusType.Message, broadcastRadius_, AI_.Character_.getPosition(), AI_.Character_, message));
            if (communicationLevel_ == CommunicationLevel.High && message.MessageType_ == Message.MessageType.Data)
            {
                CommLogger.sentMsg();
            }
        }
        /// <summary>
        /// Try to broadcast something based on a preference list of BeliefTypes
        /// </summary>
        /// <param name="bTList">The preference list of BeliefTypes</param>
        /// <param name="hiTime">How long to wait before issuing a Hi message</param>
        protected void broadcastHelper(List<BeliefType> bTList, int hiTime)
        {
            for (int i = 0; i < bTList.Count; i++)
            {
                framesSinceCommunicatingThisType_[bTList[i]]++;
            }

            bool communicatedSomething = false;

            if (communicationRequested_)
            {
                // Attempt to broadcast new info
                for (int i = 0; i < bTList.Count; i++)
                {
                    if (AI_.Memory_.getBeliefs(bTList[i]).Count > 0 && framesSinceCommunicatingThisType_[bTList[i]] > SECONDS_BEFORE_REPEATING_SELF * FRAMERATE)
                    {
                        Message msg = new Message(Message.MessageType.Data);
                        msg.Belief_ = AI_.Memory_.getFirstBelief(bTList[i]);
                        broadcastMessage(msg);
                        framesSinceCommunicatingThisType_[bTList[i]] = 0;
                        communicatedSomething = true;
                        break;
                    }
                }

                // If we can't broadcast new info, then re-broadcast old info.
                if (!communicatedSomething)
                {
                    for (int i = 0; i < bTList.Count; i++)
                    {
                        if (AI_.Memory_.getBeliefs(bTList[i]).Count > 0)
                        {
                            Message msg = new Message(Message.MessageType.Data);
                            msg.Belief_ = AI_.Memory_.getFirstBelief(bTList[i]);
                            broadcastMessage(msg);
                            framesSinceCommunicatingThisType_[bTList[i]] = 0;
                            communicatedSomething = true;
                            break;
                        }
                    }
                }
            }

            // If there is still nothing to be communicated, then we announce
            // that we are ready to communicate.
            if (!communicatedSomething)
            {
                if (framesSinceLastCommunication_ > hiTime * FRAMERATE)
                {
                    Message msg = new Message(Message.MessageType.Hi);
                    broadcastMessage(msg);
                }
            }
        }