/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik Strfeldt, Tom Madsen, and Katja Nyboe.    *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Merc Diku Mud, you must comply with   *
 *  both the original Diku license in 'license.doc' as well the Merc       *
 *  license in 'license.txt'.  In particular, you may not remove either of *
 *  these copyright notices.                                               *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 ***************************************************************************/

/***************************************************************************
 *  ROM 2.4 is copyright 1993-1998 Russ Taylor                             *
 *  ROM has been brought to you by the ROM consortium                      *
 *      Russ Taylor (rtaylor@hypercube.org)                                *
 *      Gabrielle Taylor (gtaylor@hypercube.org)                           *
 *      Brian Moore (zump@rom.org)                                         *
 *  By using this code, you have agreed to follow the terms of the         *
 *  ROM license, in the file Rom24/doc/rom.license                         *
 ***************************************************************************/

#if defined(macintosh)
#include <types.h>
#include <ctime>
#else
#include <sys/types.h>
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include "merc.h"
#include "interp.h"

/*
 * The following special functions are available for mobiles.
 */

DECLARE_SPEC_FUN (spec_executioner);
DECLARE_SPEC_FUN (spec_fido);
DECLARE_SPEC_FUN (spec_guard);
DECLARE_SPEC_FUN (spec_janitor);
DECLARE_SPEC_FUN (spec_mayor);
DECLARE_SPEC_FUN (spec_poison);
DECLARE_SPEC_FUN (spec_thief);
DECLARE_SPEC_FUN (spec_nasty);

/* the function table */
const struct spec_type spec_table[] = {
    {"spec_executioner", spec_executioner},
    {"spec_fido", spec_fido},
    {"spec_guard", spec_guard},
    {"spec_janitor", spec_janitor},
    {"spec_mayor", spec_mayor},
    {"spec_poison", spec_poison},
    {"spec_thief", spec_thief},
    {"spec_nasty", spec_nasty},
    {NULL, NULL}
};

/*
 * Given a name, return the appropriate spec fun.
 */
SPEC_FUN *spec_lookup (const char *name)
{
    int i;

    for (i = 0; spec_table[i].name != NULL; i++)
    {
        if (LOWER (name[0]) == LOWER (spec_table[i].name[0])
            && !str_prefix (name, spec_table[i].name))
            return spec_table[i].function;
    }

    return 0;
}

char *spec_name (SPEC_FUN * function)
{
    int i;

    for (i = 0; spec_table[i].function != NULL; i++)
    {
        if (function == spec_table[i].function)
            return spec_table[i].name;
    }

    return NULL;
}


bool spec_nasty (CHAR_DATA * ch)
{
    CHAR_DATA *victim, *v_next;
    long gold;

    if (!IS_AWAKE (ch))
    {
        return FALSE;
    }

    if (ch->position != POS_FIGHTING)
    {
        for (victim = ch->in_room->people; victim != NULL; victim = v_next)
        {
            v_next = victim->next_in_room;
            if (!IS_NPC (victim)
                && (victim->level > ch->level)
                && (victim->level < ch->level + 10))
            {
                if (ch->position != POS_FIGHTING)
                {
                    do_function (ch, &do_murder, victim->name);
                }

                /* should steal some coins right away? :) */
                return TRUE;
            }
        }
        return FALSE;            /*  No one to attack */
    }

    /* okay, we must be fighting.... steal some coins and flee */
    if ((victim = ch->fighting) == NULL)
        return FALSE;            /* let's be paranoid.... */

    switch (number_bits (2))
    {
        case 0:
            act ("$n rips apart your coin purse, spilling your gold!",
                 ch, NULL, victim, TO_VICT);
            act ("You slash apart $N's coin purse and gather his gold.",
                 ch, NULL, victim, TO_CHAR);
            act ("$N's coin purse is ripped apart!",
                 ch, NULL, victim, TO_NOTVICT);
            gold = victim->gold / 10;    /* steal 10% of his gold */
            victim->gold -= gold;
            ch->gold += gold;
            return TRUE;

        case 1:
            do_function (ch, &do_flee, "");
            return TRUE;

        default:
            return FALSE;
    }
}

