// 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;

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
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);

	message_dialog("Looks like the ogre wizard was mortal after all. You thought his wounds were lethal ages ago, but only now does he stumble and fall. However, the ogre does not go quietly.","Apparently, he was channeling so much magic that is bursts out of him! The wizard explodes in an orange flaming ball, damaging anyone or anything unfortunate enough to get caught in the blastwave!");
	damage_near_loc(my_loc_x(),my_loc_y,40,2,1);
	set_flag(1,7,1);

	if ((my_loc_x() == 11) && (my_loc_y() == 16)) {
		put_boom_on_space(12,15,0,0);
		run_animation_sound(152);
		set_terrain(12,14,137);
		set_terrain(11,14,36);
		force_instant_terrain_redraw();
}

	if (dist_to_waypoint(my_number(),2) <= 1) {
		put_boom_on_space(12,9,0,0);
		run_animation_sound(152);
		set_terrain(12,9,137);
		set_terrain(12,10,138);
		force_instant_terrain_redraw();
}
break;

beginstate START_STATE;


	if (get_flag(1,2) == 1) {
		stop_moving(my_number());
		approach_waypoint(my_number(),1,0);
	}

	if ((get_flag(1,8) == 0) && (can_see_char(1000) == TRUE)) {
		message_dialog("The ogres have clearly recovered from the shock of your entry. They are attacking you with unmistakable glee. Suddenly, you sense an extremely powerful wave of magic. An ogre wizard dressed in an embroidered green tunic starts barking orders.","_Smash the bladesmen! Fang! Claw! Kill!_ Two frighteningly large worgs jump at the words and attack along with the ogres.");
		set_flag(1,8,1);
}

	if ((get_flag(1,2) == 0) && (get_health(my_number()) < 200)) {
		set_flag(1,2,1);
		set_char_status(my_number(),4,5,1,1);
		message_dialog("You have wounded the leader of the ogres, this quite powerful wizard, so much that it howls in pain. _No! Lord of the Worgs dies not today!_ It snatches a potion from its satchel and downs it in a second.","Then it focuses on an incredible amount of magical energy and releases it in your direction. Fortunately for you it missed.");
		set_target(my_number(),-1);
		set_aggression(my_number(),0);
		set_terrain(15,17,14);
		stop_moving(my_number());
		approach_waypoint(my_number(),1,0);
		put_straight_zap(my_loc_x(),my_loc_y(),14,9,0);
		put_straight_zap(my_loc_x(),my_loc_y(),15,9,0);
		put_boom_on_space(14,9,0,0);
		put_boom_on_space(15,9,0,0);
		run_animation_sound(152);
		set_terrain(14,9,137);
		set_terrain(15,9,137);
		force_instant_terrain_redraw();
		pause(2);
		place_monster(my_loc_x(),my_loc_y() + 1,121,0);
		place_monster(my_loc_x(),my_loc_y() - 1,121,0);
		put_sparkles_on_space(my_loc_x(),my_loc_y() + 1,6,6);
		put_sparkles_on_space(my_loc_x(),my_loc_y() - 1,6,6);
		run_animation_sound(61);
		force_instant_terrain_redraw();

}		


	if ((my_loc_x() == 11) && (my_loc_y() == 16) && (get_flag(1,2) == 1)) {
		set_flag(1,2,3);
		set_aggression(my_number(),100);
		set_mobility(my_number(),0);
		

		if (get_flag(1,3) == 0) {
			play_sound(28);
			heal_char(my_number(),get_max_health(my_number()));
			set_flag(1,3,2);
			set_terrain(11,15,447);
}

		if (get_flag(1,3) == 1)
			message_dialog("The whole Yelbro trade post seems to shake as an enraged howl rings through the compound. Apparently, the ogre wizard found something that displeased him.","");
}if ((get_flag(1,2) == 1) && (approach_waypoint(my_number(),1,0) == 1)) {
		message_dialog("The ogre wizard bellows in rage. _Out my way! Begone!_ You sense an incredible amount of magic coming off the ogre. This time it does not miss.","");
		stop_moving(my_number());
		if (char_ok(0) == TRUE) {
			put_straight_zap(my_loc_x(),my_loc_y(),char_loc_x(0),char_loc_y(0),0);
			put_boom_on_space(char_loc_x(0),char_loc_y(0),0,0);
					}
		if (char_ok(1) == TRUE) {
			put_straight_zap(my_loc_x(),my_loc_y(),char_loc_x(1),char_loc_y(1),0);
			put_boom_on_space(char_loc_x(1),char_loc_y(1),0,0);
					}
		if (char_ok(2) == TRUE) {
			put_straight_zap(my_loc_x(),my_loc_y(),char_loc_x(2),char_loc_y(2),0);
			put_boom_on_space(char_loc_x(2),char_loc_y(2),0,0);
					}
		if (char_ok(3) == TRUE) {
			put_straight_zap(my_loc_x(),my_loc_y(),char_loc_x(3),char_loc_y(3),0);
			put_boom_on_space(char_loc_x(3),char_loc_y(3),0,0);
					}
		run_animation_sound(152);
		damage_char(1000,80,1);
		approach_waypoint(my_number(),1,0);
}


	// 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 (get_flag(1,2) == 1) {
		stop_moving(my_number());
		approach_waypoint(my_number(),1,0);
	}

	if ((get_flag(1,2) == 0) && (get_health(my_number()) < 200)) {
		set_flag(1,2,1);
		set_char_status(my_number(),4,5,1,1);
		message_dialog("You have wounded the leader of the ogres, this quite powerful wizard, so much that it howls in pain. _No! Lord of the Worgs dies not today!_ It snatches a potion from its satchel and downs it in a second.","Then it focuses on an incredible amount of magical energy and releases it in your direction, fortunately for you it missed.");
		set_target(my_number(),-1);
		set_aggression(my_number(),0);
		set_terrain(15,17,14);
		approach_waypoint(my_number(),1,0);
		put_straight_zap(my_loc_x(),my_loc_y(),14,9,0);
		put_straight_zap(my_loc_x(),my_loc_y(),15,9,0);
		put_boom_on_space(14,9,0,0);
		put_boom_on_space(15,9,0,0);
		run_animation_sound(152);
		set_terrain(14,9,137);
		set_terrain(15,9,137);
		force_instant_terrain_redraw();
		pause(2);
		place_monster(my_loc_x(),my_loc_y() + 1,121,0);
		place_monster(my_loc_x(),my_loc_y() - 1,121,0);
		put_sparkles_on_space(my_loc_x(),my_loc_y() + 1,6,6);
		put_sparkles_on_space(my_loc_x(),my_loc_y() - 1,6,6);
		run_animation_sound(61);
		force_instant_terrain_redraw();
}	

	if (get_flag(1,2) == 1)
		set_state(START_STATE);

	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
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;
