// 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 talky = 0;
short which_beam = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Tizka-Eye");
	set_boss_level(ME,2);
	set_aggression(ME,50);
	
	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(62,20) > 0) && (get_attitude(ME) < 10))
		set_attitude(ME,10);
	
	if (gf(62,20) == 0) {
		if (talky == 0) {
			if ((get_nearest_party_char(7) >= 0) || (who_shot_me() >= 0)) {
				talky = 1;
				talk_no_exit(70);
				}
			}
			else approach_nav_point(ME,0,2);
		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);
		}
		
	// 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) {
			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);
	used_abil = 0;

	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);	

		if (which_beam == 0) { // beam debuff
			print_named_str(ME,"surrounds you with static.");
			set_char_status(get_target(),9,-15);
			set_char_status(get_target(),11,-15);
			set_char_status(get_target(),12,-15);
			set_char_status(get_target(),13,-15);
			set_char_status(get_target(),14,-15);
			set_char_status(get_target(),15,-15);
			set_char_status(get_target(),16,-15);
			set_char_status(get_target(),20,-15);
			set_char_status(get_target(),21,-15);
			set_char_status(get_target(),28,-15);
			set_char_status(get_target(),29,-15);
			if ((num_chars_in_group(0) > 1) && (my_dist_from_start() <= 20)) {
				print_str_color("  You find yourself somewhere else!",2);
				relocate_char(get_target(),395,268,0);
				}
			place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(195,100);						
			
			which_beam = 1;
			used_abil = 1;

			get_foe_target(ME,12,0);
			if (target_ok() == FALSE)
				set_state(START_STATE);
			}
		else if (which_beam == 1) { // beam charm
			print_named_str(ME,"tries to take over your mind.");
			if (num_chars_in_group(0) > 1)
				set_char_status(get_target(),5,60);
				else set_char_status(get_target(),17,60);
			place_particle_num(get_target(),8,2,8);
			pc_heard_sound_delay(100,100);						
			which_beam = 2;
			used_abil = 1;
			}

		else if (which_beam == 2) { // lightnign debuff
			print_named_str(ME,"uses its trusty death ray.");
			set_char_status(get_target(),22,4);
		  	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);
			damage_char(get_target(),get_ran(1,400,500),2);
			which_beam = 0;
			used_abil = 1;
			}

		last_abil = get_current_tick();
		}

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

beginstate TALKING_STATE;
	if (talky > 0) {
		print_str("Talking: The gazer ignores you.");
		end();
		}
				talky = 1;
	talk_no_exit(70);
break;