// minibossnpc.txt
// Like basicnpc, but character automatically talks to party when they get 
// within 6 spaces or combat starts. The dialogue is meant to only
// be said once, and not again. This script is meant to be for
// foes of the party.
// 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 the character talks from.


begincreaturescript;

variables;

short i,target;
short talk_done = 0;
short last_abil;
short last_abil2;
short last_abil3;
short mess1 = 0;
short mess2 = 0;
short phase = 0;
short which_mode = 0;

body;

beginstate INIT_STATE;
	if (which_town() == 66)
		which_mode = 1;
		else set_walk_speed(ME,0);
	
	set_name(ME,"Sulfras");
	//set_level(ME,);
	change_max_health(ME,400);
	set_boss_level(ME,2);
	set_coloradj(ME,0);
	set_scaling(ME,100);
	set_resistance(ME,1,100);
	
	
	set_courage(ME,100);
	set_resistance(ME,7,90);
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	last_abil3 = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(65,12,1);
	
	if (which_mode == 0) {
		erase_char(6548);
		erase_char(6549);
		erase_char(6550);
		erase_char(6551);
		erase_char(6552);
		}
	if (which_mode == 1) {
		erase_char(6614);
		erase_char(6615);
		erase_char(6617);
		erase_char(6618);
		erase_char(6619);
		}
	force_char_status(30000,26,0);
	if (gf(66,11) == 0) {
		set_spec_item(23,1);
	
		ok_dlog(100,4);
		}
		else ok_dlog(100,3);
	//begin_talk_mode();
break;

beginstate START_STATE; 
	set_resistance(ME,1,100);
	if ((tick_difference(last_abil,get_current_tick()) > 0) && (get_nearest_hate_char(8) < 0) && (get_health(ME) < get_max_health(ME))) {
		last_abil = get_current_tick();
		print_str_color("Sulfras magically heals her wounds.",1);
		heal_char(ME,500);
		}

	// 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,8,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 hostile, hunt party down
	if (get_attitude(ME) >= 10) {
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}		



	// 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 ((tick_difference(last_abil,get_current_tick()) > 0) && (get_nearest_hate_char(8) < 0) && (get_health(ME) < get_max_health(ME))) {
		last_abil = get_current_tick();
		print_str_color("Sulfras magically heals her wounds.",1);
		heal_char(ME,500);
		}


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

	i = -1;
	if (which_mode == 0) {
		if ((mess1 == 0) && (get_health(ME) < (get_max_health(ME) * 4) / 5)) {
			mess1 = 1;
			i = 6548;
			set_terrain(0,48,9,0);
			}
			else if ((mess1 == 1) && (get_health(ME) < (get_max_health(ME) * 3) / 5)) {
				mess1 = 2;
				i = 6549;
				set_terrain(0,50,9,0);
				}
			else if ((mess1 == 2) && (get_health(ME) < (get_max_health(ME) * 2) / 5)) {
				mess1 = 3;
				i = 6550;
				set_terrain(0,52,9,0);
				}
		}
	if (which_mode == 1) {
		if ((mess1 == 0) && (get_health(ME) < (get_max_health(ME) * 4) / 5)) {
			mess1 = 1;
			i = 6614;
			set_terrain(0,42,12,0);
			}
			else if ((mess1 == 1) && (get_health(ME) < (get_max_health(ME) * 3) / 5)) {
				mess1 = 2;
				i = 6615;
				set_terrain(0,45,12,0);
				}
			else if ((mess1 == 2) && (get_health(ME) < (get_max_health(ME) * 2) / 5)) {
				mess1 = 3;
				i = 6617;
				set_terrain(0,48,12,0);
				}
		}
		
	if (i >= 0) {
		spawn_creature(i);
		run_char_animation(3,1,35);	
		pc_heard_sound_delay(171,100);	
		place_particle_num(i,1,1,8);
		run_text_on_char(ME,"Calls drake.",0);
		
		set_name(i,"Redstone Drake");
		set_coloradj(i,20);
		set_level(i,30);
		set_attack_bonus(i,20);
		set_resistance(i,0,95);
		set_resistance(i,1,95);
		set_resistance(i,2,95);
		set_resistance(i,3,95);
		set_resistance(i,4,95);
		set_resistance(i,5,95);
		set_resistance(i,6,95);
		set_resistance(i,7,100);
		
		if (gf(65,13) == 0) {
			sf(65,13,1);
			ok_dlog(104,2);
			}
			
		end();
		}
		
	if (tick_difference(last_abil,get_current_tick()) > 0) {
		last_abil = get_current_tick();
		phase = phase + 1;
		
		set_attack_bonus(ME,phase * (difficulty() + 2));
		run_text_on_char(ME,"Grows stronger.",0);
		
		
		
		}

	if ((mess2 == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
		mess2 = 1;
		last_abil2 = get_current_tick();
		
		if (which_mode == 0)  {
			run_text_on_char(ME,"Calls shade.",0);
			spawn_creature(6551);
			run_char_animation(3,1,35);	
			pc_heard_sound_delay(171,100);	
			place_particle_num(6551,1,1,8);
			}
		if (which_mode == 1)  {
			run_text_on_char(ME,"Calls shade.",0);
			spawn_creature(6618);
			run_char_animation(3,1,35);	
			pc_heard_sound_delay(171,100);	
			place_particle_num(6618,1,1,8);
			}
			
		ok_dlog(107,2);
		end();
		}


	if ((mess2 > 0) && (tick_difference(last_abil2,get_current_tick()) > 0)) {
		last_abil2 = get_current_tick();
		
		i = get_ran(1,0,1);
		if (which_mode == 0) {
			if (char_ok(6551 + i) == FALSE) {
				run_text_on_char(ME,"Calls shade.",0);
				spawn_creature(6551 + i);
				run_char_animation(3,1,35);	
				pc_heard_sound_delay(171,100);	
				place_particle_num(6551 + i,1,1,8);
				}
			}		
		if (which_mode == 1) {
			if (char_ok(6618 + i) == FALSE) {
				run_text_on_char(ME,"Calls shade.",0);
				spawn_creature(6618 + i);
				run_char_animation(3,1,35);	
				pc_heard_sound_delay(171,100);	
				place_particle_num(6618 + i,1,1,8);
				}
			}		
		
		end();
		}


	//	run_char_animation(3,1,35);	
	//	pc_heard_sound_delay(171,100);	
	
	//	place_particle_num(,1,1,8);
	
		


	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 ((which_mode == 0) && (gf(65,9) > 0)) {
		ok_dlog(52,1);
		end();
		}
		
	if (get_memory_cell(3) == 0) {
		print_named_str(ME,"doesn't respond.");
		end();
		}
	talk_done = 1;
	begin_talk_mode(get_memory_cell(3));
break;