// 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 last_abil3;
short last_abil4;
short used_abil = 0;
short did_travel = 0;
short mess1 = 0;

body;

beginstate INIT_STATE;
	change_max_health(ME,300);
	set_boss_level(ME,2);
	
	set_courage(ME,100);
	set_resistance(ME,2,100);
	set_resistance(ME,6,0);
	set_resistance(ME,7,100);

	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	last_abil3 = get_current_tick();
	last_abil4 = get_current_tick();

		place_particle_num(ME,22,12,3);		
		place_particle_num(ME,22,13,3);		
		place_particle_num(ME,24,12,3);		
		place_particle_num(ME,24,13,3);		
	break;

beginstate DEAD_STATE;
	erase_char(7391);
	erase_char(7392);
	erase_char(7393);
	erase_char(7394);
	erase_char(7395);
	erase_char(7396);

	erase_char(7382);
	erase_char(7383);
	erase_char(7384);
	erase_char(7385);
	erase_char(7386);
	erase_char(7387);
	
	sf(170,8,10);
	begin_talk_mode(23);
break;

beginstate START_STATE; 
	if (did_travel == 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 (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;

	if ((get_ran(1,0,100) < 80) && (is_combat()) && (get_nearest_hate_char(5) >= 0) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

			print_named_str(ME,"calls down a pillar of fire.");
		  	place_particle_num(ME,2,4,8);
			damage_nearby(get_ran(1,125,250),3,2,0);
			pc_heard_sound_delay(178,100);						
	  		create_missile_spiral(154,40,3,2);
			used_abil = 1;
		end();
		}
		
	// call in more warriors
	if ((is_combat()) && (tick_difference(last_abil2,get_current_tick()) > 1)) {
		i = get_ran(1,0,5);
		if (char_ok(7382 + i) == FALSE) {
			last_abil2 = get_current_tick();
			spawn_creature(7382 + i);
			set_name(7382 + i,"Horde Guardian");
			set_boss_level(7382 + i,1);
			set_strategy(7382 + i,10);
			
			}
		}
	
	// summon imps
	if ((is_combat()) && (tick_difference(last_abil3,get_current_tick()) > 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
		i = get_ran(1,0,5);
		
		if (char_ok(7391 + i) == FALSE) {
			if (summon_creature(7391 + i)) {
				last_abil3 = get_current_tick();

				print_named_str(ME,"summons a firebug.");
				place_particle_num(7391 + i,1,1,8);
		
				pc_heard_sound_delay(136,100);						
				run_char_animation(2,1,35);	

				
				if (mess1 == 0) {
					mess1 = 1;
					begin_talk_mode(21);
					}
				end();
				}
			}
		}

	// solberg lance of fire
	if ((char_ok(7364)) && (dist_to_char(7364) <= 9) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil4,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	

			print_named_str(ME,"lashes Solberg with a whip of flame.");
			damage_char(7364,get_ran(1,150,200),2);
			pc_heard_sound_delay(178,100);						
			shoot_projectile(ME,char_loc_x(7364),char_loc_y(7364),70);
			used_abil = 1;

		last_abil4 = get_current_tick();
		}

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

beginstate 4; // nav poin t
	if (dist_to_nav_point(ME,2) <= 1) {
		set_walk_speed(ME,0);
		did_travel = 1;
		set_state(START_STATE);
		}

	// base ae
	if ((is_combat()) && (get_nearest_hate_char(3) >= 0) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

			print_named_str(ME,"calls down a pillar of fire.");
		  	place_particle_num(ME,2,4,8);
			damage_nearby(get_ran(1,200,300),3,2,0);
			status_nearby(50,2,7,4);
			pc_heard_sound_delay(178,100);						
	  		create_missile_spiral(154,40,3,2);
			used_abil = 1;
		end();
		}
		
	approach_nav_point(ME,2,1);
	
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;