// 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 met_once = 0;
short phase = 0;
short attack_time = 2;
short port_fails = 0;
short x,y;

body;

beginstate INIT_STATE;
	set_boss_level(ME,2);
	set_courage(ME,100);
	set_resistance(ME,7,90);
	change_max_health(ME,200);
	
	sf(40,29,0);
	
	alter_stat(ME,19,6);
	alter_stat(ME,20,4);
	alter_stat(ME,28,4);
	
	set_char_unkillable(ME,1);
	
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(170,6,1);
	sf(40,21,1);
	set_spec_item(58,1);
	sf(21,23,0);
	award_party_xp(300,30);
	begin_talk_mode(17);
break;

beginstate START_STATE; 
	if (met_once == 0) {
		if ((who_shot_me() >= 0) || (get_nearest_party_char(3) >= 0)) {
			met_once = 1;
			talk_no_exit(70);
			}
			
		if ((get_attitude(ME) >= 10) && (dist_to_party() <= 30)) {
			approach_char(ME,30000,1);
			}
			else {
				approach_nav_point(ME,0,1);
				}
		
		
		
		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) && (dist_to_party() <= 30)){
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}
		else {
			approach_nav_point(ME,0,1);
			}
		


	// 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 (phase > 0)
		attack_time = 1;
		
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((phase == 0) && (get_health(ME) < get_max_health(ME) - 500)) {
		if ((gf(41,25) > 0) && (gf(41,26) > 0) && (gf(41,27) > 0) && (gf(41,28) > 0)) {
			phase = 1;
			heal_char(ME,2000);
			set_char_unkillable(ME,0);
			talk_no_exit(80);
			}
			
		i = get_ran(1,0,3);
		if (gf(41,25 + i) > 0)
			i = get_ran(1,0,3);
		if (gf(41,25 + i) > 0)
			i = get_ran(1,0,3);
		if (gf(41,25 + i) > 0)
			i = get_ran(1,0,3);
		if (gf(41,25 + i) > 0)
			i = get_ran(1,0,3);
		if (gf(41,25 + i) > 0)
			i = get_ran(1,0,3);

		if (i == 0) {
			x = 775; y = 170;
			}
		if (i == 1) {
			x = 776; y = 162;
			}
		if (i == 2) {
			x = 822; y = 163;
			}
		if (i == 3) {
			x = 824; y = 180;
			}
		if ((dist_to_loc(ME,x,y) <= 6) || (gf(41,25 + i) > 0)) {
			if (get_nearest_party_char(8) >= 0) {
				port_fails = port_fails + 1;
				if (port_fails >= 4) {
					phase = 1;
					heal_char(ME,2000);
					set_char_unkillable(ME,0);
					talk_no_exit(80);
					}
				}
			}
			else {
				port_fails = 0;
				if (get_nearest_party_char(8) >= 0) {
					inc_flag(40,29,1);
					print_named_str(ME,"makes an odd hand motion. In a moment, he has vanished.");
					}
				if (i == 0) {
					if (char_ok(4321) == FALSE) {
						spawn_creature(4321);
						place_particle_num(4321,1,1,8);
						}
					if (char_ok(4322) == FALSE) {
						spawn_creature(4322);
						place_particle_num(4322,1,1,8);
						}
					}
				if (i == 1) {
					if (char_ok(4323) == FALSE) {
						spawn_creature(4323);
						place_particle_num(4323,1,1,8);
						}
					if (char_ok(4324) == FALSE) {
						spawn_creature(4324);
						place_particle_num(4324,1,1,8);
						}
					}
				if (i == 2) {
					if (char_ok(4325) == FALSE) {
						spawn_creature(4325);
						place_particle_num(4325,1,1,8);
						}
					if (char_ok(4326) == FALSE) {
						spawn_creature(4326);
						place_particle_num(4326,1,1,8);
						}
					}
				if (i == 3) {
					if (char_ok(4327) == FALSE) {
						spawn_creature(4327);
						place_particle_num(4327,1,1,8);
						}
					if (char_ok(4328) == FALSE) {
						spawn_creature(4328);
						place_particle_num(4328,1,1,8);
						}
					}
					
				heal_char(ME,1000);
				relocate_char(ME,x,y,0);
				set_foe_target(ME,-1);
				if (met_once == 1) {
					met_once = 2;
					begin_talk_mode(16);
					}
				if ((gf(40,29) == 3) && (gf(41,25) == 0)  && (gf(41,26) == 0)  && (gf(41,27) == 0) && (gf(41,28) == 0)) {
					begin_talk_mode(18);
					}
					
				set_state(START_STATE);
				
				}
		}
		
	if ((can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > attack_time)) {
		run_char_animation(2,1,35);	
		if (used_abil == 0) { // beam slow
			print_named_str(ME,"throws an orb at you. When it breaks, you feel sluggish.");
			set_char_status(get_target(),1,-12);
			set_char_status(get_target(),0,-12);
			set_char_status(get_target(),8,-12);
			set_char_status(get_target(),24,-12);
			set_char_status(get_target(),28,-12);
			pc_heard_sound_delay(174,100);									
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),34);
			used_abil = 1;
			}
		else if (used_abil == 1) { // beam acid
			print_named_str(ME,"throws an orb at you. When it breaks, you are sprayed with acid.");
			set_char_status(get_target(),6,12);
			damage_char(get_target(),get_ran(1,50,100),5);
			pc_heard_sound_delay(177,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),32);
			used_abil = 2;
			}
		else if (used_abil == 2) { // beam acid
			print_named_str(ME,"throws an orb at you. When it breaks, you are consumed with fear.");
			set_char_status(get_target(),7,50);
			place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(170,100);						

			used_abil = 3;
			}

		else if (used_abil == 3) { // beam energy
			print_named_str(ME,"throws an orb at you. When it breaks, lightning covers you.");
			damage_char(get_target(),get_ran(1,50,100),1);
			pc_heard_sound_delay(179,100);						
			set_char_status(get_target(),22,4);
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),38);
			used_abil = 4;
			}
		else if (used_abil == 4) { // beam charm
			print_named_str(ME,"throws an orb at you. When it breaks, you feel confused.");
			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);						
			used_abil = 0;
			}

		if (phase > 0) {
			i = get_ran(1,0,7);
			if (char_ok(4321 + i) == FALSE) {
				spawn_creature(4321 + i);
				place_particle_num(4321 + i,1,1,8);
				set_char_status(4321 + i,24,6);
				}
			}
			
		last_abil = get_current_tick();
		end();
		}

	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;