// Stat Targeter v1.0.0
// stattargeter.txt
// by Kelandon (tomwatts@berkeley.edu) - modified from basicnpc
//
// Like basicnpc, but instead of a simple select_target, it targets the visible
// creature with the lowest or highest value of a particular stat.
//
// 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 - Which town state to call when dead. If left at 0, none.
//   Cell 2 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//	 Cell 3 - Which stat to target.
//	 Cell 4 - Whether to target the character with the lowest or highest stat
//		value. If 0, lowest. If not, highest.
//	 Cell 5 - What kind of targets to accept. If 0, anything. If 1, never target
//		summoned creatures. If 2, only target PCs.

begincreaturescript;

variables;

short i,j,k,l;

body;

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

beginstate DEAD_STATE;
	// Call a town state.
	if (get_memory_cell(1) > 0)
		run_town_script(get_memory_cell(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(4);
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0))
		set_state_continue(4);
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0)
		set_state_continue(4);
		
	// 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 (target_ok() == FALSE)
		set_state_continue(4);
	do_attack();
break;

beginstate 4; // getting new targets
	i = 0; // number of target
	if (get_memory_cell(5) == 0)
		j = 119; // max number of target
	if (get_memory_cell(5) == 1)
		j = 84;
	if (get_memory_cell(5) == 2)
		j = 5;
	k = 0; // stat value
	
	while (i <= j) {
		if (char_ok(i))
			if (char_attitude_to_char(my_number(),i) == 2)
				if (can_see_loc(char_loc_x(i),char_loc_y(i))) {
					if (get_memory_cell(4) == 0)
						if (get_stat(i,get_memory_cell(3)) < k)
							set_target(my_number(),i);
					else
						if (get_stat(i,get_memory_cell(3)) > k)
							set_target(my_number(),i);
					}
		i = i + 1;
		}
	
	if (target_ok() == 1)
		set_state_continue(3);
	else
		set_state(START_STATE);
	
break;

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