iron_chest.c and iron_chest_funcs.c - examples for course 15

[previous example] [course07] [Table of contents]
/* File          : iron_chest.c
 * Description   : example for use of a container
 *
 * written       : 22-Nov-2000 - Ghorwin - Icewind
 * last modified : 06-Mar-2001 - Ghorwin - Icewind
 */

#include <mudlib.h>
#include <container.h>
inherit I_CONTAINER;

#define FUNC "/doc/examples/container/iron_chest_funcs"

void
create() {
    ::create();
    set_name("iron chest");
    set_short("an iron chest");
    set_long("This iron chest looks very heavy and very robust.\n");
    add_id(({"iron chest","chest","heavy chest"}));
    add_property("iron");
    set_value(140);
    set_max_weight(58);     // weight of all items in the chest cannot be > 58
    set_weight(40);         // own weight of the chest without inventory

    set_can_open(TRUE);     // enable opening functionality
    set_def_open(FALSE);

    set_can_lock(TRUE);     // create a lock
    set_def_lock(TRUE);
    set_keycode("key for chest examples");

    // I want to print my own message, when somebody unlocks/opens the chest
    set_msgs(TO_PLAYER, UNLOCK_MSG, 0);
    set_msgs(TO_ROOM, UNLOCK_MSG, 0);
    
    set_msgs(TO_PLAYER, OPEN_MSG, 0);
    set_msgs(TO_ROOM, OPEN_MSG, 0);

    // let some special things happen when these actions take place
    add_hook("__unlock",({ FUNC, "unlock_chest" }) );
    add_hook("__open",({ FUNC, "open_chest" }) );

    replace_program(I_CONTAINER);
}

iron_chest_funcs.c


/* File          : iron_chest_funcs.c
 * Description   : Function file for the iron chest
 *
 * written       : 07-Dez-2000 - Ghorwin - Icewind
 * last modified : 06-Mar-2001 - Ghorwin - Icewind
 */

#include <mudlib.h>
inherit I_DAEMON;

void
unlock_chest(object keyobject) {
    object player = this_player();
    if (!player)
        return;
    tell_object(player, 
        "As you turn the key the lock snaps suddendly open with a loud "
        "crack. Unfortunately it was your key that cracked. But the chest is "
        "unlocked and you really don't want to carry a useless key, don't "
        "you?\n");
    tell_room(environment(player), player->query_name() + " unlocks the iron "
        "chest but unfortunately " + player->query_possessive() + 
        " key breaks with a loud crack.\n", ({ player }) );
    destruct(keyobject);
}

void
open_chest() {
    object player = this_player();
    if (!player)
        return;
    tell_object(player,
        "As you open the lid of the chest suddendly a burning liquid spurt "
        "forth and burns your hands and face.\n");
    tell_room(environment(player), player->query_name() + " opens the iron "
        "chest and suddendly a burning liquid sprays over " + 
        player->query_objective() + ".\n", ({ player }) );
    if (player->query_hp() > 20)
        player->reduce_hit_point(20);
    else
        player->reduce_hit_point(player->query_hp()-1);
}