Stendhal NPC Coding: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>Kymara
No edit summary
imported>Madmetzger
 
(91 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Navigation for Stendhal Top|Contributing}}
{{Navigation for Stendhal Top|Contributing}}
{{Navigation for Stendhal Extenders}}
{{Navigation for Stendhal Extenders}}
{{Stendhal NPCs}}


Usually we add NPCs (non-player characters) to make world more alive and to use them in Quests.
== Adding 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.


This is how NPCs should be added to the world.
This is how NPCs are added to the world.

A java file should define the path they walk on, basic dialog and how they look. It can also define the selling, buying, healing or producing behaviour of an NPC. The NPC is only loaded into a zone if you also edit the zone's xml file, to configure the zone using that java file.

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 ==

First you need to decide what region your NPC will be in, so that we can create the Java file in the correct place. The location for the file will be:
src/games/stendhal/server/maps/''region''/''subregion''

You should name the NPC file after the function of the NPC. Examples are GreeterNPC, HealerNPC, BuyerNPC, ChefNPC, SellerNPC, LifeguardNPC

In this example we'll make a wizard in a magician's hut.

So we will create a file <code>src/games/stendhal/server/maps/ados/magician_house/WizardNPC.java</code>

The start of the file will need to specify some standard things to make it work, don't worry about these just copy them for now. Notice that the ''class'' is the same as our ''filename'' and the ''package'' is related to where we put the file.


''SpeakerNPC npc = new SpeakerNPC() { ... }'' creates a new NPC. When added to a zone, NPCs are added it to a global list so next time you need to get the NPC for participating in a quest, or if you want to teleportto it, you can call it by its ''name'' using ''NPCList.get().get(name)''.
<source lang="java">
<source lang="java">
package games.stendhal.server.maps.ados.magician_house;
SpeakerNPC npc = new SpeakerNPC("name") {

/...
import games.stendhal.server.core.config.ZoneConfigurator;
});
import games.stendhal.server.core.engine.SingletonRepository;
import games.stendhal.server.core.engine.StendhalRPZone;
import games.stendhal.server.core.pathfinder.FixedPath;
import games.stendhal.server.core.pathfinder.Node;
// this one is just because our NPC is a seller
import games.stendhal.server.entity.npc.ShopList;
import games.stendhal.server.entity.npc.SpeakerNPC;
// this one is just because our NPC is a seller
import games.stendhal.server.entity.npc.behaviour.adder.SellerAdder;
// this one is just because our NPC is a seller
import games.stendhal.server.entity.npc.behaviour.impl.SellerBehaviour;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class WizardNPC implements ZoneConfigurator {
// this is just because he has a 'shop' to sell potions
private final ShopList shops = SingletonRepository.getShopList();

/**
* Configure a zone.
*
* @param zone The zone to be configured.
* @param attributes Configuration attributes.
*/
public void configureZone(final StendhalRPZone zone, final Map<String, String> attributes) {
buildNPC(zone);
}

</source>
</source>


Now we have set up this standard stuff we can build the actual NPC. We'll call the method which builds the NPC, ''buildNPC''.
An example is:


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


protected void createDialog() {
protected void createDialog() {
// Lets the NPC reply with "Hallo" when a player greets him
// 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);
addGoodbye();
// respond about a special trigger word
addReply("potions","Please ask for my #offer.");
}
// use standard goodbye, but you can also set one inside the ()
});
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.
// 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>


The NPC is added to a list of NPC from where you can later obtain it for adding more dialogues for quests. So, it is very important to make sure the name you give to your NPC is unique.
The first thing defined is the name. The NPC is added to a list of NPCs so you can later fetch it for adding more dialogues for quests. So, it is very important to make sure the name you give to your NPC is unique.


It is a good idea to create a path that the NPC will follow on that area. Just used your tiled map or walk around in game and check coordinates to choose your nodes. You can also make the NPC stand still by using this instead:
Next define a path that the NPC will follow on that area. Just used your tiled map or walk around in game and check coordinates to choose your nodes.
Each node is where the NPC turns. Make sure you right down every turning point and don't miss one (don't go from one corner, diagonally to another corner.)

You can also make the NPC stand still by using this instead:


<source lang="java">
<source lang="java">
Line 61: Line 122:
</source>
</source>


Your NPC will stand still at whatever point you set as the initial position with <code>npc.setPosition(x, y);</code>.
In that case your NPC will stand still at whatever point you set as the initial position with <code>npc.setPosition(x, y);</code>.


And then create a dialog. Dialogs and such are explained in Quest sections, but you should find the exmaple above covers most options and the options like addHelp are quite self-explanatory.
And then create a dialog. Dialogs and such are explained in Quest sections, but you should find the example above covers most options and the options like addHelp are quite self-explanatory. There are:
:addGreeting
:addOffer
:addHelp
:addJob
:addQuest
:addGoodbye
:addReply ''specify a '''trigger''' and a '''reply'''''


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''
* 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 [http://stendhalgame.org/hudson/job/stendhal_HEAD/javadoc/games/stendhal/server/entity/Outfit.html Outfit javadoc]
* setting its outfit with setOutfit method.

See [[StendhalOpenTasks#Player's outfits| How to know player's outfits specifications]]
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.
Line 74: Line 143:
Once that is done add the NPC to zone using ''zone.add()'' method.
Once that is done add the NPC to zone using ''zone.add()'' method.


== Configure zone xml ==
We need to tell the zone to load this configuration class file. The NPC in the example lives in the ados area, so the information is stored in data/conf/zones/ados.xml. The map she's on is called int_ados_magician_house, so the information is inside the <zone name="int_ados_magician_house" file="interiors/ados/magician_house.tmx"> </zone> section.
We need to tell a zone to load this configuration class file, so the NPC is added to the world somewhere. The NPC in the example lives in the ados area, so the information is stored in data/conf/zones/ados.xml. The map she's on is called int_ados_magician_house, so the information is inside the <zone name="int_ados_magician_house" file="interiors/ados/magician_house.tmx"> </zone> section.
To load the java class file you created just add the line
To load the java class file you created just add the line
<source lang="xml">
<source lang="xml">