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

// 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 = 6;
short pc_hunter = 0;

body;

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

	if (get_ran(1,0,1) == 0)
		pc_hunter = 1;
		
	if (get_memory_cell(6) == 0) {
		set_level(ME,28);
		set_attack_bonus(ME,20);
		set_new_abil(ME,11);
		if (get_ran(1,0,100) < 50) {
			set_name(ME,"Runeward Imp");
			set_resistance(ME,1,90);
			set_resistance(ME,2,90);
			set_resistance(ME,6,90);
			}
			else {
				set_name(ME,"Bladeward Imp");
				set_resistance(ME,0,90);
				set_coloradj(ME,128);
				}
		}
	if (get_memory_cell(6) == 1) {
		set_name(ME,"Flaming Hellbeast");
		set_level(ME,30);
		set_new_abil(ME,20);
		set_attack_bonus(ME,10);
		
		set_resistance(ME,1,100);
		set_resistance(ME,2,100);
		set_resistance(ME,6,0);
		}
	if (get_memory_cell(6) == 2) {
		set_name(ME,"Terror Wight");
		set_level(ME,28);
		set_new_abil(ME,12);
		set_attack_bonus(ME,10);
		set_resistance(ME,2,0);
		}
	if (get_memory_cell(6) == 3) {
		set_name(ME,"Icy Horror");
		set_level(ME,34);
		set_new_abil(ME,20);
		set_attack_bonus(ME,10);
		set_resistance(ME,1,0);
		set_resistance(ME,2,100);
		set_resistance(ME,6,100);
		}
	if (get_memory_cell(6) == 4) {
		set_name(ME,"Infernal Thrasher");
		pc_hunter = 1;
		set_level(ME,34);
		set_boss_level(ME,1);
		set_new_abil(ME,20);
		set_attack_bonus(ME,10);
		}
	if (get_memory_cell(6) == 5) {
		set_name(ME,"Infernal Thrasher");
		pc_hunter = 1;
		set_level(ME,34);
		set_boss_level(ME,1);
		set_new_abil(ME,20);
		set_attack_bonus(ME,10);
		}
	if (get_memory_cell(6) == 7) {
		set_name(ME,"Infernal Lord");
		pc_hunter = 1;
		set_boss_level(ME,2);
		change_max_health(ME,600);
		set_coloradj(ME,1);
		}


	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);
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 (pc_hunter == 0) {
		if (dist_to_loc(ME,700 + gf(164,26),700 + gf(164,27)) > 5)
			move_to_loc(ME,700 + gf(164,26),700 + gf(164,27));
			else if (get_foe_target(ME,8,0) == FALSE)
				pc_hunter = 1;
		}
	if ((pc_hunter > 0) && (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 (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;

	if ((get_ran(1,0,100) < 80) && (is_combat()) && 
	  ((get_memory_cell(6) == 1) || (get_memory_cell(6) == 2) || (get_memory_cell(6) == 7)) && (tick_difference(last_abil,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		if (get_memory_cell(6) == 1) { // ae fire
			print_named_str(ME,"breathes a powerful gout of fire.");
		  	place_particle_num(ME,2,4,8);
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,6),ae_range,2,0);
			pc_heard_sound_delay(178,100);						
	  		create_missile_spiral(154,40,ae_range,2);
			used_abil = 1;
			}
		if (get_memory_cell(6) == 2) { // ae terror
		  	place_particle_num(ME,4,10,8);
			print_named_str(ME,"radiates pure terror.");
			status_nearby(get_level(ME) + 5,ae_range,7,4);
			//particle_nearby(ae_range,23,2,4,2);
			pc_heard_sound_delay(177,100);						
			used_abil = 1;
			}
			
	
		if (get_memory_cell(6) == 7) { // ae terror
		  	place_particle_num(ME,18,5,10);
			print_named_str(ME,"buffets you with a wave of infernal power.");
			drain_energy_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,4),ae_range,0);
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,12),ae_range,1,0);
			pc_heard_sound_delay(131,100);			
	  		create_missile_spiral(147,40,ae_range,2);
			status_nearby(get_level(ME) / 4,ae_range,22,0);
			used_abil = 1;
			}

		}

	if (used_abil == 0)
		do_attack();
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;