//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 = 0;
short last_walk;
short talk5 = 0;
short talk4 = 0;
short talk3 = 0;
short talk2 = 0;
short talk1 = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Vanderin");
	set_level(ME,3);
	set_boss_level(ME,1);
	set_act_at_dist(ME,1);
	set_courage(ME,100);
	set_resistance(ME,7,90);
	set_strategy(ME,1);
	set_attack_bonus(ME,4);
	
	last_walk = get_current_tick();

	if (gf(160,2) == 3)
		sf(160,2,2);
	if (gf(160,3) > 0)
		erase_char(ME);
	if (gf(160,0) > 0)
		erase_char(ME);

	break;

beginstate DEAD_STATE;
	sf(160,3,1);
	sf(160,2,1);
break;

beginstate START_STATE; 
	// go home?
	if (gf(160,2) == 3) {
		if (section_i_am_in() != 160) {
			if (on_which_layer() == 0) {
				print_str_color("Vanderin has strayed too far from the Goblin Mines. He returns",2);
				print_str_color("  to the road where you met him.",2);
				erase_char(ME);
				spawn_creature(17298);
				}
			if (on_which_layer() == 1) {
				print_str_color("Vanderin says, 'We've reached safety at last! It was",2);
				print_str_color("  an honor to fight with you!' He slips off into the tunnels.",2);
				erase_char(ME);
				}
			end();
			}
		}
	
	// gobbo disappear
	if (gf(160,0) > 0) {
		erase_char(ME);
		if (gf(160,2) == 3)
			begin_talk_mode(52);
		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);
		}

	// force talk
	if ((gf(160,2) == 0) && (get_nearest_party_char(5) >= 0))
		begin_talk_mode(30);
	if ((talk2 == 0) && (on_which_layer() == 1)) {
		talk2 = 1;
		begin_talk_mode(51);
		}
	if ((gf(160,2) == 3) && (gf(160,26) == 0) && (on_which_layer() == 0) && (dist_to_loc(ME,392,727) <= 4)) {
		sf(160,26,1);
		begin_talk_mode(50);
		}
	if ((gf(160,2) == 3) &&	(talk3 == 0) && (get_ran(1,0,100) == 50)) {
		talk3 = 1;
		begin_talk_mode(53);
		}
	if ((gf(160,2) == 3) &&	(talk4 == 0) && (get_ran(1,0,100) == 50)) {
		talk4 = 1;
		begin_talk_mode(54);
		}
	if ((gf(160,2) == 3) &&	(talk5 == 0) && (get_ran(1,0,100) == 50)) {
		talk5 = 1;
		begin_talk_mode(55);
		}
		
	//  follow
	if (gf(160,2) == 3) {

		if (dist_to_party() >= 12) {
			telep_char_to_char(ME,random_group_member(0),0);
			print_named_str(ME,"hurries to catch up with you.");
			}
			else if (dist_to_party() < 3)
				fidget(ME,25);
				else approach_char(ME,random_group_member(0),2);
		
		}
	
	// no follow
	if (gf(160,2) < 3) {
		if (path_a_or_b == 0) {
			if ((tick_difference(last_walk,get_current_tick()) > 10) || (get_memory_cell(4) > 0)) {
				if (approach_nav_point(ME,0,2)) {
					last_walk = get_current_tick();
					path_a_or_b = 1;	
					}
				}	
			}
			else {
				if (tick_difference(last_walk,get_current_tick()) > 10) {
					if (return_to_start(ME,2)) {
						last_walk = get_current_tick();
						path_a_or_b = 0;		
						}
					}
				}
		}

	// 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) {
		if (get_foe_target(ME,12,0) == FALSE)
			set_state(START_STATE);
		}
	if (dist_to_char(get_target()) >= 10) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}
		
	//  follow
	if (gf(160,2) == 3) {
		if (dist_to_party() >= 12) {
			telep_char_to_char(ME,random_group_member(0),0);
			print_named_str(ME,"hurries to catch up with you.");
			set_foe_target(ME,-1);
			set_state(START_STATE);
			}
		
		}

	do_attack();
break;

beginstate TALKING_STATE;
	if (gf(160,2) == 3) {
		begin_talk_mode(45);
		}
		else begin_talk_mode(30);
break;