// Galthrax v1.0
// t21Galthrax.txt
// by Kelandon (tomwatts@berkeley.edu)
//
// Kind of like basicnpc (with the specdeath addition of calling a town state
// when dead), but fires off increasingly deadly special spells when injured.
//
// NOTE: Character does not talk at all.
//
// 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 - Which town state to call when the creature dies. If left at 0,
//		none.
//	Cell 2,3 - Coordinates of the northwest corner of the room.
//	Cell 4,5 - Coordinates of the southeast corner of the room.
//	Cell 6 - Level of dread curse to give the party when Galthrax hits half his
//		hp.

begincreaturescript;

variables;

short i,j,target;

// Variables to keep track of whether the creature has fired off the special
// spells yet.
short first_spell = 0;
short second_spell = 0;
short third_spell = 0;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	break;

beginstate DEAD_STATE;
	// Remove the Dread Curse.
	i = 0;
	while (i < 6) {
		alter_stat(i,31,0 - get_memory_cell(6));
		i = i + 1;
		}
	
	// Call a town state.
	if (get_memory_cell(1) > 0)
		run_town_script(get_memory_cell(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 damaged in combat, do something.
	// First spell happens if health is below 3/4 of max.
	// Remove all statuses, then curse and slow.
	if (get_health(my_number()) < (get_max_health(my_number()) * 3) / 4)
		if (first_spell == 0) {
			first_spell = 1;
			message_dialog("Galthrax, lightly wounded, speaks a few powerful words of magic and destroys the protective spells you have around you!","");
			i = 0;
			while (i < 6) {
				if (char_ok(i)) {
					set_char_status(i,1,0 - get_char_status(i,1),1,0); // Blessed
					set_char_status(i,2,0 - get_char_status(i,2),1,0); // Shield
					set_char_status(i,3,0 - get_char_status(i,3),1,0); // Haste
					set_char_status(i,4,0 - get_char_status(i,4),1,0); // Invulnerable
					set_char_status(i,5,0 - get_char_status(i,5),1,0); // Magic Resistant
					set_char_status(i,9,0 - get_char_status(i,9),1,0); // Berserk
					set_char_status(i,14,0 - get_char_status(i,14),1,0); // Sanctuary
					set_char_status(i,15,0 - get_char_status(i,15),1,0); // Martyr's Shield
					set_char_status(i,16,0 - get_char_status(i,16),1,0); // Divinely Touched
					set_char_status(i,17,0 - get_char_status(i,17),1,0); // Resistant
					set_char_status(i,22,0 - get_char_status(i,22),1,0); // Regenerating
					set_char_status(i,23,0 - get_char_status(i,23),1,0); // Nimble Fingers
					set_char_status(i,24,0 - get_char_status(i,24),1,0); // Featherfall
					set_char_status(i,25,0 - get_char_status(i,25),1,0); // Flying
					set_char_status(i,26,0 - get_char_status(i,26),1,0); // Safe Travel
					set_char_status(i,27,0 - get_char_status(i,27),1,0); // Hovering Feet
					}
				i = i + 1;
				}
			}
			
	// Second spell happens if health is below 1/2 of max.
	// Dread curse the party. BADLY.
	if (get_health(my_number()) < get_max_health(my_number()) / 2)
		if (second_spell == 0) {
			second_spell = 1;
			message_dialog("Galthrax, bleeding noticeably from his wounds now, utters the words of a tremendously powerful curse.","You feel yourself growing weaker.");
			i = 0;
			while (i < 6) {
				alter_stat(i,31,get_memory_cell(6));
				i = i + 1;
				}
			}
			
	// Third spell happens if health is below 1/4 of max. This spell repeats,
	// although the message does not.
	// Flood the room with anti-magic fields.
	if (get_health(my_number()) < get_max_health(my_number()) / 4) {
		if (third_spell == 0) {
			third_spell = 1;
			message_dialog("Galthrax, reeling badly from his wounds, speaks a few powerful words of magic and floods the room with energy!","");
			}
		i = get_memory_cell(2);
		while (i <= get_memory_cell(4)) {
			j = get_memory_cell(3);
			while (j <= get_memory_cell(5)) {
				put_field_on_space(i,j,2);
				j = j + 1;
				}
			i = i + 1;
			}
	}
	
	do_attack();
break;

beginstate TALKING_STATE;
	print_str("Talking: It doesn't respond.");
break;