// ethalia.txt
// The script for Ethalia, from the scenario Warp.
//  Functionally identical to basicnpc, aside from custom spells and dialogue.

begincreaturescript;

variables;

short i,target;
short speller;
short pc_counter;
short i;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
		
	//fix up my spell levels
	// 1. Reset everything to zero.
	speller = 0;
	while(speller <= 19) {
		if(get_spell_level(ME,0,speller) > 0)
			change_spell_level(ME, 0, speller, get_spell_level(ME,0,speller) * -1);
		speller = speller + 1;
		}
	// 2. Re-learn the important spells.
	change_spell_level(ME,0,0,5); //Bolt of Fire - Level 5
	change_spell_level(ME,0,4,10); //Haste - Level 10
	change_spell_level(ME,0,5,10); //Slow - Level 10
	change_spell_level(ME,0,10,1); //Lightning Spray - Level 1
	change_spell_level(ME,0,15,3); //Forcecage - Level 3
	

	
	//...clear the stupid text box, because stupid Windows computers
	//    have a stupid bug that shows all the stupid spell-learning
	//    text that Ethalia and Ach'deniz'toh learn. Stupid.
	
	i = 0;
	while (i < 64) {
		print_str(" ");
		i = i + 1;
		}

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);
		
	//Contingency plans:
	// Normally, the parts of the scenario where a fight is timed
	// should be just enough that the party can't actually win.
	// Just in case they somehow kill me, time to move the plot along.

	//First fight
	if(get_flag(10,0) == 1) {
		reset_dialog();
		add_dialog_str(0,"Ethalia stumbles, looking up at you with sheer disgust in her eyes. You can see strands of magic shimmering around her hands as she speaks.",0);
		add_dialog_str(1,"_How dare you! You have no idea what I am sacrificing to be here right now, or what I have endured. I... cannot be set back. Not now._",15);
		add_dialog_str(2,"With that, she traces a complex sigil in the air, and you become aware of a set of thin green lines leading from you back to the room where you first appeared.",0);
		add_dialog_str(3,"You watch as Ethalia makes a chopping motion with her hand, and then you briefly see the green lines severed. You almost manage to swear before everything goes black.",0);
		add_dialog_str(4,"[Move in any direction to continue.]",40);
		run_dialog(1);
		
		//remove any pesky forcecages
		pc_counter = 0;
		while(pc_counter < 4) {
			if(get_char_status(pc_counter,29) > 0)
				set_char_status(pc_counter,29, get_char_status(pc_counter, 29) * -1, 0,0);
			pc_counter = pc_counter + 1;
			}
		
		//move party into the blackness
		relocate_character(0,4,4);
		relocate_character(1,6,4);
		relocate_character(2,6,6);
		relocate_character(3,4,6);
		force_view_center(47,47);
		force_instant_terrain_redraw();
		
		award_party_xp(500,15);
		
		set_flag(2,3,2);
		erase_char(ME);
		end();
		}
	
	//Second fight
	if(get_flag(10,0) == 7) {
		reset_dialog();
		add_dialog_str(0,"As you deliver the killing blow, Ethalia freezes. Her face softens, and the aura of energy around her begins to subside.",0);
		add_dialog_str(1,"_I... have failed._",15);
		add_dialog_str(2,"She turns to look at her younger self. Her voice barely rises above a whisper.",0);
		add_dialog_str(3,"_I have failed you._",15);
		add_dialog_str(4,"She collapses to the ground without a sound.",0);
		force_instant_terrain_redraw();
		run_dialog(1);
		take_all_of_item(447);
		set_flag(2,9,1);
		set_attitude(10,4); //Make Ach'den friendly
		award_party_xp(500,15);
		}
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();
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;