// aenpc.txt
// A monster with a special ability scripted in. The ability is an AE, used 
// every 2-3 turns or so.

// 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.
//   Cell 5 - The range of the ae, defaults to 6

// The abilities:
// 0 - AE Slow
// 1 - AE Curse
// 2 - AE Fire (1-4 dam per level)
// 3 - AE Acid
// 4 - AE Terror
// 5 - AE Daze
// 6 - AE Debuff
// 7 - AE Cold (1-6 dam per level)
// 8 - AE Energy (1-8 dam per level)
// 9 - AE Charm
// 10 - AE Poison
// 11 - AE mana drain
// 12 - AE mana drain + damage
// 13 - stomps feet
// 14 - lightnign aura
// 15 - cleave (1-8 dam per level)
// 16 - AE Curse & Weaken
// 17 - AE Acid + damage

begincreaturescript;

variables;

short i,target;
short last_abil;
short used_abil = 0;
short ae_range = 3;
short which_ae = 0;
short weak_targ = 0;
short dest;
short force_change = 0;
short h_level;

body;

beginstate INIT_STATE;
	set_name(ME,"Lord Farrar");
	set_level(ME,35);
	set_boss_level(ME,2);
	change_max_health(ME,50);
	set_courage(ME,100);
	set_resistance(ME,7,90);
	set_new_abil(ME,8);

	alter_stat(ME,19,10);
	alter_stat(ME,28,10);

	h_level = get_max_health(ME);
	
	last_abil = get_current_tick();
	
	if (gf(170,10) > 0)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
	sf(170,10,1);
	begin_talk_mode(15);
break;

beginstate START_STATE; 
	if ((gf(30,5) > 0) && (on_which_layer() == 0))
		set_state(4);
	
	// 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 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 ((gf(30,5) > 0) && (on_which_layer() == 0))
		set_state(4);

	if ((dest < 5) && (on_which_layer() == 1)) {
		if (h_level - get_health(ME) > 800) {
			set_state(4);	
			}
		}
	
	if ((gf(30,14) == 0) && (on_which_layer() == 0)) {
		sf(30,14,1);
		begin_talk_mode(16);
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;

	if ((is_combat()) && (get_nearest_hate_char(ae_range) >= 0) && (tick_difference(last_abil,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		if (which_ae == 0) { // ae terror
		  	place_particle_num(ME,4,10,8);
			print_named_str(ME,"uses a magical charm. It radiates pure terror.");
			status_nearby(get_level(ME) + 2,ae_range,7,4);
			//particle_nearby(ae_range,23,2,4,2);
			pc_heard_sound_delay(177,100);						
			which_ae = 1;
			used_abil = 1;
			}
		else if (which_ae == 1) { // ae daze
		  	place_particle_num(ME,3,1,8);
			print_named_str(ME,"uses a magical charm. You feel dizzy.");
			status_nearby(get_level(ME) + 2,ae_range,17,4);
			//particle_nearby(ae_range,8,2,4,2);
			pc_heard_sound_delay(195,100);			
			which_ae = 2;
			used_abil = 1;
			}
		else if (which_ae == 2) { // ae charm
		  	place_particle_num(ME,16,10,10);
			print_named_str(ME,"uses a magical charm. You feel compelled to follow Farrar.");
			status_nearby(get_level(ME) + 2,ae_range,5,4);
			//particle_nearby(ae_range,17,2,4,8);
			pc_heard_sound_delay(100,100);						
			which_ae = 0;
			used_abil = 1;
			}

		
		}
		
	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));

	if (used_abil == 0)
		do_attack();
break;

beginstate 4; // returning to start
	if (force_change > 0) {
		force_change = 0;
		set_state(START_STATE);
		}		
		
	if (dest > 4)
		set_state(START_STATE);
		
	if (on_which_layer() == 0)
		dest = 3;
	
	if (dist_to_nav_point(ME,dest) <= 1) {
		set_foe_target(ME,-1);
		if (on_which_layer() == 0) {
			spawn_creature(3307);
			erase_char(ME);
			}
			else {
				i = 0;
				if ((get_nearest_party_char(10) >= 0) || (dist_to_party() <= 9))
					i = 1;
					
				if (dest == 0) {
					relocate_char(ME,161,147,1);
					heal_char(ME,800);
					}
				if (dest == 1) {
					relocate_char(ME,158,158,1);
					heal_char(ME,800);
					}
				if (dest == 2) {
					relocate_char(ME,166,165,1);
					heal_char(ME,800);
					}
				if (dest == 3) {
					relocate_char(ME,175,160,1);
					heal_char(ME,800);
					}

				h_level = get_health(ME);
				
				if (dest < 4) {
					summon_creature(3303);
					summon_creature(3304);
					if (char_ok(3305) == FALSE) {
						summon_creature(3305);
						set_name(3305,"Barghan Guard");
						set_level(3305,30);
						}
					summon_creature(3306);
					alert_char(3303);
					alert_char(3304);
					alert_char(3305);
					alert_char(3306);
					}
					
				dest = dest + 1;
				if ((dest >= 5) && (get_nearest_party_char(10) >= 0)) {
					force_change = 1;
					begin_talk_mode(18);
					}
				if (i > 0) {
					if ((dest < 4) && (gf(30,13) == 0)) {
						sf(30,13,1);
						force_change = 1;
						begin_talk_mode(17);
						}
						else print_str_color("There is a flash of light, and Lord Farrar disappears.",3);
					}
				}
		end();
		}
		
	if ((get_nearest_hate_char(ae_range) >= 0) && (tick_difference(last_abil,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		  	place_particle_num(ME,4,10,8);
			print_named_str(ME,"uses a magical charm. It radiates pure terror.");
			status_nearby(70,ae_range,7,4);
			//particle_nearby(ae_range,23,2,4,2);
			pc_heard_sound_delay(177,100);						
			used_abil = 1;
		
		}
		
	approach_nav_point(ME,dest,1);
break;

beginstate TALKING_STATE;
	if (gf(30,5) > 0)
		print_str("Talking: Lord Farrar has nothing else to say to you.");
		else begin_talk_mode(30);
break;