/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, 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			   *
***************************************************************************/

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "merc.h"
#include "interp.h"
#include "magic.h"

extern char *target_name;

MAGIC ( spell_farsight )
{
    if ( IS_AFFECTED ( ch, AFF_BLIND ) )
    {
        chsend ( "Maybe it would help if you could see?\n\r", ch );
        return;
    }

    do_function ( ch, &do_scan, target_name );
}

MAGIC ( spell_portal )
{
    CHAR_DATA *victim;
    OBJ_DATA *portal, *stone;

    if ( ( victim = get_char_world ( ch, target_name ) ) == NULL || victim == ch || victim->in_room == NULL || !can_see_room ( ch, victim->in_room ) || IS_SET ( victim->in_room->room_flags, ROOM_SAFE ) || IS_SET ( victim->in_room->room_flags, ROOM_PRIVATE ) || IS_SET ( victim->in_room->room_flags, ROOM_SOLITARY ) || IS_SET ( victim->in_room->room_flags, ROOM_NO_RECALL ) || IS_SET ( ch->in_room->room_flags, ROOM_NO_RECALL ) || victim->level >= level + 3 || ( !IS_NPC ( victim ) && victim->level >= LEVEL_HERO )   /* NOT trust */
         || ( IS_NPC ( victim ) && IS_SET ( victim->imm_flags, IMM_SUMMON ) )
         || ( IS_NPC ( victim ) && saves_spell ( level, victim, DAM_NONE ) )
         || ( is_clan ( victim ) && !is_same_clan ( ch, victim ) ) )
    {
        chsend ( "You failed.\n\r", ch );
        return;
    }

    stone = get_eq_char ( ch, WEAR_HOLD );
    if ( !IS_IMMORTAL ( ch ) &&
         ( stone == NULL || stone->item_type != ITEM_WARP_STONE ) )
    {
        chsend ( "You lack the proper component for this spell.\n\r", ch );
        return;
    }

    if ( stone != NULL && stone->item_type == ITEM_WARP_STONE )
    {
        act ( "You draw upon the power of $p.", ch, stone, NULL, TO_CHAR );
        act ( "It flares brightly and vanishes!", ch, stone, NULL, TO_CHAR );
        extract_obj ( stone );
    }

    portal = create_object ( get_obj_index ( OBJ_VNUM_PORTAL ), 0 );
    portal->timer = 2 + level / 25;
    portal->value[3] = victim->in_room->vnum;

    obj_to_room ( portal, ch->in_room );

    act ( "$p rises up from the ground.", ch, portal, NULL, TO_ROOM );
    act ( "$p rises up before you.", ch, portal, NULL, TO_CHAR );
}

MAGIC ( spell_nexus )
{
    CHAR_DATA *victim;
    OBJ_DATA *portal, *stone;
    ROOM_INDEX_DATA *to_room, *from_room;

    from_room = ch->in_room;

    if ( ( victim = get_char_world ( ch, target_name ) ) == NULL || victim == ch || ( to_room = victim->in_room ) == NULL || !can_see_room ( ch, to_room ) || !can_see_room ( ch, from_room ) || IS_SET ( to_room->room_flags, ROOM_SAFE ) || IS_SET ( from_room->room_flags, ROOM_SAFE ) || IS_SET ( to_room->room_flags, ROOM_PRIVATE ) || IS_SET ( to_room->room_flags, ROOM_SOLITARY ) || IS_SET ( to_room->room_flags, ROOM_NO_RECALL ) || IS_SET ( from_room->room_flags, ROOM_NO_RECALL ) || victim->level >= level + 3 || ( !IS_NPC ( victim ) && victim->level >= LEVEL_HERO ) /* NOT trust */
         || ( IS_NPC ( victim ) && IS_SET ( victim->imm_flags, IMM_SUMMON ) )
         || ( IS_NPC ( victim ) && saves_spell ( level, victim, DAM_NONE ) )
         || ( is_clan ( victim ) && !is_same_clan ( ch, victim ) ) )
    {
        chsend ( "You failed.\n\r", ch );
        return;
    }

    stone = get_eq_char ( ch, WEAR_HOLD );
    if ( !IS_IMMORTAL ( ch ) &&
         ( stone == NULL || stone->item_type != ITEM_WARP_STONE ) )
    {
        chsend ( "You lack the proper component for this spell.\n\r", ch );
        return;
    }

    if ( stone != NULL && stone->item_type == ITEM_WARP_STONE )
    {
        act ( "You draw upon the power of $p.", ch, stone, NULL, TO_CHAR );
        act ( "It flares brightly and vanishes!", ch, stone, NULL, TO_CHAR );
        extract_obj ( stone );
    }

    /* portal one */
    portal = create_object ( get_obj_index ( OBJ_VNUM_PORTAL ), 0 );
    portal->timer = 1 + level / 10;
    portal->value[3] = to_room->vnum;

    obj_to_room ( portal, from_room );

    act ( "$p rises up from the ground.", ch, portal, NULL, TO_ROOM );
    act ( "$p rises up before you.", ch, portal, NULL, TO_CHAR );

    /* no second portal if rooms are the same */
    if ( to_room == from_room )
        return;

    /* portal two */
    portal = create_object ( get_obj_index ( OBJ_VNUM_PORTAL ), 0 );
    portal->timer = 1 + level / 10;
    portal->value[3] = from_room->vnum;

    obj_to_room ( portal, to_room );

    if ( to_room->people != NULL )
    {
        act ( "$p rises up from the ground.", to_room->people, portal, NULL,
              TO_ROOM );
        act ( "$p rises up from the ground.", to_room->people, portal, NULL,
              TO_CHAR );
    }
}