#!/usr/bin/env python """ Pets Create interactive 'pets' that consume some sort of food, and need to be 'fed'. Each level of pet requires more aspects for interaction. """ from enum import Enum from re import sub from time import time import random from evennia import TICKER_HANDLER from typeclasses.objects import Object from typeclasses.characters import Character # from typeclasses.lightables import LightSource from commands.feedables import CmdFeedSet from utils.word_list import squish, choices class Hunger(Enum): "States of being for a Pet." RAVENOUS = 100 HUNGRY = 300 FED = 850 FULL = 1000 class Pet(Object): """ This is a base class for Pets. """ fullname = None pers_pronoun = "it" # or he/she/they poss_pronoun = "its" # or his/her/them # Feed about once a day: hungry_rate = -.7 hunger_states = { Hunger.RAVENOUS: [ "looks crazy with hunger", ], Hunger.HUNGRY: [ "looks hungry", "seems hungry", ], Hunger.FED: "looks well", Hunger.FULL: [ "looks content", "looks sated", ] } def at_object_creation(self): """ Called when object is first created. We set up a ticker to update hunger levels regularly. """ self.cmdset.add_default(CmdFeedSet) self.db.hunger_level = Hunger.FULL.value # subscribe ourselves to a ticker to repeatedly call the hook # "update_weather" on this object. The interval is randomized # so as to not have all weather rooms update at the same time. self.db.interval = random.randint(70, 90) TICKER_HANDLER.add(interval=self.db.interval, callback=self.update_state) super().at_object_creation() def hunger(self): if self.db.hunger_level < Hunger.RAVENOUS.value: return Hunger.RAVENOUS if self.db.hunger_level < Hunger.HUNGRY.value: return Hunger.HUNGRY if self.db.hunger_level < Hunger.FED.value: return Hunger.FED return Hunger.FULL def hunger_appearance(self): msgs = self.hunger_states.get(self.hunger()) if isinstance(msgs, str): return f"{self.fullname or self.pers_pronoun} {msgs}" return f"{self.fullname or self.pers_pronoun} {random.choice(msgs)}" def update_state(self, *args, **kwargs): """ Called by the tickerhandler at regular intervals. Even so, we only update at a hungry_rate of the time, picking a random weather message when we do. The tickerhandler requires that this hook accepts any arguments and keyword arguments (hence the *args, **kwargs even though we don't actually use them in this example) """ curr = self.hunger() amount = kwargs.get('amount', self.hungry_rate) self.db.hunger_level = max(self.db.hunger_level + amount, 0) if self.hunger() != curr: self.location.msg_contents(f"|w{self.hunger_appearance()}|n\n") def return_appearance(self, looker, **kwargs): """ This formats the description of this object based on 'hunger'. Called by the 'look' command. Args: looker (Object): Object doing the looking. **kwargs (dict): Arbitrary, optional arguments for users overriding the call (unused by default). """ return f"{self.db.desc} {self.hunger_appearance()}" def feed(self, giver, obj=None): pass # ------------------------------------------------------------ # Fireplace, both a pet that is hungry and consumes food, # but also emits light when it isn't starving. # ------------------------------------------------------------ class Fire(Pet): """ Fire in this world, is a cute fireplace pet. """ fullname = "The fire in the fireplace" hungry_rate = -5 hunger_states = { Hunger.RAVENOUS: [ "is out. It is dark.", ], Hunger.HUNGRY: [ "shows glowing red embers that seems breathe in a memorizing way.", "is little more than glowing embers casting shadows on the wall.", ], Hunger.FED: [ "dances and shimmies with warmth and light.", "crackles and pops with warmth and light." ], Hunger.FULL: [ "roars to life, casting a bright light around the room.", "burns brightly and is very hot. You feel the need to move back a bit.", ] } def feed_msg(self, giver): now = time() last_fed = giver.ndb.fire_last_fed # could be None if last_fed and (now - last_fed < 30): adj = "some more" else: adj = "some" giver.ndb.fire_last_fed = now get_up = "" gets_up = "" if giver.db.is_sitting: get_up = "get up and" gets_up = "gets up and" if self.db.hunger_level < 5: giver.msg(squish(f"You {get_up} put {adj} wood in the " f"fireplace, and start a fire.")) self.location.msg_contents(squish(f"{giver.name} {gets_up} starts a fire."), exclude=giver) else: giver.msg(squish(f"You {get_up} put {adj} wood on the " f"fire in the fireplace.")) self.location.msg_contents(squish(f"{giver.name} {gets_up} puts {adj} wood on the fire."), exclude=giver) def feed(self, giver, obj=None): """ Feed the fire the default object, wood. """ self.feed_msg(giver) self.update_state(giver=giver, amount=300) def at_pre_object_receive(self, moved_obj, giver, move_type="move", **kwargs): "Burn something in the fireplace." if moved_obj.is_typeclass("typeclasses.things.Wood"): self.feed_msg(giver) self.update_state(giver=giver, amount=400) else: giver.msg(f"You throw {moved_obj.name} in the fireplace, destroying it.") return True # def at_post_move(self, source_location, move_type, **kwargs): self.update_hunger(feeder=feeder, amount=300)