// 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 last_abil_time;
int x_to_place;
int y_to_place;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	last_abil_time = get_current_tick();
	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);
	if (get_flag(4,3) == 250) {
		end();
		}
	reset_dialog();
	add_dialog_str(1,"You defeat the Hunan's massive champion. He isn't dead; just wounded. Lu Liath sinks to his knees and groans. _That witch betrayed me... Keria, curse you. May your rivers be poison, and your children imbeciles._",0);
	add_dialog_str(2,"Lu Liath is surprisingly articulate for a Hunan savage. But that's not the important thing; did he just say Keria? Lady Keria, the daughter of Lord Kato and betrothed of Prince Valion?",0);
	add_dialog_str(3,"Lu Liath notices that he has your attention. _Ha... ha... Yes, you were betrayed. As I was betrayed. Keria, that harpy, came to me in the dead of night. She planted seeds of deceit in my mind._",0);
	add_dialog_str(4,"_Look at you,_ he groans. _Champions? Invaders? I think not. Keria said there would be an attack, that we must be prepared... She must have lied; and if you are truly the attackers then we are fools. Puppets, all of us..._",0);
	add_dialog_str(5,"_She told me that Megiddo's General planned to attack us, and that the best thing would be to strike the Tower, to keep the fight away from our homeland. She said she would lead our scouts... Doubtless she had some evil in mind for this._",0);
	add_dialog_str(6,"_She has dominion over the forest,_ Lu Liath says idly. _She was leading our scouts through the forest with her magic... But we have not heard from them. I will never know what happened to those brave brothers,_ he says mournfully.",0);
	add_dialog_choice(0,"We killed your scouts.");
	add_dialog_choice(1,"Keria's treachery knows no bounds.");
	add_dialog_choice(2,"It's time for you to die.");
	i = run_dialog(1);
	
	reset_dialog();
	message_dialog("Lu Liath nods glumly. _It was an unjust thing, to try to invade your lands... I wish I had the wisdom to forsee this... All of this..._","Lu Liath shakes his head. _A life of disgrace is not a life I would choose for myself._ Before you can stop him, Lu Liath pulls out a dagger. He slashes his own throat and quickly dies. How strange.");
	
	reset_dialog();
	add_dialog_str(1,"The Hunan, of course, have been enraptured by this display of battlefield honor. They seem unsure what to do with you, and with themselves, now.",0);
	add_dialog_str(2,"But then events become even more surreal. General Seth and two guards charge over the crest of the north hills. He is wearing his lustrous golden armor and he shouts, _For my God and my glory!_",0);
	add_dialog_str(3,"Where is the army? How can he possibly survive? But, as General Seth charges toward the Hunan, he gains even more speed; he begins moving faster than naturally possible. In his wake, he leaves a trail of pure, blinding light.",0);
	add_dialog_str(4,"The Hunan are momentarily stunned, but recover. Most of them roar and charge at General Seth; a few turn on you.",0);
	add_dialog_str(5,"When this mess is cleaned up, you should tell General Seth about Lady Keria's betrayal.",0);
	add_dialog_choice(0,"Oh good, General Seth is here...");
	i = run_dialog(1);
	
	activate_hidden_group(1);
	i = 6;
	while (i < 38) {
		set_attitude(i,10);
		set_target(i,random_party_member());
		if (get_ran(1,1,100) > 5) {
			set_target(i,38);
			}
		i = i + 1;
		}
	set_flag(4,3,250);
	set_flag(4,4,250);
	toggle_quest(3,0);
	toggle_quest(6,1);
break;

beginstate START_STATE; 
	if (get_flag(4,4) == 0 && group_dist_to_loc(0,my_loc_x(),my_loc_y()) <= 2) {
		set_attitude(ME,10);
		set_flag(4,4,2);
		message_dialog("_That's far enough,_ Lu Liath barks. _Champions of the Megiddo! I will give your bodies to the birds and all the wild beasts of the plains. Prepare yourself._","Lu Liath lowers his massive club into a ready position.");
		}
	// 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);
	do_attack();
	if (tick_difference(last_abil_time,get_current_tick()) >= 1 && enemies_nearby(2) >= 2) {
		i = 0;
		while (i < 120) {
			if ((char_ok(i)) && (can_see_char(i)) && (char_attitude_to_char(ME,i) == 2) && dist_to_char(i) <= 1) {
				set_char_status(i,3,-2,0,1);
				}
			i = i + 1;
			}
		print_named_str(ME,"uses ability Ground Pounder!");
		put_effect_on_space(my_loc_x(),my_loc_y(),3,3,1);
		run_animation_sound(152);
		//status_near_loc(my_loc_x(),my_loc_y(),-2,1,3);
		damage_nearby(get_ran(4,1,6),1,0,0);
		
		last_abil_time = get_current_tick();
		}
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;