Stendhal NPC Coding: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>Kymara
No edit summary
imported>Kymara
flesh out
Line 12: Line 12:


== Define NPC with Java ==
== Define NPC with Java ==

''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)''.
First you need to decide what region your NOC 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.

<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, attributes);
}

</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, final Map<String, String> attributes) {
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>();
Line 31: Line 74:


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 one 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.");
Line 41: Line 84:
// Lets the NPC heal players for free
// Lets the NPC heal players for free
addHealer(0);
addHealer(0);
// 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();
addGoodbye();
}
}
Line 46: Line 92:


zone.assignRPObjectID(npc);
zone.assignRPObjectID(npc);
// This determines how the NPC will look like.
// This determines how the NPC will look like. welcomernpc.png is a picture in data/sprites/npc/
npc.setEntityClass("welcomernpc");
npc.setEntityClass("welcomernpc");
// 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.
Line 55: Line 101:
</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.

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.)


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:
You can also make the NPC stand still by using this instead:


<source lang="java">
<source lang="java">
Line 66: Line 115:
</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 PNG image that exists at ''data/sprites/npc'', e.g. <code>npc.setEntityClass("welcomernpc");</code>
* setting its outfit with setOutfit method.
* 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]]
See [[StendhalOpenTasks#Player's outfits| How to know player's outfits specifications]]