// Go-To NPC v1.0
// by Kelandon (tomwatts@berkeley.edu)
// goto.txt
// Creature runs to a particular point and stays there.
//
// Memory Cells:
//   Cell 0,1 - Coordinates of point to run to.
//	 Cell 2 - Distractibility. Percentage chance of standing and fighting
//		instead of running.

begincreaturescript;

variables;

short i,target;

body;

beginstate INIT_STATE;
break;

beginstate DEAD_STATE;
break;

beginstate START_STATE; 
	// If distractible this turn, check the normal things.
	if (get_ran(1,1,100) <= get_memory_cell(2)) {
		// 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 run to a point.
	if (move_to_loc_x_y(my_number(),get_memory_cell(0),get_memory_cell(1)) == 0)
		fidget(my_number(),100);

	// 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;
	print_str("Talking: It doesn't respond.");
break;