Stendhal NPC Coding: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>Kymara
No edit summary
imported>Madmetzger
 
(55 intermediate revisions by 2 users not shown)
Line 3: Line 3:
{{Stendhal NPCs}}
{{Stendhal NPCs}}


Usually we add NPCs (non-player characters) to make world more alive and to use them in Quests. It is because of that reason, that is so important that you add NPC on the zone they are.
Usually we add NPCs (non-player characters) to make world more alive and to use them in Quests.


This is how NPCs are added to the world.
This is how NPCs are added to the world.
Line 10: Line 10:


More complicated dialog for NPCs, such as you'd find in a quest, is covered in [[Stendhal Quest Coding]]. But the basics for any NPC used in a quest should still be written as below.
More complicated dialog for NPCs, such as you'd find in a quest, is covered in [[Stendhal Quest Coding]]. But the basics for any NPC used in a quest should still be written as below.

== Before you start ==

This page describes how to code an NPC. You don't need to know a lot about Java. You should, however, already have [[Configure a development environment (IDE)|setup an IDE]] and be able to compile and start a local Stendhal server.


== Define NPC with Java ==
== Define NPC with Java ==
Line 56: Line 60:
*/
*/
public void configureZone(final StendhalRPZone zone, final Map<String, String> attributes) {
public void configureZone(final StendhalRPZone zone, final Map<String, String> attributes) {
buildNPC(zone, attributes);
buildNPC(zone);
}
}


Line 64: Line 68:


<source lang="java">
<source lang="java">
private void buildNPC(final StendhalRPZone zone, final Map<String, String> attributes) {
private void buildNPC(final StendhalRPZone zone) {
final SpeakerNPC npc = new SpeakerNPC("Mr Healer") {
final SpeakerNPC npc = new SpeakerNPC("Mr Healer") {
protected void createPath() {
protected void createPath() {
List<Node> nodes=new LinkedList<Node>();
List<Node> nodes=new LinkedList<Node>();
nodes.add(new Path.Node(9,5));
nodes.add(new Path.Node(9,5));
nodes.add(new Path.Node(14,5));
nodes.add(new Path.Node(14,5));
setPath(nodes,true);
setPath(nodes,true);
}
}


protected void createDialog() {
protected void createDialog() {
// Lets the NPC reply with "Hallo" when a player greets him. But we could have set one inside the ()
// Lets the NPC reply with "Hallo" when a player greets him. But we could have set a custom greeting inside the ()
addGreeting();
addGreeting();
// Lets the NPC reply when a player says "job"
// Lets the NPC reply when a player says "job"
addJob("I have healing abilities and I heal wounded players. I also sell #potions and antidotes.");
addJob("I have healing abilities and I heal wounded players. I also sell #potions and antidotes.");
// Lets the NPC reply when a player asks for help
// Lets the NPC reply when a player asks for help
addHelp("Ask me to #heal you and I will help you or ask me #offer and I will show my shop's stuff.");
addHelp("Ask me to #heal you and I will help you or ask me #offer and I will show my shop's stuff.");
// Makes the NPC sell potions and antidote
// Makes the NPC sell potions and antidote
addSeller(new SellerBehaviour(shops.get("healing")));
addSeller(new SellerBehaviour(shops.get("healing")));
// Lets the NPC heal players for free
// Lets the NPC heal players for free
addHealer(0);
addHealer(0);
// respond about a special trigger word
// respond about a special trigger word
addReply("potions","Please ask for my #offer."):
addReply("potions","Please ask for my #offer.");
// use standard goodbye, but you can also set one inside the ()
// use standard goodbye, but you can also set one inside the ()
addGoodbye();
addGoodbye();
}
}
});
});


// This determines how the NPC will look like. welcomernpc.png is a picture in data/sprites/npc/
zone.assignRPObjectID(npc);
npc.setEntityClass("welcomernpc");
// This determines how the NPC will look like. welcomernpc.png is a picture in data/sprites/npc/
// set a description for when a player does 'Look'
npc.setEntityClass("welcomernpc");
npc.setDescription("You see Mr Healer, he looks a a bit busy at the moment but perhaps he can help you anyway.");
// Set the initial position to be the first node on the Path you defined above.
// Set the initial position to be the first node on the Path you defined above.
npc.setPosition(9, 5);
npc.initHP(100);
npc.setPosition(9, 5);
npc.initHP(100);


zone.add(npc);
zone.add(npc);
}
}
</source>
</source>


Line 127: Line 134:


We set its outfit either by:
We set its outfit either by:
* setting its class to a PNG image that exists at ''data/sprites/npc'', e.g. <code>npc.setEntityClass("welcomernpc");</code>
* setting its class to a [[StendhalRefactoringGraphics#NPCs|PNG image]] that exists at ''data/sprites/npc'', e.g. <code>npc.setEntityClass("welcomernpc");</code>
* setting its outfit with setOutfit method e.g. <code>npc.setOutfit(new Outfit(0, 05, 01, 06, 01));</code>. See [[StendhalOpenTasks#Player's outfits| How to know player's outfits specifications]]
* setting its outfit with setOutfit method e.g. <code>npc.setOutfit(new Outfit(0, 05, 01, 06, 01));</code> - see [http://stendhalgame.org/hudson/job/stendhal_HEAD/javadoc/games/stendhal/server/entity/Outfit.html Outfit javadoc]

It is nice to set a description for when the player does ''Look''. If you don't set one, it will just say: ''You see [NPC Name].''


Finally set its initial position and its HP. Don't worry for your NPC. It can't be attacked nor killed.
Finally set its initial position and its HP. Don't worry for your NPC. It can't be attacked nor killed.