// achden.txt
// The script for the demon Ach'deniz'toh, from the scenario Warp.
//  Functionally identical to basicnpc, aside from custom spells and dialogue.

begincreaturescript;

variables;

short i,target;
short myx, myy;
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,3,10); //Spray Acid - 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,11,5); //Capture Mind - Level 5
	change_spell_level(ME,0,16,1); //Fireblast - Level 1
	
	//...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) == 3) {
		reset_dialog();
		add_dialog_str(0,"The demon pauses. You've damaged it rather a lot, but it doesn't seem to be all that fazed.",0);
		add_dialog_str(1,"It snaps several fingers, and some familiar green lines appear in the air, connecting you to where the portal deposited you.",0);
		add_dialog_str(2,"The demon snarls, displaying several sets of mismatched teeth. Then it holds up a single, glowing claw, and once more everything goes black.",0);
		add_dialog_str(3,"[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,5,2);
		erase_char(ME);
		end();
		}
		
	//Second fight
	if(get_flag(10,0) == 7) {
		reset_dialog();
		add_dialog_str(0,"The demon's features contort into a furious and horrifying knot as you strike a final blow. It screams something in a language you can't understand.",0);
		add_dialog_str(1,"As it staggers, it turns to look at the younger Ethalia. Its voice trembles with rage.",0);
		add_dialog_str(2,"_Traitorous wretch! I swear on the name of my master that---_",15);
		add_dialog_str(3,"It doesn't get anything else out. The demon falls.",0);
		force_instant_terrain_redraw();
		run_dialog(1);
		take_all_of_item(447);
		set_flag(2,9,2);
		set_attitude(9,4); //Make Ethalia friendly
		award_party_xp(500,15);
		}
	
break;

beginstate START_STATE; 

	//aura of flames, only if hostile
	if(get_attitude(ME) > 4) {
		myx = my_loc_x();
		myy = my_loc_y();
		put_field_on_space(myx - 1, myy + 1, 1);
		put_field_on_space(myx, myy + 1, 1);
		put_field_on_space(myx + 1, myy + 1, 1);
		put_field_on_space(myx - 1, myy, 1);
		put_field_on_space(myx + 1, myy, 1);
		put_field_on_space(myx - 1, myy - 1, 1);
		put_field_on_space(myx, myy - 1, 1);
		put_field_on_space(myx + 1, myy - 1, 1);
		force_instant_terrain_redraw();
		}

	// 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();
	
	//aura of flames, only if hostile
	if(get_attitude(ME) > 4) {
		myx = my_loc_x();
		myy = my_loc_y();
		put_field_on_space(myx - 1, myy + 1, 1);
		put_field_on_space(myx, myy + 1, 1);
		put_field_on_space(myx + 1, myy + 1, 1);
		put_field_on_space(myx - 1, myy, 1);
		put_field_on_space(myx + 1, myy, 1);
		put_field_on_space(myx - 1, myy - 1, 1);
		put_field_on_space(myx, myy - 1, 1);
		put_field_on_space(myx + 1, myy - 1, 1);
		force_instant_terrain_redraw();
		}
		
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;