// 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 tried_return = 0;
short r1 = 0;
short last_abil;
short mess1 = 0;
short mess2 = 0;
short mess3 = 0;
short mess4 = 0;
short buff_level = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Elderan");
	set_level(ME,28);
	change_max_health(ME,200);
	set_boss_level(ME,2);
	set_coloradj(ME,0);
	set_scaling(ME,100);
	set_strategy(ME,0);
	
	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();
	break;

beginstate DEAD_STATE;
	sf(102,11,1);
	run_hardcode(1034);
	
			erase_char(10214);
			erase_char(10215);
			erase_char(10216);
			erase_char(10217);
	ok_dlog(31,2);
break;

beginstate START_STATE; 

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

	set_strategy(ME,0);
	add_ability(ME, 11, -1, 0);

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

	if ((my_loc_x() >= 21) && (my_loc_x() <= 23) && (my_loc_y() >= 26) && (my_loc_y() <= 28)) {
		//if ((get_char_status(ME,4) > 0) || (get_char_status(ME,18) > 0)) {
			//print_str_color("Elderan's magical shield flows into the circle //below his feet.",2);
			//force_char_status(ME,4,0);
			//force_char_status(ME,18,0);
			//give_char_text_bubble(ME,"Shield absorbed.");
			//if (mess1 == 0) {
			//	mess1 = 2;
			//	ok_dlog(42,1);
			//	}
			//}
		}
		else {
			print_str_color("Elderan's magic circle summons and shields him.",2);
			
			run_char_animation(3,1,35);	
			play_sound(102);
			give_char_text_bubble(ME,"Shielded.");
			if (difficulty() == 0)
				set_char_status(ME,4,2);
				else set_char_status(ME,18,2);
			set_char_status(ME,21,3);
			relocate_char(ME,22,27,TRUE);
			set_foe_target(ME,-1);
			get_foe_target(ME,8,0);
			if (mess2 == 0) {
				mess2 = 2;
				ok_dlog(40,1);
				}
			end();
			}
	
	if (char_ok(10214)) {
		if (dist_to_char(10214) <= 2) {
			print_str_color("Elderan absorbs one of his shades! He is stronger!",2);
			play_sound(181);
			erase_char(10214);
			buff_level = buff_level + 1;
			run_char_animation(3,1,35);	
			if (difficulty() > 0)
				heal_char(ME,difficulty() * 50);
			if (mess3 == 0) {
				mess3 = 2;
				ok_dlog(44,2);
				}
			end();
			}
		}
	if (char_ok(10215)) {
		if (dist_to_char(10215) <= 2) {
			print_str_color("Elderan absorbs one of his shades! He is stronger!",2);
			play_sound(181);
			erase_char(10215);
			buff_level = buff_level + 1;
			run_char_animation(3,1,35);	
			if (difficulty() > 0)
				heal_char(ME,difficulty() * 50);
			if (mess3 == 0) {
				mess3 = 2;
				ok_dlog(44,2);
				}
			end();
			}
		}
	if (char_ok(10216)) {
		if (dist_to_char(10216) <= 2) {
			print_str_color("Elderan absorbs one of his shades! He is stronger!",2);
			play_sound(181);
			erase_char(10216);
			buff_level = buff_level + 1;
			run_char_animation(3,1,35);	
			if (difficulty() > 0)
				heal_char(ME,difficulty() * 50);
			if (mess3 == 0) {
				mess3 = 2;
				ok_dlog(44,2);
				}
			end();
			}
		}
	if (char_ok(10217)) {
		if (dist_to_char(10217) <= 2) {
			print_str_color("Elderan absorbs one of his shades! He is stronger!",2);
			play_sound(181);
			erase_char(10217);
			buff_level = buff_level + 1;
			run_char_animation(3,1,35);	
			if (difficulty() > 0)
				heal_char(ME,difficulty() * 50);
			if (mess3 == 0) {
				mess3 = 2;
				ok_dlog(44,2);
				}
			end();
			}
		}
	set_attack_bonus(ME,buff_level * 5 * (difficulty() + 1));
	
	i = 0;
	if (difficulty() < 2)
		i = 1;
	if (tick_difference(last_abil,get_current_tick()) > i) {
		last_abil = get_current_tick();

		r1 = get_ran(1,0,3);
		if (char_ok(10214 + r1))
			r1 = get_ran(1,0,3);
		if (char_ok(10214 + r1) == FALSE) {
			print_str_color("Elderan summons forth a shade!",2);
			spawn_creature(10214 + r1);
			run_char_animation(3,1,35);	
			pc_heard_sound_delay(171,100);	
			add_ability(ME, 2, -1, 0);
			place_particle_num(10214 + r1,1,1,8);
			end();
			}
		
		}

	//	run_char_animation(3,1,35);	
	//	pc_heard_sound_delay(171,100);	
	
	//	place_particle_num(,1,1,8);
	
	//if ((mess1 == 0) && (get_health(ME) < (get_max_health(ME) * 5) / 10)) {
	//	mess1 = 1;
	//	}
		
	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);
		}

	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;

	talk_no_exit(30);
break;