8. Non-Player-Characters¶
Non-Player-Characters, or NPCs, is the common term for all active agents that are not controlled by players. NPCs could be anything from merchants and quest givers, to monsters and bosses. They could also be ‘flavor’ - townsfolk doing their chores, farmers tending their fields - there to make the world feel “more alive”.
In this lesson we will create the base class of EvAdventure NPCs based on the Knave ruleset. According to the Knave rules, NPCs have some simplified stats compared to the PC characters we designed earlier.
8.1. The NPC base class¶
Create a new module
evadventure/npcs.py
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # in evadventure/npcs.py from evennia import DefaultCharacter, AttributeProperty from .characters import LivingMixin from .enums import Ability class EvAdventureNPC(LivingMixin, DefaultCharacter): """Base class for NPCs""" is_pc = False hit_dice = AttributeProperty(default=1, autocreate=False) armor = AttributeProperty(default=1, autocreate=False) # +10 to get armor defense hp_multiplier = AttributeProperty(default=4, autocreate=False) # 4 default in Knave hp = AttributeProperty(default=None, autocreate=False) # internal tracking, use .hp property morale = AttributeProperty(default=9, autocreate=False) allegiance = AttributeProperty(default=Ability.ALLEGIANCE_HOSTILE, autocreate=False) weapon = AttributeProperty(default=BARE_HANDS, autocreate=False) # instead of inventory coins = AttributeProperty(default=1, autocreate=False) # coin loot is_idle = AttributeProperty(default=False, autocreate=False) @property def strength(self): return self.hit_dice @property def dexterity(self): return self.hit_dice @property def constitution(self): return self.hit_dice @property def intelligence(self): return self.hit_dice @property def wisdom(self): return self.hit_dice @property def charisma(self): return self.hit_dice @property def hp_max(self): return self.hit_dice * self.hp_multiplier def at_object_creation(self): """ Start with max health. """ self.hp = self.hp_max self.tags.add("npcs", category="group") class EvAdventureMob(EvAdventureNPC): """ Mob(ile) NPC to be used for enemies. """ |
Line 9: By use of multiple inheritance we use the
LinvingMixin
we created in the Character lesson. This includes a lot of useful methods, such as showing our ‘hurt level’, methods to use to heal, hooks to call when getting attacked, hurt and so on. We can re-use all of those in upcoming NPC subclasses.Line 12: The
is_pc
is a quick and convenient way to check if this is, well, a PC or not. We will use it in the upcoming Combat base lesson.Line 13: The NPC is simplified in that all stats are just based on the
Hit dice
number (see Lines 25-51). We storearmor
and aweapon
as direct Attributes on the class rather than bother implementing a full equipment system.Lines 17, 18: The
morale
andallegiance
are Knave properties determining how likely the NPC is to flee in a combat situation and if they are hostile or friendly.Line 19: The
is_idle
Attribute is a useful property. It should be available on all NPCs and will be used to disable AI entirely.Line 59: We make sure to tag NPCs. We may want to group different NPCs together later, for example to have all NPCs with the same tag respond if one of them is attacked.
We make an empty subclass EvAdventureMob
. A ‘mob’ (short for ‘mobile’) is a common MUD term for NPCs that can move around on their own. We will in the future use this class to represent enemies in the game. We will get back to this class [in the lesson about adding AI][Beginner-Tutoroal-AI].
8.2. Testing¶
Create a new module
evadventure/tests/test_npcs.py
Not so much to test yet, but we will be using the same module to test other aspects of NPCs in the future, so let’s create it now.
# in evadventure/tests/test_npcs.py
from evennia import create_object
from evennia.utils.test_resources import EvenniaTest
from .. import npcs
class TestNPCBase(EvenniaTest):
"""Test the NPC base class"""
def test_npc_base(self):
npc = create_object(
npcs.EvAdventureNPC,
key="TestNPC",
attributes=[("hit_dice", 4)], # set hit_dice to 4
)
self.assertEqual(npc.hp_multiplier, 4)
self.assertEqual(npc.hp, 16)
self.assertEqual(npc.strength, 4)
self.assertEqual(npc.charisma, 4)
Nothing special here. Note how the create_object
helper function takes attributes
as a keyword. This is a list of tuples we use to set different values than the default ones to Attributes. We then check a few of the properties to make sure they return what we expect.
8.3. Conclusions¶
In Knave, an NPC is a simplified version of a Player Character. In other games and rule systems, they may be all but identical.
With the NPC class in place, we have enough to create a ‘test dummy’. Since it has no AI yet, it won’t fight back, but it will be enough to have something to hit when we test our combat in the upcoming lessons.