// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// 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.

begincreaturescript;

variables;

short i,target;
int a,b,c,d,e,f,g,h;
int x_to_place,y_to_place,position;
int ready;
short last_abil_time;

body;

beginstate INIT_STATE;
	last_abil_time = get_current_tick();
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	set_script_mode(3);
	ready = 0;
	a = -1;
	b = 0;
	c = 0;
	d = 0;
	e = 0;
	f = 0;
	g = 0;
	h = 0;
	i = 0;
	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	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; 
	//relocate_character(my_number(),(char_loc_x(a)+1),(char_loc_y(a)+1));
	//why the hell was that there?
	// 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 (target_ok() == FALSE)
		set_state(START_STATE);
	if (get_target() == -1) {
		end();
		}
	// magic
	if (tick_difference(last_abil_time,get_current_tick()) >= 1) {
		//last_abil_time = get_current_tick();
		//put this in the primary and secondary attack sequences; so instead of finding targets once per turn, it does either secondary or primary attack once per turn
		
		// run my ability once per turn
		// 2/5/10: LIES
		a = -1;
		b = -1;
		c = -1;
		d = -1;
		e = -1;
		//e = 60; only used during testing
		// remember to remove this when done
		// e is iterations of ALL the while loops; the limit is arbitrarily set at a "safe" limit of 60
		i = 0;
		// reset my variables so that I don't attack the same people each turn
		while (i < 120) {
			if ((char_ok(i)) && (can_see_char(i)) && (get_attitude(i) > 5)) {
			// check who I'm trying to kill, to make sure I want to kill them
				if (a <= -1)
					a = i;
				if (b <= a)
					b = i;
				if (c <= b)
					c = i;
				if (d <= c)
					d = i;
				}
			i = i + 1;
			}
		f = 0;
		//select_target(ME,8,0);
		// that's not really necessary, is it?
		set_character_pose(ME,1);
		force_instant_terrain_redraw();
		
		//new, important code right here
		// was originally just while (e < 60) [], but this meant that so long as e had not tripped past 60, the state would just keep on running, because there was no check if the ability had run before that turn
	
		while (e < 60 && tick_difference(last_abil_time,get_current_tick()) >= 1) {
			ready = 0;
			e = 0;
			//a = get_nearest_evil_char(1);
			// that's not really necessary, is it?
			while (ready == 0 && e < 60) {
				x_to_place = char_loc_x(a) - 1;
				y_to_place = char_loc_y(a) - 1;
				x_to_place = get_ran(1,0,2) + x_to_place;
				y_to_place = get_ran(1,0,2) + y_to_place;
				if (char_on_loc(x_to_place,y_to_place) <= -1)
					ready = 1;
				e = e + 1;
				}
			if (ready == 1) {
				change_char_health(a,-1 * get_ran(2,1,12));
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_boom_on_char(ME,2,0);
				relocate_character(my_number(),x_to_place,y_to_place);
				run_animation_sound(71);
				set_character_pose(ME,2);
				set_character_facing(ME,get_ran(1,0,7));
				force_instant_terrain_redraw();
				set_character_pose(ME,1);
				force_instant_terrain_redraw();
				f = 1;
				}
			ready = 0;
			e = 0;
			while (ready == 0 && e < 60) {
				x_to_place = char_loc_x(b) - 1;
				y_to_place = char_loc_y(b) - 1;
				x_to_place = get_ran(1,0,2) + x_to_place;
				y_to_place = get_ran(1,0,2) + y_to_place;
				if (char_on_loc(x_to_place,y_to_place) <= -1)
					ready = 1;
				e = e + 1;
				}
			if (ready == 1) {
				change_char_health(b,-1 * get_ran(2,1,12));
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_boom_on_char(ME,2,0);
				relocate_character(my_number(),x_to_place,y_to_place);
				run_animation_sound(71);
				set_character_pose(ME,2);
				set_character_facing(ME,get_ran(1,0,7));
				force_instant_terrain_redraw();
				set_character_pose(ME,1);
				force_instant_terrain_redraw();
				f = 1;
				}
			ready = 0;
			e = 0;
			while (ready == 0 && e < 60) {
				x_to_place = char_loc_x(c) - 1;
				y_to_place = char_loc_y(c) - 1;
				x_to_place = get_ran(1,0,2) + x_to_place;
				y_to_place = get_ran(1,0,2) + y_to_place;
				if (char_on_loc(x_to_place,y_to_place) <= -1)
					ready = 1;
				e = e + 1;
				}
			if (ready == 1) {
				change_char_health(c,-1 * get_ran(2,1,12));
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_boom_on_char(ME,2,0);
				relocate_character(my_number(),x_to_place,y_to_place);
				run_animation_sound(71);
				set_character_pose(ME,2);
				set_character_facing(ME,get_ran(1,0,7));
				force_instant_terrain_redraw();
				set_character_pose(ME,1);
				force_instant_terrain_redraw();
				f = 1;
				}
			ready = 0;
			e = 0;
			while (ready == 0 && e < 60) {
				x_to_place = char_loc_x(d) - 1;
				y_to_place = char_loc_y(d) - 1;
				x_to_place = get_ran(1,0,2) + x_to_place;
				y_to_place = get_ran(1,0,2) + y_to_place;
				if (char_on_loc(x_to_place,y_to_place) <= -1)
					ready = 1;
				e = e + 1;
				}
			if (ready == 1) {
				change_char_health(d,-1 * get_ran(2,1,12));
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_jagged_zap(my_loc_x(),my_loc_y(),x_to_place,y_to_place,1);
				put_boom_on_char(ME,2,0);
				relocate_character(my_number(),x_to_place,y_to_place);
				run_animation_sound(71);
				set_character_pose(ME,2);
				set_character_facing(ME,get_ran(1,0,7));
				force_instant_terrain_redraw();
				set_character_pose(ME,1);
				force_instant_terrain_redraw();
				f = 1;
				}
			//e = 120;
			// 2/5/10: why is "e = 120" there? That makes the loop stop running, and turns on the secondary attack
			//last_abil_time = get_current_tick();
			// that's really unnecessary, see down at the bottom of the big conditional
			set_character_pose(ME,0);
			force_instant_terrain_redraw();
			last_abil_time = get_current_tick();
			}// new, important code right here

		if (f == 0) {
			//if (last_abil_time == get_current_tick()) {
				//end();
				//}
			if (a > -1) {
				put_field_on_space(char_loc_x(a),char_loc_y(a),5);
				put_sparkles_on_char(a,11,3);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(a),char_loc_y(a),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(a),char_loc_y(a),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(a),char_loc_y(a),1);
				}
			if (b > -1) {
				put_field_on_space(char_loc_x(b),char_loc_y(b),5);
				put_sparkles_on_char(b,11,3);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(b),char_loc_y(b),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(b),char_loc_y(b),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(b),char_loc_y(b),1);
				}			
			if (c > -1) {
				put_field_on_space(char_loc_x(c),char_loc_y(c),5);
				put_sparkles_on_char(c,11,3);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(c),char_loc_y(c),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(c),char_loc_y(c),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(c),char_loc_y(c),1);
				}				
			if (d > -1) {
				put_field_on_space(char_loc_x(d),char_loc_y(d),5);
				put_sparkles_on_char(d,11,3);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(d),char_loc_y(d),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(d),char_loc_y(d),1);
				put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(d),char_loc_y(d),1);
				}
			print_named_str(ME,"casts Rogue Blades!");
			run_animation_sound(102);
			set_character_pose(ME,0);
			force_instant_terrain_redraw();
			last_abil_time = get_current_tick();
			// 2/5/10: Niemand says Seth gets stuck in the attacking phase, and he has never led me astray!
			}
		last_abil_time = get_current_tick();
		end_combat_turn();
		}
