"""
Red Button
Griatch - 2011
This is a more advanced example object. It combines functions from
script.examples as well as commands.examples to make an interactive
button typeclass.
Create this button with
create/drop tutorials.red_button.RedButton
Note that you must drop the button before you can see its messages!
## Technical
The button's functionality is controlled by CmdSets that gets added and removed
depending on the 'state' the button is in.
- Lid-closed state: In this state the button is covered by a glass cover and trying
to 'push' it will fail. You can 'nudge', 'smash' or 'open' the lid.
- Lid-open state: In this state the lid is open but will close again after a certain
time. Using 'push' now will press the button and trigger the Blind-state.
- Blind-state: In this mode you are blinded by a bright flash. This will affect your
normal commands like 'look' and help until the blindness wears off after a certain
time.
Timers are handled by persistent delays on the button. These are examples of
`evennia.utils.utils.delay` calls that wait a certain time before calling a method -
such as when closing the lid and un-blinding a character.
"""
import random
from evennia import CmdSet, Command, DefaultObject
from evennia.utils.utils import delay, interactive, repeat
# Commands on the button (not all awailable at the same time)
# Commands for the state when the lid covering the button is closed.
[docs]class CmdPushLidClosed(Command):
"""
Push the red button (lid closed)
Usage:
push button
"""
key = "push button"
aliases = ["push", "press button", "press"]
locks = "cmd:all()"
[docs] def func(self):
"""
This is the version of push used when the lid is closed.
An alternative would be to make a 'push' command in a default cmdset
that is always available on the button and then use if-statements to
check if the lid is open or closed.
"""
self.caller.msg("You cannot push the button = there is a glass lid covering it.")
[docs]class CmdNudge(Command):
"""
Try to nudge the button's lid.
Usage:
nudge lid
This command will have you try to push the lid of the button away.
"""
key = "nudge lid" # two-word command name!
aliases = ["nudge"]
locks = "cmd:all()"
[docs] def func(self):
"""
Nudge the lid. Random chance of success to open it.
"""
rand = random.random()
if rand < 0.5:
self.caller.msg("You nudge at the lid. It seems stuck.")
elif rand < 0.7:
self.caller.msg("You move the lid back and forth. It won't budge.")
else:
self.caller.msg("You manage to get a nail under the lid.")
# self.obj is the button object
self.obj.to_open_state()
[docs]class CmdSmashGlass(Command):
"""
Smash the protective glass.
Usage:
smash glass
Try to smash the glass of the button.
"""
key = "smash glass"
aliases = ["smash lid", "break lid", "smash"]
locks = "cmd:all()"
[docs] def func(self):
"""
The lid won't open, but there is a small chance of causing the lamp to
break.
"""
rand = random.random()
self.caller.location.msg_contents(
f"{self.caller.name} tries to smash the glass of the button.", exclude=self.caller
)
if rand < 0.2:
string = (
"You smash your hand against the glass"
" with all your might. The lid won't budge"
" but you cause quite the tremor through the button's mount."
"\nIt looks like the button's lamp stopped working for the time being, "
"but the lid is still as closed as ever."
)
# self.obj is the button itself
self.obj.break_lamp()
elif rand < 0.6:
string = "You hit the lid hard. It doesn't move an inch."
else:
string = (
"You place a well-aimed fist against the glass of the lid."
" Unfortunately all you get is a pain in your hand. Maybe"
" you should just try to just ... open the lid instead?"
)
self.caller.msg(string)
[docs]class CmdOpenLid(Command):
"""
open lid
Usage:
open lid
"""
key = "open lid"
aliases = ["open button"]
locks = "cmd:all()"
[docs] def func(self):
"simply call the right function."
if self.obj.db.lid_locked:
self.caller.msg("This lid seems locked in place for the moment.")
return
string = "\nA ticking sound is heard, like a winding mechanism. Seems "
string += "the lid will soon close again."
self.caller.msg(string)
self.caller.location.msg_contents(
f"{self.caller.name} opens the lid of the button.", exclude=self.caller
)
self.obj.to_open_state()
[docs]class LidClosedCmdSet(CmdSet):
"""
A simple cmdset tied to the redbutton object.
It contains the commands that launches the other
command sets, making the red button a self-contained
item (i.e. you don't have to manually add any
scripts etc to it when creating it).
Note that this is given with a `key_mergetype` set. This
is set up so that the cmdset with merge with Union merge type
*except* if the other cmdset to merge with is LidOpenCmdSet,
in which case it will Replace that. So these two cmdsets will
be mutually exclusive.
"""
key = "LidClosedCmdSet"
[docs] def at_cmdset_creation(self):
"Populates the cmdset when it is instantiated."
self.add(CmdPushLidClosed())
self.add(CmdNudge())
self.add(CmdSmashGlass())
self.add(CmdOpenLid())
# Commands for the state when the button's protective cover is open - now the
# push command will work. You can also close the lid again.
[docs]class CmdPushLidOpen(Command):
"""
Push the red button
Usage:
push button
"""
key = "push button"
aliases = ["push", "press button", "press"]
locks = "cmd:all()"
@interactive
def func(self):
"""
This version of push will immediately trigger the next button state.
The use of the @interactive decorator allows for using `yield` to add
simple pauses in how quickly a message is returned to the user. This
kind of pause will not survive a server reload.
"""
# pause a little between each message.
self.caller.msg("You reach out to press the big red button ...")
yield (2) # pause 2s before next message
self.caller.msg("\n\n|wBOOOOM! A bright light blinds you!|n")
yield (1) # pause 1s before next message
self.caller.msg("\n\n|xThe world goes dark ...|n")
name = self.caller.name
self.caller.location.msg_contents(
f"{name} presses the button. BOOM! {name} is blinded by a flash!", exclude=self.caller
)
self.obj.blind_target(self.caller)
[docs]class CmdCloseLid(Command):
"""
Close the lid
Usage:
close lid
Closes the lid of the red button.
"""
key = "close lid"
aliases = ["close"]
locks = "cmd:all()"
[docs] def func(self):
"Close the lid"
self.obj.to_closed_state()
# this will clean out scripts dependent on lid being open.
self.caller.msg("You close the button's lid. It clicks back into place.")
self.caller.location.msg_contents(
f"{self.caller.name} closes the button's lid.", exclude=self.caller
)
[docs]class LidOpenCmdSet(CmdSet):
"""
This is the opposite of the Closed cmdset.
Note that this is given with a `key_mergetype` set. This
is set up so that the cmdset with merge with Union merge type
*except* if the other cmdset to merge with is LidClosedCmdSet,
in which case it will Replace that. So these two cmdsets will
be mutually exclusive.
"""
key = "LidOpenCmdSet"
[docs] def at_cmdset_creation(self):
"""Setup the cmdset"""
self.add(CmdPushLidOpen())
self.add(CmdCloseLid())
# Commands for when the button has been pushed and the player is blinded. This
# replaces commands on the player making them 'blind' for a while.
[docs]class CmdBlindLook(Command):
"""
Looking around in darkness
Usage:
look <obj>
... not that there's much to see in the dark.
"""
key = "look"
aliases = ["l", "get", "examine", "ex", "feel", "listen"]
locks = "cmd:all()"
[docs] def func(self):
"This replaces all the senses when blinded."
# we decide what to reply based on which command was
# actually tried
if self.cmdstring == "get":
string = "You fumble around blindly without finding anything."
elif self.cmdstring == "examine":
string = "You try to examine your surroundings, but can't see a thing."
elif self.cmdstring == "listen":
string = "You are deafened by the boom."
elif self.cmdstring == "feel":
string = "You fumble around, hands outstretched. You bump your knee."
else:
# trying to look
string = (
"You are temporarily blinded by the flash. "
"Until it wears off, all you can do is feel around blindly."
)
self.caller.msg(string)
self.caller.location.msg_contents(
f"{self.caller.name} stumbles around, blinded.", exclude=self.caller
)
[docs]class CmdBlindHelp(Command):
"""
Help function while in the blinded state
Usage:
help
"""
key = "help"
aliases = "h"
locks = "cmd:all()"
[docs] def func(self):
"""
Just give a message while blinded. We could have added this to the
CmdBlindLook command too if we wanted to keep things more compact.
"""
self.caller.msg("You are beyond help ... until you can see again.")
[docs]class BlindCmdSet(CmdSet):
"""
This is the cmdset added to the *account* when
the button is pushed.
Since this has mergetype Replace it will completely remove the commands of
all other cmdsets while active. To allow some limited interaction
(pose/say) we import those default commands and add them too.
We also disable all exit-commands generated by exits and
object-interactions while blinded by setting `no_exits` and `no_objs` flags
on the cmdset. This is to avoid the player walking off or interfering with
other objects while blinded. Account-level commands however (channel messaging
etc) will not be affected by the blinding.
"""
key = "BlindCmdSet"
# we want it to completely replace all normal commands
# until the timed script removes it again.
mergetype = "Replace"
# we want to stop the player from walking around
# in this blinded state, so we hide all exits too.
# (channel commands will still work).
no_exits = True # keep player in the same room
no_objs = True # don't allow object commands
[docs] def at_cmdset_creation(self):
"Setup the blind cmdset"
from evennia.commands.default.general import CmdPose, CmdSay
self.add(CmdSay())
self.add(CmdPose())
self.add(CmdBlindLook())
self.add(CmdBlindHelp())
#
# Definition of the object itself
#