/***************************************************************************
 *  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			   *
***************************************************************************/

#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#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;

void
spell_farsight (int sn, int level, CHAR_DATA * ch, void *vo, int target)
{
  if (IS_AFFECTED (ch, AFF_BLIND))
    {
      send_to_char ("Maybe it would help if you could see?\n\r", ch);
      return;
    }

  do_function (ch, &do_scan, target_name);
}


void
spell_portal (int sn, int level, CHAR_DATA * ch, void *vo, int target)
{
  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)))
    {
      send_to_char ("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))
    {
      send_to_char ("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);
}

void
spell_nexus (int sn, int level, CHAR_DATA * ch, void *vo, int target)
{
  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)))
    {
      send_to_char ("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))
    {
      send_to_char ("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);
    }
}

void
spell_summon_ggolem (int sn, int level, CHAR_DATA * ch, void *vo, int target)
{
  CHAR_DATA *gch;
  CHAR_DATA *golem;
  AFFECT_DATA af;
  int i = 0;

  if (is_affected (ch, sn))
    {
      send_to_char
	("You lack the power to summon another golem right now.\n\r", ch);
      return;
    }

  send_to_char ("You attempt to summon a greater golem.\n\r", ch);
  act ("$n attempts to summon a greater golem.", ch, NULL, NULL, TO_ROOM);

  for (gch = char_list; gch != NULL; gch = gch->next)
    {
      if (IS_NPC (gch) && IS_AFFECTED (gch, AFF_CHARM) && gch->master == ch &&
	  (gch->pIndexData->vnum == MOB_VNUM_GGOLEM))
	{
	  send_to_char ("More golems are more than you can control!\n\r", ch);
	  return;
	}
    }

  golem = create_mobile (get_mob_index (MOB_VNUM_GGOLEM));


  for (i = 0; i < MAX_STATS; i++)
    golem->perm_stat[i] = UMIN (25, 15 + ch->level / 10);

  golem->perm_stat[STAT_STR] += 3;
  golem->perm_stat[STAT_INT] -= 1;
  golem->perm_stat[STAT_AGI] += 2;

  golem->max_hit = IS_NPC (ch) ? URANGE (ch->max_hit, 1 * ch->max_hit, 30000)
    : UMIN ((10 * ch->pcdata->perm_hit) + 4000, 30000);
  golem->hit = golem->max_hit;
  golem->max_mana = IS_NPC (ch) ? ch->max_mana : ch->pcdata->perm_mana;
  golem->mana = golem->max_mana;
  golem->level = ch->level;
  for (i = 0; i < 5; i++)
    golem->armor[i] = interpolate (golem->level, 100, -100);
  golem->armor[4] = interpolate (golem->level, 100, 0);
  golem->armor[5] = golem->level;
  golem->gold = 0;
  golem->timer = 0;
  golem->damage[DICE_NUMBER] = 13;
  golem->damage[DICE_TYPE] = 9;
  golem->damage[DICE_BONUS] = ch->level / 2 + 10;

  char_to_room (golem, ch->in_room);
  send_to_char ("You summoned a greater golem!\n\r", ch);
  act ("$n summons a greater golem!", ch, NULL, NULL, TO_ROOM);

  af.where = TO_AFFECTS;
  af.type = sn;
  af.level = level;
  af.duration = 30;
  af.bitvector = 0;
  af.modifier = 0;
  af.location = APPLY_NONE;
  affect_to_char (ch, &af);

  SET_BIT (golem->affected_by, AFF_CHARM);
  golem->master = golem->leader = ch;

}