// Fleeing Group NPC v1.0
// fleegroupnpc.txt
// By Kelandon  (tomwatts@berkeley.edu)
// Based on fleeoncenpc by Jeff Vogel.
// Like basicnpc, but this creature flees one time to a waypoint. 
// Script cheats slightly ... when it flees, heals itself slightly 
// to protect from backshots.
// Flees when enough allies of the same script are wounded or dead.

// 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 - Waypoint it flees to.
//	Cell 5 - Maximum number of characters that can be wounded or dead for
//	character not to flee. Once the number exceeds this, char flees.
//	Cell 6,7 - SDF that counts the number of allies that are dead or
//	wounded. NOTE: An ally being wounded increments by one and an ally
//	being dead increments by two. All chars that flee together should
//	refer to the same SDF.
//
//	Also, if you have a lot of characters with this script going to
//	the same waypoint, I advise changing the the line immediately
//	following beginstate 4 to
//	if (approach_waypoint(ME,get_memory_cell(4),3) > 0)
//	or even
//	if (approach_waypoint(ME,get_memory_cell(4),4) > 0)
//	because the creatures all flee at the same time, and it could get
//	cluttered.

begincreaturescript;

variables;

short i,target;
short has_fled = 0; 
short hurt = 0;

body;

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

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	inc_flag(get_memory_cell(6),get_memory_cell(7),2);
	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_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		set_state(3);
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	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 (has_fled == 0)
		{if ((get_sdf(get_memory_cell(6),get_memory_cell(7))) > (get_memory_cell(5)) && (get_char_status(ME,8) == 0))
			{heal_char(ME,get_max_health(ME) / 8);
			has_fled = 1;
			print_named_str(ME,"runs away!");
			set_state(4);
			}
		if ((hurt == 0) && (get_health(ME) < (get_max_health(ME) * 1) / 2))
			{hurt = 1;
			inc_flag(get_memory_cell(6),get_memory_cell(7),1);
			do_attack(); 
			} 
		}
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate 4; // fleeing
	if (approach_waypoint(ME,get_memory_cell(4),2) > 0)
		set_state(START_STATE);	
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;