moss-n-puddles/typeclasses/puzzles.py
Howard Abrams 2719bce82b Fix numerous bugs
Finally fixed the 'alias' bug that has plaqued me.
2025-08-12 13:55:18 -07:00

99 lines
3.3 KiB
Python
Executable file

#!/usr/bin/env python
from os.path import exists
from datetime import datetime
from random import randint
from re import split, match
from shutil import copyfile
from evennia import CmdSet
from evennia.utils import logger, delay
from commands.command import Command
from typeclasses.objects import Object, Listener
from typeclasses.characters import Character
from utils.word_list import routput
class StoryCube(Object, Listener):
"""
An invisible puppet to respond to situations.
@create/drop cube1:typeclasses.puppets.StoryCube
@desc cube1 = Description for Admins
@lock cube1 = view:false() # to make it invisible
"""
def return_appearance(self, caller):
return "What?"
class Changling(StoryCube):
"""
Object that changes appearance every time someone looks at it.
Important Attribute:
desc_prefix (string): The initial part of the description.
This can be an empty string.
desc_postfix (string): The final part of the description.
This can be an empty string.
descs (list): list of descriptions. One of these is
picked randomly when this object is looked at and its index
in the list is used as a key for to solve the puzzle.
"""
def at_object_creation(self):
"""Called when object is created."""
super().at_object_creation()
self.db.desc_prefix = ""
self.db.desc_postfix = ""
self.db.descs = ["You don't see anything particular."]
def return_appearance(self, caller):
"""
This hook is called by the look command to get the description
of the object. We overload it with our own version.
"""
def optional(text, prefix=False):
if text and text != "":
return ("" if prefix else " ") + \
routput(text) + \
(" " if prefix else "")
else:
return ""
# Randomly get the index for one of the descriptions:
descs = self.db.descs
clueindex = randint(0, len(descs) - 1)
# Remember the last view as it could be a clue for later:
key_id = self.key.lower().replace(" ", "_") + "_view_index"
if self.attributes.get("desc_first_prefix") and \
not caller.nattributes.get(key_id):
prefix = optional(self.db.desc_first_prefix, True)
else:
prefix = optional(self.db.desc_prefix, True)
# set this description, with the random extra
self.db.desc = prefix + descs[clueindex] + optional(self.db.desc_postfix)
caller.nattributes.add(key=key_id, value=clueindex)
# call the parent function as normal (this will use
# the new desc Attribute we just set)
# return super().return_appearance(caller)
return self.db.desc
def at_object_receive(self, obj, giver, **kwargs):
"""
Workaround for a particular puzzle. Perhaps we can get rid of this.
"""
if obj.key == 'conch':
return super().at_object_receive(obj, giver)
obj.move_to(self.location, quiet=True, move_type="drop")
delay(2, giver.announce_action,
f"Soon after $you() $conj(place) a {obj.name} in the open cavity of the obelisk, it falls to the floor.")
return True