// beamnpc.txt
// A monster with a special ability scripted in. The ability is an beam, used 
// 50% of the time.

// 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 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - The ability.

// The abilities:
// 0 - Beam Slow
// 1 - Beam Curse
// 2 - Beam Fire (1-6 dam per level)
// 3 - Beam Acid
// 4 - Beam Terror
// 5 - Beam Daze
// 6 - Beam Debuff
// 7 - Beam Cold (1-8 dam per level)
// 8 - Beam Energy (1-10 dam per level)
// 9 - Beam Charm
// 10 - Beam Freeze
// 11 - lightning
// 12 poison

begincreaturescript;

variables;

short i,target;
short last_abil;
short is_close = 0;
short num_flees = 0;
short mess1 = 0;
short mess2 = 0;
short mess3 = 0;
short walk_status = 0;
short weak_targ;

body;

beginstate INIT_STATE;
	set_name(ME,"Tunnel Horror");
	set_level(ME,20);
	set_boss_level(ME,2);
	set_aggression(ME,70);
	
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(166,3,1);
	begin_talk_mode(34);
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,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 (gf(166,14) > 0) {
		if (get_attitude(ME) >= 10) {
			set_foe_target(ME,random_group_member(0));
			set_state(3);
			}
		}
		else if (gf(166,2) > 0) {
			if (walk_status == 0)
				i = 0;
			if (walk_status == 1)
				i = 1;
			if (walk_status == 2)
				i = 0;
			if (walk_status == 3)
				i = 2;
			if (walk_status == 4)
				i = 3;
			if (walk_status == 5)
				i = 4;
			if (walk_status == 6)
				i = 3;
			if (walk_status == 7)
				i = 2;
			if (dist_to_nav_point(ME,i) <= 2) {
				walk_status = walk_status + 1;
				if (walk_status == 8)
					walk_status = 0;
				}
				else approach_nav_point(ME,i,2);
			}

	if ((dist_to_party() <= 14) && (is_close == 0)) {
		print_str_color("You hear something large and dangerous. It's getting close to you.",3);
		is_close = 1;
		}
		else if ((dist_to_party() > 14) && (is_close == 1)) {
			print_str_color("The ominous presence is getting farther away.",3);
			is_close = 0;
			}
			
	// 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 ((dist_to_loc(ME,781,709) <= 9) || (dist_to_loc(ME,801,751) <= 9)) {
		print_str_color("The horror is too far from its lair. It runs away.",3);
		relocate_char(ME,779,760,0);
		heal_char(ME,500);
		walk_status = 0;
		set_foe_target(ME,-1);
		
		set_state(START_STATE);
		}
		
	if (mess1 == 0) {
		mess1 = 1;
		begin_talk_mode(31);
		}
	if ((mess2 == 0) && (get_health(ME) < get_max_health(ME) / 2)) {
		mess2 = 1;
		set_new_abil(ME,20);
		alter_stat(ME,28,8);
		begin_talk_mode(33);
		}
	if ((mess3 == 0) && (my_dist_from_start() < 12) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
		mess3 = 1;
		sf(166,13,1);
		set_attitude(17957 + gf(166,13) - 1,10);
		set_act_at_dist(17957 + gf(166,13) - 1,1);
		set_new_abil(ME,20);
		begin_talk_mode(32);
		}
	if ((num_flees < 3) && (get_health(ME) < get_max_health(ME) - 200) && (my_dist_from_start() > 7)) {
		num_flees = num_flees + 1;
		print_str_color("The horror is in too much pain. It turns and begins to lurch away.",3);
		set_foe_target(ME,-1);
		set_state(4);
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if (mess3 > 0) {
		weak_targ = most_frail_pc();
		if ((can_see_char(get_pc_id(weak_targ))) && 	(dist_to_char(get_pc_id(weak_targ)) <= 7))
			set_foe_target(ME,get_pc_id(weak_targ));
		}
	
	// big abil
	if ((mess2 > 0) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		
		// activate spawn
		if (gf(166,13) < 6) {
			mess3 = 1;
			inc_flag(166,13,1);
			set_attitude(17957 + gf(166,13) - 1,10);
			set_act_at_dist(17957 + gf(166,13) - 1,1);
			
			}

		run_char_animation(2,1,35);	

			print_named_str(ME,"screeches and spits a hideous gout of acid!");
			set_char_status(get_target(),6,12);
			//run_sparkles_on_char(get_target(),171,1,2);
			pc_heard_sound_delay(177,100);						
			damage_char(get_target(),get_ran(1,60,70),5);
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),32);

			
		last_abil = get_current_tick();
		end();
		}

		do_attack();
break;

beginstate 4; // returning to start
	if (my_dist_from_start() < 4) {
		walk_status = 0;
		heal_char(ME,200);
		set_state(START_STATE);
		}

	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		print_str_color("Your attack angers the horror. It is no longer retreating!",3);
		heal_char(ME,200);
		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(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;