Stendhal Quest Coding - Part 2: Difference between revisions
Content deleted Content added
imported>Ufizavipupu No edit summary |
imported>Kribbel m replace old link |
||
| (17 intermediate revisions by 4 users not shown) | |||
Line 1:
{{Navigation for Stendhal Top|Contributing}}
{{Navigation for Stendhal Contributors}}
Line 30 ⟶ 22:
As a little exercise you can code this part to check whether you understood the [[Stendhal Quest Coding|first part of this tutorial]].
There is, however, a small problem with the current solution: There can only be one reply for
Fortunately there is a solution: The NPCs needs to remember the state of the conversation:
Line 36 ⟶ 28:
[[Image:npc simple.png]]
Currently our NPC knows two states: IDLE for walking around and ATTENDING for talking to a player. You can change between states by talking to the NPC. So if the NPC is IDLE, it will accept
So, lets return to our example. We want the NPC to reply to
[[Image:npc with quest question.png]]
You may have noticed in the above diagram that there is something called
Okay, enough theory for now, lets write some code:
public void prepareQuestStep() {
// get a reference to the Hayunn npc
SpeakerNPC npc = npcs.get(
// ...
Line 59 ⟶ 51:
null,
ConversationStates.QUEST_OFFERED,
null);
// in state QUEST_OFFERED, accept
npc.add(
ConversationStates.QUEST_OFFERED,
Line 68 ⟶ 60:
null,
ConversationStates.ATTENDING,
null);
// in state QUEST_OFFERED, accept
npc.add(
ConversationStates.QUEST_OFFERED,
Line 77 ⟶ 69:
null,
ConversationStates.ATTENDING,
null);
}
As you can see, we now have to use
We have predefined a number of states in the class [
===Graphical representation===
There is one last thing that makes your life easier: If you are using Linux, have the graphviz package installed and you are an admin in game, you can select
== Conditions And Actions ==
Line 92 ⟶ 85:
Hayunn now has a short term memory and that is cool for asking questions.
Before we have a look at long term memory, we make a little excursion to the topic of
npc.add(ConversationStates.IDLE,
new LevelLessThanCondition(6),
ConversationStates.IDLE,
null);
The NPC will refuse to talk to players who are below level 6.
Line 107 ⟶ 100:
A ChatAction is executed after a transition is taken. The following example opens the map of Semos city if the player asks for it.
npc.add(
ConversationStates.ATTENDING,
null,
ConversationStates.ATTENDING,
new ExamineChatAction(
There is a number of premade [http://stendhal.game-host.org/hudson/job/stendhal_HEAD/javadoc/games/stendhal/server/entity/npc/condition/package-summary.html ChatConditions] and [http://stendhal.game-host.org/hudson/job/stendhal_HEAD/javadoc/games/stendhal/server/entity/npc/action/package-summary.html ChatActions]. You can use them easily out of the box. Many of them require some parameters which are documented at the linked places. Of course you can write your own conditions and actions for special cases.
Line 123 ⟶ 116:
Okay, lets get back to the topic: Hayunn needs a long term memory in order to only accept one beer per player. After all he is on duty and a totally drunken teacher is no good...
The long term memory is called
public static final String QUEST_SLOT =
That's the name of the slot we are using. It has to be unique, but other than that, we can write anything we want in there. There are, however, two conventions that make things a lot easier: Multiple values are separated by an
Having said that, let us make Hayunn check the quest state and reply accordingly:
public void prepareQuestStep() {
// get a reference to the Hayunn npc
SpeakerNPC npc = npcs.get(
// ...
Line 145 ⟶ 138:
new QuestNotCompletedCondition(QUEST_SLOT),
ConversationStates.QUEST_OFFERED,
null);
Line 153 ⟶ 146:
new QuestCompletedCondition(QUEST_SLOT),
ConversationStates.ATTENDING,
null);
// ...
}
Note that if the quest is already completed, Hayunn stays in ATTENDING state.
Line 164 ⟶ 157:
The next step is to save that the quest was completed. We take a simple approach for the moment and only check that the player owns a beer:
private void prepareBringingStep() {
SpeakerNPC npc = npcs.get(
// if the players says
npc.add(
ConversationStates.ATTENDING,
new PlayerHasItemWithHimCondition(
ConversationStates.ATTENDING,
new SetQuestAction(QUEST_SLOT,
}
Of course we need to add a call to
== Third Part of this Tutorial ==
| |||