// captain.txt - Modified basicnpc, only used for The Captain. Does a bunch
//	of stuff.
// 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 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.

begincreaturescript;

variables;
short smitee = 0;
short firsthit = 0;
short firstphit = 0;
string ownage;

short i,target;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	set_char_dialogue_pic(ME,520,0);
	if(get_flag(1,19) == 2) {
		force_instant_terrain_redraw();
		begin_talk_mode(10);
		}
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);

	//death text
	reset_dialog();
	add_dialog_str(0,"The captain's swords soundlessly fall to the floor, and almost instantly disappear. The captain, on the other hand, utters a long string of what is probably cursing. In your experience, it normally is.",0);
	add_dialog_str(2,"As the edges of the captain's form start to shimmer and fade, a low keening sound reaches your ears. It seems to be coming from the ship itself.",0);
	add_dialog_str(3,"The sound starts rising in pitch until you can't hear it anymore, at which point the captain finally disappears. It feels as if a heavy pressure has lifted from your mind.",0);
	run_dialog(1);

	pause(4);

	//yay special effects!
	if(get_memory_cell(4) == 0) {
		put_effect_on_char(ME,5,5,1);
		run_animation_sound(162); //weird chime thing
		}

	//clear barriers in captain's chambers
	set_flag(1,20,1);

	//...don't forget the barriers blocking off captain's equipment
	// down in the holds
	//Actually, on second thought, these will remain blocked off
	// just so that the party has to exit to the deck via the stairs
	// they approached the captain with.
	//set_flag(0,18,1);
	//set_flag(0,19,1);

	//dispelling sfx
	put_object_on_space(21,8,-1);
	put_object_on_space(28,8,-1);
	put_object_on_space(24,10,-1);
	put_object_on_space(25,10,-1);
	put_effect_on_space(21,8,8,4,1);
	put_effect_on_space(28,8,8,4,1);
	put_effect_on_space(24,10,8,4,1);
	put_effect_on_space(25,10,8,4,1);
	run_animation_sound(112); //smite spell

break;

beginstate START_STATE;
	// Have I been hit? Jump to the attack state, own them, and come
	//  back to get a new target!
	if (who_hit_me() >= 0)
		set_state_continue(3);

	// 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);
		}
		

		
	// 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 who_hit_me() returns a priest PC, then disable the priest
	// Only works if get_spell_level(char,1,4) returns > 0 and
	// get_stat(char,12) >= 3... so only for priests who can cast
	// Repel Spirit.

	if(who_hit_me() > -1) {
		smitee = who_hit_me();
		if((get_stat(smitee,12) >= 3) && (get_spell_level(smitee,1,4) > 0)) {
			if((firsthit == 0) && (firstphit == 0)) {
				reset_dialog();
				add_dialog_str(0,"The captain bellows something incoherent, and you feel the air in the room crackle.",0);
				run_dialog(1);
				}
			else
				print_str_color("The captain bellows and fires another beam!",2);
			//give 3 dumbfounded
			set_char_status(smitee, 13,3,1,0);
			//give 3 paralyzed
			set_char_status(smitee, 11,3,1,0);
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(smitee),char_loc_y(smitee),2);
			put_effect_on_char(smitee,5,5,1);
			run_animation_sound(43); //stoning ray
			if(firstphit == 0) {
				clear_buffer();
				append_string("The beam strikes ");
				append_char_name(smitee);
				append_string(", who immediately stops moving.");
				get_buffer_text(ownage);
				message_dialog(ownage,"");
				firstphit = 1;
				}
			}
		else {
			if((firsthit == 0) && (firstphit == 0)) {
				reset_dialog();
				add_dialog_str(0,"The captain bellows something incoherent, and you feel the air in the room crackle.",0);
				run_dialog(1);
				}
			else
				print_str_color("The captain bellows and fires another beam!",2);
			//give 3 curse
			set_char_status(smitee, 1,-3,1,0);
			//give 3 weakening
			set_char_status(smitee, 2,-3,1,0);
			put_jagged_zap(my_loc_x(),my_loc_y(),char_loc_x(smitee),char_loc_y(smitee),2);
			put_effect_on_char(smitee,2,5,1);
			run_animation_sound(43); //stoning ray
			if(firsthit == 0) {
				clear_buffer();
				append_string("The beam strikes ");
				append_char_name(smitee);
				append_string(", who cries out in pain and sinks to one knee.");
				get_buffer_text(ownage);
				message_dialog(ownage,"");
				firsthit = 1;
				}
			}
		}

	// ****INCOMPLETE****

	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;