// turret.txt - Only exists to create creatures. Never moves or attacks. 
// Cell 4,5 - Stuff done flag to set when spawner is killed. If both are 0, no flag set.
// Cell 6 - talking cell

begincreaturescript;

variables;

short i,target;
short phase = 0;
short health_level = 0;
short talk1 = 0;
short talk2 = 0;
short last_abil;
short last_abil2;
short ae_range = 8;

body;

beginstate INIT_STATE;
	set_name(ME,"Hirickis");
	set_level(ME,35);
	change_max_health(ME,200);
	set_boss_level(ME,2);
	
	set_char_unkillable(ME,1);
	
	set_courage(ME,100);
	set_resistance(ME,7,90);

	set_walk_speed(ME,0);

	sf(122,19,0);

	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(122,1,1);
	set_spec_item(55,1);
	award_party_xp(500,30);
	begin_talk_mode(19);
break;

beginstate START_STATE; 
	if (health_level == 0)
		health_level = get_health(ME);
		
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if ((talk1 > 0) && (dist_to_party() <= 30)) {
		if (tick_difference(last_abil,get_current_tick()) > 2) {
			i = get_ran(1,0,7);
			
			if (char_ok(13235 + i) == FALSE) {
				if (summon_creature(13235 + i)) {	
					player_see_text("Hirickis uses raw magic to create an ally.");
					place_particle_num(13235 + i,1,1,8);
					set_summon_level(13235 + i,1);
					set_attack_bonus(13235 + i,8);
					force_char_status(13235 + i,0,8);
					force_char_status(13235 + i,1,8);
					set_resistance(13235 + i,7,60);
					pc_heard_sound_delay(136,100);						
					last_abil = get_current_tick();
					run_char_animation(2,1,35);	
					end();
					}
				}
			}
		}
		
	if (get_foe_target(ME,10,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (talk1 == 0) {
		talk1 = 1;
		talk_no_exit(50);
		}
	if ((talk2 == 0) && (get_health(ME) < get_max_health(ME) / 3) && (phase > 2)) {
		talk2 = 1;
		talk_no_exit(70);
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
		
	if ((phase == 0) && (get_health(ME) < health_level - (get_max_health(ME) / 6))) {
		phase = 1;
		relocate_char(ME,651,543,1);
		heal_char(ME,(get_max_health(ME) - get_health(ME)) / 2);
		health_level = health_level - (get_max_health(ME) / 6);
		begin_talk_mode(16);
		}
	if ((phase == 1) && (get_health(ME) < health_level - (get_max_health(ME) / 6))) {
		phase = 2;
		relocate_char(ME,668,543,1);
		heal_char(ME,(get_max_health(ME) - get_health(ME)) / 2);
		health_level = health_level - (get_max_health(ME) / 6);
		begin_talk_mode(17);
		}
	if ((phase == 2) && (get_health(ME) < health_level - (get_max_health(ME) / 6))) {
		phase = 3;
		sf(122,19,1);
		set_char_unkillable(ME,0);
		heal_char(ME,(get_max_health(ME) - get_health(ME)) / 2);
		health_level = health_level - (get_max_health(ME) / 6);
		relocate_char(ME,681,521,1);
		begin_talk_mode(18);
		}
	if ((phase == 3) && (get_health(ME) < health_level - (get_max_health(ME) / 12))) {
		phase = 4;
		relocate_char(ME,689,528,1);
		heal_char(ME,get_max_health(ME) / 20);
		health_level = health_level - (get_max_health(ME) / 12);
		print_str_color("Hirickis fades away in a flash of light. Soon, he reappears ...",3);
		}
	if ((phase == 4) && (get_health(ME) < health_level - (get_max_health(ME) / 12))) {
		phase = 5;
		relocate_char(ME,682,535,1);
		heal_char(ME,get_max_health(ME) / 20);
		health_level = health_level - (get_max_health(ME) / 12);
		print_str_color("Hirickis fades away in a flash of light. Soon, he reappears ...",3);
		}
	if ((phase == 5) && (get_health(ME) < health_level - (get_max_health(ME) / 12))) {
		phase = 3;
		relocate_char(ME,681,521,1);
		heal_char(ME,get_max_health(ME) / 20);
		health_level = health_level - (get_max_health(ME) / 12);
		print_str_color("Hirickis fades away in a flash of light. Soon, he reappears ...",3);
		}

	if ((talk1 > 0) && (dist_to_party() <= 30)) {
		if (tick_difference(last_abil,get_current_tick()) > 1) {
			i = get_ran(1,0,7);
			
			if (char_ok(13235 + i) == FALSE) {
				if (summon_creature(13235 + i)) {	
					player_see_text("Hirickis uses raw magic to create an ally.");
					place_particle_num(13235 + i,1,1,8);
					set_summon_level(13235 + i,1);
					set_attack_bonus(13235 + i,8);
					force_char_status(13235 + i,0,8);
					force_char_status(13235 + i,1,8);
					set_resistance(13235 + i,7,60);
					pc_heard_sound_delay(136,100);						
					last_abil = get_current_tick();
					run_char_animation(2,1,35);	
					end();
					}
				}
			}
		}

	
	if (dist_to_char(get_target()) > 8) {
			set_foe_target(ME,-1);
			set_state(START_STATE);
		}
		
	if ((get_ran(1,0,100) < 80) && (is_combat()) && (get_nearest_party_char(8) >= 0) && (tick_difference(last_abil2,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		last_abil2 = get_current_tick();

		if (phase == 0) { // ae daze
		  	place_particle_num(ME,3,1,8);
			print_named_str(ME,"casts a daze spell.");
			status_nearby(get_level(ME) + 12,ae_range,17,4);
			//particle_nearby(ae_range,8,2,4,2);
			pc_heard_sound_delay(195,100);			
			}

		if (phase == 1) { // ae daze
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"creates a shower of ice.");
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,6),ae_range,6,0);
			pc_heard_sound_delay(144,100);						
	  		create_missile_spiral(151,40,ae_range,2);
			}
		if (phase == 2) { // 
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"radiates a cloud of searing energy.");
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),ae_range,1,0);
			pc_heard_sound_delay(179,100);						
	  		create_missile_spiral(155,40,ae_range,2);
			}
		if (phase == 3) { // 
		  	place_particle_num(ME,16,10,10);
			print_named_str(ME,"casts a powerful charm spell.");
			status_nearby(get_level(ME) + 5,ae_range,5,4);
			//particle_nearby(ae_range,17,2,4,8);
			pc_heard_sound_delay(100,100);						
			}

		if (phase == 4) { // 
		  	place_particle_num(ME,18,5,10);
			print_named_str(ME,"radiates a powerful magical field.");
			drain_energy_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,3),ae_range,0);
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,13),ae_range,1,0);
			pc_heard_sound_delay(131,100);			
	  		create_missile_spiral(147,40,ae_range,2);
			particle_nearby(ae_range,19,3,4,8);
			}

		if (phase == 5) { // 
			print_named_str(ME,"fills the air with a shower of summoned blades.");
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,14),ae_range,0,0);
			pc_heard_sound_delay(3,100);				
			spray_missiles(15,8,ae_range);
			spray_missiles(20,8,ae_range);
			}
		end();
		
		}
	do_attack();

break;


beginstate TALKING_STATE;
	if (get_memory_cell(6) == 0) {
		print_str("Talking: It doesn't respond. It can't talk.");
		end();
		}
	begin_talk_mode(get_memory_cell(6));
break;