ring.c and ringfuncs.c - examples for course 05

[previous example] [course05] [Table of contents] [next example]
/* Filename      : ring.c
 * Description   : a magical ring with spellpower
 *
 * written       : 02-10-1996 - Gunner
 * last modified : 26-05-1998 - Gunner
 * HTML-Version  : 02-02-2000 - Ghorwin
 */

#include <mudlib.h>
inherit I_ARMOUR;

#define FUNC "/doc/crashcourse/course05/ringfuncs"

create()
{
    ::create();
    set_light(1);
    set_name("ruby ring");
    add_id(({"ring","shining ring"}));
    set_short("A ruby ring");
    set_long("It's a shining ring made out of ruby.\n");
    set_weight(1);
    set_ac(1);
    set_type("ring");
    set_value(200+random(100));

    // hidden = when sold in a shop, the item vanishes
    // stone = It's made out of some kind of stone
    add_property(({"hidden","stone"}));
    set_info("This ring will keep account of your killings. But, "
        "you may only wear it if you have the right kind of soul.\n");

    // Let's check out a blocking hook this time
    add_hook("__bwear",(:call_other,FUNC,"wear_ring",this_object():));
    add_hook("__remove",(:call_other,FUNC,"remove_ring",this_object():));

    replace_program(I_ARMOUR);
}

ringfuncs.c


/* Filename      : ringfuncs.c
 * Description   : the functionfile for ring.c
 *
 * written       : 02-10-1996 - Gunner
 * last modified : 05-11-1997 - Gunner
 * HTML-Version  : 02-02-2000 - Ghorwin
 */

#include <mudlib.h>
#include "/doc/crashcourse/defs.h"
inherit I_DAEMON;

/* 
 * wear_ring : This function is called whenever someone tries to wear
 *             the ring.
 */
int
wear_ring(object ring)
{
    object wearer = environment(ring);

    // Only players with nasty or worse alignment may wear it
    if (wearer->query_alignment() > -40)
    {
        tell_object(TP,"The ring just doesn't fit on your finger.\n");
        return 1;
    }

    // Let's add a kill hook in the player
    wearer->add_hook("__kill",(:call_other,TO,"player_kill",wearer:));

    tell_object(wearer,"As you wear the ring, you feel a slight tingeling "
         "sensation in your finger.\n");

    return 0; // To allow the wear to complete.
}

/* 
 * player_kill : The player has killed something! Cheers!
 */
void
player_kill(object player)
{
    player->add_tmp_prop("ring_kills",1);
}

/* 
 * remove_ring : The player removes the ring and the ring tells him/her/it
 *               how many kills he/she/it has made
 */
void
remove_ring(object ring)
{
    int kills = 0;
    object wearer = environment(ring);

    kills = wearer->query_tmp_prop("ring_kills");
    wearer->remove_tmp_prop("ring_kills");

    tell_object(wearer,"The ring shivers slightly as you hear a voice "
        "inside your head: 'You have taken " + kills + " lives!'\n");
}