Stendhal Quest Coding - Part 2: Difference between revisions

Content deleted Content added
imported>Hendrik Brummermann
imported>Hendrik Brummermann
Line 82:
 
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 "View Transitions" in the right click menu of NPCs. This will generate an image of the current transition graph very similar to the images above.
 
== Conditions And Actions ==
 
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 <code>ChatConditions</code> and <code>ChatActions</code>. A ChatCondition, if specified, must evaluate to <code>true</code> in order for the transition to be taken into account. Let's have a look at an easy example:
 
<source lang="java">
npc.add(ConversationStates.IDLE,
"hi",
new LevelLessThanCondition(6),
ConversationStates.IDLE,
"Oh sorry, your have way too little experience.",
null);
</source>
 
The NPC will refuse to talk to players who are below level 6.
 
A ChatAction is executed after a transition is taken. The following example opens the map of Semos city if the player asks for it.
 
<source lang="java">
npc.add(
ConversationStates.ATTENDING,
"map",
null,
ConversationStates.ATTENDING,
"1 Townhall, Tad lives here, 2 Library, 3 Bank, 4 Bakery, ...",
new ExamineChatAction("map-semos-city.png", "Semos City", "Map of Semos City"));
</source>
 
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.
 
== Teaching the NPC to remember the player ==
 
Okay, lets get back to the topic: Hayunn needs a long term memory in order to only accept one bear per player. After all he is on duty and a totally drunken teacher is no good...
 
<source lang="java">
public void prepareQuestStep() {
 
// get a reference to the Hayunn npc
SpeakerNPC npc = npcs.get("Hayunn Naratha");
 
// ...
 
// check that the player has not completed the quest, yet.
npc.add(ConversationStates.ATTENDING,
ConversationPhrases.QUEST_MESSAGES,
new QuestNotCompletedCondition(QUEST_SLOT),
ConversationStates.QUEST_OFFERED,
"My mouth is dry, but I can't be seen to abandon this teaching room! Could you bring me some #beer from the #tavern?",
null);
 
// send him away if he has completed the quest already.
npc.add(ConversationStates.ATTENDING,
ConversationPhrases.QUEST_MESSAGES,
new QuestCompletedCondition(QUEST_SLOT),
ConversationStates.ATTENDING,
"Thanks all the same, but I don't want to get too heavily into drinking; I'm still on duty, you know! I'll need my wits about me if a student shows up...",
null);
 
// ...
}
 
private void prepareBringingStep() {
SpeakerNPC npc = npcs.get("Hayunn Naratha");
 
npc.add(
ConversationStates.QUEST_ITEM_BROUGHT,
ConversationPhrases.YES_MESSAGES,
new PlayerHasItemWithHimCondition("beer"),
ConversationStates.ATTENDING,
"*glug glug* Ah! That hit the spot. Let me know if you need anything, ok?",
new SetQuestAction(QUEST_SLOT, "done"));
}
</source>
 
Of course we need to add a call to <code>prepareBringingStep();</code> in <code>addToWorld</code>.
 
{{TODO|
* Explain quest slot concept
* ChatAction
* Explain multi value fields in quest slot
* ChatCondition
}}