// mage.txt
// Like basicnpc.txt, but with a host of magical abilities. The abilities it 
// uses depends on the level of the character.

// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.

//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//	 Cell 4 - what sort of spec mage
//		1 - sage angela
//      2 - Kahlash
//      3 - Tower Lich
//      4 - Gladwell
//		5 - Loyalist Stovall
//		6 - Wyrgryss
//		7 - skunky joe
//      8 - ozimus 2
//      9 - nociduas
//      10 - hunts solberg
//      11 - The Lich Kavarus
//      12 - Kian-Ihrno
//		13 - Hraithe Shade
//      14 - Dagas-Ihrno
//	    15 - Vorus of the Circle
//      16 - Battlemage Winant

// spell_class - The quality of spells this character can cast, is level / 4, 
//    starts at 1
// 1 - can haste, summom help, slow
// 2 - single target terror
// 3 - can ae haste
// 4 - can ae slow, ae acid
// 6 - ae rain of fire

begincreaturescript;

variables;

short i,target;
short spell_class;
short haste_target;
short did_spell;
short misc_var = 0;
short misc_var2 = 0;
short tried_return = 0;
short last_abil;
short move_flag = 0;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);

	bless_char(ME,get_level(ME) / 8);
	set_courage(ME,100);
	set_resistance(ME,7,90);

	last_abil = get_current_tick();

	if (get_memory_cell(4) == 2) { // Kahlash
		set_name(ME,"Kahlash");
		set_level(ME,20);
		set_boss_level(ME,2);
		change_max_health(ME,50);
		}
	if (get_memory_cell(4) == 3) { // Tower Lich
		set_name(ME,"Tower Lich");
		set_level(ME,40);
		set_boss_level(ME,2);
		change_max_health(ME,200);
		set_strategy(ME,0);
		
		sf(71,23,0);
		sf(71,24,0);
		
		set_resistance(ME,0,100);
		set_resistance(ME,1,100);
		set_resistance(ME,2,100);
		set_resistance(ME,3,100);
		set_resistance(ME,4,100);
		set_resistance(ME,5,100);
		set_resistance(ME,6,100);
		}
	if (get_memory_cell(4) == 4) { // Gladwell
		set_name(ME,"Gladwell");
		set_level(ME,42);
		set_boss_level(ME,2);
		change_max_health(ME,600);
		set_strategy(ME,0);
		set_new_abil(ME,20);
		set_attack_bonus(ME,30);
		
		sf(71,23,0);
		sf(71,24,0);
		

		}
	if (get_memory_cell(4) == 5) { // Loyalist Stovall
		set_name(ME,"Loyalist Stovall");
		set_level(ME,26);
		set_boss_level(ME,2);
		change_max_health(ME,50);

		}
	if (get_memory_cell(4) == 6) { // Wyrgryss
		set_name(ME,"Wyrgryss");
		set_level(ME,34);
		set_boss_level(ME,2);
		change_max_health(ME,50);
		set_strategy(ME,0);
		set_new_abil(ME,14);
		}
	if (get_memory_cell(4) == 7) { // Ghost of Skunky Joe
		set_name(ME,"Ghost of Skunky Joe");
		set_level(ME,24);
		set_boss_level(ME,2);
		spawn_creature(6831);
		spawn_creature(6832);
		}
	if (get_memory_cell(4) == 8) { // Ozimus
		set_name(ME,"Ozimus");
		set_level(ME,24);
		set_boss_level(ME,2);

		}
	if (get_memory_cell(4) == 9) { // Nociduas
		set_name(ME,"Nociduas");
		set_level(ME,35);
		set_walk_speed(ME,0);
		}
	if (get_memory_cell(4) == 11) { // The Lich Kavarus
		set_name(ME,"The Lich Kavarus");
		set_level(ME,40);
		set_boss_level(ME,2);
		change_max_health(ME,100);
		set_walk_speed(ME,0);
		set_new_abil(ME,8);
		
		set_resistance(ME,0,100);
		set_resistance(ME,1,100);
		set_resistance(ME,2,100);
		set_resistance(ME,3,100);
		set_resistance(ME,4,100);
		set_resistance(ME,5,100);
		set_resistance(ME,6,100);
		}
	if (get_memory_cell(4) == 12) { // Kian-Ihrno
		set_name(ME,"Kian-Ihrno");
		set_boss_level(ME,2);
		}
	if (get_memory_cell(4) == 13) { // 13 - Hraithe Shade
		set_name(ME,"Hraithe Shade");
		set_level(ME,30);
		set_boss_level(ME,2);
		set_walk_speed(ME,0);
		set_char_unkillable(ME,1);
		set_new_abil(ME,20);
		
		set_resistance(ME,0,100);
		set_resistance(ME,1,100);
		set_resistance(ME,2,100);
		set_resistance(ME,3,100);
		set_resistance(ME,4,100);
		set_resistance(ME,5,100);
		set_resistance(ME,6,100);
		}
	if (get_memory_cell(4) == 14) { // 14 - Dagas-Ihrno
		set_name(ME,"Dagas-Ihrno");
		set_level(ME,35);
		set_boss_level(ME,2);
		set_walk_speed(ME,0);
		set_char_unkillable(ME,1);
		
		set_resistance(ME,0,100);
		set_resistance(ME,1,100);
		set_resistance(ME,2,100);
		set_resistance(ME,3,100);
		set_resistance(ME,4,100);
		set_resistance(ME,5,100);
		set_resistance(ME,6,100);

		place_particle_num(ME,7,12,5);		
		place_particle_num(ME,7,13,5);	
		}
	if (get_memory_cell(4) == 15) { // 15 - Vorus of the Circle
		set_name(ME,"Vorus of the Circle");
		set_level(ME,36);
		change_max_health(ME,200);
		set_boss_level(ME,2);
		set_new_abil(ME,8);
		}
	if (get_memory_cell(4) == 16) { // Battlemage Winant
		set_name(ME,"Battlemage Winant");
		set_level(ME,30);
		set_strategy(ME,1);
		set_boss_level(ME,1);
		}

	break;