break;

beginstate TALKING_STATE;
	if (current_town() == 4) {
		// both of General Seth's guards are okay
		if (char_ok(39) == 1 && char_ok(38) == 1) {
			message_dialog("You tell General Seth the story of Keria's betrayal. He listens avidly, and nods. _There is not a moment to waste; we must both return to the tower as quickly as possible._","_Lady Keria will pay dearly for siding with these animals._ General Seth storms off toward Megiddo. He snaps his fingers at his guards, and they silently follow.");
			erase_char(ME);
			erase_char(39);
			erase_char(38);
			set_flag(4,5,2);
			end();
			}
		// one of General Seth's guards is dead
		if ((char_ok(39) == 1 && char_ok(38) == 0) || (char_ok(39) == 0 && char_ok(39) == 1)) {
			message_dialog("You tell General Seth the story of Keria's betrayal. He listens avidly, and nods. _There is not a moment to waste; we must both return to the tower as quickly as possible._","_Lady Keria will pay dearly for siding with these animals._ General Seth storms off toward Megiddo. He snaps his fingers at his one surviving guard, who silently follows.");
			erase_char(ME);
			erase_char(39);
			erase_char(38);
			set_flag(4,5,2);
			end();
			}
		if (char_ok(39) == 0 && char_ok(38) == 0) {
			message_dialog("You tell General Seth the story of Keria's betrayal. He listens avidly, and nods. _There is not a moment to waste; we must both return to the tower as quickly as possible. Lady Keria will pay dearly for siding with these animals._","General Seth storms off toward Megiddo. He doesn't even check if his guards survived the deadly Hunan melee.");
			erase_char(ME);
			erase_char(39);
			erase_char(38);
			set_flag(4,5,2);
			end();
			}

		}
	if (get_memory_cell(3) == 0 && current_town() != 4) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));

break;