// beamnpc.txt
// A monster with a special ability scripted in. The ability is an beam, used 
// 50% of the time.

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

// The abilities:
// 0 - Beam Slow
// 1 - Beam Curse
// 2 - Beam Fire (1-6 dam per level)
// 3 - Beam Acid
// 4 - Beam Terror
// 5 - Beam Daze
// 6 - Beam Debuff
// 7 - Beam Cold (1-8 dam per level)
// 8 - Beam Energy (1-10 dam per level)
// 9 - Beam Charm
// 10 - Beam Freeze
// 11 - lightning
// 12 poison

begincreaturescript;

variables;

short i,target;
short last_abil;
short used_abil = 0;
short what_beam;
short ae_range = 8;

body;

beginstate INIT_STATE;
	set_name(ME,"Ancient Specter");
	set_level(ME,10);
	change_max_health(ME,2000);
	set_char_unkillable(ME,1);
	
	what_beam = get_ran(1,0,5);
	
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
	if (gf(155,22) >= 10)
		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 hostile, hunt party down
	if (get_attitude(ME) >= 10) {
		set_foe_target(ME,random_group_member(0));
		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
	give_object_message(28055,103);
	
	if (gf(155,22) >= 10)
		set_state(4);
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;

	if ((get_health(ME) < get_max_health(ME) - 50) && (gf(155,23) == 0)) {
		sf(155,23,1);
		begin_talk_mode(27);
		}
	
	if ((get_ran(1,0,100) < 70) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		heal_char(ME,500);
		if (what_beam == 0) { // beam slow
			print_named_str(ME,"fires a beam of slowing energy.");
			set_char_status(get_target(),1,-12);
			//place_particle_num(get_target(),2,2,8);
			pc_heard_sound_delay(174,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),34);
			used_abil = 1;
			}

		if (what_beam == 1) { // beam fire
		  	//place_particle_num(get_target(),1,4,8);
			print_named_str(ME,"flings a spray of icy particles.");
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,8),6);
			run_sparkles_on_char(get_target(),172,1,4);
			pc_heard_sound_delay(144,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),41);
			used_abil = 1;
			}

		if (what_beam == 2) { // beam acid
			print_named_str(ME,"summons a shower of acid.");
			set_char_status(get_target(),6,get_ran(3,1,4) + get_attack_bonus(ME));
			//run_sparkles_on_char(get_target(),171,1,2);
			pc_heard_sound_delay(177,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),32);
			used_abil = 1;
			}
		if (what_beam == 3) { // beam terror
			print_named_str(ME,"radiates pure terror.");
			set_char_status(get_target(),7,get_level(ME) + 5 + get_attack_bonus(ME));
			place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(170,100);						
			used_abil = 1;
			}
		if (what_beam == 4) { // lightnign debuff
			print_named_str(ME,"launches a spray of lightning.");
			set_char_status(get_target(),22,4 + get_attack_bonus(ME));
		  	place_particle_num(get_target(),1,4,8);
			pc_heard_sound_delay(237,100);					
			shoot_projectile(ME, char_loc_x(get_target()),char_loc_y(get_target()),43);
			used_abil = 1;
			}


		if (what_beam == 5) { // beam energy
			print_named_str(ME,"fires a beam of searing energy.");
		  	//place_particle_num(get_target(),1,4,8);
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,10),1);
			//run_sparkles_on_char(get_target(),177,1,3);
			pc_heard_sound_delay(179,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),38);
			used_abil = 1;
			}

		last_abil = get_current_tick();
		}

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

beginstate 4; // returning to start
	give_object_message(28055,103);

	if (tick_difference(last_abil,get_current_tick()) > 2) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		  	place_particle_num(ME,4,10,8);
			print_named_str(ME,"radiates pure terror.");
			status_nearby(50,ae_range,7,4);
			//particle_nearby(ae_range,23,2,4,2);
			pc_heard_sound_delay(177,100);						
			used_abil = 1;

		}

	if (dist_to_nav_point(ME,0) <= 3) {
		print_named_str(ME,"reaches the door and fades away.");
	
		erase_char(ME);
		}
	approach_nav_point(ME,0,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;