#include <std.h>
#include <daemons.h>
#include "/d/islands/argentrock/argabbr.h"

inherit MONSTER;

object *names;
int count;

void create() {
   object obj;
   ::create();
   set_name("batlin the mad mage");
   set_id( ({"man","batlin","mage","mad mage","batlin the mad mage"}) );
   set_short("Batlin the Mad Mage");
    set_long("%^BOLD%^%^CYAN%^This old mage has very long greyish beard "+
       "and moustache. His whiskers cover almost half of his face. Two "+
      "black eyes shine with wisdom and knowledge. Yet, his movement "+
      "does not seem to be hindered by his age. Effusions of magical "+
       "energy diffuse out of his body. Concentrating on his experiments"+
      ", he is ignoring everything else in the room. He is a loyal "+
      "worshiper of the demon princes. He has contributed his "+
      "whole life to the demon princes. In return he is gifted the "+
      "great knowledge in demonic magic and spells which differs a great "+
       "deal from the nature of existing magic of the known realms.");
   set_gender("male");
   set_race("human");
   set_body_type("humanoid");
   set_hd(50,8);
   set_alignment(8);
   set("aggressive",2);
   set_size(2);
   set_class("mage");
   set_mlevel("mage",50);
   set_guild_level("mage",50);
   set_overall_ac(-15);
   set_stats("intelligence",25);
   set_stats("wisdom",25);
   set_stats("strength",25);
   set_stats("charisma",18);
   set_stats("dexterity",14);
   set_stats("constitution",18);
   set_exp(60000);
   set_mob_magic_resistance("average");
   set_property("no death",1);
   set_property("magic",1);
   set_property("no animate",1);
   set_property("no steal",1);
   set_property("no bow",1);
   set_property("no web",1);
   set_property("weapon resistance",3);
   set_hp(900+(random(300)));
   set_max_hp(query_hp());
   set_emotes(2,
       ({"%^MAGENTA%^Batlin says%^RESET%^: Where did I put my spellbook?",
       "%^MAGENTA%^Batlin says%^RESET%^: I need to gather more components.",
       "%^MAGENTA%^Batlin says%^RESET%^: One wormheart, some bloodmoss ...",
       "%^MAGENTA%^Batlin says%^RESET%^: Where is that spider web?",
       "%^MAGENTA%^Batlin says%^RESET%^: I can use some corpses again.",
      "%^MAGENTA%^Batlin says%^RESET%^: Better lock up the cells ...",
      "%^MAGENTA%^Batlin says%^RESET%^: Black pearl is hard to find!"}),0);
   add_money( "gold",5000+random(1000) );
   set_funcs( ({"daemonic_surprise",
      "mind_blast",
      "energy_strike"}) );
   set_func_chance(90);
   set_spells(({"frost orb",
                "greater shout",
                "cone of cold",
                "powerword stun",
                "prismatic spary",
                "meteor swarm",
                "scorcher",
                "greater shout",
                "ice storm"}));
   set_spell_chance(85);
   obj = new(OBJ+"elemental_staff")->move(TO);
   command("wield staff in left hand and right hand");
   obj = new("/d/antioch/ruins/obj/robes")->move(TO);
   command("wear robe");
   obj = new(OBJ+"obsidian_ring");
   obj->set_weight(500);
   obj->move(TO);
   command("wear ring");
   obj = new("/d/magic/scroll");
   obj->set_spell(9);
   obj->move(TO);
   names = ({});
   count = 0;
}

void heart_beat() {
   ::heart_beat();
   if(!objectp(TO))
      return;
   if( query_hp() < query_max_hp() )
      TO->do_damage( "torso",-(random(6) + 1) );
   if( query_hp() <= 150 ) {
      if(count <= 0) {
         count = 12;
         set_func_chance(0);
         call_out("imbalance",0);
         set_func_chance(90);
      }
      count--;
      return;
   }
   TO->check_them();
}