bool spec_executioner (CHAR_DATA * ch)
{
    char buf[MAX_STRING_LENGTH];
    CHAR_DATA *victim;
    CHAR_DATA *v_next;
    char *crime;

    if (!IS_AWAKE (ch) || ch->fighting != NULL)
        return FALSE;

    crime = "";
    for (victim = ch->in_room->people; victim != NULL; victim = v_next)
    {
        v_next = victim->next_in_room;

        if (!IS_NPC (victim) && IS_SET (victim->act, PLR_KILLER)
            && can_see (ch, victim))
        {
            crime = "KILLER";
            break;
        }

        if (!IS_NPC (victim) && IS_SET (victim->act, PLR_THIEF)
            && can_see (ch, victim))
        {
            crime = "THIEF";
            break;
        }
    }

    if (victim == NULL)
        return FALSE;

    sprintf (buf, "%s is a %s!  PROTECT THE INNOCENT!  MORE BLOOOOD!!!",
             victim->name, crime);
    REMOVE_BIT (ch->comm, COMM_NOSHOUT);
    do_function (ch, &do_yell, buf);
    multi_hit (ch, victim, TYPE_UNDEFINED);
    return TRUE;
}



bool spec_fido (CHAR_DATA * ch)
{
    OBJ_DATA *corpse;
    OBJ_DATA *c_next;
    OBJ_DATA *obj;
    OBJ_DATA *obj_next;

    if (!IS_AWAKE (ch))
        return FALSE;

    for (corpse = ch->in_room->contents; corpse != NULL; corpse = c_next)
    {
        c_next = corpse->next_content;
        if (corpse->item_type != ITEM_CORPSE_NPC)
            continue;

        act ("$n savagely devours a corpse.", ch, NULL, NULL, TO_ROOM);
        for (obj = corpse->contains; obj; obj = obj_next)
        {
            obj_next = obj->next_content;
            obj_from_obj (obj);
            obj_to_room (obj, ch->in_room);
        }
        extract_obj (corpse);
        return TRUE;
    }

    return FALSE;
}



bool spec_guard (CHAR_DATA * ch)
{
    char buf[MAX_STRING_LENGTH];
    CHAR_DATA *victim;
    CHAR_DATA *v_next;
    CHAR_DATA *ech;
    char *crime;
    int max_evil;

    if (!IS_AWAKE (ch) || ch->fighting != NULL)
        return FALSE;

    max_evil = 300;
    ech = NULL;
    crime = "";

    for (victim = ch->in_room->people; victim != NULL; victim = v_next)
    {
        v_next = victim->next_in_room;

        if (!IS_NPC (victim) && IS_SET (victim->act, PLR_KILLER)
            && can_see (ch, victim))
        {
            crime = "KILLER";
            break;
        }

        if (!IS_NPC (victim) && IS_SET (victim->act, PLR_THIEF)
            && can_see (ch, victim))
        {
            crime = "THIEF";
            break;
        }

        if (victim->fighting != NULL
            && victim->fighting != ch && victim->alignment < max_evil)
        {
            max_evil = victim->alignment;
            ech = victim;
        }
    }

    if (victim != NULL)
    {
        sprintf (buf, "%s is a %s!  PROTECT THE INNOCENT!!  BANZAI!!",
                 victim->name, crime);
        REMOVE_BIT (ch->comm, COMM_NOSHOUT);
        do_function (ch, &do_yell, buf);
        multi_hit (ch, victim, TYPE_UNDEFINED);
        return TRUE;
    }

    if (ech != NULL)
    {
        act ("$n screams 'PROTECT THE INNOCENT!!  BANZAI!!",
             ch, NULL, NULL, TO_ROOM);
        multi_hit (ch, ech, TYPE_UNDEFINED);
        return TRUE;
    }

    return FALSE;
}



bool spec_janitor (CHAR_DATA * ch)
{
    OBJ_DATA *trash;
    OBJ_DATA *trash_next;

    if (!IS_AWAKE (ch))
        return FALSE;

    for (trash = ch->in_room->contents; trash != NULL; trash = trash_next)
    {
        trash_next = trash->next_content;
        if (!IS_SET (trash->wear_flags, ITEM_TAKE) || !can_loot (ch, trash))
            continue;
        if (trash->item_type == ITEM_DRINK_CON
            || trash->item_type == ITEM_TRASH || trash->cost < 10)
        {
            act ("$n picks up some trash.", ch, NULL, NULL, TO_ROOM);
            obj_from_room (trash);
            obj_to_char (trash, ch);
            return TRUE;
        }
    }

    return FALSE;
}



