// 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 last_abil2;
short used_abil = 0;
short mess1 = 0;
short mess2 = 0;
short mess3 = 0;

body;

beginstate INIT_STATE;
		set_name(ME,"Akhal Many-Eyes");
	set_level(ME,36);
	change_max_health(ME,500);
	set_boss_level(ME,2);
	set_courage(ME,100);
	set_resistance(ME,7,90);


	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(32,5,2);
	sf(32,10,1);
	sf(32,9,1);


		sf(32,11,0);
		sf(32,12,0);
		sf(32,13,0);
		sf(32,14,0);
		sf(32,15,0);

		erase_char(3494);
		erase_char(3495);
	begin_talk_mode(4);
break;

beginstate START_STATE; 
	if (gf(32,10) > 0) {
		if ((who_shot_me() >= 0) || (get_attitude(ME) >= 10)) {
			if (party_within_dist(284,163,8)) {
				sf(32,10,0);
				}
				else {
					set_attitude(ME,4);
					heal_char(ME,10000);
					begin_talk_mode(9);
					}
			}
			else end();
		}
		
	// 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
	if ((mess1 == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
		mess1 = 1;
		begin_talk_mode(40);
		}
	if ((mess2 == 0) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) {
		mess2 = 1;
		begin_talk_mode(41);
		}
	if ((mess3 == 0) && (get_health(ME) < (get_max_health(ME) * 1) / 4)) {
		mess3 = 1;
		talk_no_exit(30);
		}
	
	if (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;


	if ( (mess1 > 0) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		if (char_ok(3494) == FALSE) {
			summon_creature(3494);
			print_named_str(ME,"returns life to a pile of bones.");
			place_particle_num(3494,1,1,8);

			set_summon_level(3494,1);
			pc_heard_sound_delay(136,100);						
			run_char_animation(2,1,35);	
			end();
			}
			else if (char_ok(3495) == FALSE) {
				summon_creature(3495);
				print_named_str(ME,"returns life to a pile of bones.");
				place_particle_num(3495,1,1,8);
	
				set_summon_level(3495,1);
				pc_heard_sound_delay(136,100);						
				run_char_animation(2,1,35);	
				end();
				}
		}
	if ( (mess2 > 0) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil2,get_current_tick()) > 2)) {
		run_char_animation(2,1,35);	

			print_named_str(ME,"directs a beam of lovely, sparkling light at you.");
			if (num_chars_in_group(0) > 1)
				set_char_status(get_target(),5,50);
				else set_char_status(get_target(),17,50);
			place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(100,100);						

		last_abil2 = get_current_tick();
		end();
		}

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

beginstate TALKING_STATE;
	if (party_within_dist(284,163,8) == FALSE) 
		begin_talk_mode(8);
	if (gf(32,5) == 1)	
		begin_talk_mode(7);

	begin_talk_mode(10);
break;