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 183: Line 183:
Now let's add some options when player is in state 1 like job, offer, buy, sell, etc.
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, "job", null, 1, "I work as a part time example showman",null)
add(1, "offer", 1, "I sell best quality swords",null)
add(1, "offer", null, 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
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.
the NPC can listen something like job.


Easy, isn't it?
add(1, "buy", 20, null, new ChatAction()

{
Now let's add something harder.
public void fire(Player player, String text, SpeakerNPC engine)
Let's make the NPC ask a question and let's expect a reply to the question.

I hope you see that this means adding a new state different of 1, because while we are waiting for the reply we are not interested in anything else.

add(1, "fun", null, 50, "Do you want me to convert you in a heady bare elf?", null)

If player says ''fun'' then NPC will ask the question. Note that new state is 50 instead of 1.
Now let's add two new states for the reply: yes and no.
<pre>
add(50, "yes", new ChatCondition()
{
{
public boolean fire(Player player, String text, SpeakerNPC engine)
int i=text.indexOf(" ");
String item=text.substring(i+1);
if(item.equals("sword"))
{
{
return player.equals(HEADY_BARE_ELF);
engine.say(item+" costs 10 GP. Do you want to buy?");
}
}
else
},
1, "Sorry!, you are already a heady bare elf!", null);
</pre>

Heh! We add a condition on the yes to handle the case of the player being already a heady bare elf.
This condition is perhaps handle better on the Fire part as we really want to execute something.
<pre>
add(50, "yes", null,
1, null, new ChatAction()
{
public void fire(Player player, String text, SpeakerNPC engine)
{
{
if(player.equals(HEADY_BARE_ELF))
engine.say("Sorry, I don't sell "+item);
{
engine.setActualState(1);
engine.say("Sorry!, you are already a heady bare elf!");
}
else
{
engine.say("Ok! But there is no return way! KAAABOOOM!");
player.setOutfit("0");
world.modify(player);
}
}
}
}
});
</pre>
});

See? This way is simpler and you save having to adding two states.

Let add the no state to complete it.
<pre>
add(50, "no", null, 1, "Oh! :-(", null);
</pre>

Now NPC will only accept on state 50 either yes or no.

We could add a help message in case player get blocked.
<pre>
add(50, "help", null, 50, "Do you want me to convert you in a bare elf?", null);
</pre>


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.


Finally we want to finish the conversation, so whatever state we are we want to finish a conversation with Bye.
add(20, "yes", 1, null, null); // See Behaviours.java for exact code.
add(20, "no", 1, null, null); // See Behaviours.java for exact code.


add(-1, "bye", 0, "Bye!.", null);
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.


We use -1 as a wildcard, so it text is bye the transition happens.
add(-1, "bye", 0, "Bye!.", null);


Let's continue with our example and I comment anything that is really important beyond this point.
We use -1 as a wildcard, so it text is bye the transition.happens.


<pre>
<pre>