// 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();
	x_to_place = 0;
	y_to_place = 0;
	break;

beginstate DEAD_STATE;
	if (get_flag(0,8) == 250)
		end();
	// 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);
	set_flag(0,8,250);
	//begin_talk_mode(get_memory_cell(3));
	i = 0;
	while (i < 120) {
		// delete all plant monsters
		if (creature_type(i) == 51)
			kill_char(i,3,0);
		if (creature_type(i) == 52)
			kill_char(i,3,0);
		if (creature_type(i) == 53)
			kill_char(i,3,0);
		i = i + 1;
		}
	award_party_xp(500,5);
	toggle_quest(6,0);
	toggle_quest(8,1);
	//run_town_script(80);
	reset_dialog();
	add_dialog_str(1,"You strike a powerful blow. Lady Keria is stunned and knocked to her knees, and her magical plants disintegrate when she loses focus. She looks around desperately. _Enough! I'll tell you anything you want, I surrender!_",0);
	add_dialog_str(2,"General Seth didn't send you here to make Lady Keria surrender; he sent you here to kill her. But this could be a good opportunity to find out just what is going on around here.",0);
	add_dialog_choice(0,"I'll just leave you here. (Walk away)");
	add_dialog_choice(1,"It's time for you to die. (Kill Lady Keria)");
	add_dialog_choice(2,"Why did you betray your country? Where is our king? How did you get these magical powers?");
	i = 0;
	i = run_dialog(1);
	
	reset_dialog();
	if (i == 1) {
		run_town_script(90);
		}
	if (i == 2) {
		run_town_script(91);
		}
	if (i == 3) {
		run_town_script(92);
		}
break;

beginstate START_STATE; 
	// 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 (get_flag(5,16) == 0) {
		message_dialog("You finally catch up to Lady Keria. She has backed against a wall, and makes a final defiant stand against you. _You can do nothing against the power of nature itself!_ she shouts.","She waves her hands, and begins performing her animation magic.");
		set_flag(5,16,250);
		}
	if (tick_difference(last_abil_time,get_current_tick()) >= 1) {
		last_abil_time = get_current_tick();
		x_to_place = 0;
		y_to_place = 0;
		place_monster(my_loc_x(),my_loc_y(),get_ran(1,51,52),0);
		print_named_str(ME," creates a new plant monster!");
		}
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;

beginstate 10;

break;

beginstate 11;
	reset_dialog();
	add_dialog_str(1,"Keria sniffs piteously. _I suppose I deserve that cruelty. Shall I wait here in agony until I rot away?_",0);
	add_dialog_str(2,"You begin walking away. Lady Keria continues speaking, almost to herself. _It is strange to think that I gave life to plants, and now my life flows back into the earth..._",0);
	add_dialog_str(3,"I suppose, in the end, a King and a coward both are dust... I aspired to be so much... more..._ She slowly exhales; she doesn't breathe back in.",0);
	i = run_dialog(1);
break;

// remember to eventually give the party the quest and end call

beginstate 12;
	// this doesn't actually get called
	reset_dialog();
	add_dialog_str(1,"Lady Keria sighs. _I will answer your questions, if I must. I am in no position to argue... I was persuaded to try to kill his majesty King Valion. Not that I needed much provocation, after he interfered with my dear prince..._",0);
	add_dialog_str(2,"_I led the Hunan through the northwestern forests. I had planned to try to kill the King while the Hunan attacked. Of course they would be crushed,_ she says casually. _They are dumb animals, and they had no chance against the Megiddo guards._",0);
	add_dialog_str(3,"_And this same person who persuaded me to kill the King is the one who granted me power over the flora._ Keria pauses; she is wracked by a coughing fit. _I cannot reveal this figure; if I did, everything I know would be destroyed..._",0);
	if (get_flag(0,13) > 0)
		add_dialog_str(4,"_So I paid the custodian to build a secret room in the back of my apartment in the city, and I kept a special poison for the King there. And of course the custodian could not be trusted to keep quiet... I had to silence him._",0);
	add_dialog_str(5,"_But when General Seth burst into the audience chamber, I knew my time had come. And an earthquake struck, so I took the opportunity and ran. I ran here. I had nothing to do with the King's disappearance._",0);
	add_dialog_str(6,"_And that is all I will say._ She winces as she touch-inspects her wounds. _I'm dying; I can feel it. I will spend my last moments at peace._",0);
	add_dialog_choice(0,"I'll just leave you here. (Walk away)");
	add_dialog_choice(1,"It's time for you to die. (Kill Lady Keria)");
	i = 0;
	i = run_dialog(1);
	if (i == 1)
		set_state(11);
	if (i == 2)
		set_state(12);
break;