Class SpeakerNPC

All Implemented Interfaces:
Killer, Cloneable, Iterable<String>, Serializable
Direct Known Subclasses:
AssassinRepairerAdder.AssassinRepairer, CroupierNPC, GhostNPCBase, MarketManagerNPC, RatKidsNPCBase, ScriptingNPC, SheepBuyerNPC.SheepBuyerSpeakerNPC, TrainingArea.TrainerNPC

public class SpeakerNPC extends PassiveNPC
This is a finite state machine that implements a chat system. See: https://en.wikipedia.org/wiki/Finite_state_machine In fact, it is a transducer. States are denoted by the enum ConversationStates. Input is the text that the player says to the SpeakerNPC. Output is the text that the SpeakerNPC answers. See examples to understand how it works. RULES: State IDLE is both the start state and the state that will end the conversation between the player and the SpeakerNPC. State ATTENDING is the state where only one player can talk to NPC and where the prior talk doesn't matter. State ANY is a wildcard and is used to jump from any state whenever the trigger is active. There are states that are reserved for special behaviours and quests. Example how it works: First we need to create a message to greet the player and attend it. We add a hi event: add(ConversationStates.IDLE, ConversationPhrases.GREETING_MESSAGES, ConversationStates.ATTENDING, "Welcome, player!", null) Once the NPC is in the IDLE state and hears the word "hi", it will say "Welcome player!" and move to ATTENDING. Now let's add some options when player is in ATTENDING_STATE, like job, offer, buy, sell, etc. add(ConversationStates.ATTENDING, ConversationPhrases.JOB_MESSAGES, ConversationStates.ATTENDING, "I work as a part time example showman", null) add(ConversationStates.ATTENDING_STATE, "offer", ConversationStates.ATTENDING_STATE, "I sell best quality swords", null) Ok, two new events: job and offer, they go from ATTENDING state to ATTENDING state, because after reacting to "job" or "offer", the NPC can directly react to one of these again.
 add(ConversationStates.ATTENDING, "buy", ConversationStates.BUY_PRICE_OFFERED, null, new ChatAction() {
    public void fire(Player player, String text, SpeakerNPC npc) {
        int i = text.indexOf(" ");
        String item = text.substring(i + 1);
        if (item.equals("sword")) {
            npc.say(item + "costs 10 coins. Do you want to buy?");
        } else {
            npc.say("Sorry, I don't sell " + item + ".");
            npc.setActualState(ConversationStates.ATTENDING);
        }
    }
 });
 
