MARS Trainer Plug-in

From Multiverse

Jump to: navigation, search

Contents

Introduction

The MARS Trainer Plug-in provides a framework for player-characters to learn skills and abilities. By using server and client scripting you can now setup NPCs as trainers and assign specific skills that they offer to players.

This article provides a brief overview of how to designate an NPC as a trainer and assign skills that can be offered to players in your world.

Prerequisite: Download the Complete Asset Repository to follow along.

Server changes

This tutorial uses the human female model in the SampleWorld asset repository. On the server side, the tutorial uses the Python script file multiverse/config/sampleworld/templates.py as an example.

Setting up a NPC trainer

The first thing to do to setup a skill trainer is to add a mob to the world that will be flaged as a trainer. For information on how to add a mob to your world, see Adding Mobs to Your World.

Once you have the trainer mob in place, open the file multiverse/config/sampleworld/templates.py and locate the definition for the Human Female Trainer Template:

tmpl = Template("Human Female Trainer")
tmpl.put(WorldManagerClient.NAMESPACE,    
WorldManagerClient.TEMPL_DISPLAY_CONTEXT, human_female_base_DC)
tmpl.put(WorldManagerClient.NAMESPACE, 
WorldManagerClient.TEMPL_OBJECT_TYPE, ObjectTypes.mob)
tmpl.put(TrainerClient.NAMESPACE, "skills", "Sword;Axe;Dagger;First Aid")
tmpl.put(CombatClient.NAMESPACE, "istrainer", Boolean(True))
ObjectManagerClient.registerTemplate(tmpl)

The lines in bold above are the keys to flagging a NPC as a skill trainer. The first line tells the TrainerPlugin what skills the NPC will offer players to train.

tmpl.put(TrainerClient.NAMESPACE, "skills", "Sword;Axe;Dagger;First Aid")

The skill names must match exactly as defined in the server's mv-home/config/sampleworld/skill_db.py file. A semi-colon separates the names so that the client scripts can easily parse the skills and display them to the player.

See MARS Abilities and Combat Plugin for more information on defining skills.

The second line sets the flag on the NPC to mark it as a skill trainer. This flag triggers the training interface.

tmpl.put(CombatClient.NAMESPACE, "istrainer", Boolean(True))

Example Client implementation

The SampleWorld assets provide a basic example of how to use the trainer mob set up above to display training information to the player and handle training requests.

Setup

All of the files that this section refers to are in the asset repository \Scripts and the \Interface\FrameXML directories.

Handling trainer messages

The \Scripts\MarsTraining.py script handles communication with the TrainerPlugin class.

The basic example for interacting with trainer mobs in Sample World handles two incoming messages and two outgoing messages. Each outgoing message is paired with one responding message.

Getting training information

When a player right clicks on a trainer NPC, the Client sends a message to the server requesting information on the skills the trainer offers:

def SendTrainingInfoRequestMessage(npcId):
	playerOid = ClientAPI.GetPlayerObject().OID
	props = {“playerOid” : playerOid, “npcOid” : npcId}
ClientAPI.Network.SendExtensionMessage(0,False,”mv.REQ_TRAINER_INFO”,props)

In the above code, the Client gathers information about the trainer mob and the player and sends the information to the server via an extension message. The mv.REQ_TRAINER_INFO message type indicates that the client is requesting information about the trainer. The server then sends a response back to the player who requested the information, and the following message handler script handles that message:

def _HandleTrainingInfo(props):
	ClientAPI.Interface.DispatchEvent(“TRAINING_INFO”, [str(props[“skills”])])

ClientAPI.Network.RegisterExtensionMessageHandler(“mv.TRAINING_INFO”, _HandleTrainingInfo)

The message handler tells the client to listen for an extension message with the sub-type of mv.TRAINING_INFO, and if one is received, call the _HandleTrainingInfo() method. For an example of how to display the information returned, see Interface\FrameXML\mvTrainingSelection.py in Sampleworld.

Requesting skill training

In Sampleworld , once information for the trainer is received, the player is presented with a list of training options. When the player chooses a skill to train we sending a training request to the server and then handle the response using the following methods:

def SendSkillTrainingRequest(skill):
	playerOid = ClientAPI.GetPlayerObject().OID
	props = {“playerOid” : playerOid, “skill” : skill}
	ClientAPI.Network.SendExtensionMessage(0,False,”mv.REQ_SKILL_TRAINING”,props)

In the above code, the client sends a message to the server request that the player be granted the newly chosen skill. If the training successful, then a skill update message would be kicked off. For more information about how skill updates are handled, you may refer to the \Scripts\MarsSkill.py script file. If the training failed saying that the player already knows the skill then we handle that with the following message handler:

def _HandleTrainingFailed(props):
	ClientAPI.Interface.DispatchEvent(“TRAINING_FAILED”, [str(props[“skills”])])

ClientAPI.Network.RegisterExtensionMessageHandler(“mv.TRAINING_FAILED”, _HandleTrainingFailed)

The training failed message, like the training info message simply kicks of an event advising any UI components that are listening that a TRAINING_FAILED event has occurred. To see how this is being handled in the Sample World demo, again refer to the \Interface\FrameXML\mvTrainingSelection.py file.

Interacting with the trainer NPC

Now that you've seen how messages are handled for the trainer plug-in you can now put them to use. For the player to know what skills the trainer mob has to offer, there must be a way to interact with the NPC. We will do this by modifying code in \Scripts\MarsCursor.py file.

When you added the trainer mob to the server files, you set a flag, the istrainer property, on the mob to indicate it is a trainer. You are now going to leverage this property in the OnRightClick event in \Scripts\MarsCursor.py.

Locate the OnRightClick definition and look for the section of code that looks like the following :

if worldObj.CheckBooleanProperty("istrainer"):
	if dist < 6.0:
		MarsTraining.SendTrainingInfoRequestMessage(worldObj.OID)
	else:
		ClientAPI.Write(“That object is too far away (%f meters)” % dist)

This section of code checks to see if the istrainer property is set to true on the object the player is right clicking on. If the istrainer flag is true, then the client sends the training info request message shown in the previous section.

Personal tools