// 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 r1,target;
short last_abil;
short last_abil2;
short used_abil = 0;
short ae_range = 4;
short which_ae = 0;
short desc1 = 0,desc2 = 0;
short bonusbugs = 0;

body;

beginstate INIT_STATE;
	set_boss_level(ME,2);
	activate_hidden_group(43);
	
	sf(158,8,0);
	sf(158,10,1);
	
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(158,7,1);
	begin_talk_mode(9);
break;

beginstate START_STATE; 
	if ((get_nearest_party_char(8) >= 0) && (desc1 == 0)) {
		desc1 = 1;
		begin_talk_mode(7);
		}
	if ((bonusbugs == 0) && (gf(158,8) > 10)) {
		bonusbugs = 1;
		activate_hidden_group(44);
		add_range_to_group(17085,17087,43);
		set_name(30043,"Cauldron Worm");
		set_level(30043,8);
		begin_talk_mode(14);
		}
		
	// 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 ((party_dist_to_spot(289,735) <= 28) && (tick_difference(last_abil2,get_current_tick()) > 20)) {
		last_abil2 = get_current_tick();
		if (gf(158,10) == 0) {
			print_str_color("A long, low moan echoes through the caverns.",2);
			sf(158,10,1);
			}
			else {
				print_str_color("A high, warbling moan echoes through the caverns.",2);
				sf(158,10,0);
				}
		r1 = get_ran(1,0,19);
		if (char_ok(17065 + r1) == FALSE) {
			spawn_creature(17065 + r1);
			set_name(17065 + r1,"Spawned Worm");
			set_level(17065 + r1,4);
			set_attack_bonus(17065 + r1,4);
			set_summon_level(17065 + r1,1);
			print_str_color("The fungal beast calls a fresh worm out of the spawning pools.",2);			
			end();
			}

		}

	// 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 (desc1 == 0) {
		desc1 = 1;
		begin_talk_mode(7);
		}

	if (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;
	
	if ((is_combat()) && (tick_difference(last_abil2,get_current_tick()) > 1)) {
		r1 = get_ran(1,0,19);
		if (char_ok(17065 + r1) == FALSE) {
			last_abil2 = get_current_tick();
			spawn_creature(17065 + r1);
			set_name(17065 + r1,"Spawned Worm");
			set_level(17065 + r1,7);
			set_attack_bonus(17065 + r1,7);
			set_summon_level(17065 + r1,1);
			place_particle_num(17065 + r1,1,1,8);
			run_char_animation(2,1,35);	
			play_sound(256);
			player_see_text("The fungal beast calls forth another disgusting worm.");
			if (desc2 == 0) {
				desc2 = 1;
				begin_talk_mode(8);
				}
			end();
			}
			
		}
		
	if ((is_combat()) && (tick_difference(last_abil,get_current_tick()) > 2)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		if (which_ae == 0) { // ae poison
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits a cloud of poisonous spores.");
			status_nearby(6,ae_range,2,4);
	  		create_missile_spiral(142,40,ae_range,2);
			pc_heard_sound_delay(177,100);						
			which_ae = 1;
			used_abil = 1;
			}
		else if (which_ae == 1) { // ae terror
		  	place_particle_num(ME,4,10,8);
			print_named_str(ME,"radiates pure terror.");
			status_nearby(15,ae_range,7,4);
			//particle_nearby(ae_range,23,2,4,2);
			pc_heard_sound_delay(177,100);						
			used_abil = 1;
			which_ae = 2;
			}
		else if (which_ae == 2) { // ae weaken
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits a cloud of spores. They confuse you.");
			status_nearby(-6,ae_range,8,4);
	  		create_missile_spiral(142,40,ae_range,2);
	  		damage_nearby(get_ran(6,1,8),ae_range,5,0);			pc_heard_sound_delay(177,100);						
			used_abil = 1;
			which_ae = 0;
			}


		
		}

	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;