// forcetalk.txt
// Exactly like basicnpc, but when party gets close, character hunts them down 
// and talks to them
// 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,5 - Stuff done flag. Set to 1 if the character has hunted them down 
//     once.
//   Cell 6 - range to initiate talking, defaults to 14 if left at 1

begincreaturescript;

variables;

short i,target;
short hunted_party = 0;
short hunt_range = 14;
short used_potion = 0;
short pot_type = 2;
short last_abil;

body;

beginstate INIT_STATE;
	set_name(ME,"Weylin");
	set_level(ME,22);
	set_boss_level(ME,1);
	set_courage(ME,100);
	set_resistance(ME,7,90);
	

	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	begin_talk_mode(41);
break;

beginstate START_STATE; 
	if (section_i_am_in() != 124) {
		player_see_text("Someone was following you. You see him slip silently away.");
		relocate_char(ME,812,537,0);
		set_state(START_STATE);
		
		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 ((in_boat() == FALSE) && (dist_to_party() <= 32) && (gf(124,4) == 0) && (get_attitude(ME) < 10)) {
		set_state(4);
		}

	if ((my_dist_from_start() <= 3) && (gf(124,4) == 1))
		sf(124,4,2);
		
	if ((my_dist_from_start() <= 6) && (get_nearest_party_char(8) >= 0) && (gf(124,4) > 0) && (gf(124,4) < 3)) {
		begin_talk_mode(40);
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if (my_dist_from_start() >= 3)
		return_to_start(ME,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 (target_ok() == FALSE)
		set_state(START_STATE);

	if (tick_difference(last_abil,get_current_tick()) > 1) {
		last_abil = get_current_tick();
		if ((pot_type == 1) && (used_potion < 2)) {
			if (get_health(ME) < get_max_health(ME) / 2) {
				print_named_str(ME,"drinks a healing potion.");
				used_potion = used_potion + 1;
				//place_particle_num(ME,3,1,8);
				heal_char(ME,get_max_health(ME) / 2);
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(120,100);						
				end();
				}
			}
		if ((pot_type == 2) && (used_potion < 2)) {
			if ((get_char_status(ME,1) <= 0) && (get_ran(1,0,100) < 70)) {
				print_named_str(ME,"drinks a speed potion.");
				used_potion = used_potion + 1;
				//place_particle_num(ME,3,0,8);
				set_char_status(ME,24,8);
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(101,100);						
				
				if (used_potion >= 2) {
					used_potion = 0;
					pot_type = 1;
					}
				end();
				}
			
			}
		if ((pot_type == 3) && (used_potion < 2)) {
			if ((get_char_status(ME,18) <= 0) && (get_ran(1,0,100) < 40)) {
				print_named_str(ME,"drinks an invulnerability potion.");
				used_potion = used_potion + 1;
				//place_particle_num(ME,3,0,8);
				set_char_status(ME,18,4);
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(131,100);						
	
				if (used_potion >= 2) {
					used_potion = 0;
					pot_type = 2;
					}
				end();
				}
			}
		}
		
	do_attack();
break;

beginstate 4; // hunt party
	if (section_i_am_in() != 124) {
		player_see_text("Someone was following you. You see him slip silently away.");
		relocate_char(ME,812,537,0);
		set_state(START_STATE);
		
		end();
		}

	if (gf(124,4) > 0)
		set_state(START_STATE);
	if (in_boat())
		set_state(START_STATE);
	if (get_attitude(ME) >= 10)
		set_state(START_STATE);
		
	if (get_ran(1,0,1) == 0) {
		stop_moving();
		}
	approach_char(ME,30000,3);
	
	if (get_nearest_party_char(4) >= 0) {
		sf(124,4,1);
		hunted_party = 1;
		talk_no_exit(30);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
break;

beginstate TALKING_STATE;
	hunted_party = 1;
	if (gf(124,4) > 0) {
		print_str("Talking: Weylin doesn't stop to respond.");
		end();
		}
	set_flag(124,4,1);
		talk_no_exit(30);
break;