// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home. A conversation can be set for it,
// if it's friendly.
// 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.

begincreaturescript;

variables;

short i,target;
short tried_return = 0;
short enraged = 0;
short num_regen = 0;
short used_abil = 0;
short last_abil;
short last_abil2;
short last_abil3;

body;

beginstate INIT_STATE;
	set_name(ME,"Ancient Worm");
	set_level(ME,20);
	set_boss_level(ME,2);
	change_max_health(ME,200);
	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();
	break;

beginstate DEAD_STATE;
	begin_talk_mode(14);
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);
		}
		
	// 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) {
			tried_return = 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 (target_ok() == FALSE)
		set_state(START_STATE);

	if (get_ran(1,0,3) == 1)
		tried_return = 0;
		
	if ((my_dist_from_start() >= 17) && (tried_return == 0)) {
		print_named_str(ME,"tries to slither back to its lair.");
		tried_return = 1;
		set_foe_target(ME,-1);
		return_to_start(ME,2);
		set_state(4);
		}

	used_abil = 0;

	if ((get_ran(1,0,100) < 80) && (is_combat()) && (num_regen < 20) && (tick_difference(last_abil,get_current_tick()) > 0) &&
	(get_health(ME) < get_max_health(ME) - 300)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"regenerates itself.");
				heal_char(ME,280);
			pc_heard_sound_delay(177,100);						
		
		num_regen = num_regen + 1;
		if (num_regen == 1)
			begin_talk_mode(10);
		if ((num_regen == 2) && (gf(155,14) == 1) && (has_spec_item(27) == 0)) {
			set_spec_item(27,1);
			begin_talk_mode(11);
			}
		if ((num_regen == 3) && (gf(155,14) == 1)) {
			begin_talk_mode(15);
			}
		if (num_regen == 5) {
			enraged = 1;
			begin_talk_mode(13);
			}
		used_abil = 1;
		end();
		}

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

		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"spits out a spray of acid.");
			status_nearby(5,7,6,4);
			if (enraged == 0)
		  		damage_nearby(get_ran(1,40,80),7,5,0);
		  		else damage_nearby(get_ran(1,250,350),7,5,0);
	  		create_missile_spiral(142,40,7,2);
			pc_heard_sound_delay(177,100);						
			used_abil = 1;
		end();
		}
	
	if ((get_ran(1,0,100) < 80) && (is_combat()) && (num_regen > 0) && (char_ok(15147) + char_ok(15148) + char_ok(15149) < 2) && (tick_difference(last_abil3,get_current_tick()) > 3)) {
		run_char_animation(2,1,35);	
		last_abil3 = get_current_tick();

		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"gives birth to some of its disgusting young.");

			pc_heard_sound_delay(177,100);						
			used_abil = 1;
		
		if (summon_creature(15147)) {
			place_particle_num(15147,1,1,8);
			if (enraged == 0)
				set_level(15147,12);
				else {
					set_level(15147,25);
					alter_stat(15147,28,10);
					set_attack_bonus(15147,10);
					set_resistance(15147,7,90);
					}
			set_summon_level(15147,1);
			}
		if (summon_creature(15148)) {
			place_particle_num(15148,1,1,8);
			if (enraged == 0)
				set_level(15148,12);
				else {
					set_level(15148,25);
					alter_stat(15148,28,10);
					set_attack_bonus(15148,10);
					set_resistance(15148,7,90);
					}
			set_summon_level(15148,1);
			}
		if (summon_creature(15149)) {
			place_particle_num(15149,1,1,8);
			if (enraged == 0)
				set_level(15149,12);
				else {
					set_level(15149,25);
					alter_stat(15149,28,10);
					set_attack_bonus(15149,10);
					set_resistance(15149,7,90);
					}
			set_summon_level(15149,1);
			}
			
		if (gf(140,7) == 0) {
			sf(140,7,1);
			begin_talk_mode(12);
			}
		end();
		}
	
	
	do_attack();
break;

beginstate 4; // returning to start
	if (my_dist_from_start() < 10)
		set_state(START_STATE);

	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	return_to_start(ME,2);
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;