Stendhal Quest Coding: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>MiguelAngelBlanchLardin
No edit summary
imported>MiguelAngelBlanchLardin
No edit summary
Line 122: Line 122:


This class has a set of predefined triggers that you can use:
This class has a set of predefined triggers that you can use:
* addGreeting(npc, text)<br>Replies to anyone that greet this NPC with the given text.<br>The trigger condition is ''hi'', ''hello'', ''hola''. To start any conversation with a NPC the player '''MUST''' first greet the NPC:
* addGreeting(npc, text)<br>Replies to anyone that greet this NPC with the given text.<br>The trigger condition is ''hi'', ''hello'', ''hola''. To start any conversation with a NPC the player '''MUST''' first greet the NPC.
* addGoodbye(npc, text)<br>It is what NPC writes when listen to '''bye''' or '''adios'''.
* addReply(npc, trigger, text)<br>Reply the attended player with text when NPC listen the keyword trigger or a word that contains the keyword.
* addReply(npc, trigger, text)<br>Reply the attended player with text when NPC listen the keyword trigger or a word that contains the keyword.
* addQuest(npc, text)<br>Show text to player when NPC listen the keyword ''quest'' or ''task''
* addQuest(npc, text)<br>Show text to player when NPC listen the keyword ''quest'' or ''task''
Line 133: Line 134:
We use a very simple method to denote special keywords by placing a '''#''' before it.
We use a very simple method to denote special keywords by placing a '''#''' before it.
The client renders the next word in a bold color.
The client renders the next word in a bold color.



The other way of creating a NPC dialog is by adding states to the FSM.
The other way of creating a NPC dialog is by adding states to the FSM.

The best way of describing a dialog is using a [http://en.wikipedia.org/wiki/Finite_state_machine Finite state machine]. Have a look to the link to make sure you understand the idea.

The first set of rules about states is that:
* State 0 is always the initial state.
* State 1 is the state where only one player can talk to NPC. Any other player that try to talk to NPC will see only the text set with addWaitMessage.
* State -1 is used for jump from any state when the trigger is present. For example very helpful for bye keyword.
* States from 2 to 50 are reserved for Behaviours uses.
* States above 50 are free at your disposal.

If you use twice the same state with the same trigger and the same condition NPC will advise you on server startup.

To add a state to NPC we use the method:

public void add(int state, String trigger, ChatCondition condition, int next_state, String reply, ChatAction action)

This add a new state that is run when listen the trigger text and the condition is run and evaluated to true or it is null. If this happen then NPC moves to a new state set by next_state and says reply and if it is different of null run the action code.

It is a wise thing to make sure that condition code '''DOES NOT''' modify anything at player or NPC.

Let's see how it works.

First we need to create a message to greet the player and attend it.
We add a hi event

add(0, "hi", null, 1, "Welcome player!", null)

State 0 is the initial state, so once NPC is in that state and listen "hi",
it will say "Welcome player!" and pass to state 1.

We can personalize more the message like:
<pre>
add(0, "hi", null, 1, null, new ChatAction()
{
public void fire(Player player, String text, SpeakerNPC engine)
{
engine.say("Welcome "+player.getName()+"!");
}
})
</pre>

If we add these two states the NPC will choose randomly between them because both of them are suitable to start a conversation.

Let's add more states.

Now let's add some options when player is in state 1 like job, offer, buy, sell, etc.

add(1, "job", 1, "I work as a part time example showman",null)
add(1, "offer", 1, "I sell best quality swords",null)

Ok, two new events: job and offer, they go from state 1 to 1, because after listening to them
the NPC can listen something like job.

add(1, "buy", 20, null, new ChatAction()
{
public void fire(Player player, String text, SpeakerNPC engine)
{
int i=text.indexOf(" ");
String item=text.substring(i+1);
if(item.equals("sword"))
{
engine.say(item+" costs 10 GP. Do you want to buy?");
}
else
{
engine.say("Sorry, I don't sell "+item);
engine.setActualState(1);
}
}
});

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, 20, because we are replying to a question, so
only expect two possible replies: yes or no.

add(20, "yes", 1, null, null); // See Behaviours.java for exact code.
add(20, "no", 1, null, null); // See Behaviours.java for exact code.

Whatever the reply is, return to state 1 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(-1, "bye", 0, "Bye!.", null);

We use -1 as a wildcard, so it text is bye the transition.happens.

<pre>
<pre>
/** In case Quest is completed */
/** In case Quest is completed */