void check_them() {
   object *stuff;
   int i,num;
   if( !objectp(ETO) )
      return;
   stuff = all_living(ETO);
   stuff -= ({TO});
   num = sizeof(stuff);
   if( (string *)TO->query_attackers() != ({}) ) {
      for(i = 0;i < num;i++) {
         if( !userp(stuff[i]) && !interactive(stuff[i]) ) {
            command("say DIE! You brainless creature, your presence here "+
               "is a mistake!");
            tell_room(ETO,"%^BOLD%^BLUE%^Batlin chants a short phrase.");
            stuff[i]->die();
         }
      }
   }
   else {
      if( base_name(ETO) == PATH+"arghouse11" ) {
         if(member_array( "door",ETO->query_exits() ) == -1) {
            tell_room(ETO,"%^BLUE%^The door reappears as the battle "+
               "ceases.");
            ETO->reset();
         }
         names = ({});
         return;
      }
   }
}

int kill_ob(object vic,int num) {
   int hold;
   command("enable ring");
/*   if( TP == present("horse") ){  // TP s/b vic???
       command("say How dare this creature enter my laboratory!? To the "+
         "hell now!");
      tell_room(ETO,"%^BOLD%^BLUE%^Batlin chants a short phrase.");
      TP->die(TO);  // TP s/b vic or ???
   }
*/
   if(member_array( vic,query_attackers() ) == -1 &&
      member_array(vic,names) == -1)
      call_out("vortex",1,vic);
   hold = ::kill_ob(vic,num);
   return hold;
}

void vortex(object vic) {
   object *exits,obj;
   names += ({vic});
   if(!objectp(vic) || !objectp(TO))   // added to squash another bug *Styx*  3/22/03
      return;
   if( !present(vic,ETO) )
      return;
   tell_room(ETO,"%^BOLD%^CYAN%^Batlin gathers some reagents and chants "+
      "a short phase.");
   tell_room(ETO,"%^B_RED%^CYAN%^A large black death vortex rises from "+
      "nowhere and drives towards "+vic->query_cap_name()+"!!",vic);
    tell_object(vic,"%^B_RED%^CYAN%^A large black death vortex rises "+
      "from nowhere and drives towards you!!");
   obj = new(OBJ+"vortex");
   obj->set_target(vic);
   obj->move(ETO);
   if( base_name(ETO) == PATH+"arghouse11" ) {
      exits = ETO->query_exits();
      if(member_array("door",exits) != -1) {
         tell_object(ETO,"%^RED%^Batlin raises his hands and completes a "+
            "circular motion. The door behind you disappears!");
         command("say Now fight for your life!");
         ETO->remove_exit("door");
      }
   }
}

void daemonic_surprise(object vic) {
   tell_room(ETO,"%^BOLD%^CYAN%^Batlin gathers some reagents and chants "+
      "a short phase.");
    tell_room(ETO,"%^RED%^As he finishes the chant, a red shadowy thing "+
      "flashes across your eyes.");
   if( !SAVING_D->saving_throw(TP,"spell",-8) ) {
      tell_object(vic,"%^RED%^You suddenly feel tremedous pain and "+
         "scream! An image of a demon appears and vanishes.");
      tell_room(ETO,"%^RED%^"+vic->query_cap_name()+" suddenly screams "+
         "in pain!",vic);
      vic->do_damage( vic->return_target_limb(),roll_dice(5,20) );
   }
}

void energy_strike(object vic) {
   tell_room(ETO,"%^BOLD%^CYAN%^Batlin gathers some reagents and chants "+
      "a short phase.");
    tell_room(ETO,"%^BLUE%^As he finishes the chant, you sense tremedous "+
      "flow of energy in the room.");
   if( !SAVING_D->saving_throw(TP,"spell",-10) ) {
      tell_object(vic,"%^BLUE%^A bundle of energy strikes you like a "+
          "sword. You scream in pain!");
      tell_room(ETO,"%^BLUE%^"+vic->query_cap_name()+" suddenly screams "+
         "in pain!",vic);
      vic->do_damage( vic->return_target_limb(),roll_dice(5,16) );
   }
}

void mind_blast(object vic) {
   tell_room(ETO,"%^BOLD%^CYAN%^Batlin gathers some reagents and chants "+
      "a short phase.");
   tell_room(ETO,"%^CYAN%^Batlin concentrates for a moment.");
   if( !SAVING_D->saving_throw(TP,"spell",-8) ) {
      tell_object(vic,"%^CYAN%^You feel a strange assult on your brain, "+
         "an awfully strong power grows in your head, you start to lose "+
         "control of your mind and your body!");
      tell_room(ETO,"%^CYAN%^"+vic->query_cap_name()+" suddenly looks "+
         "odd, "+vic->query_possessive()+"+ face looks twisted, some "+
          "expression of extreme pain appears.",vic);
      vic->do_damage( vic->return_target_limb(),roll_dice(4,20) );
      vic->set_paralyzed(random(20) + 10,"%^CYAN%^Your mind is in chaos.");
   }
}

