// v1.0.3
// guard.txt
// Xoid (xoid@hotmail.com)

// Version History:
//   v1.0.3 - Version by Xoid. Altered so that blocked tiles can be placed as
//            sparsely as possible. If blocked spaces are everywhere, and an NPC
//            cannot reach its destination, Jeff's inept coding causes major
//            overhead.
//   v1.0.2 - Version by Xoid. Neater, mainly. Increments the SDF instead of,
//            setting it.
//   v1.0.1 - Version by Xoid. Extra dialogue strings added.
//   v1.0.0 - Version by Jeff Vogel. Original.

// A simple script for guards or soldiers in towns. Acts like a regular npc
// unless it's in a town the party has angered, in which case it goes mobile and
// hunts the party down.

// Memory Cells:
//   0 - Movement type.
//     Legal values are: 0 - Wander randomly.
//                       1 - Stand still until a target appears.
//                       2 - Completely immobile, even if target appears.
//                       3 - Walk to a waypoint and then back to start. Use
//                           memory cell 4 to indicate which waypoint.

// 1,2 - SDF coordinates. If these are left at 0 and 0, these memory cells are
//     ignored. Otherwise, the SDF is incremented by 1 when this creature dies.

//   3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

//   4 - Waypoint. If memory 0 is != 3, then this value is ignored. Otherwise,
//     it is the number of the waypoint the guard or soldier spends its time
//     walking to and from.

//   5 - Maximum distance. The maximum distance the creature will roam from its
//       starting position. If left at 0, this will default to 6 tiles.
//
//     [Making this script backwards compatible with v1.0.0 of guard.txt]

begincreaturescript;

variables;
	short i,target;
	short gone_to_waypoint = 0;
body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);

	if (get_memory_cell(5) == 0)
		set_memory_cell(4,6);

	set_char_script_mode(ME,1); // This NPC will act even at distance.

	if (town_status(current_town()) == 2)
		set_mobility(ME,1);

	if (creature_type(ME) == 4)
		set_summon_level(ME,1);
break;

beginstate DEAD_STATE;
	// Increment the appropriate SDF for this NPC's death.
	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;
	// React if the town gone hostile.
	if (town_status(current_town()) == 2) {
		alert_char(ME);
		set_mobility(ME,1);
	}

	// If I have a target, go attack it.
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
		else set_target(ME,-1);
	}

	// Look for a target, and attack it if its visible.
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
	}

	// React if I have been attacked.
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
	}

	// If this NPC is hostile, then hunt the party down. Otherwise, just
	// peacefully move around. Go back to start if I roam too far.
	if (get_attitude(ME) >= 10)
		approach_char(ME,random_party_member(),6);
	else if ((my_dist_from_start() >= get_memory_cell(5)) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0) && (get_memory_cell(0) != 3))) {
		if (get_ran(1,1,100) < 40)
			return_to_start(ME,1);
	}
	else if (get_memory_cell(0) == 0) {
		fidget(ME,25);
	}
	else if (get_memory_cell(0) == 3) {
		// March to waypoint and back.
		if ((gone_to_waypoint == 0) && (get_ran(1,1,100) < 15)) {
			if (approach_waypoint(ME,get_memory_cell(4),1))
				gone_to_waypoint = 1;
		}
		else if ((gone_to_waypoint == 1) && (get_ran(1,1,100) < 15)) {
			if (return_to_start(ME,1))
				gone_to_waypoint = 0;
		}
	}

	// If I'm in combat and nothing above gave me something to do, stop now.
	// Otherwise the script will keep running, eating up CPU cycles.
	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 (creature_type(ME) == 4) {
			print_str_color("Talking: You make small talk with the guard, but you don't",2);
			print_str_color("  learn anything new.",2);
		}
		else print_str_color("Talking: The soldier doesn't have anything to tell you.",2);
		end();
	}
	else begin_talk_mode(get_memory_cell(3));
break;