From af1655fadb59e1fcbf72eb5bc3225ceaaed4854b Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Thu, 30 Jan 2025 21:35:38 -0800 Subject: [PATCH] Multiple chairs and a fire bugfix --- typeclasses/pets.py | 16 +++++++++--- typeclasses/sittables.py | 55 +++++++++++++++++++++++++++++++++++----- utils/word_list.py | 40 +++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 11 deletions(-) create mode 100755 utils/word_list.py diff --git a/typeclasses/pets.py b/typeclasses/pets.py index d341355..4983672 100755 --- a/typeclasses/pets.py +++ b/typeclasses/pets.py @@ -9,6 +9,7 @@ Each level of pet requires more aspects for interaction. from typeclasses.objects import Object from commands.feedables import CmdFeed, CmdFeedSet +from utils.word_list import squish from evennia import ( TICKER_HANDLER, @@ -119,8 +120,9 @@ class Pet(Object): """ curr = self.hunger() amount = kwargs.get('amount', self.hungry_rate) - self.db.hunger_level = self.db.hunger_level + amount + if self.db.hunger_level < 0: + self.db.hunger_level = 0 if self.hunger() != curr: self.location.msg_contents("|w%s|n\n" % self.hunger_appearance()) @@ -162,7 +164,7 @@ class Fire(Pet): } def feed(self, feeder, args): - super().update_hunger(feeder, amount=300) + self.update_hunger(feeder=feeder, amount=300) now = time() last_fed = feeder.ndb.fire_last_fed # could be None @@ -173,6 +175,12 @@ class Fire(Pet): feeder.ndb.fire_last_fed = now - feeder.msg(f"You put some {adj} wood on the fire in the fireplace.") - self.location.msg_contents(f"{feeder.name} gets up and puts {adj} wood on the fire.", + get_up = "" + gets_up = "" + if feeder.db.is_sitting: + get_up = "get up and" + gets_up = "gets up and" + + feeder.msg(squish(f"You {get_up} put some {adj} wood on the fire in the fireplace.")) + self.location.msg_contents(squish(f"{feeder.name} {gets_up} puts {adj} wood on the fire."), exclude=feeder) diff --git a/typeclasses/sittables.py b/typeclasses/sittables.py index e0b9743..819d61d 100755 --- a/typeclasses/sittables.py +++ b/typeclasses/sittables.py @@ -2,11 +2,24 @@ from typeclasses.objects import Object from commands.sittables import CmdSetSit +from utils.word_list import routput class Sittable(Object): + multiple = False + def at_object_creation(self): self.cmdset.add_default(CmdSetSit) + def sit_msg(self): + adjective = self.db.adjective or "on" + article = self.db.article or "the" + extra = self.db.extra or "" + + return routput(f"You sit {adjective} {article} {self.key}. {extra}") + + def stand_msg(self): + return f"You stand up from {self.key}." + def do_sit(self, sitter): """ Called when trying to sit on/in this object. @@ -16,17 +29,24 @@ class Sittable(Object): """ adjective = self.db.adjective or "on" + article = self.db.article or "the" current = self.db.sitter + if current: if current == sitter: - sitter.msg(f"You are already sitting {adjective} {self.key}.") - else: - sitter.msg(f"You can't sit {adjective} {self.key} " - f"- {current.key} is already sitting there!") - return + sitter.msg(f"You are already sitting {adjective} {article} {self.key}.") + return + elif not self.multiple: + sitter.msg(f"You can't sit {adjective} {article} {self.key} " + f"- {current.key} is already sitting there!") + return + self.db.sitter = sitter sitter.db.is_sitting = self - sitter.msg(f"You sit {adjective} {self.key}") + sitter.msg(self.sit_msg()) + self.location.msg_contents(f"{sitter.name} sits {adjective} {article} {self.key}.", + exclude=sitter) + def do_stand(self, stander): """ @@ -43,4 +63,25 @@ class Sittable(Object): else: self.db.sitter = None del stander.db.is_sitting - stander.msg(f"You stand up from {self.key}.") + stander.msg(self.stand_msg()) + +class Sittables(Sittable): + multiple = True + + def sit_msg(self): + adjective = self.db.adjective or "on" + article = self.db.article or "the" + default = f"{article} {self.db.name}" + singular = self.db.singular or default + extra = self.db.extra or "" + + aliases = self.aliases.all() + name = aliases[1] # Second alias is singular + + return routput(f"You sit {adjective} {singular}. {extra}") + + def stand_msg(self): + article = self.db.article or "the" + default = f"{article} {self.db.name}" + singular = self.db.singular or default + return f"You stand up from {singular}." diff --git a/utils/word_list.py b/utils/word_list.py new file mode 100755 index 0000000..db11027 --- /dev/null +++ b/utils/word_list.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +import random +import re +import string + +class Token(): + def __init__(self, string): + self.words = ''.join(ch for ch in string if ch not in string.punctuation).split() + + def contains(self, word): + if len(self.words) > 0: + return [True for w in self.words if w == word][0] + else: + return False + +def squish(text): + "Remove series of spaces from the text." + return re.sub(' *', ' ', text) + +def routput(string): + """ + Return string with internal word choices replaced randomly. + For instance, the string: + + 'This feels [|very|quite] [nice|cozy|comfortable].' + + Could return any of the following strings: + + 'This feels quite nice.' + 'This feels cozy.' + 'This feels very comfortable.' + """ + # string = + acc = [] + for s in string.split("["): + choices, *rest = s.split("]") + choice = random.choice(choices.split('|')) + acc = acc + [choice] + rest + return squish(''.join(acc))