// Berekh v1.0
// berekh.txt
// by Kelandon (tomwatts@berkeley.edu), based on basicnpc by Jeff Vogel
//
// Like Exodus's BasicNPC, but speechless, easily distracted, and changes
// allegiances frequently.
//
// 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 - Distractibility factor. This number minus one times ten is the
//		percent chance of a creature getting distracted from its current target
//		and attacking the most recent thing that hit it. If left at 0, this
//		defaults to EIGHT (unlike BasicNPC), which gives a 70% chance.
//	 Cell 3 - Percent chance to switch alignments. If left at 0, defaults to 25.

begincreaturescript;

variables;

short i,j,target;

body;

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

	if (get_memory_cell(2) == 0)
		set_memory_cell(2,8);
	if (get_memory_cell(3) == 0)
		set_memory_cell(3,25);
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; 
	// Randomly switch alignment.
	if (get_ran(1,1,100) <= get_memory_cell(3)) {
		if (get_attitude(my_number()) == 10)
			set_attitude(my_number(),11);
		else
			set_attitude(my_number(),10);
		}
	
	// 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. If there is one, go to targeting algorithm.
	if (select_target(ME,8,0))
		set_state_continue(10);
		
	// If hit, go to the targeting algorithm.
	if (who_hit_me() >= 0)
		set_state_continue(10);
		
	// 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
	// Randomly switch alignment.
	if (get_ran(1,1,100) <= get_memory_cell(3)) {
		if (get_attitude(my_number()) == 10)
			set_attitude(my_number(),11);
		else
			set_attitude(my_number(),10);
		}
	
	if (target_ok() == FALSE)
		set_state_continue(10);
	if (char_attitude_to_char(my_number(),get_target()) != 2)
		set_state_continue(10);
	
	// Distractibility.
	if (who_hit_me() >= 0)
		if (get_ran(1,1,10) < get_memory_cell(2))
			set_target(my_number(),who_hit_me());
			
	do_attack();
break;

beginstate 10; // Targeting.
	// The idea of this madness is that the berekhs don't attack other berekhs,
	// but they do attack just about everything else, especially the wounded.
	alert_char(my_number());
	if (select_target(ME,8,0) == 0)
		set_state(START_STATE);
	
	if (creature_type(get_target()) == creature_type(my_number())) {
		select_target(ME,8,1);
		if (creature_type(get_target()) == creature_type(my_number())) {
			i = 0;
			j = 0;
			while ((i == 0) && (j <= 30)) {
				set_target(my_number(),get_ran(1,1,119));
				i = 1;
				if (char_ok(get_target()) == 0)
					i = 0;
				if (char_attitude_to_char(my_number(),get_target()) != 2)
					i = 0;
				if (can_see_char(get_target()) == 0)
					i = 0;
				if (creature_type(get_target()) == creature_type(my_number()))
					i = 0;
				j = j + 1;
				}
			}
		}
		
	if (get_target() < 0)
		set_target(my_number(),get_nearest_evil_char(8));
	
	if (get_target() >= 0)		
		set_state_continue(3);
	else
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
	print_str("Talking: The berekh will not speak to you.");
break;