2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
Example of scripts.
|
|
|
|
|
|
2012-03-30 23:57:04 +02:00
|
|
|
These are scripts intended for a particular object - the
|
2015-01-07 15:36:32 +01:00
|
|
|
red_button object type in contrib/examples. A few variations
|
2010-08-29 18:46:58 +00:00
|
|
|
on uses of scripts are included.
|
|
|
|
|
|
|
|
|
|
"""
|
2015-01-07 15:36:32 +01:00
|
|
|
from evennia import Script
|
2015-01-09 00:10:18 +01:00
|
|
|
from contrib.tutorial_examples import cmdset_red_button as cmdsetexamples
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Scripts as state-managers
|
2012-03-30 23:57:04 +02:00
|
|
|
#
|
2010-08-29 18:46:58 +00:00
|
|
|
# Scripts have many uses, one of which is to statically
|
|
|
|
|
# make changes when a particular state of an object changes.
|
|
|
|
|
# There is no "timer" involved in this case (although there could be),
|
|
|
|
|
# whenever the script determines it is "invalid", it simply shuts down
|
2012-03-30 23:57:04 +02:00
|
|
|
# along with all the things it controls.
|
|
|
|
|
#
|
2010-08-29 18:46:58 +00:00
|
|
|
# To show as many features as possible of the script and cmdset systems,
|
|
|
|
|
# we will use three scripts controlling one state each of the red_button,
|
2012-03-30 23:57:04 +02:00
|
|
|
# each with its own set of commands, handled by cmdsets - one for when
|
2010-08-29 18:46:58 +00:00
|
|
|
# the button has its lid open, and one for when it is closed and a
|
|
|
|
|
# last one for when the player pushed the button and gets blinded by
|
|
|
|
|
# a bright light. The last one also has a timer component that allows it
|
2012-03-30 23:57:04 +02:00
|
|
|
# to remove itself after a while (and the player recovers their eyesight).
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
class ClosedLidState(Script):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
This manages the cmdset for the "closed" button state. What this
|
2010-08-29 18:46:58 +00:00
|
|
|
means is that while this script is valid, we add the RedButtonClosed
|
|
|
|
|
cmdset to it (with commands like open, nudge lid etc)
|
|
|
|
|
"""
|
|
|
|
|
def at_script_creation(self):
|
|
|
|
|
"Called when script first created."
|
|
|
|
|
self.desc = "Script that manages the closed-state cmdsets for red button."
|
2012-03-30 23:57:04 +02:00
|
|
|
self.persistent = True
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def at_start(self):
|
|
|
|
|
"""
|
|
|
|
|
This is called once every server restart, so we want to add the
|
|
|
|
|
(memory-resident) cmdset to the object here. is_valid is automatically
|
|
|
|
|
checked so we don't need to worry about adding the script to an
|
2012-03-30 23:57:04 +02:00
|
|
|
open lid.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
#All we do is add the cmdset for the closed state.
|
|
|
|
|
self.obj.cmdset.add(cmdsetexamples.LidClosedCmdSet)
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def is_valid(self):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
The script is only valid while the lid is closed.
|
2010-08-29 18:46:58 +00:00
|
|
|
self.obj is the red_button on which this script is defined.
|
|
|
|
|
"""
|
|
|
|
|
return not self.obj.db.lid_open
|
|
|
|
|
|
|
|
|
|
def at_stop(self):
|
|
|
|
|
"""
|
|
|
|
|
When the script stops we must make sure to clean up after us.
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
self.obj.cmdset.delete(cmdsetexamples.LidClosedCmdSet)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpenLidState(Script):
|
|
|
|
|
"""
|
|
|
|
|
This manages the cmdset for the "open" button state. This will add
|
|
|
|
|
the RedButtonOpen
|
|
|
|
|
"""
|
|
|
|
|
def at_script_creation(self):
|
2012-03-30 23:57:04 +02:00
|
|
|
"Called when script first created."
|
2010-08-29 18:46:58 +00:00
|
|
|
self.desc = "Script that manages the opened-state cmdsets for red button."
|
2012-03-30 23:57:04 +02:00
|
|
|
self.persistent = True
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def at_start(self):
|
|
|
|
|
"""
|
|
|
|
|
This is called once every server restart, so we want to add the
|
2012-03-30 23:57:04 +02:00
|
|
|
(memory-resident) cmdset to the object here. is_valid is
|
|
|
|
|
automatically checked, so we don't need to worry about
|
2010-08-29 18:46:58 +00:00
|
|
|
adding the cmdset to a closed lid-button.
|
|
|
|
|
"""
|
|
|
|
|
#print "In Open at_start (should add cmdset)"
|
|
|
|
|
self.obj.cmdset.add(cmdsetexamples.LidOpenCmdSet)
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def is_valid(self):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
The script is only valid while the lid is open.
|
2010-08-29 18:46:58 +00:00
|
|
|
self.obj is the red_button on which this script is defined.
|
|
|
|
|
"""
|
|
|
|
|
return self.obj.db.lid_open
|
|
|
|
|
|
|
|
|
|
def at_stop(self):
|
|
|
|
|
"""
|
|
|
|
|
When the script stops (like if the lid is closed again)
|
|
|
|
|
we must make sure to clean up after us.
|
|
|
|
|
"""
|
|
|
|
|
self.obj.cmdset.delete(cmdsetexamples.LidOpenCmdSet)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlindedState(Script):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
This is a timed state.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
This adds a (very limited) cmdset TO THE PLAYER, during a certain time,
|
2012-03-30 23:57:04 +02:00
|
|
|
after which the script will close and all functions are
|
2010-08-29 18:46:58 +00:00
|
|
|
restored. It's up to the function starting the script to actually
|
2012-03-30 23:57:04 +02:00
|
|
|
set it on the right player object.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
def at_script_creation(self):
|
|
|
|
|
"""
|
|
|
|
|
We set up the script here.
|
|
|
|
|
"""
|
|
|
|
|
self.key = "temporary_blinder"
|
|
|
|
|
self.desc = "Temporarily blinds the player for a little while."
|
2013-11-14 19:31:17 +01:00
|
|
|
self.interval = 20 # seconds
|
|
|
|
|
self.start_delay = True # we don't want it to stop until after 20s.
|
|
|
|
|
self.repeats = 1 # this will go away after interval seconds.
|
|
|
|
|
self.persistent = False # we will ditch this if server goes down
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def at_start(self):
|
|
|
|
|
"""
|
|
|
|
|
We want to add the cmdset to the linked object.
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
Note that the RedButtonBlind cmdset is defined to completly
|
|
|
|
|
replace the other cmdsets on the stack while it is active
|
|
|
|
|
(this means that while blinded, only operations in this cmdset
|
|
|
|
|
will be possible for the player to perform). It is however
|
|
|
|
|
not persistent, so should there be a bug in it, we just need
|
|
|
|
|
to restart the server to clear out of it during development.
|
|
|
|
|
"""
|
|
|
|
|
self.obj.cmdset.add(cmdsetexamples.BlindCmdSet)
|
|
|
|
|
|
|
|
|
|
def at_stop(self):
|
|
|
|
|
"""
|
|
|
|
|
It's important that we clear out that blinded cmdset
|
2012-03-30 23:57:04 +02:00
|
|
|
when we are done!
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-10-19 01:39:25 +00:00
|
|
|
self.obj.msg("You blink feverishly as your eyesight slowly returns.")
|
2012-03-30 23:57:04 +02:00
|
|
|
self.obj.location.msg_contents("%s seems to be recovering their eyesight."
|
|
|
|
|
% self.obj.name,
|
2010-08-29 18:46:58 +00:00
|
|
|
exclude=self.obj)
|
2013-11-14 19:31:17 +01:00
|
|
|
self.obj.cmdset.delete() # this will clear the latest added cmdset,
|
|
|
|
|
# (which is the blinded one).
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Timer/Event-like Scripts
|
|
|
|
|
#
|
2012-03-30 23:57:04 +02:00
|
|
|
# Scripts can also work like timers, or "events". Below we
|
2010-08-29 18:46:58 +00:00
|
|
|
# define three such timed events that makes the button a little
|
2012-03-30 23:57:04 +02:00
|
|
|
# more "alive" - one that makes the button blink menacingly, another
|
|
|
|
|
# that makes the lid covering the button slide back after a while.
|
2010-08-29 18:46:58 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
class CloseLidEvent(Script):
|
|
|
|
|
"""
|
|
|
|
|
This event closes the glass lid over the button
|
|
|
|
|
some time after it was opened. It's a one-off
|
2012-03-30 23:57:04 +02:00
|
|
|
script that should be started/created when the
|
|
|
|
|
lid is opened.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
def at_script_creation(self):
|
|
|
|
|
"""
|
|
|
|
|
Called when script object is first created. Sets things up.
|
|
|
|
|
We want to have a lid on the button that the user can pull
|
2012-03-30 23:57:04 +02:00
|
|
|
aside in order to make the button 'pressable'. But after a set
|
2010-08-29 18:46:58 +00:00
|
|
|
time that lid should auto-close again, making the button safe
|
2012-03-30 23:57:04 +02:00
|
|
|
from pressing (and deleting this command).
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
self.key = "lid_closer"
|
|
|
|
|
self.desc = "Closes lid on a red buttons"
|
2013-11-14 19:31:17 +01:00
|
|
|
self.interval = 20 # seconds
|
|
|
|
|
self.start_delay = True # we want to pospone the launch.
|
|
|
|
|
self.repeats = 1 # we only close the lid once
|
|
|
|
|
self.persistent = True # even if the server crashes in those 20 seconds,
|
|
|
|
|
# the lid will still close once the game restarts.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def is_valid(self):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
This script can only operate if the lid is open; if it
|
2010-08-29 18:46:58 +00:00
|
|
|
is already closed, the script is clearly invalid.
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
Note that we are here relying on an self.obj being
|
2012-03-30 23:57:04 +02:00
|
|
|
defined (and being a RedButton object) - this we should be able to
|
|
|
|
|
expect since this type of script is always tied to one individual
|
2010-08-29 18:46:58 +00:00
|
|
|
red button object and not having it would be an error.
|
|
|
|
|
"""
|
|
|
|
|
return self.obj.db.lid_open
|
|
|
|
|
|
|
|
|
|
def at_repeat(self):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
Called after self.interval seconds. It closes the lid. Before this method is
|
|
|
|
|
called, self.is_valid() is automatically checked, so there is no need to
|
2010-08-29 18:46:58 +00:00
|
|
|
check this manually.
|
|
|
|
|
"""
|
|
|
|
|
self.obj.close_lid()
|
|
|
|
|
|
|
|
|
|
class BlinkButtonEvent(Script):
|
2012-03-30 23:57:04 +02:00
|
|
|
"""
|
|
|
|
|
This timed script lets the button flash at regular intervals.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
def at_script_creation(self):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
Sets things up. We want the button's lamp to blink at
|
2010-08-29 18:46:58 +00:00
|
|
|
regular intervals, unless it's broken (can happen
|
2012-03-30 23:57:04 +02:00
|
|
|
if you try to smash the glass, say).
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
self.key = "blink_button"
|
|
|
|
|
self.desc = "Blinks red buttons"
|
2013-11-14 19:31:17 +01:00
|
|
|
self.interval = 35 #seconds
|
|
|
|
|
self.start_delay = False #blink right away
|
|
|
|
|
self.persistent = True #keep blinking also after server reboot
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def is_valid(self):
|
|
|
|
|
"""
|
|
|
|
|
Button will keep blinking unless it is broken.
|
|
|
|
|
"""
|
|
|
|
|
#print "self.obj.db.lamp_works:", self.obj.db.lamp_works
|
|
|
|
|
return self.obj.db.lamp_works
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def at_repeat(self):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
Called every self.interval seconds. Makes the lamp in
|
2010-08-29 18:46:58 +00:00
|
|
|
the button blink.
|
|
|
|
|
"""
|
|
|
|
|
self.obj.blink()
|
|
|
|
|
|
|
|
|
|
class DeactivateButtonEvent(Script):
|
|
|
|
|
"""
|
2012-03-30 23:57:04 +02:00
|
|
|
This deactivates the button for a short while (it won't blink, won't
|
2010-08-29 18:46:58 +00:00
|
|
|
close its lid etc). It is meant to be called when the button is pushed
|
|
|
|
|
and run as long as the blinded effect lasts. We cannot put these methods
|
|
|
|
|
in the AddBlindedCmdSet script since that script is defined on the *player*
|
2012-03-30 23:57:04 +02:00
|
|
|
whereas this one must be defined on the *button*.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
def at_script_creation(self):
|
|
|
|
|
"""
|
|
|
|
|
Sets things up.
|
|
|
|
|
"""
|
|
|
|
|
self.key = "deactivate_button"
|
|
|
|
|
self.desc = "Deactivate red button temporarily"
|
2013-11-14 19:31:17 +01:00
|
|
|
self.interval = 21 #seconds
|
|
|
|
|
self.start_delay = True # wait with the first repeat for self.interval seconds.
|
2012-03-30 23:57:04 +02:00
|
|
|
self.persistent = True
|
2013-11-14 19:31:17 +01:00
|
|
|
self.repeats = 1 # only do this once
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
def at_start(self):
|
|
|
|
|
"""
|
|
|
|
|
Deactivate the button. Observe that this method is always
|
|
|
|
|
called directly, regardless of the value of self.start_delay
|
|
|
|
|
(that just controls when at_repeat() is called)
|
|
|
|
|
"""
|
|
|
|
|
# closing the lid will also add the ClosedState script
|
2011-06-24 20:12:59 +00:00
|
|
|
self.obj.close_lid()
|
2012-03-30 23:57:04 +02:00
|
|
|
# lock the lid so other players can't access it until the
|
2010-08-29 18:46:58 +00:00
|
|
|
# first one's effect has worn off.
|
2012-03-30 23:57:04 +02:00
|
|
|
self.obj.db.lid_locked = True
|
2010-08-29 18:46:58 +00:00
|
|
|
# breaking the lamp also sets a correct desc
|
|
|
|
|
self.obj.break_lamp(feedback=False)
|
|
|
|
|
|
|
|
|
|
def at_repeat(self):
|
|
|
|
|
"""
|
|
|
|
|
When this is called, reset the functionality of the button.
|
|
|
|
|
"""
|
|
|
|
|
# restore button's desc.
|
2012-03-30 23:57:04 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
self.obj.db.lamp_works = True
|
|
|
|
|
desc = "This is a large red button, inviting yet evil-looking. "
|
2012-03-30 23:57:04 +02:00
|
|
|
desc += "Its glass cover is closed, protecting it."
|
|
|
|
|
self.db.desc = desc
|
2010-08-29 18:46:58 +00:00
|
|
|
# re-activate the blink button event.
|
|
|
|
|
self.obj.scripts.add(BlinkButtonEvent)
|
|
|
|
|
# unlock the lid
|
|
|
|
|
self.obj.db.lid_locked = False
|
|
|
|
|
self.obj.scripts.validate()
|