// 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_freq;
short mess1 = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Archmage Solberg");
	set_boss_level(ME,2);
	change_max_health(ME,100);
	set_attack_bonus(ME,100);
	
	set_courage(ME,100);
	set_resistance(ME,2,80);
	set_resistance(ME,7,100);
	
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();

	break;

beginstate DEAD_STATE;
	begin_talk_mode(22);
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,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 (gf(68,6) == 2) {
		if (dist_to_nav_point(ME,0) < 2) {
			if ((get_nearest_party_char(4) >= 0) && (is_talking() == FALSE)) {
				sf(68,14,1);
				sf(68,6,3);
				begin_talk_mode(15);
				}
				
			}
			else approach_nav_point(ME,0,1);
		}
	
	if (gf(68,6) == 3) {
		if (dist_to_nav_point(ME,1) <= 2) {
			if ((party_within_dist(799,276,6)) && (is_talking() == FALSE)) {
				sf(68,14,0);
				sf(68,6,4);
				
				set_walk_speed(ME,0);
				
				// make lizards
				activate_hidden_group(19);
				add_range_to_group(7366,7373,22);
				set_name(30022,"Horde Warlizard");
				set_boss_level(30022,1);
				alter_stat(30022,10,8);
				alter_stat(30022,19,8);
				set_attack_bonus(30022,8);
				set_resistance(30022,6,0);
				
				begin_talk_mode(17);
				}
				
			}
			else approach_nav_point(ME,1,1);
		}
	if (gf(68,6) == 4) {
		if (((gf(68,20) >= 8) || (num_chars_in_group(22) == 0)) && (is_talking() == FALSE)) {
			sf(68,6,5);

			// reset group
			remove_char_from_group(7366,22);
			remove_char_from_group(7367,22);
			remove_char_from_group(7368,22);
			remove_char_from_group(7369,22);
			remove_char_from_group(7370,22);
			remove_char_from_group(7371,22);
			remove_char_from_group(7372,22);
			remove_char_from_group(7373,22);
				
			// make humans
			activate_hidden_group(20);
			add_range_to_group(7374,7381,22);
			set_name(7374,"Drake Minion");
			set_level(7374,30);
			set_name(7375,"Drake Minion");
			set_level(7375,30);
			set_name(7376,"Warrior Minion");
			set_strategy(7376,10);
			set_name(7377,"Warrior Minion");
			set_strategy(7377,10);
			set_name(7378,"Warrior Minion");
			set_strategy(7378,10);
			set_name(7379,"Warrior Minion");
			set_strategy(7379,10);
			set_name(7380,"Archer Minion");
			set_strategy(7380,12);
			set_name(7381,"Archer Minion");
			set_strategy(7381,12);
			
			set_boss_level(30022,1);
			set_resistance(30022,6,0);

			begin_talk_mode(18);	
			}
		}		
	if (gf(68,6) == 5) {
		if (((gf(68,21) >= 8) || (num_chars_in_group(22) == 0)) && (is_talking() == FALSE)) {
			sf(68,6,6);

			// reset group
			remove_char_from_group(7374,22);
			remove_char_from_group(7375,22);
			remove_char_from_group(7376,22);
			remove_char_from_group(7377,22);
			remove_char_from_group(7378,22);
			remove_char_from_group(7379,22);
			remove_char_from_group(7380,22);
			remove_char_from_group(7381,22);
			
			// make sliths
			activate_hidden_group(21);
			add_range_to_group(7382,7387,22);
			set_name(30022,"Hero of the Horde");
			set_new_abil(7382,2);
			set_new_abil(7383,3);
			set_resistance(7382,6,80);
			set_resistance(7383,6,80);
			set_new_abil(7384,6);
			set_new_abil(7385,7);
			set_resistance(7384,0,80);
			set_resistance(7385,0,80);
			set_new_abil(7386,8);
			set_new_abil(7387,9);
			set_resistance(7386,1,80);
			set_resistance(7387,1,80);
			set_level(30022,30);
			set_boss_level(30022,1);
			set_strategy(30022,10);
			set_resistance(30022,6,0);
			
			begin_talk_mode(19);	
			}
		}		
	if (gf(68,6) == 6) {
		if (((gf(68,22) >= 8) || (num_chars_in_group(22) == 0)) && (is_talking() == FALSE)) {
			sf(68,6,7);

			// make ess kalyn servants and sentinels
			activate_hidden_group(22);

			set_name(7389,"Ess-Kalyn's Servant");
			set_level(7389,32);
			set_new_abil(7389,7);
			set_name(7390,"Ess-Kalyn's Servant");
			set_level(7390,32);
			set_new_abil(7390,9);

			spawn_creature(7382);
			set_name(7382,"Horde Guardian");
			set_boss_level(7382,1);
			set_strategy(7382,10);
			spawn_creature(7383);
			set_name(7383,"Horde Guardian");
			set_boss_level(7383,1);
			set_strategy(7383,10);
			
			spawn_creature(7397);
			set_name(7397,"Solberg's Guardian");
			set_level(7397,30);
			set_boss_level(7397,1);
			set_attack_bonus(7397,20);
			spawn_creature(7398);
			set_name(7398,"Solberg's Guardian");
			set_level(7398,30);
			set_boss_level(7398,1);
			set_attack_bonus(7398,20);
			
			begin_talk_mode(20);	
			}
		}		
	if (gf(68,6) == 7) {
		if ((gf(170,8) > 0) && (is_talking() == FALSE)) {
			sf(68,6,8);
			sf(68,14,1);
			sf(68,15,1);
			begin_talk_mode(24);	
			}
		}

	// 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;

	ae_freq = 0;
	if (gf(68,4) == 4)
		ae_freq = 1;
	if (gf(68,4) == 5)
		ae_freq = 2;
	if (gf(68,4) == 6)
		ae_freq = 3;
	if (gf(68,4) == 7)
		ae_freq = 4;
	if ((is_combat()) && (get_nearest_hate_char(6) >= 0) && (tick_difference(last_abil,get_current_tick()) > ae_freq)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"casts a powerful cold spell, draining the life from his foes.");
			damage_nearby(get_ran(1,500,600),6,6,1);
			heal_char(ME,40 * ae_freq + get_ran(1,0,100));
			heal_char(30000,50);
			pc_heard_sound_delay(144,100);						
	  		create_missile_spiral(151,40,6,2);
			used_abil = 1;

		if (mess1 == 0) {
			mess1 = 1;
			begin_talk_mode(14);
			}
		
		}

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

beginstate TALKING_STATE;
	if (gf(68,6) > 1) {
		begin_talk_mode(26);
		end();
		}
	begin_talk_mode(30);
break;