bool spec_mayor (CHAR_DATA * ch)
{
    static const char open_path[] =
        "W3a3003b33000c111d0d111Oe333333Oe22c222112212111a1S.";

    static const char close_path[] =
        "W3a3003b33000c111d0d111CE333333CE22c222112212111a1S.";

    static const char *path;
    static int pos;
    static bool move;

    if (!move)
    {
        if (time_info.hour == 6)
        {
            path = open_path;
            move = TRUE;
            pos = 0;
        }

        if (time_info.hour == 20)
        {
            path = close_path;
            move = TRUE;
            pos = 0;
        }
    }

    if (!move || ch->position < POS_SLEEPING)
        return FALSE;

    switch (path[pos])
    {
        case '0':
        case '1':
        case '2':
        case '3':
            move_char (ch, path[pos] - '0', FALSE);
            break;

        case 'W':
            ch->position = POS_STANDING;
            act ("$n awakens and groans loudly.", ch, NULL, NULL, TO_ROOM);
            break;

        case 'S':
            ch->position = POS_SLEEPING;
            act ("$n lies down and falls asleep.", ch, NULL, NULL, TO_ROOM);
            break;

        case 'a':
            act ("$n says 'Hello Honey!'", ch, NULL, NULL, TO_ROOM);
            break;

        case 'b':
            act
                ("$n says 'What a view!  I must do something about that dump!'",
                 ch, NULL, NULL, TO_ROOM);
            break;

        case 'c':
            act
                ("$n says 'Vandals!  Youngsters have no respect for anything!'",
                 ch, NULL, NULL, TO_ROOM);
            break;

        case 'd':
            act ("$n says 'Good day, citizens!'", ch, NULL, NULL, TO_ROOM);
            break;

        case 'e':
            act ("$n says 'I hereby declare the city of Midgaard open!'",
                 ch, NULL, NULL, TO_ROOM);
            break;

        case 'E':
            act ("$n says 'I hereby declare the city of Midgaard closed!'",
                 ch, NULL, NULL, TO_ROOM);
            break;

        case 'O':
/*    do_function(ch, &do_unlock, "gate" ); */
            do_function (ch, &do_open, "gate");
            break;

        case 'C':
            do_function (ch, &do_close, "gate");
/*    do_function(ch, &do_lock, "gate" ); */
            break;

        case '.':
            move = FALSE;
            break;
    }

    pos++;
    return FALSE;
}



bool spec_poison (CHAR_DATA * ch)
{
    CHAR_DATA *victim;

    if (ch->position != POS_FIGHTING
        || (victim = ch->fighting) == NULL
        || number_percent () > 2 * ch->level) return FALSE;

    act ("You bite $N!", ch, NULL, victim, TO_CHAR);
    act ("$n bites $N!", ch, NULL, victim, TO_NOTVICT);
    act ("$n bites you!", ch, NULL, victim, TO_VICT);
    //spell_poison (gsn_dummy, ch->level, ch, victim, TARGET_CHAR);
    return TRUE;
}



bool spec_thief (CHAR_DATA * ch)
{
    CHAR_DATA *victim;
    CHAR_DATA *v_next;
    long gold, silver;

    if (ch->position != POS_STANDING)
        return FALSE;

    for (victim = ch->in_room->people; victim != NULL; victim = v_next)
    {
        v_next = victim->next_in_room;

        if (IS_NPC (victim)
            || victim->level >= LEVEL_IMMORTAL
            || number_bits (5) != 0 || !can_see (ch, victim))
            continue;

        if (IS_AWAKE (victim) && number_range (0, ch->level) == 0)
        {
            act ("You discover $n's hands in your wallet!",
                 ch, NULL, victim, TO_VICT);
            act ("$N discovers $n's hands in $S wallet!",
                 ch, NULL, victim, TO_NOTVICT);
            return TRUE;
        }
        else
        {
            gold =
                victim->gold * UMIN (number_range (1, 20),
                                     ch->level / 2) / 10;
            gold = UMIN (gold, ch->level * ch->level * 10);
            ch->gold += gold;
            victim->gold -= gold;
            silver =
                victim->silver * UMIN (number_range (1, 20),
                                       ch->level / 2) / 10;
            silver = UMIN (silver, ch->level * ch->level * 25);
            ch->silver += silver;
            victim->silver -= silver;
            return TRUE;
        }
    }

    return FALSE;
}