//this method is utilized by DispatchMessage or DispatchDelayedMessages. //This method calls the message handling member function of the receiving //entity, pReceiver, with the newly created telegram public void Discharge(BaseGameEntity pReceiver, Telegram telegram) { if (!pReceiver.HandleMessage(telegram)) { Debug.WriteLine("Telegram could not be handled"); } }
public bool HandleMessage(Telegram msg) { //first see if the current state is valid and that it can handle //the message if ((m_pCurrentState != null) && m_pCurrentState.OnMessage(m_pOwner, msg)) { return true; } //if not, and if a global state has been implemented, send //the message to the global state if ((m_pGlobalState != null) && m_pGlobalState.OnMessage(m_pOwner, msg)) { return true; } return false; }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { Miner pMiner = (Miner)pEntity; switch (pTelegram.Msg) { case (int)message_type.Msg_Antagonize: { double CurrentTime = MessageDispatcher.Instance.GetRunningTime(); DebugMessages.Instance.WriteLine(String.Format("Message handled by {0} at time: {1}", MainSM.GetEntityName(pEntity.ID()), CurrentTime)); if (pMiner.Location == location_type.saloon) { if (Utils.RandFloat() <= 0.5) { MessageDispatcher.Instance.DispatchMessage((int)MessageDispatcher.SEND_MSG_IMMEDIATELY, pEntity.ID(), (int)EntityName.ent_BarFly, (int)message_type.Msg_AcceptFight, (int)MessageDispatcher.NO_ADDITIONAL_INFO); pMiner.GetFSM().ChangeState(new BobFight()); } else { DebugMessages.Instance.WriteLine(String.Format("{0}: No, I can't be bothered to fight you drunken fool!", MainSM.GetEntityName(pEntity.ID()))); MessageDispatcher.Instance.DispatchMessage((int)MessageDispatcher.SEND_MSG_IMMEDIATELY, pEntity.ID(), (int)EntityName.ent_BarFly, (int)message_type.Msg_DeclineFight, (int)MessageDispatcher.NO_ADDITIONAL_INFO); } } return true; } } return false; }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { MinersWife wife = (MinersWife)pEntity; switch (pTelegram.Msg) { case (int)message_type.Msg_HiHoneyImHome: { double CurrentTime = MessageDispatcher.Instance.GetRunningTime(); DebugMessages.Instance.WriteLine(String.Format("Message handled by {0} at time: {1}", MainSM.GetEntityName(wife.ID()), CurrentTime)); DebugMessages.Instance.WriteLine(String.Format("{0}: Hi honey. Let me make you some of mah fine country stew", MainSM.GetEntityName(wife.ID()))); wife.GetFSM().ChangeState(new CookStew()); return true; } } return false; }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { return false; }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { BarFlyJoe joe = (BarFlyJoe)pEntity; switch (pTelegram.Msg) { case (int)message_type.Msg_IncomingPunch: { if (Utils.RandFloat() <= 0.5) { joe.HP = joe.HP - 1; DebugMessages.Instance.WriteLine(String.Format("{0}: Gaah Im hit!", MainSM.GetEntityName(pEntity.ID()))); } else { DebugMessages.Instance.WriteLine(String.Format("{0}: Missed me, sucker!", MainSM.GetEntityName(pEntity.ID()))); } return true; } case (int)message_type.Msg_DeclineFight: { DebugMessages.Instance.WriteLine(String.Format("{0}: That'll teach ya to mess with an alchoholic!", MainSM.GetEntityName(pEntity.ID()))); joe.GetFSM().ChangeState(new JoeDoChillin()); return true; } } return false; }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { BarFlyJoe joe = (BarFlyJoe)pEntity; switch (pTelegram.Msg) { case (int)message_type.Msg_DeclineFight: { DebugMessages.Instance.WriteLine(String.Format("{0}: Har har, ya'll a bunch o' yellow bellied chickens!", MainSM.GetEntityName(pEntity.ID()))); joe.GetFSM().ChangeState(new JoeDoChillin()); return true; } case (int)message_type.Msg_AcceptFight: { joe.GetFSM().ChangeState(new JoeFight()); return true; } } return false; }
//---------------------------- DispatchMessage --------------------------- // // given a message, a receiver, a sender and any time delay , this function // routes the message to the correct agent (if no delay) or stores // in the message queue to be dispatched at the correct time //------------------------------------------------------------------------ public void DispatchMessage(double delay, int sender, int receiver, int msg, object ExtraInfo) { //get pointers to the sender and receiver BaseGameEntity pSender = GetRegisteredEntityFromID(sender); BaseGameEntity pReceiver = GetRegisteredEntityFromID(receiver); //make sure the receiver is valid if (pReceiver == null) { Debug.WriteLine(String.Format("Warning! No Receiver with ID of {0} found", receiver.ToString())); return; } //create the telegram Telegram telegram = new Telegram(0, sender, receiver, msg, ExtraInfo); //if there is no delay, route telegram immediately if (delay <= 0.0f) { Debug.WriteLine(String.Format("Instant telegram dispatched at time: {0} by {1} for {2}. Msg is {3}", m_CurrentRunningTime, pSender.ID().ToString(), pReceiver.ID().ToString(), msg)); //send the telegram to the recipient Discharge(pReceiver, telegram); } //else calculate the time when the telegram should be dispatched else { telegram.DispatchTime = m_CurrentRunningTime + delay; if (!m_PriorityQ.Contains(telegram)) { // put it in the queue m_PriorityQ.Add(telegram); // and sort the queue m_PriorityQ.Sort(CompareByDispatchTime); Debug.WriteLine(String.Format("Delayed telegram from {0} stored at time {1} for {2}. Msg is {3}", pSender.ID(), m_CurrentRunningTime.ToString(), pReceiver.ID(), msg)); } } }
private static int CompareByDispatchTime(Telegram x, Telegram y) { if (x == y) { return 0; } else if (x.DispatchTime > y.DispatchTime) { return 1; } else { return -1; } }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { Miner pMiner = (Miner)pEntity; switch (pTelegram.Msg) { case (int)message_type.Msg_StewReady: { DebugMessages.Instance.WriteLine(String.Format("Message handled by {0} at time {1}", MainSM.GetEntityName(pEntity.ID()), MessageDispatcher.Instance.GetRunningTime())); DebugMessages.Instance.WriteLine(String.Format("{0}: Okay Hun, ahm a comin'", MainSM.GetEntityName(pEntity.ID()))); pMiner.GetFSM().ChangeState(new EatStew()); return true; } }//end switch return false; //send message to global message handler }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { Miner pMiner = (Miner)pEntity; switch (pTelegram.Msg) { case (int)message_type.Msg_IncomingPunch: { if (Utils.RandFloat() <= 0.5) { pMiner.HP = pMiner.HP - 1; DebugMessages.Instance.WriteLine(String.Format("{0}: Ouch, my nose.", MainSM.GetEntityName(pEntity.ID()))); } else { DebugMessages.Instance.WriteLine(String.Format("{0}: Dodged!", MainSM.GetEntityName(pEntity.ID()))); } return true; } case (int)message_type.Msg_DeclineFight: { DebugMessages.Instance.WriteLine(String.Format("{0}: Yes! Victorious!", MainSM.GetEntityName(pEntity.ID()))); pMiner.GetFSM().RevertToPreviousState(); return true; } } return false; }
public override bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram) { MinersWife wife = (MinersWife)pEntity; switch (pTelegram.Msg) { case (int)message_type.Msg_StewReady: { DebugMessages.Instance.WriteLine(String.Format("Message received by {0} at time {1}", MainSM.GetEntityName(pEntity.ID()), MessageDispatcher.Instance.GetRunningTime())); DebugMessages.Instance.WriteLine(String.Format("{0}: StewReady! Lets eat", MainSM.GetEntityName(pEntity.ID()))); //let hubby know the stew is ready MessageDispatcher.Instance.DispatchMessage((int)MessageDispatcher.SEND_MSG_IMMEDIATELY, wife.ID(), (int)EntityName.ent_Miner_Bob, (int)message_type.Msg_StewReady, (int)MessageDispatcher.NO_ADDITIONAL_INFO); wife.Cooking = false; wife.GetFSM().ChangeState(new DoHouseWork()); return true; } }//end switch return false; }
//this executes if the agent receives a message from the //message dispatcher public abstract bool OnMessage(BaseGameEntity pEntity, Telegram pTelegram);