moss-n-puddles/typeclasses/npcs.py
2025-05-03 21:20:18 -07:00

71 lines
2.7 KiB
Python
Executable file

#!/usr/bin/env python
from typeclasses.objects import Object
from utils.word_list import routput
class NPC(Object):
"""
An NPC is an NPC because it can react to what it _hears_.
To do this, implement 'at_heard_say', a function that
returns a string for a response.
"""
def at_heard_say(self, message, from_obj,
is_say=True, is_whisper=False):
"Override to return a string in response to message."
pass
def msg(self, text=None, from_obj=None, **kwargs):
"Custom msg() method reacting to say."
# make sure to not repeat what we ourselves said or we'll
# create a loop
if from_obj != self:
is_say = False
is_whisper = False
try:
say_text, is_say = text[0], text[1]['type'] == 'say'
is_whisper = text[1]['type'] == 'whisper'
except Exception:
pass
try:
# message will be on the form `<Person> says, "say_text"`
# we want to get only say_text without the quotes and any spaces
message = text.split('says, ')[1].strip(' "')
shout, response = self.at_heard_say(message, from_obj,
is_say, is_whisper)
if response != None:
self.at_say(response, just_owner=(shout == "shout"))
except Exception:
pass
# this is needed if anyone ever puppets this NPC - without it
# you would never get any feedback from the server (not even
# the results of look)
super().msg(text=text, from_obj=from_obj, **kwargs)
class CarriableNPC(NPC):
"""
A carriable NPC is like any other NPC, except that since it can
be carried and isn't locked down, it can't hear conversation in a
room.
"""
def at_say(self, message, msg_self=None, msg_location=None,
receivers=None, msg_receivers=None, just_owner=True,
**kwargs):
"Does the best it can to speak out loud."
owner = self.location
if self.location.is_typeclass("typeclasses.rooms.Room"):
super().at_say(message, msg_self=msg_self,
msg_location=msg_location,
receivers=receivers,
msg_receivers=msg_receivers)
elif just_owner:
owner.msg(f"The {self.name} says, \"{message}\"")
else:
owner.msg(
f"The {self.name}, you are carrying, says, \"{message}\"")
owner.location.msg(
f"The {self.name}, carried by {self.location.name}, says, \"{message}\"", exclude=owner)