//twopts.txt - Like basic monst, but this character spends all its time 
// oscillating between its start point and a given nav point.
// Memory Cells:
//   Cell 0 - Dialogue node when spoken to
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//	 Cell 3 - The nav point the character marches out to.
//   Cell 4 - If nonzero, the character runs to its nav point immediately.
//     If 0, only goes occasionally.

begincreaturescript;

variables;

short i,target;
short path_a_or_b = 1;
short last_walk;
short at_pylon = 0;

body;

beginstate INIT_STATE;
	last_walk = get_current_tick();
	
	set_level(ME,26);
	set_name(ME,"Kerrorr");
	set_boss_level(ME,20);
	set_courage(ME,100);
	set_resistance(ME,7,90);
	set_attack_bonus(ME,40);
	alter_stat(ME,28,8);
	alter_stat(ME,29,6);
	
	if ((gf(41,20) > 0) && (on_which_layer() == 1)) {
		sf(41,20,5);
		erase_char(ME);
		}
	if (gf(170,7) > 0)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
		begin_talk_mode(56);
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);
		}

	if (gf(41,20) == 1) {
		if (path_a_or_b == 1) {
			if (dist_to_nav_point(ME,1) <= 2)
				path_a_or_b = 2;
				else approach_nav_point(ME,1,2);
			}
		else if (path_a_or_b == 2) {
			if (dist_to_nav_point(ME,2) <= 2)
				path_a_or_b = 3;
				else approach_nav_point(ME,2,2);
			}
		else if (path_a_or_b == 3) {
			if (dist_to_nav_point(ME,3) <= 2)
				path_a_or_b = 4;
				else approach_nav_point(ME,3,2);
			}
		else if (path_a_or_b == 4) {
			if (dist_to_nav_point(ME,4) <= 2) {
				sf(41,20,2);
				player_see_text("Kerrorr goes upstairs.");
				erase_char(ME);
				}
				else approach_nav_point(ME,4,2);
			}
		}
	else if (gf(41,20) == 2) {
		if (get_nearest_party_char(3) >= 0) {
			sf(41,20,3);
			begin_talk_mode(58);
			}
		}
	else if (gf(41,20) == 3) {
		if (path_a_or_b == 1) {
			if (dist_to_nav_point(ME,0) <= 2)
				path_a_or_b = 2;
				else approach_nav_point(ME,0,2);
			}
		else if (path_a_or_b == 2) {
			if (dist_to_nav_point(ME,1) <= 2)
				path_a_or_b = 3;
				else approach_nav_point(ME,1,2);
			}
		else if (path_a_or_b == 3) {
			if (dist_to_nav_point(ME,2) <= 2)
				path_a_or_b = 4;
				else approach_nav_point(ME,2,2);
			}
		else if (path_a_or_b == 4) {
			if (gf(170,7) > 0)
				path_a_or_b = 5;
			}
		else if (path_a_or_b == 5) {
			if (dist_to_nav_point(ME,3) <= 2) {
				if (get_nearest_party_char(5) >= 0) {
					at_pylon = 1;
					path_a_or_b = 6;
					sf(41,29,1);
					begin_talk_mode(57);
					}
				}
				else approach_nav_point(ME,3,2);
			}
		}
		
	// 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);
	do_attack();
break;

beginstate TALKING_STATE;
	if (at_pylon > 0) {
		print_str("Kerrorr is waiting for you to go through the pylon.");
		end();
		}
		
	if (gf(41,20) == 0)
		begin_talk_mode(30);
		else begin_talk_mode(55);

break;