// guard.txt
// The default script for guards in towns. Functions much like basicnpc, but
// the creature has guard emoptes. Also, can be set to patrol out to a certain 
// point and back.
// 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.
//	   3 - Patrol guard. Goes to nav point in cell 4, and back.
//   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 number of the nav point this guard goes to and back from if
//     patroller
//   Cell 5 - If 0, plays the bubble text.

begincreaturescript;

variables;

short i,target;
short go_out_or_in = 0;

body;

beginstate INIT_STATE;
	// remove for av3###
	if (which_town() == 1) { //formello
			set_name(ME,"Formellan Guard");
			}
		else if (which_town() == 3) { //ToM
			set_name(ME,"Tower Guard");
			}
		else if (which_town() == 54) { //patrick
			set_name(ME,"Tower Guard");
			}
		else if (which_town() == 4) { //Silvar
			set_name(ME,"Silvar Guard");
			}
		else if (which_town() == 6) { //almaria
			set_name(ME,"Almarian Guard");
			}
		else if (which_town() == 8) { //Castle
			set_name(ME,"Royal Guard");
			}
			
	if (get_memory_cell(0) == 3)
		set_act_at_dist(ME,1);
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	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))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);
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 we're hostile, hunt party down
	if ((get_attitude(ME) >= 10) && (dist_to_party() <= 40)) {
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}

	if ((get_attitude(ME) < 10) && (get_memory_cell(5) == 0)) {
		if ((get_memory_cell(0) == 1) && (get_ran(1,0,100) < 5))
			give_char_text_bubble(ME,"Zzzz.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Yawn.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"So bored.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Keep in line.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"I'm watching you.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Did you hear something?");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Time for a break.");
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if (get_memory_cell(0) == 3) {
		if (go_out_or_in == 0) {
			 if (dist_to_nav_point(ME,get_memory_cell(4)) <= 2)
			 	go_out_or_in = 1;
			 	else if (get_ran(1,0,100) < 20) 
			 		approach_nav_point(ME,get_memory_cell(4),2);
			 }
			else {
				if (my_dist_from_start() <= 2)
					go_out_or_in = 0;
					else if (get_ran(1,0,100) < 20)
						return_to_start(ME,1);
				}
		}
		else 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);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		if (which_town() == 20) { //ft draco ### remove in av3
			if (gf(20,25) == 0)
				ok_dlog(50,2);
				else ok_dlog(50,1);
			end();
			}
		else if (which_town() == 1) { //formello
			begin_talk_mode(1);
			}
		else if (which_town() == 3) { //ToM
			begin_talk_mode(1);
			}
		else if (which_town() == 4) { //Silvar
			begin_talk_mode(1);
			}
		else if (which_town() == 6) { //almaria
			begin_talk_mode(1);
			}
		else if (which_town() == 8) { //Castle
			begin_talk_mode(1);
			}
		else if (which_town() == 10) { //Blosk
			begin_talk_mode(2);
			}
		else if (which_town() == 11) { //
			begin_talk_mode(1);
			}
		else if (which_town() == 37) { //mertis
			begin_talk_mode(1);
			}
		else if (which_town() == 41) { //ft duvno
			begin_talk_mode(1);
			}
		else if (which_town() == 46) { //ft dranlon
			begin_talk_mode(1);
			}
		else if (which_town() == 51) { //harston
			begin_talk_mode(1);
			}
		else if (which_town() == 54) { //patrick
			begin_talk_mode(1);
			}
		else if (which_town() == 56) { //emerald
			begin_talk_mode(2);
			}
		else if (which_town() == 61) { //
			begin_talk_mode(1);
			}
		else if (which_town() == 106) { //
			begin_talk_mode(1);
			}
			else {
				print_str("Talking: You make small talk with the guard for a while, but you");
				print_str("  don't learn anything interesting.");
				end();
				}
		}
	begin_talk_mode(get_memory_cell(3));
break;