// 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 last_abil2 = 0;
short mess1 = 0;
short mess2 = 0;
short talked = 0;
short abil_count = 0;
short weak_targ;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	last_abil2 = get_current_tick();

	set_courage(ME,100);
	set_resistance(ME,7,90);

		set_level(ME,40);
		set_boss_level(ME,2);
		change_max_health(ME,100);

	set_resistance(ME,1,90);
	set_resistance(ME,2,90);
	set_resistance(ME,6,90);
	set_resistance(ME,7,90);

			alter_stat(ME,10,8);
			alter_stat(ME,19,8);
			alter_stat(ME,20,5);
			alter_stat(ME,28,10);
			alter_stat(ME,29,5);
	
	break;

beginstate DEAD_STATE;
	sf(90,5,1);
	begin_talk_mode(20);
break;

beginstate START_STATE; 
	if ((talked == 0) && (get_attitude(ME) < 10) && (get_nearest_party_char(6) >= 0)) {
		talked = 1;
		talk_no_exit(30);
		}
		
	// 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 (target_ok() == FALSE)
		set_state(START_STATE);

	if ((is_combat()) && (get_nearest_hate_char(6) >= 0) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		abil_count = abil_count + 1;
			last_abil = get_current_tick();
		if (abil_count == 4) {
			print_str_color("The nephil chieftain bends his knees. He is about to leap",3);
			print_str_color("  into the air.",3);

			set_resistance(ME,0,90);

			set_resistance(ME,1,20);
			set_resistance(ME,2,20);
			set_resistance(ME,6,20);

			set_walk_speed(ME,0);
			end_combat_turn();
			
			if (mess1 == 0) {
				mess1 = 1;
				begin_talk_mode(18);
				}
			
			end();
			}
		
		if ((abil_count == 5) || (abil_count == 6)) {
	
			run_char_animation(2,1,35);	
			print_str_color("The nephil chieftain spins around, cleaving the air with mighty blows.",3);
			damage_nearby(get_ran(1,1200,1300),2,0,0);
			pc_heard_sound_delay(3,100);				
			spray_missiles(15,8,2);
			spray_missiles(20,8,2);

			end_combat_turn();

			if (abil_count == 6) {
				set_walk_speed(ME,24);

				abil_count = 0;
				set_resistance(ME,0,20);

				set_resistance(ME,1,90);
				set_resistance(ME,2,90);
				set_resistance(ME,6,90);
				}
			if (mess2 == 0) {
				mess2 = 1;
				begin_talk_mode(19);
				}
			end();
			}

		
		}

	if ((is_combat()) && (get_nearest_hate_char(6) >= 0) && (tick_difference(last_abil2,get_current_tick()) > 0)) {
		last_abil2 = get_current_tick();
		
		i = get_ran(1,0,3);
		if (char_ok(9768 + i) == FALSE) {
			print_str_color("You can hear Fang Clan warriors climbing up the cliff.",2);
			spawn_creature(9768 + i);
			set_name(9768 + i,"Fang Clan Defender");
			set_summon_level(9768 + i,1);
			set_level(9768 + i,36);
			set_boss_level(9768 + i,-1);
			force_char_status(9768 + i,24,25);
			alter_stat(9768 + i,10,8);
			alter_stat(9768 + i,20,8);
			set_resistance(9768 + i,7,60);
			}
		}

	weak_targ = most_frail_pc();

	if ((can_see_char(get_pc_id(weak_targ))) && (dist_to_char(get_pc_id(weak_targ)) <= 5))
		set_foe_target(ME,get_pc_id(weak_targ));

	do_attack();
break;

beginstate TALKING_STATE;
	if (talked > 0) {
		begin_talk_mode(17);
		end();
		}
	talked = 1;	
	talk_no_exit(20);
break;