Source for file DpLiving.php
Documentation is available at DpLiving.php
* An object which is "alive", common code shared between users and NPCs
* DutchPIPE version 0.4; PHP version 5
* LICENSE: This source file is subject to version 1.0 of the DutchPIPE license.
* If you did not receive a copy of the DutchPIPE license, you can obtain one at
* http://dutchpipe.org/license/1_0.txt or by sending a note to
* license@dutchpipe.org, in which case you will be mailed a copy immediately.
* @subpackage dpuniverse_std
* @author Lennert Stock <ls@dutchpipe.org>
* @copyright 2006, 2007 Lennert Stock
* @license http://dutchpipe.org/license/1_0.txt DutchPIPE License
* @version Subversion: $Id: DpLiving.php 311 2007-09-03 12:48:09Z ls $
* @link http://dutchpipe.org/manual/package/DutchPIPE
* Builts upon the standard DpObject class
* An object which is "alive", common code shared between users and NPCs
* Creates the following DutchPIPE properties:<br />
* - boolean <b>isLiving</b> - Set to TRUE
* - string <b>displayMode</b> - "graphical" or "abstract"
* - integer <b>sessionAge</b> - Age in seconds of this object
* - integer <b>weightCarry</b> - Combined weight of objects in our inventory
* - integer <b>maxWeightCarry</b> - Maximum weight this object can carry
* - integer <b>volumeCarry</b> - Combined volume of objects in our inventory
* - integer <b>maxVolumeCarry</b> - Maximum volume this object can carry
* - string <b>actionFailure</b> - Last action error message, "Read what?"
* - string <b>actionDefaultFailure</b> - Default action error message, "What?"
* - int <b>lastActionTime</b> - UNIX time stamp of last action performed
* - string <b>inputMode</b> - Input area mode, 'say' or 'cmd'
* @subpackage dpuniverse_std
* @author Lennert Stock <ls@dutchpipe.org>
* @copyright 2006, 2007 Lennert Stock
* @license http://dutchpipe.org/license/1_0.txt DutchPIPE License
* @version Release: 0.2.1
* @link http://dutchpipe.org/manual/package/DutchPIPE
* Creates this living object
* Called by DpObject when this object is created. Adds standard actions
* which can be performed by this object, usually a user or a computer
* Calls {@link createDpLiving()} in the inheriting class.
* Starts a "heartbeat", see {@link timeoutHeartBeat()}.
* @see createDpLiving(), timeoutHeartBeat()
$this->isDraggable = FALSE;
$this->weight = 70000; /* Grams */
$this->weight = 2458; /* Ounces */
* Sets the message shown to the user when an action fails
* Call this method from action methods, for example actionFoo(). When the
* user performs an action but the action fails, there are two ways for the
* method implementing the action to communicate the failure to the user.
* Call {@link DpUser:tell()} in the user and return TRUE, for
*$user->tell('Action foo failed because of bar.');
* The action system will stop looking for other ways to perform the
* Call this setActionFailure method in the user and return FALSE. This
* failure setup is used when different objects can have the same actions
* implemented with addAction - if one fails, another might still
* succeed. Example:<br><br>
*$user->setActionFailure('Action foo failed because of bar.');
* The action system will continue looking for other ways to perform
* the action. If it doesn't find any, the previously set failure message
* is communicated to the user. Otherwise, the next method implementing
* the action takes over. The action system will continue looking for
* object/methods implementing the action, until TRUE is returned or no
* more implementations are found. In that case, the last set action
* failure is returned. If no action failure was set, the default failure
* message is shown, see {@link setActionDefaultFailure()}.
* @param string $actionFailure message to be shown when an action fails
* @example /websites/dutchpipe.org/dpuniverse/obj/note.php A readable note
* @see getActionFailure(), setActionDefaultFailure(),
* getActionDefaultFailure()
* Sets the default message shown to the user when an action fails
* Used when no message was set with setActionFailure, for example "What?".
* @param string $actionDefaultFailure default message to be shown when
* @see setActionFailure(), getActionFailure(),
* getActionDefaultFailure()
$this->setBody(dp_text("This description hasn't been set yet.<br />"));
/* Actions for both NPCs and users */
//$this->addAction(array($this, 'getMenuGiveLabel'), dp_text('give'), 'actionGive', DP_ACTION_OPERANT_METHOD_MENU, DP_ACTION_TARGET_OBJINV, DP_ACTION_AUTHORIZED_ALL, DP_ACTION_SCOPE_SELF);
* Sets this living object up at the time it is created
* An empty function which can be redefined by the living class extending
* on DpLiving. When the object is created, it has no title, HTML body, et
* cetera, so in this method methods like {@link DpObject::setTitle()} are
* called. Building blocks extending on DpLiving may define their own create
* function. For example, DpNpc defines {@link DpNpc:createDpNpc}.
* Resets this living object
* Called by DpObject at regular intervals as defined in dpuniverse-ini.php.
* Calls the method 'resetDpLiving' in this living object. You can redefine
* that function to periodically do stuff such as alter the state of this
* Resets this living object
* Called by this living object at regular intervals as defined in
* dpuniverse-ini.php. An empty function which can be redefined by the
* living class extending on DpLiving. To be used to periodically do stuff
* such as alter the state of the living object.
* Called when certain events occur, given with $name.
* Calls the method 'eventDpLiving' in this living object.
* @param object $name Name of event
* @param mixed $args One or more arguments, depends on event
* Called when certain events occur, given with $name.
* @param object $name Name of event
* @param mixed $args One or more arguments, depends on event
* Calls itself every "heartbeat"
* Redefine this method to make timed stuff happen.
* Gets the livings's age since it was created as an object in a string
* Returns the livings's age in a format like "6 hours and 14 minutes".
* @return string livings's age
* @see DpUser::getInactive(), DpUser::isInactive(), DpUser::getStatus()
* Tries to perform the action given by the living object
* Called by the system to handle both actions performed by clicking on
* menus and actions from the input area. The first method will result in an
* $action parameter such as "take object_242" (using an unique object id).
* With the second method, $action will contain what the user typed, such as
* Searches and calls for the right method linked to the action, which could
* be in another object. Handles failure messages when the action failed.
* This method can also be used to force living objects to perform actions.
* @param string $action the action given by the living object
* @return boolean TRUE for success, FALSE for unsuccessful action
* @see setActionFailure(), getActionFailure(), setActionDefaultFailure(),
* getActionDefaultFailure(), DpObject::addAction(),
* DpObject::getActionData(), DpObject::getActionsMenu(),
* DpObject::getTargettedActions(),
* DpObject::performActionSubject()
global $grCurrentDpObject;
$this->lastActionTime = !isset ($this->lastActionTime)
$grCurrentDpObject = $this;
$action_failure = $this->getActionFailure();
if (!isset ($this->_GET['menuaction'])) {
$this->tell($action_failure);
$this->setActionFailure($this->getActionDefaultFailure());
$grCurrentDpObject = FALSE;
if (isset ($ob->weight)) {
if (isset ($ob->volume)) {
* Makes this living object examine an object
* @param string $verb the action, "examine"
* @param string $noun what to examine, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
$this->setActionFailure($verb == dp_text('look')
if (!($ob = $env->isPresent($noun))
&& !($description = $env->getItemDescription($noun))) {
dp_text('There is no %s here.<br />'), $noun));
$description = $ob->getAppearance(0, TRUE, NULL, $this->displayMode,
$this->tell("<window>$description</window>");
* Makes this living object take an object
* @param string $verb the action, "take"
* @param string $noun what to take, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
$inv = $env->getInventory();
if (!isset ($ob->isLiving) || TRUE !== $ob->isLiving) {
$result = $ob->moveDpObject($this);
if (E_MOVEOBJECT_HEAVY === $result) {
$this->tell(dp_text("You can't carry more weight, drop something first.<br />"));
$this->tell(dp_text("You can't carry more volume, drop something first.<br />"));
if (FALSE === $picked_up) {
$this->tell(dp_text('There is nothing to pick up here.<br />'));
if (FALSE === ($ob = $env->isPresent($noun))) {
dp_text('There is no %s here.<br />'), $noun));
if (isset ($ob->isLiving) && TRUE === $ob->isLiving) {
dp_text('%s refuses to be taken.<br />'), $ob->getTitle())));
$noun, $matches))) && $matches[1] > 0
&& $matches[1] < $ob->amount) {
$result = $ob->moveDpObject($this, FALSE, $matches[1]);
$title_definite = $title_indefinite = $noun;
$result = $ob->moveDpObject($this);
if (E_MOVEOBJECT_HEAVY === $result) {
dp_text("You can't carry more weight, drop something first.<br />")));
dp_text("You can't carry more volume, drop something first.<br />")));
* Drops an object in the user's environment
* @param string $verb the action, "drop"
* @param string $noun what to drop, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
function actionDrop($verb, $noun, $silently = FALSE)
if (FALSE === $silently) {
$this->setActionFailure(dp_text('Drop what?<br />'));
if (FALSE === $silently) {
$this->tell(dp_text('You have nothing to drop.<br />'));
if (FALSE === $silently) {
if (FALSE === ($ob = $this->isPresent($noun))) {
if (FALSE === $silently) {
dp_text('There is no %s here.<br />'), $noun));
$noun, $matches))) && $matches[1] > 0
&& $matches[1] < $ob->amount) {
$ob->moveDpObject($env, FALSE, $matches[1]);
$title_definite = $title_indefinite = $noun;
if (FALSE === $silently) {
$title_indefinite)), $this);
* Shows this living object a list of objects it is carrying
* @param string $verb the action, "inventory"
* @param string $noun empty string
* @return boolean TRUE for action completed, FALSE otherwise
$this->displayMode, - 1, 'dpobinv');
$carrying_str = dp_text('You are carrying:');
$inventory = str_replace($carrying_str, "<b>$carrying_str</b>",
$this->tell("<window name=\"inventory\">$inventory</window>");
* Makes this living object say something
* @param string $verb the action, "say"
* @param string $noun what to say, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
//echo "$verb: THIS: {$this->title} DEFINED: {$defined_by->title}; TARGET: " . $target->title . "; PERFORMER: " . $performer->title . "\n";
return $defined_by === $performer
: array(dp_text('emotions'), $verb);
return $defined_by->title;
* Completes the give action performed by clicking on an object
* Called by the action system when someone clicks on an object and selects
* "give...". Returns something like "beer to", which allows the system to
* fill the input area with "give beer to " using the title of the object
* @param string $verb the action, "give"
* @return string a string such as "beer to "
$inv = $env->getInventory();
if ($ob->isLiving && $ob !== $user) {
$menu[$to_title] = sprintf($rval, $ob_title, $to_title);
return !count($menu) ? FALSE : $menu;
} elseif ($verb === dp_text('tell')) {
foreach ($users as &$u) {
$menu[$to_title] = sprintf($rval, $to_title);
return !count($menu) ? FALSE : $menu;
$inv = $env->getInventory();
$inv = $env->getInventory();
return dp_text("<span style='color: #999'>give to...</span>");
* Makes this living object give an object to another living object
* @param string $verb the action, "give"
* @param string $noun what and who to give, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
* @see actionGiveOperant()
$this->Tell(dp_text('Give what to who?<br />'));
if (!is_object($env) || !($who_ob = $env->isPresent($who))) {
$what_ob->moveDpObject($who_ob);
* Makes this living object tell something to another user object
* @param string $verb the action, "tell"
* @param string $noun who and what to tell, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
if (FALSE === ($pos = dp_strpos($noun, ' '))) {
$this->Tell(dp_text('Tell who what?<br />'));
* Makes this living object shout something to everyone on the site
* @param string $verb the action, "shout"
* @param string $noun what to shout, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
$env->getTitle(), $noun));
foreach ($users as &$u) {
* Makes this living object smile happily
* @param string $verb the action, "smile"
* @param string $noun empty string
* Makes this living object grin evilly
* @param string $verb the action, "grin"
* @param string $noun empty string
* Makes this living object fall down on the floor laughing
* @param string $verb the action, "laugh"
* @param string $noun empty string
$this->tell(dp_text('You fall down on the floor laughing.<br />'));
dp_text('%s falls down on the floor laughing.<br />'),
* Makes this living object cheer wildly
* @param string $verb the action, "cheer"
* @param string $noun who to cheer on, could be empty
$this->tell(dp_text('You jump up and down cheering.<br />'));
dp_text('%s jumps up and down cheering.<br />'),
($noun && !($dest_ob = $env->isPresent($noun)))) {
$this->setActionFailure(dp_text('Cheer on who?<br />'));
dp_text('You jump up and down cheering on %s.<br />'),
dp_text('%s jumps up and down cheering on %s.<br />'),
dp_text('%s jumps up and down cheering you on.<br />'),
* Makes this living object nod solemnly
* @param string $verb the action, "nod"
* @param string $noun empty string
* Makes this living object shrug
* @param string $verb the action, "laugh"
* @param string $noun empty string
* Makes this living object pat another living object on the head
* @param string $verb the action, "pat"
* @param string $noun who to pat, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
($noun && !($dest_ob = $env->isPresent($noun)))) {
$this->setActionFailure(dp_text('Pat who?<br />'));
dp_text('You pat %s on the head with a bone-crushing sound.<br />'),
dp_text('%s pats %s on the head with a bone-crushing sound.<br />'),
dp_text('%s pats you on the head with a bone-crushing sound.<br />'),
* Makes this living object slap a high-five with another living object
* @param string $verb the action, "high5"
* @param string $noun who to high5, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
($noun && !($dest_ob = $env->isPresent($noun)))) {
$this->setActionFailure(dp_text('High5 who?<br />'));
dp_text('You jump up, and slap a thundering high-five with %s.<br />'),
dp_text('%s jumps up, and slaps a thundering high-five with %s.<br />'),
dp_text('%s jumps up, and slaps a thundering high-five with you.<br />'),
* Makes this living object hug another living object
* @param string $verb the action, "hug"
* @param string $noun who to hug, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
($noun && !($dest_ob = $env->isPresent($noun)))) {
$this->setActionFailure(dp_text('Hug who?<br />'));
* Makes this living object give another living object a passionate kiss
* @param string $verb the action, "kiss"
* @param string $noun who to kiss, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
($noun && !($dest_ob = $env->isPresent($noun)))) {
$this->setActionFailure(dp_text('Kiss who?<br />'));
dp_text('You give %s a deep and passionate kiss... It seems to last forever...<br />'),
dp_text('%s gives %s a deep and passionate kiss... It seems to last forever...<br />'),
dp_text('%s gives you a deep and passionate kiss... It seems to last forever...<br />'),
* Makes this living object take another living object for a dance
* @param string $verb the action, "dance"
* @param string $noun who to dance with, could be empty
* @return boolean TRUE for action completed, FALSE otherwise
($noun && !($dest_ob = $env->isPresent($noun)))) {
$this->setActionFailure(dp_text('Dance with who?<br />'));
dp_text('You take %s for a dance... The tango!<br />'),
dp_text('%s takes %s for a dance... The tango!<br />'),
dp_text('%s takes you for a dance... The tango!<br />'),
* Makes this living object communicate a custom message to its environment
* @param string $verb the action, "emote"
* @param string $noun string to "emote"
* @return boolean TRUE for action completed, FALSE otherwise
$this->setActionFailure(dp_text('Try: emote <i>text</i><br />'));
. " $noun<br />", $this);
|