void imbalance() {
   int i,all;
   object *living;
   living = all_living(ETO);
   living -= ({TO});
   all = sizeof(living);
   command("say You are good so far.");
   command("say Now let fate decide who the winner is!");
   command("say Prepare yourself ...");
   tell_room(ETO,"%^BOLD%^CYAN%^Batlin gathers some reagents and chants "+
      "a short phase.\n%^RED%^He chants the spell of imbalance!\n\n");
   call_out("do_it",5);
   for(i = 0;i < all;i++) {
      living[i]->cease_all_attacks();
      living[i]->set_paralyzed(5,"For some reason you cannot move!");
   }
}

void do_it() {
   int i,all;
   object *living;
   living = all_living(ETO);
   living -= ({TO});
   all = sizeof(living);
    tell_room(ETO,"%^B_RED%^ORANGE%^The room starts shaking voilently.\n"+
       "The air around you seems to vaporize.\nThe floor opens.\nYour "+
      "sight begins to blur.\n\n\n...\n\n... ...\n\n... ... ...\n\n\n\n");
   message("broadcast","%^RED%^A tremble passes from beneath your feet "+
       "to your body as you hear a loud sound of an earthquake coming from a "+
      "distance.",users(),all_living(ETO));
   for(i = 0;i < all;i++) {
      if( !userp(living[i]) && !interactive(living[i]) )
         living[i]->die();
      if( wizardp(living[i]) ) continue;
      living[i]->add_attacker(TO);
      living[i]->continue_attack();
      switch( random(50) ) {
         case 0:
            if(wizardp(living[i])) {
               tell_object(living[i],"You should be dead by now ;)");
               break;
            }
            tell_object(living[i],"%^BOLD%^RED%^Your god has abandoned "+
               "you. Your soul has been taken over to the demon princes.\n\n"+
               "%^BOLD%^CYAN%^An expression of triumph across Batlin's "+
               "face.\n");
            living[i]->add_attacker(TO);
            living[i]->set_hp(-10);
            living[i]->do_damage("torso",100);
            living[i]->continue_attack();
            break;
         case 1..20:
            tell_object(living[i],"%^BOLD%^BLUE%^Your god decided to "+
               "assist you in your fight with the demonic power.\n\n"+
               "Batlin grins evilly.");
            living[i]->set_hp(1);
            living[i]->do_damage("torso",-19);
            living[i]->add_attacker(TO);
            living[i]->continue_attack();
            break;
         case 21..49:
            tell_object(living[i],"%^BOLD%^CYAN%^Your prayers go unanswered"+
			".\n\n%^BOLD%^CYAN%^Batlin grins evilly.");
            living[i]->set_hp(1);
            living[i]->do_damage("torso",-4);
            living[i]->add_attacker(TO);
            living[i]->continue_attack();
            break;
         case 50:
            tell_object(living[i],"%^GREEN%^Your god trusted in you and "+
               "helps you in resisting the pagan power!\n\n"+
               "%^BOLD%^CYAN%^Batlin seems disappointed.\n");
            break;
      }
   }
}

void die(object obj) {
   object *stuff;
   int i,num;
   stuff = all_inventory(ETO);
   num = sizeof(stuff);
   command("say %^BOLD%^BLUE%^This ring is a gift by the demon princes!");
   command("unwear ring");
   command("offer ring");
   command("say %^BOLD%^BLUE%^You are cursed forever!!");
   tell_room(ETO,"%^BOLD%^BLUE%^As Batlin vanishes away in a black oily mist, the "+
      "magic in the room dissipates.");
   if( base_name(ETO) == PATH+"arghouse11" ) {
      tell_room(ETO,"%^BOLD%^RED%^The door reappears.");
      ETO->reset();
   }
   for(i = 0;i < num;i++) {
      if( (string)stuff[i]->query_name() == "black death vortex" ) {
         tell_room(ETO,"%^BOLD%^BLUE%^The vortex lost its power and "+
            "vanishes.");
         stuff[i]->remove();
      }
   }
   ::die(obj);
}


void add_attacker(object new) {
   kill_ob(new,1);
   ::add_attacker(new);
}
