pets: Fix PetObserve

This commit is contained in:
John Cote 2024-09-11 20:57:55 -04:00
parent 6ba9b959c9
commit 07b548a237
1 changed files with 141 additions and 119 deletions

View File

@ -1,200 +1,222 @@
from direct.directnotify import DirectNotifyGlobal
from toontown.pets import PetTricks
from enum import IntEnum
notify = DirectNotifyGlobal.directNotify.newCategory('PetObserve')
"""
PetObserve
A PetObserve is something that pets should be able to observe. Examples
are things that avatars say, the fact that an avatar or pet is paying
attention to the pet, sounds, etc. PetObserves serve the function of the
senses, the eyes, ears, etc. of the pets.
"""
# PetObserves are broadcast to all pets in a zone, or listening to that
# zone's event
def getEventName(zoneId):
return 'PetObserve-%s' % zoneId
def send(zoneIds, petObserve):
if petObserve.isValid():
#notify.debug('sending pet observe %s to %s' % (petObserve, zoneIds))
if type(zoneIds) not in (list, tuple):
zoneIds = [zoneIds]
for zoneId in zoneIds:
messenger.send(getEventName(zoneId), [petObserve])
Phrases = IntEnum('Phrases', ('HI', 'BYE', 'YES', 'NO', 'SOOTHE', 'PRAISE', 'CRITICISM', 'HAPPY', 'SAD', 'ANGRY', 'HURRY', 'QUESTION', 'FRIENDLY', 'LETS_PLAY', 'COME', 'FOLLOW_ME', 'STAY', 'NEED_LAFF', 'NEED_GAGS', 'NEED_JB', 'GO_AWAY', 'DO_TRICK'), start=0)
Actions = IntEnum('Actions', ('FEED', 'SCRATCH', 'ATTENDED_START', 'ATTENDED_STOP', 'ATTENDING_START', 'ATTENDING_STOP', 'CHANGE_ZONE', 'LOGOUT', 'GARDEN'), start=0)
# spoken messages that pets understand; speedchat phrases map to one of these
Phrases = IntEnum('Phrases', ('HI', 'BYE', 'YES', 'NO', 'SOOTHE', 'PRAISE', 'CRITICISM', 'HAPPY',
'SAD', 'ANGRY', 'HURRY', 'QUESTION', 'FRIENDLY', 'LETS_PLAY',
'COME', 'FOLLOW_ME', 'STAY', 'NEED_LAFF', 'NEED_GAGS', 'NEED_JB',
'GO_AWAY', 'DO_TRICK',
), start=0)
# actions of other avatars that the pet can observe
Actions = IntEnum('Actions', ('FEED', 'SCRATCH',
'ATTENDED_START', 'ATTENDED_STOP',
'ATTENDING_START', 'ATTENDING_STOP',
'CHANGE_ZONE', 'LOGOUT',
'GARDEN'
), start=0)
class PetObserve:
# base class for all pet observes
def isValid(self):
# override this and return zero to prevent observe from being
# sent to pets
return 1
def isForgettable(self):
# override this and return non-zero to enable Pets to forget
# this observation
return 0
def _influence(self, petBrain):
# override this and call the appropriate observe handler on the
# pet brain. This is here to avoid a fugly type-switch in the brain.
petBrain._handleGenericObserve(self)
def __repr__(self):
return '%s()' % self.__class__.__name__
class PetActionObserve(PetObserve):
def __init__(self, action, avId, data = None):
"""An avatar or pet has done something that pets should take note of."""
def __init__(self, action, avId, data=None):
self.action = action
self.avId = avId
self.data = data
def getAction(self):
return self.action
def getAvId(self):
return self.avId
def getData(self):
return self.data
def _influence(self, petBrain):
petBrain._handleActionObserve(self)
def __repr__(self):
return '%s(%s,%s)' % (self.__class__.__name__, Actions.getString(self.action), self.avId)
return '%s(%s,%s)' % (
self.__class__.__name__,
Actions(self.action).name, self.avId)
class PetPhraseObserve(PetObserve):
"""An avatar is 'communicating' in a way that can be expressed with a
PetPhrase. This could be pressing a button on a pet panel, or saying
something in SpeedChat, etc."""
def __init__(self, petPhrase, avId):
self.petPhrase = petPhrase
self.avId = avId
def getPetPhrase(self):
return self.petPhrase
def getAvId(self):
return self.avId
def isForgettable(self):
return 1
def _influence(self, petBrain):
petBrain._handlePhraseObserve(self)
def __repr__(self):
return '%s(%s,%s)' % (self.__class__.__name__, Phrases.getString(self.petPhrase), self.avId)
return '%s(%s,%s)' % (
self.__class__.__name__,
Phrases(self.petPhrase).name, self.avId)
class SCObserve(PetPhraseObserve):
# avatar said something in SpeedChat
def __init__(self, msgId, petPhrase, avId):
self.msgId = msgId
PetPhraseObserve.__init__(self, petPhrase, avId)
def isValid(self):
return self.petPhrase is not None
class TrickRequestObserve(PetPhraseObserve):
# avatar requested that we do a trick
def __init__(self, trickId, avId):
self.trickId = trickId
PetPhraseObserve.__init__(self, Phrases.DO_TRICK, avId)
def isForgettable(self):
# this would just be annoying
return 0
def getTrickId(self):
return self.trickId
OP = Phrases
_scPhrase2petPhrase = {1: OP.YES,
2: OP.NO,
3: OP.SOOTHE,
100: OP.HI,
101: OP.HI,
102: OP.HI,
103: OP.HI,
104: OP.HI,
105: OP.HI,
107: OP.HI,
108: OP.HI,
200: OP.BYE,
201: OP.BYE,
202: OP.BYE,
203: OP.BYE,
204: OP.BYE,
205: OP.BYE,
206: OP.BYE,
207: OP.BYE,
300: OP.HAPPY,
301: OP.HAPPY,
302: OP.HAPPY,
303: OP.HAPPY,
304: OP.HAPPY,
305: OP.HAPPY,
306: OP.HAPPY,
307: OP.HAPPY,
308: OP.HAPPY,
309: OP.HAPPY,
310: OP.HAPPY,
311: OP.HAPPY,
312: OP.HAPPY,
313: OP.HAPPY,
314: OP.HAPPY,
315: OP.HAPPY,
400: OP.SAD,
401: OP.SAD,
402: OP.SAD,
403: OP.SAD,
404: OP.SAD,
405: OP.SAD,
406: OP.SAD,
407: OP.NO,
410: OP.NEED_LAFF,
500: OP.FRIENDLY,
505: OP.PRAISE,
506: OP.HAPPY,
507: OP.FRIENDLY,
508: OP.FRIENDLY,
509: OP.FRIENDLY,
510: OP.QUESTION,
511: OP.QUESTION,
513: OP.QUESTION,
514: OP.NEED_LAFF,
600: OP.PRAISE,
601: OP.PRAISE,
602: OP.PRAISE,
603: OP.PRAISE,
700: OP.PRAISE,
701: OP.PRAISE,
900: OP.CRITICISM,
901: OP.CRITICISM,
902: OP.CRITICISM,
903: OP.CRITICISM,
904: OP.CRITICISM,
905: OP.CRITICISM,
1006: OP.FOLLOW_ME,
1007: OP.STAY,
1010: OP.STAY,
1015: OP.STAY,
1201: OP.CRITICISM,
1300: OP.NEED_LAFF,
1400: OP.HURRY,
1404: OP.PRAISE,
1405: OP.PRAISE,
1413: OP.NEED_GAGS,
1414: OP.NEED_LAFF,
1601: OP.NEED_JB,
1603: OP.HURRY,
1605: OP.LETS_PLAY,
1606: OP.LETS_PLAY,
21000: OP.COME,
21001: OP.COME,
21002: OP.STAY,
21003: OP.PRAISE,
21004: OP.PRAISE,
21005: OP.PRAISE}
_scPhrase2petPhrase = {
1: OP.YES,
2: OP.NO,
3: OP.SOOTHE,
100: OP.HI,
101: OP.HI,
102: OP.HI,
103: OP.HI,
104: OP.HI,
105: OP.HI,
107: OP.HI,
108: OP.HI,
200: OP.BYE,
201: OP.BYE,
202: OP.BYE,
203: OP.BYE,
204: OP.BYE,
205: OP.BYE,
206: OP.BYE,
207: OP.BYE,
300: OP.HAPPY,
301: OP.HAPPY,
302: OP.HAPPY,
303: OP.HAPPY,
304: OP.HAPPY,
305: OP.HAPPY,
306: OP.HAPPY,
307: OP.HAPPY,
308: OP.HAPPY,
309: OP.HAPPY,
310: OP.HAPPY,
311: OP.HAPPY,
312: OP.HAPPY,
313: OP.HAPPY,
314: OP.HAPPY,
315: OP.HAPPY,
400: OP.SAD,
401: OP.SAD,
402: OP.SAD,
403: OP.SAD,
404: OP.SAD,
405: OP.SAD,
406: OP.SAD,
407: OP.NO,
410: OP.NEED_LAFF,
500: OP.FRIENDLY,
505: OP.PRAISE,
506: OP.HAPPY,
507: OP.FRIENDLY,
508: OP.FRIENDLY,
509: OP.FRIENDLY,
510: OP.QUESTION,
511: OP.QUESTION,
513: OP.QUESTION,
514: OP.NEED_LAFF,
600: OP.PRAISE,
601: OP.PRAISE,
602: OP.PRAISE,
603: OP.PRAISE,
700: OP.PRAISE,
701: OP.PRAISE,
900: OP.CRITICISM,
901: OP.CRITICISM,
902: OP.CRITICISM,
903: OP.CRITICISM,
904: OP.CRITICISM,
905: OP.CRITICISM,
1006: OP.FOLLOW_ME,
1007: OP.STAY,
1010: OP.STAY,
1015: OP.STAY,
1201: OP.CRITICISM,
1300: OP.NEED_LAFF,
1400: OP.HURRY,
1404: OP.PRAISE,
1405: OP.PRAISE,
1413: OP.NEED_GAGS,
1414: OP.NEED_LAFF,
1601: OP.NEED_JB,
1603: OP.HURRY,
1605: OP.LETS_PLAY,
1606: OP.LETS_PLAY,
21000: OP.COME,
21001: OP.COME,
21002: OP.STAY,
21003: OP.PRAISE,
21004: OP.PRAISE,
21005: OP.PRAISE,
}
# add the trick phrases
for scId in PetTricks.ScId2trickId:
_scPhrase2petPhrase[scId] = OP.DO_TRICK
del OP
def getSCObserve(msgId, speakerDoId):
# given a speedchat msgId, returns the appropriate Observe object
# If the speedchat phrase doesn't have any PetPhrase equivalent,
# this will return None for the phrase.
phrase = _scPhrase2petPhrase.get(msgId)
if phrase == Phrases.DO_TRICK:
trickId = PetTricks.ScId2trickId[msgId]