beginstate DEAD_STATE;
		
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);

	if (get_memory_cell(4) == 2) { // Kahlash
		sf(57,5,1);
		begin_talk_mode(4);
		}
	if (get_memory_cell(4) == 3) { // Tower Lich
		sf(70,10,1);
		sf(70,12,1);
		spawn_creature(7631);
		begin_talk_mode(17);
		}
	if (get_memory_cell(4) == 4) { // Gladwell
		sf(170,12,1);
		begin_talk_mode(18);
		}
	if (get_memory_cell(4) == 7) { // Ghost of Skunky Joe
		sf(63,6,1);
		begin_talk_mode(9);
		}
	if (get_memory_cell(4) == 8) { // Ozimus
		begin_talk_mode(11);
		}
	if (get_memory_cell(4) == 9) { // Nociduas
		sf(62,6,1);
		sf(62,26,1);
		sf(62,27,1);
		sf(62,28,1);
		sf(62,29,1);
		erase_char(6751);
		erase_char(6752);
		erase_char(6753);
		erase_char(6754);
		erase_char(6755);
		erase_char(6756);
		erase_char(6757);
		erase_char(6758);
		erase_char(6759);
		erase_char(6760);
		erase_char(6761);
		erase_char(6762);
		begin_talk_mode(19);
		}
	if (get_memory_cell(4) == 11) { // Kavarus
		sf(52,1,1);
		sf(52,14,1);
		begin_talk_mode(17);
		}
	if (get_memory_cell(4) == 12) { // Kian-Ihrno
		sf(10,14,1);
		begin_talk_mode(13);
		}
	if (get_memory_cell(4) == 13) { // 13 - Hraithe Shade
		sf(10,19,3);
		kill_object(1851,0);
		kill_object(1852,0);
		begin_talk_mode(18);
		}
	if (get_memory_cell(4) == 14) { // 14 - Dagas-Ihrno
		sf(0,19,0);
		sf(0,7,1);
		begin_talk_mode(18);
		}
	if (get_memory_cell(4) == 15) { // 15 - Vorus of the Circle
		sf(13,11,1);
		sf(12,26,0);
		begin_talk_mode(22);
		}
	break;

