// ghostmage.txt
// Modified basicnpc.txt, used by the ghost mages in the cathedral.
//  If cell 5 is set to one, then the mage doesn't actually die. Instead,
//  it sets the flag in cells 1 and 2 to equal the value in cell 4, and
//  erases itself.
// If the party has any of the control crystals, the mage has a 1/3 
//   chance of spending its turn in combat destroying it.
// 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 equal cell 4.
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - What the SDF in cells 1/2 is set to.
//   Cell 5 - What happens when creature dies.
//     0 - Dies normally, sets flag.
//     1 - Erases itself, still sets flag.

begincreaturescript;

variables;

short i,target;
short crystals = 0;

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), get_memory_cell(4));

	if(current_town() == 6) {
		if(char_ok(8) == 1) {
			reset_dialog();
			add_dialog_str(0,"As you strike the mage for what you hope is the last time, you notice that the animated statue has started to crumble. Perhaps the mage cannot maintain the spell anymore.",0);
			run_dialog(1);
			pause(3);
			put_effect_on_char(8,5,5,1);
			run_animation_sound(7);
			kill_char(8,2,0);

			pause(5);
			}
		else {
			reset_dialog();
			add_dialog_str(0,"With his animated ally defeated, you strike a final blow to the ghost, dissipating its magic-rich form.",0);
			run_dialog(1);
			}
		}


	//so, if this is the fight where the mage isn't supposed to
	//actually die, it teleports away
	if(get_memory_cell(5) == 1) {
		reset_dialog();
		add_dialog_str(0,"The mage stumbles, at least as much as a ghost can stumble. For a moment, you think you'll get the pleasure of watching it fade away to nothing.",0);
		add_dialog_str(1,"However, the mage clearly has other plans. He waves his arms and mutters a few arcane syllables.",0);
		run_dialog(1);
		pause(3);

		put_boom_on_char(ME,5,0);
		run_animation_sound(52);
		erase_char(ME);
		}
	//otherwise, the flag that counts dead mages is incremented
	else {
		inc_flag(50,3,1);
		}
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);

	if(((has_item(445) == 1) || (has_item(446) == 1) || (has_item(447) == 1)) && (get_ran(1,1,1) == 1)) {
		crystals = 0;
		if((char_has_item(0,445) == 1) || (char_has_item(0,446) == 1) || (char_has_item(0,447) == 1)) {
			put_effect_on_char(0,4,4,1);
			put_jagged_zap(my_loc_x(), my_loc_y(), char_loc_x(0), char_loc_y(0),4);
			}
		if((char_has_item(1,445) == 1) || (char_has_item(1,446) == 1) || (char_has_item(1,447) == 1)) {
			put_effect_on_char(1,4,4,1);
			put_jagged_zap(my_loc_x(), my_loc_y(), char_loc_x(1), char_loc_y(1),4);
			}
		if((char_has_item(2,445) == 1) || (char_has_item(2,446) == 1) || (char_has_item(2,447) == 1)) {
			put_effect_on_char(2,4,4,1);
			put_jagged_zap(my_loc_x(), my_loc_y(), char_loc_x(2), char_loc_y(2),4);
			}
		if((char_has_item(3,445) == 1) || (char_has_item(3,446) == 1) || (char_has_item(3,447) == 1)) {
			put_effect_on_char(3,4,4,1);
			put_jagged_zap(my_loc_x(), my_loc_y(), char_loc_x(3), char_loc_y(3),4);
			}
		run_animation_sound(89);
		crystals = take_all_of_item(445);
		crystals = crystals + take_all_of_item(446);
		crystals = crystals + take_all_of_item(447);
		pause(3);
		reset_dialog();
		if(crystals > 1)
			add_dialog_str(0,"The mage hits you with a bolt of energy that remarkably leaves you unscathed. Regrettably, it seems to have reduced the crystals you were carrying to very pretty dust.",0);
		else
			add_dialog_str(0,"The mage hits you with a bolt of energy that remarkably leaves you unscathed. Regrettably, it seems to have reduced the crystal you were carrying to very pretty dust.",0); 
		run_dialog(1);
		}

	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;