// 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;
short used_abil = 0;
short ae_range = 6;
short act_counter = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Zahur Firecaller");
	set_level(ME,40);
	change_max_health(ME,400);
	set_boss_level(ME,2);
	set_walk_speed(ME,0);

	last_abil = get_current_tick();
	last_abil2 = get_current_tick();

		ae_range = 2;
	break;

beginstate DEAD_STATE;
	sf(45,5,1);
	sf(45,8,0);

	set_floor_layer(1,214,242,4,0);
	set_floor_layer(1,214,243,4,0);
	set_floor_layer(1,214,244,4,0);
	set_floor_layer(1,214,245,4,0);
	set_floor_layer(1,214,246,4,0);
	set_floor_layer(1,214,247,4,0);

	set_floor_layer(1,221,249,4,0);
	set_floor_layer(1,220,249,4,0);
	set_floor_layer(1,219,249,4,0);
	set_floor_layer(1,218,249,4,0);
	set_floor_layer(1,217,249,4,0);
	set_floor_layer(1,216,249,4,0);
	set_floor_layer(1,215,249,4,0);
	set_floor_layer(1,214,249,4,0);

	begin_talk_mode(15);
break;

beginstate START_STATE; 
	if ((get_nearest_party_char(8) >= 0) && (act_counter < 2)) {
		if (tick_difference(last_abil,get_current_tick()) > 0) {
			act_counter = act_counter + 1;
			if (act_counter == 1)
				begin_talk_mode(12);
			if (act_counter == 2)
				begin_talk_mode(13);
			}
		end();
		}
		
	// 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);
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// 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 (get_nearest_party_char(8) >= 0) {
		if (tick_difference(last_abil,get_current_tick()) > 0) {
			act_counter = act_counter + 1;
			if (act_counter == 4) {
				sf(45,8,1);
				begin_talk_mode(14);
				}
			if (act_counter == 7) {
				summon_creature(4891);
				summon_creature(4892);
				summon_creature(4893);
				summon_creature(4894);
				add_range_to_group(4891,4894,1);
				set_level(30001,25);
				set_name(30001,"Slavering Imp");
				set_attack_bonus(30001,25);
				set_resistance(30001,0,100);
				set_resistance(30001,1,100);
				set_resistance(30001,2,100);
				set_resistance(30001,4,100);
				set_resistance(30001,5,100);
				set_resistance(30001,6,100);

				set_resistance(4891,0,0);
				set_resistance(4892,1,0);
				set_resistance(4893,2,0);
				set_resistance(4894,6,0);
				
				place_particle_num(4891,21,12,3);		
				place_particle_num(4891,21,13,3);					
				place_particle_num(4892,22,12,3);		
				place_particle_num(4892,22,13,3);					
				place_particle_num(4893,23,12,3);		
				place_particle_num(4893,23,13,3);					
				place_particle_num(4894,24,12,3);		
				place_particle_num(4894,24,13,3);					
		
				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_abil2,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	
		last_abil2 = get_current_tick();

		  	place_particle_num(ME,3,9,8);
			print_named_str(ME,"casts a powerful curse.");
			status_nearby(-15,ae_range,1,4);
			status_nearby(-15,ae_range,0,4);
			drain_energy_nearby(40,ae_range,0);
	  		create_missile_spiral(151,40,ae_range,2);
			//particle_nearby(ae_range,2,2,4,8);
			pc_heard_sound_delay(174,100);						
		end();
		
		}

	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;