beginstate START_STATE;
	if ((get_memory_cell(4) == 2) && (misc_var == 0)) { // Kahlash
		if (get_nearest_party_char(6) >= 0) {
			misc_var = 1;
			talk_no_exit(2);
			}
		}

	if ((get_memory_cell(4) == 3) && (misc_var == 0)) { // Tower Lich
		if ((get_nearest_party_char(3) >= 0) || (who_shot_me() >= 0)) {
			sf(70,12,0);
			misc_var = 1;
			talk_no_exit(16);
			}
		end();
		}
	if ((get_memory_cell(4) == 5) && (misc_var == 0)) { // Loyalist Stovall
		if (get_nearest_party_char(7) >= 0) {
			misc_var = 1;
			alert_char(30027);
			talk_no_exit(30);
			}
		}

	if ((get_memory_cell(4) == 11) && (misc_var == 0)) { // Kavarus
		if (party_within_dist(647,211,8)) {
			misc_var = 1;
			set_attitude(ME,10);
			sf(52,8,0);
			sf(52,9,0);

			set_resistance(ME,0,50);
			set_resistance(ME,1,25);
			set_resistance(ME,2,0);
			set_resistance(ME,3,0);
			set_resistance(ME,4,100);
			set_resistance(ME,5,0);
			set_resistance(ME,6,100);

			begin_talk_mode(10);
			}
		set_attitude(ME,4);
		end();
		}

	if (get_memory_cell(4) == 14) { // 14 - Dagas-Ihrno
		if (gf(0,19) < 4)
			end();
		}

	if ((get_memory_cell(4) == 15) && (misc_var == 0)) { // Vorus of the Circle
		if ((get_nearest_party_char(7) >= 0) || (who_shot_me() >= 0)) {
			misc_var = 1;
			sf(12,26,1);
			talk_no_exit(19);
			}
		end();
		}

		
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (get_foe_target(ME,12,0)) {
		if (dist_to_party() > 8)
			approach_char(ME,30000,7);
			else {
				do_attack();
				set_state(3);
				}
		}
		
	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
		
	// if we're Dagas-Ihrno, hunt party down
	if (get_memory_cell(4) == 14) { // 14 - Dagas-Ihrno
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}

	if ((get_memory_cell(4) == 1) && (get_attitude(ME) < 10)) { // angela
		if (gf(145,9) < 7) 
			approach_nav_point(ME,4,1);
			else {
				if (my_dist_from_start() >= 12) {
					if (get_ran(1,1,100) < 40) 
						return_to_start(ME,1);
					}
					else fidget(ME,25);
				}
		}
	if (get_memory_cell(4) == 10) { // go to solberg
		approach_nav_point(ME,2,1);
		}

	if (get_memory_cell(4) == 16) { // Battlemage Winant
		if (gf(6,13) == 3) {
			if (dist_to_nav_point(ME,1) <= 3)
				fidget(ME,25);
				else approach_nav_point(ME,1,2);
			}
			else if (my_dist_from_start() >= 6) {
				if (get_ran(1,1,100) < 40) 
					return_to_start(ME,1);
				}
		}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_memory_cell(4) == 2) && (misc_var == 0)) { // Kahlash
			misc_var = 1;
			talk_no_exit(2);
		}
	if ((get_memory_cell(4) == 5) && (misc_var == 0)) { // Loyalist Stovall
		if (get_nearest_party_char(9) >= 0) {
			misc_var = 1;
			alert_char(30027);
			talk_no_exit(30);
			}
		}

	spell_class = get_level(ME) / 4;
	if (spell_class == 0)
		spell_class = 1;

	if (am_i_doing_action())
		end();
		
	if (target_ok() == FALSE)
		set_state(START_STATE);

	// special stuff to check
	//if (get_memory_cell(4) == 1) { // hirickis
		//if ((get_health(ME) < 150) && (gf(56,13) == 0)) {
		//	sf(56,13,1);
		//	end_combat_turn();
		//	talk_no_exit(40);
		//	}
		//}

		
	// special abilities
	if ((is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		last_abil = get_current_tick();

		if ((get_memory_cell(4) == 3) && (gf(71,24) > 0)) { // Tower Lich,
			inc_flag(71,24,-1);
			if (gf(71,24) == 0) {
				print_str_color("The lich recreates its shield.",3);
				set_resistance(ME,0,100);
				set_resistance(ME,1,100);
				set_resistance(ME,2,100);
				set_resistance(ME,3,100);
				set_resistance(ME,4,100);
				set_resistance(ME,5,100);
				set_resistance(ME,6,100);
				}
			}

		if ((get_memory_cell(4) == 3) && (char_ok(7628) == FALSE) && (char_ok(7629) == FALSE) && (char_ok(7630) == FALSE)) { // Tower Lich
			i = get_ran(1,0,2);
			print_str_color("A magical orb appears on one of the circles.",2);
			spawn_creature(7628 + i);
			place_particle_num(7628 + i,1,1,8);
			}

		if (((get_memory_cell(4) == 3) || (get_memory_cell(4) == 4)) && (can_see_char(get_target())) && (dist_to_char(get_target()) <= 8)) { // Tower Lich, gladwell
			print_named_str(ME,"directs a lance of flame at you.");
			if (num_chars_in_group(0) > 1) {
				if (misc_var2 != get_target()) {
					misc_var2 = get_target();
					sf(71,23,0);
					print_str_color("  The flame level is low now, but it's growing.",2);
					}
					else print_str_color("  The fire grows in intensity.",2);
				inc_flag(71,23,2);
				if (get_memory_cell(4) == 4)
					inc_flag(71,23,2);
				}
				else {
					sf(71,23,2);
					if (get_memory_cell(4) == 4)
						sf(71,23,6);
					}
			
			damage_char(get_target(),100 + gf(71,23) * 200,2);
			pc_heard_sound_delay(178,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),70);
			if (num_chars_in_group(0) == 1) 
				set_char_status(get_target(),22,3);
			run_char_animation(2,1,35);	
			end();
			}

		if ((get_memory_cell(4) == 9) && (gf(62,16) == 0))  // Nociduas
			talk_no_exit(50);
		if ((get_memory_cell(4) == 9) && (gf(62,16) > 0)) { // Nociduas
	
			i = get_ran(1,0,3);
			if (char_ok(6751 + i) == FALSE) {
				print_str_color("Nociduas uses the power of his circle. He calls a",2);
				print_str_color("  servant from the chasm.",2);
				spawn_creature(6751 + i);
				set_name(6751 + i,"Hunter of Nociduas");
				set_boss_level(6751 + i,-1);
				set_summon_level(6751 + i,1);
				place_particle_num(6751 + i,1,1,8);
				force_char_status(ME,24,4);
				}
			}

		if (get_memory_cell(4) == 12) { // Kian-Ihrno
			run_char_animation(2,1,35);	
			last_abil = get_current_tick();
			place_particle_num(ME,18,5,10);
			print_named_str(ME,"summons a terrifying cloud of energy.");
			drain_energy_nearby(get_ran(1,20,30),4,0);
			damage_nearby(150,4,1,0);
			pc_heard_sound_delay(131,100);			
			create_missile_spiral(147,40,4,2);
			particle_nearby(4,19,3,4,8);
			
			alert_char(1085);
			alert_char(1086);
			
			end();
			}

		if ((get_memory_cell(4) == 16) && (get_ran(1,0,100) < 33)) { // Battlemage Winant
			run_char_animation(2,1,35);	

			
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"creates a cloud of freezing mist.");
			damage_nearby(get_ran(1,150,250),5,6,4);
			pc_heard_sound_delay(144,100);						
	  		create_missile_spiral(151,40,5,2);

			end();
			}

		} // end once per turns

	if ((get_memory_cell(4) == 6) && (get_attitude(ME) >= 10)) { // Wyrgryss
		if (char_ok(8672)) {
			set_act_at_dist(8672,1);
			alert_char(8672);
			}
		if (char_ok(8673)) {
			set_act_at_dist(8673,1);
			alert_char(8673);
			}
		if (char_ok(8674)) {
			set_act_at_dist(8674,1);
			alert_char(8674);
			}
		}
		
	if (get_memory_cell(4) == 11) { // Kavarus make bones
		if (char_ok(5682) == FALSE) {
			spawn_creature(5682);
			set_name(5682,"Vile Guardian");
			set_summon_level(5682,1);
			set_level(5682,12);
			}
		if (char_ok(5683) == FALSE) {
			spawn_creature(5683);
			set_name(5683,"Vile Guardian");
			set_summon_level(5683,1);
			set_level(5683,12);
			}
		}

	if (get_memory_cell(4) == 11) { // kavarus death ray
		if (gf(52,8) == 10) {
			sf(52,8,11);
			i = get_ran(1,0,3);
			if ((char_ok(get_pc_id(i)) == FALSE) || (dist_to_char(get_pc_id(i)) > 5))
				i = get_ran(1,0,3);
			if ((char_ok(get_pc_id(i)) == FALSE) || (dist_to_char(get_pc_id(i)) > 5))
				i = get_ran(1,0,3);
			if ((char_ok(get_pc_id(i)) == FALSE) || (dist_to_char(get_pc_id(i)) > 5))
				i = get_ran(1,0,3);
			if ((char_ok(get_pc_id(i)) == FALSE) || (dist_to_char(get_pc_id(i)) > 5))
				i = get_ran(1,0,3);
			if ((char_ok(get_pc_id(i)) == FALSE) || (dist_to_char(get_pc_id(i)) > 5))
				i = -1;
			if (i >= 0) {
				force_char_status(get_pc_id(i),26,4);

				begin_talk_mode(16);
				}
				else begin_talk_mode(15);
			}
		}
		
	if ((is_combat()) && (tick_difference(last_abil,get_current_tick()) > 2)) {
		last_abil = get_current_tick();
		//if (get_memory_cell(4) == 9) { // solberg

		//	}

		}
	
	// REGULAR MAGE STUFF
	
	if ((my_dist_from_start() >= 25) && (tried_return == 0)) {
		tried_return = 1;
		set_foe_target(ME,-1);
		return_to_start(ME,2);
		set_state(4);
		}
		
	// if our target is terrified or charmed or dazed, get new target
	if ((get_char_status(get_target(),7) > 0) || (get_char_status(get_target(),5) > 0) || (get_char_status(get_target(),17) > 0)) {
		get_foe_target(ME,12,0);
		if (target_ok() == FALSE)
			set_state(START_STATE);	
		}
		 
	// if party ducks out of combat mode, force it to restart
	if ((is_combat() == FALSE) && (can_see_char(get_target()))) {
		do_attack();
		end();
		}
	
	if (can_see_char(get_target()) == FALSE) {
		do_attack();
		end();
		}

	// now we are seeing if we want to cast a spell. the answer is probably yes.
	did_spell = 0;
	
	// first, if slowed, haste self or those nearby
	haste_target = -3;
	if (((get_char_status(ME,1) < 0) && (get_ran(1,0,100) < 80)) ||
	  ((get_char_status(ME,1) == 0) && (get_ran(1,0,100) < 40))) {
	  	if (spell_class >= 3)
	  		haste_target = -2;
	  		else haste_target = -1;
	  	}
	if ((haste_target == -3) &&
	  (get_friendly_target(ME,8,3) > 0) && (get_ran(1,0,100) < 40)) {
	  	if (spell_class >= 3)
	  		haste_target = -2;
	  		else haste_target = get_friend_target();
	  	}

	if ((haste_target == -3) &&
	  (get_friendly_target(ME,8,4) > 0) && (get_ran(1,0,100) < 20) && (get_char_status(ME,1) <= 0)) { 
	  	if (spell_class >= 3)
	  		haste_target = -2;
	  		else haste_target = get_friend_target();
	  	}
	if (haste_target == -2) { // AE haste
		if (get_char_status(ME,1) <= 0) { // don't rehaste myself
			print_named_str(ME,"casts a powerful haste spell.");
			status_nearby(12 + get_level(ME),8,1,3);
			if (spell_class >= 6)
				status_nearby(5,8,24,3);

			run_char_animation(2,1,35);	
			pc_heard_sound_delay(101,100);						
			deduct_ap(9);
			did_spell = 1;
			}
		}
			
	if (haste_target >= -1) { // 1 target haste
		if (haste_target == -1)
			haste_target = my_id();
		if (get_char_status(haste_target,1) <= 0) { // don't rehaste
			if (haste_target == my_id())
				print_named_str(ME,"casts a haste spell.");
				else print_named_str(ME,"hastes an ally.");
		  	//place_particle_num(haste_target,3,0,8);
			set_char_status(haste_target,1,12 + get_level(ME));
			run_char_animation(2,1,35);	
			pc_heard_sound_delay(101,100);						
			deduct_ap(9);
			did_spell = 1;
			}
		}		
	
	// if alone, summon aid
	if ((did_spell == 0) && (get_char_status(ME,5) <= 0) && (((friends_nearby(6) < enemies_nearby(6)) && 
		(get_ran(1,0,100) < 25)) || (get_ran(1,0,100) < 15))) {
			if (spell_class >= 8)
				i = return_summon_char(7);
				else if (spell_class >= 6)
					i = return_summon_char(3);
				else if (spell_class >= 4)
					i = return_summon_char(2);
				else if (spell_class >= 2)
					i = return_summon_char(1);
				else i = return_summon_char(0);
			summon_char(i,12);
			i = get_summoned_char_id();
			if (i >= 0) {
				print_named_str(ME,"summons aid.");
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(136,100);						
				deduct_ap(9);
				did_spell = 1;
				
				if (get_level(ME) >= 10)
					change_max_health(i,1 * get_level(ME));
				
				if ((get_level(ME) * 3) / 4 > get_level(i))
					set_level(i,(get_level(ME) * 3) / 4);
				}
			}
			
	// cast terror on a foe
	if ((did_spell == 0) && (spell_class > 1) && (num_party_uncharmed() > 1) && 
	  (get_char_status(get_target(),7) <= 0) && (get_ran(1,0,100) < 25)) {
		print_named_str(ME,"radiates pure terror.");
	  	place_particle_num(get_target(),23,2,4);

		set_char_status(get_target(),7,10 * spell_class);
		run_char_animation(2,1,35);	
		pc_heard_sound_delay(123,100);						
		deduct_ap(9);
		did_spell = 1;
	  	}
	  
	// slow or AE slow?
	if ((did_spell == 0) && (spell_class > 3) &&
	  ((get_ran(1,0,100) < 15) || ((get_char_status(get_target(),1) >= 0) && (get_ran(1,0,100) < 30)))) {
		print_named_str(ME,"radiates slowing energy.");
		status_nearby(-8,8,1,4);
		//particle_nearby(8,2,2,4,8);
		run_char_animation(2,1,35);	
		pc_heard_sound_delay(124,100);						
		deduct_ap(9);
		did_spell = 1;
		}
	if ((did_spell == 0) && (spell_class <= 3) &&
	  ((get_ran(1,0,100) < 15) || ((get_char_status(get_target(),1) >= 0) && (get_ran(1,0,100) < 30)))) {
		print_named_str(ME,"fires a beam of slowing energy.");
		set_char_status(get_target(),1,-8);
	  	//place_particle_num(get_target(),2,6,8);
		run_char_animation(2,1,35);	
		pc_heard_sound_delay(124,100);						
		deduct_ap(9);
		did_spell = 1;
		}
			
	// AE acid or fire?
	if ((did_spell == 0) && ((spell_class == 4)	|| (spell_class == 5)) && 
	  (enemies_nearby(8) > 1) && (get_ran(1,0,100) < 60) ) {
		print_named_str(ME,"summons a shower of acid.");
		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,3),7,5,4);
		status_nearby(get_level(ME) + get_attack_bonus(ME),7,6,4);
	  		create_missile_spiral(142,40,7,2);
		run_char_animation(2,1,35);	
		pc_heard_sound_delay(177,100);						
		deduct_ap(9);
		did_spell = 1;
	  	}
	if ((did_spell == 0) && (spell_class > 5) && 
	  (enemies_nearby(8) > 1) && (get_ran(1,0,100) < 70)) {
		print_named_str(ME,"summons a rain of fire.");
		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),7,2,4);
		run_char_animation(2,1,35);	
		pc_heard_sound_delay(178,100);						
		deduct_ap(9);
	  		create_missile_spiral(154,40,7,2);
		did_spell = 1;
	  	}
	
	if (did_spell > 0)
		end();
		
	//if (my_ap() < 5)
	//	end_combat_turn();
	
	if (am_i_doing_action() == FALSE)
		do_attack();
break;

beginstate 4; // returning to start
	if (my_dist_from_start() < 10)
		set_state(START_STATE);

	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	return_to_start(ME,2);
break;

beginstate TALKING_STATE;
	if ((get_memory_cell(4) == 1) && (gf(145,9) < 7)) { // angela

		begin_talk_mode(16);
		end();
		}

	if (get_memory_cell(4) == 16) { // Battlemage Winant
		if (gf(6,13) == 3)
			begin_talk_mode(17);
		if (my_dist_from_start() > 7) {
			print_str_color("This battlemage quickly runs past you.",2);
			end();
			}
		}
		
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;