13
« on: June 10, 2007, 07:58:20 am »
This thread is a month old, so perhaps the person who asked the question isn't still around, but I'm going to respond anyway for reference if there are other people wondering.
The npcclient Controls all of the actions of the NPCs in game. It is a program separate to the server that receives information about each NPC in game, and then makes desicons about what they should do, and proceeds to send the appropriate network messages back to the server to control the actions of the NPCs.
Each NPC contains a list of behaviors, which are actions it can 'do'. Each behavior will do some processing and then send a network message to the server to execute that behavior- things like move, start attacking, turn around, pick up and item, etc. This is the same way that the psclient controls it's own character, by sending these network messages to the server. Each behavior has it's own class (MoveOperation, MeleeOperation, etc) which inherits from the base class Behavior. Each behavior has an overloaded method Run() which will execute the code necessary to 'run' this behavior, as I mentioned above. There are also some other methods like interrupt, etc. Each behavior also has a 'delta', which is how much the NPC wants to do that action. This is how the NPC decides what to do. It iterates through all it's assigned behaviors each timestep, and looks for the one with the highest delta, and then runs it.
The two other bits of the AI are perceptions and reactions. A perception is something that happens to an NPC; it 'perceives' something about its environment. For this to happen the server must send messages to the npcclient whenever something happens that an NPC would be interested in, and then npcclient will trigger the perception on the appropriate NPC.
Lastly, reactions 'react' to perceptions. A reaction specifies what perception it is to react to, and what behavior's delta it should modify as a result. Thus, whenever a perception is triggered on an NPC, it will loop through each of its reactions, and see whether that reaction is for that perception. If it is, it will modify the delta of the associated behavior accordingly.
To run through an example:
A player attacks an NPC.
On the server:
The server generates an 'NPC attacked' message with appropriate information, and dispatches it to NPC client.
On NPC client:
npcclient receives NPC attacked message, and matches up which NPC it is for from the NPC id.
npcclient then triggers a perception of type "attacked" on the correct NPC.
The npc iterates through it's list of reactions, and finds which one(s) respond to being attacked.
It finds a reaction to being attacked, which is to add 400 delta to the "fight" behavior, which is does. Fight behavior now takes over the "do nothing" behavior as the highest delta.
On the next step through all the NPCs, "fight" behavior (which is a MeleeOperation) will be the highest delta, and its run method will be executed.
MeleeOperation->Run() does some processing and if the conditions are correct dispatches a StartAttack message to the Server.
On the server:
Server receives startattack message for that NPC, and does the server side processing of attacking (calculating damage, when it can attack, etc).
Thus, to write an AI using this framework, you need to specify Behaviors and Reactions. These are specified in the npcbehave.xml file, which is then loaded in by npcclient. The methods to load these in and convert them to valid Behavior and Reaction objects are handled by /src/npcclient/npcbehave.cpp and /src/npcclient/perceptions.cpp.
Hope this gives you a general overview. I'd strongly encourage you to download the npcclient client and have a look around if you're interested, as its quite an interesting part of planeshift, and a part that will definitely need committed contributers for in the future. There are lots of enchantments that you could add if you know how to code c++.
Some of the specifics I mentioned above might not be 100% correct (like the name of some methods, and the names of some network messages as I didnt have the code open at the time) but it should give you a general idea of how it all works, and you can look it up yourself if you'd like to.