Now the hard part. We listen to "buy", so we need to process the text, and for that we use the ChatAction class, we create a new class that will handle the event. Also see that we move to a new state, BUY_PRICE_OFFERED. The player is then replying to a question, so we only expect two possible replies: yes or no. add(ConversationStates.BUY_PRICE_OFFERED, ConversationPhrases.YES_MESSAGES, ConversationStates.ATTENDING, "Sorry, I changed my mind. I won't sell anything.", null); // See SellerBehaviour.java for a working example. Whatever the reply is, return to ATTENDING state so we can listen to new things. Finally we want to finish the conversation, so whatever state we are, we want to finish a conversation with "Bye!". add(ConversationStates.ANY, ConversationPhrases.GOODBYE_MESSAGES, ConversationStates.IDLE, "Bye!", null); We use the state ANY as a wildcard, so if the input text is "bye" the transition happens, no matter in which state the FSM really is, with the exception of the IDLE state.
  • Constructor Details

    • SpeakerNPC

      public SpeakerNPC(String name)
      Creates a new SpeakerNPC.
      Parameters:
      name - The NPC's name. Please note that names should be unique.
  • Method Details

    • setAllowToActAlone

      public void setAllowToActAlone(boolean allow)
      allow or disallow for npc to act without players in his zone.
      Parameters:
      allow - - flag for allowing/disallowing npc's acting
    • isAllowedToActAlone

      public boolean isAllowedToActAlone()
    • createDialog

      protected void createDialog()
    • onGoodbye

      protected void onGoodbye(RPEntity attending2)
      Is called when the NPC stops chatting with a player. Override it if needed.
      Parameters:
      attending2 - who has been talked to.
    • getAttending

      public RPEntity getAttending()
      The entity who is currently talking to the NPC, or null if the NPC is currently not taking part in a conversation.
      Returns:
      RPEntity
    • setAttending

      public void setAttending(RPEntity rpentity)
      Sets the rpentity to whom the NPC is currently listening. Note: You don't need to use this for most NPCs.
      Parameters:
      rpentity - the entity with whom the NPC should be talking.
    • setIdleDirection

      public void setIdleDirection(Direction dir)
      Sets the direction the entity should face while idle (not moving & not attending).
      Parameters:
      dir - Direction to face.
    • onDead

      public void onDead(Killer killer, boolean remove)
      Description copied from class: RPEntity
      This method is called when this entity has been killed (hp == 0).
      Overrides:
      onDead in class RPEntity
      Parameters:
      killer - The entity who caused the death, i.e. who did the last hit.
      remove - true iff this entity should be removed from the world. For almost everything remove is true, but not for the players, who are instead moved to afterlife ("reborn").
    • dropItemsOn

      protected void dropItemsOn(Corpse corpse)
      Overrides:
      dropItemsOn in class NPC
    • setPlayerChatTimeout

      public void setPlayerChatTimeout(long playerChatTimeout)
      Sets the time a conversation can be paused before it will be terminated by the NPC.
      Parameters:
      playerChatTimeout - the time, in seconds
    • setPerceptionRange

      public void setPerceptionRange(int perceptionRange)
      Description copied from class: NPC
      Set the perception range.
      Overrides:
      setPerceptionRange in class NPC
    • logic

      public void logic()
      Description copied from class: RPEntity
      Perform cycle logic.
      Overrides:
      logic in class NPC
    • preLogic

      public void preLogic()
    • endConversation

      public void endConversation()
    • inConversationRange

      public boolean inConversationRange()
    • isTalking

      public boolean isTalking()
    • say

      public void say(String text)
      Overrides:
      say in class NPC
    • say

      protected void say(String text, boolean turnToPlayer)
    • addWaitMessage

      public void addWaitMessage(String text, ChatAction action)
      Message when NPC is attending another player.
      Parameters:
      text - to say to bothering player
      action - to perform
    • addInitChatMessage

      public void addInitChatMessage(ChatCondition condition, ChatAction action)
    • add

      public void add(ConversationStates state, String trigger, ChatCondition condition, ConversationStates nextState, String reply, ChatAction action)
      Adds a new transition to the FSM.
      Parameters:
      state - the starting state of the FSM
      trigger - input for this transition
      condition - null or condition that has to return true for this transition to be considered
      nextState - the new state of the FSM
      reply - a simple text reply (may be null for no reply)
      action - a special action to be taken (may be null)
    • add

      public void add(ConversationStates state, String trigger, ChatCondition condition, ConversationStates nextState, String reply, ChatAction action, String label)
      Adds a new transition to the FSM.
      Parameters:
      state - the starting state of the FSM
      trigger - input for this transition
      condition - null or condition that has to return true for this transition to be considered
      nextState - the new state of the FSM
      reply - a simple text reply (may be null for no reply)
      action - a special action to be taken (may be null)
      label - a label string to handle transitions
    • addMatching

      public void addMatching(ConversationStates state, String trigger, ExpressionMatcher matcher, ChatCondition condition, ConversationStates nextState, String reply, ChatAction action)
      Adds a new transition with explicit ExpressionMatcher to the FSM.
      Parameters:
      state -
      trigger -
      matcher -
      condition -
      nextState -
      reply -
      action -
    • add

      public void add(ConversationStates state, Collection<String> triggerStrings, ChatCondition condition, ConversationStates nextState, String reply, ChatAction action, String label)
      Adds a new set of transitions to the FSM.
      Parameters:
      state - the starting state of the FSM
      triggerStrings - a list of inputs for this transition
      condition - null or condition that has to return true for this transition to be considered
      nextState - the new state of the FSM
      reply - a simple text reply (may be null for no reply)
      action - a special action to be taken (may be null)
      label -
    • add

      public void add(ConversationStates state, Collection<String> triggerStrings, ChatCondition condition, ConversationStates nextState, String reply, ChatAction action)
      Adds a new set of transitions to the FSM.
      Parameters:
      state - the starting state of the FSM
      triggerStrings - a list of inputs for this transition
      condition - null or condition that has to return true for this transition to be considered
      nextState - the new state of the FSM
      reply - a simple text reply (may be null for no reply)
      action - a special action to be taken (may be null)
    • add

      public void add(ConversationStates state, Collection<String> triggerStrings, ChatCondition condition, boolean secondary, ConversationStates nextState, String reply, ChatAction action)
      Adds a new set of transitions to the FSM.
      Parameters:
      state - the starting state of the FSM
      triggerStrings - a list of inputs for this transition
      condition - null or condition that has to return true for this transition to be considered
      secondary - flag to mark secondary transitions to be taken into account after preferred transitions
      nextState - the new state of the FSM
      reply - a simple text reply (may be null for no reply)
      action - a special action to be taken (may be null)
    • add

      public void add(ConversationStates[] states, String trigger, ChatCondition condition, ConversationStates nextState, String reply, ChatAction action)
      Adds a new set of transitions to the FSM.
      Parameters:
      states - the starting states of the FSM
      trigger - input for this transition
      condition - null or condition that has to return true for this transition to be considered
      nextState - the new state of the FSM
      reply - a simple text reply (may be null for no reply)
      action - a special action to be taken (may be null)
    • add

      public void add(ConversationStates[] states, Collection<String> triggerStrings, ChatCondition condition, ConversationStates nextState, String reply, ChatAction action)
      Adds a new set of transitions to the FSM.
      Parameters:
      states - the starting states of the FSM
      triggerStrings - a list of inputs for this transition
      condition - null or condition that has to return true for this transition to be considered
      nextState - the new state of the FSM
      reply - a simple text reply (may be null for no reply)
      action - a special action to be taken (may be null)
    • add

      public void add(ConversationStates state, Collection<String> triggerStrings, ConversationStates nextState, String reply, ChatAction action)
    • add

      public void add(ConversationStates state, Collection<String> triggerStrings, ConversationStates nextState, String reply, ChatAction action, String label)
    • del

      public boolean del(String label)
      delete transition that match label
      Parameters:
      label -
      Returns:
      - deleting state
    • listenTo

      public void listenTo(Player player, String text)
    • setCurrentState

      public void setCurrentState(ConversationStates state)
    • addGreeting

      public void addGreeting()
      Add default greeting transition with optional recognition of the NPC name.
    • addGreeting

      public void addGreeting(String text)
      Add greeting transition with name recognition.
      Parameters:
      text -
    • addGreeting

      public void addGreeting(String text, ChatAction action)
      Add greeting transition with name recognition.
      Parameters:
      text -
      action -
    • addReply

      public void addReply(String trigger, String text)
      Makes this NPC say a text when it hears a certain trigger during a conversation.
      Parameters:
      trigger - The text that causes the NPC to answer
      text - The answer
    • addReply

      public void addReply(Collection<String> triggerStrings, String text)
      Parameters:
      triggerStrings -
      text -
    • addReply

      public void addReply(String trigger, String text, ChatAction action)
      Makes NPC say a text and/or do an action when a trigger is said.
      Parameters:
      trigger -
      text -
      action -
    • addReply

      public void addReply(Collection<String> triggerStrings, String text, ChatAction action)
      Makes NPC say a text and/or do an action when a trigger is said.
      Parameters:
      triggerStrings -
      text -
      action -
    • addQuest

      public void addQuest(String text)
    • addJob

      public void addJob(String jobDescription)
    • addHelp

      public void addHelp(String helpDescription)
    • addOffer

      public void addOffer(String offerDescription)
    • addEmotionReply

      public void addEmotionReply(String playerAction, String npcAction)
      make npc's emotion reply on player's emotion
      Parameters:
      playerAction - - what player doing with npc
      npcAction - - npc's emotion reply on player's emotion
    • addEmotion

      public void addEmotion(Collection<String> triggerStrings, String npcAction)
      make npc's emotion
      Parameters:
      triggerStrings - - player's keywords for npc emotion
      npcAction - - npc's emotion
    • addEmotion

      public void addEmotion(String trigger, String npcAction)
      make npc's emotion
      Parameters:
      trigger - - player's keywords for npc emotion
      npcAction - - npc's emotion
    • addReplyOnEmotion

      public void addReplyOnEmotion(String playerAction, String reply)
      make npc's reply on player's emotion
      Parameters:
      playerAction - - what player doing with npc
      reply - - npc's reply on player's emotion
    • addGoodbye

      public void addGoodbye()
    • addGoodbye

      public void addGoodbye(String text)
    • getTransitions

      public List<Transition> getTransitions()
      Returns a copy of the transition table.
      Returns:
      list of transitions
    • getEngine

      public Engine getEngine()
    • getAlternativeImage

      public String getAlternativeImage()
      gets an alternative image for example for the website
      Returns:
      name of alternative image or null in case the normal image should be used.
    • setAlternativeImage

      public void setAlternativeImage(String alternativeImage)
      sets an alternative image for example for the website
      Parameters:
      alternativeImage - name of alternative image or null in case the normal image should be used.
    • getJob

      public String getJob()
      gets the answer to the "job" question in ATTENDING state.
      Returns:
      the answer to the job question or null in case there is no job specified
    • getReply

      public String getReply(String trigger, ConversationStates state, String expressionType)
      Retrieves string reply to trigger word.
      Parameters:
      trigger - Word or phrase that triggers reply.
      state - The conversation state the NPC is in when trigger is activated.
      expressionType -
      Returns:
      String reply or null.
    • getReply

      public String getReply(String trigger)
      Retrieves string reply to trigger word when NPC is in attending state.
      Parameters:
      trigger - Word or phrase that triggers reply.
      Returns:
      String reply or null.
    • onRejectedAttackStart

      public void onRejectedAttackStart(RPEntity attacker)
      someone tried to attack us
      Parameters:
      attacker -