// Town 22 Dalaghant v1.0
// t22Dalaghant.txt
// by Kelandon (tomwatts@berkeley.edu), based on basicnpc by Jeff Vogel
//
// Like Status Nearby, but specific. Also fires charm ray when damaged enough.
//
// 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.

begincreaturescript;

variables;

short i,j,target,last_abil_time,spec_attack;

body;

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

beginstate DEAD_STATE;
	message_dialog("For the second time, the magic around the demon lord is failing, and this time, he lacks the strength to control it. _No!_ he shrieks, now flailing wildly as if surrounded by stinging bees. _You cannot escape! You will not leave the Lava Ocean!_","He tries to crawl away, moving some distance, but you strike him again with your weapons, and again, and again, wounding him so badly that he will not be able to escape. And this time he does not.");
	j = 0;
	i = 0;
	while (i <= 6) {
		if (dist_to_waypoint(my_number(),i) < dist_to_waypoint(my_number(),j))
			j = i;
		i = i + 1;
		}
	if (j == 0)
		relocate_character(my_number(),28,30);
	if (j == 1)
		relocate_character(my_number(),24,32);
	if (j == 2)
		relocate_character(my_number(),19,30);
	if (j == 3)
		relocate_character(my_number(),17,37);
	if (j == 4)
		relocate_character(my_number(),19,34);
	if (j == 5)
		relocate_character(my_number(),27,33);
	if (j == 6)
		relocate_character(my_number(),18,40);
	force_instant_terrain_redraw();
	
	put_boom_on_char(my_number(),1,0);
	put_boom_on_space(31,29,1,0);
	put_boom_on_space(31,30,1,0);
	put_boom_on_space(31,31,1,0);
	put_boom_on_space(31,32,1,0);
	put_boom_on_space(31,33,1,0);
	put_boom_on_space(31,34,1,0);
	put_boom_on_space(16,38,1,0);
	put_boom_on_space(16,39,1,0);
	put_boom_on_space(16,40,1,0);
	set_terrain(31,29,138);
	set_terrain(31,30,138);
	set_terrain(31,31,138);
	set_terrain(31,32,138);
	set_terrain(31,33,138);
	set_terrain(31,34,138);
	set_terrain(16,38,138);
	set_terrain(16,39,138);
	set_terrain(16,40,138);
	play_sound(-152);
	play_sound(-152);
	play_sound(-152);
	play_sound(-152);
	run_animation_sound(152);
	message_dialog("Dalaghant, rasping with his last breaths, struggles to force out one last sentence: _I... will... have my revenge!_","With that, he dies.");

	set_terrain(my_loc_x(),my_loc_y(),269);
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 (spec_attack == 0)
		if (get_health(my_number()) <= (get_max_health(my_number()) / 10) * 6) {
			message_dialog("_This grows tiresome,_ growls Dalaghant. _We must finish this!_ He speaks several words of magic.","");
			i = 0;
			while (i <= 3) {
				if (get_spell_level(i,1,8) > 0) {
					put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(i),char_loc_y(i),4);
					put_effect_on_char(i,7,1,2);
					set_char_status(i,8,4,0,1);
					set_char_status(i,8,4,0,1);
					}
				i = i + 1;
				}
			set_character_pose(my_number(),1);
			run_animation_sound(165);
			
			set_character_pose(my_number(),2);
			force_instant_terrain_redraw();
			spec_attack = 1;
			}
		
	if (spec_attack == 1)
		if (get_health(my_number()) <= (get_max_health(my_number()) / 10) * 3) {
			message_dialog("Dalaghant roars with fury. _You will not defeat me again!_","He raises his arms, shouting, _In the name of my masters, Rashank the Mighty, Vorthak the Devourer, Hathnazriakh the Temptress, Anthalak the Twisted, Kelora-Nak the Liar, and most of all, Nathaganth the King, I smite you!_");
			damage_char(1000,get_ran(40,10,20),3);
			change_char_health(my_number(),(get_max_health(my_number()) - get_health(my_number())) / 2);
			set_character_pose(my_number(),1);
			run_animation_sound(51);
			
			set_character_pose(my_number(),2);
			force_instant_terrain_redraw();
			spec_attack = 2;
			}
		
	if (tick_difference(last_abil_time,get_current_tick()) > 2) {
		status_nearby(7,6,8,0,80);
		set_character_pose(my_number(),1);
		run_animation_sound(25);
		
		set_character_pose(my_number(),2);
		force_instant_terrain_redraw();
		last_abil_time = get_current_tick();
		}
		
	do_attack();
break;

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