// warrior.txt
// The script for a character that can use the warrior disciplines.

// 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.
//     3 - All moving handled by custom code for that char type.
//   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.
//	 Cell 4 - Which special character code is active?
//		1 - koepp
//		2 - fahdrtoth
//		3 - treviass
//		4 - arrmoora
//		5 - Kah'Horrr;
//		6 - tamas
//	    7 - sepko
//		8 - Barghan warrior
//      9 - Loyalist ambush
//		10 - Commander Myranna
//	    11 - VeressYss
//      12 - hunt solberg
//      13 - hunt solberg + AE
//		14 - Yahsso of the Spear
//      15 - arena warrior
//      16 - undead slith
//      17 - Tial of the Circle
//      18 - Fort Monastery Warrior
//      19 - Fort Monastery Commander

begincreaturescript;

variables;

short i,target;
short tried_return = 0;
short disc_level;
short used_disc;
short misc_var = 0;
short misc_var2 = 0;
short last_abil;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	last_abil = get_current_tick();
	set_courage(ME,100);
	set_resistance(ME,7,90);
	
	if (get_memory_cell(4) == 2) { // fahdrtoth
		set_name(ME,"Fahdroth Benttail");
		set_level(ME,7);
		set_boss_level(ME,2);
		change_max_health(ME,50);
		set_strategy(ME,1);
		}
	if (get_memory_cell(4) == 3) { // treviass
		set_name(ME,"Treviass");
		set_level(ME,7);
		set_boss_level(ME,1);
		change_max_health(ME,20);
		set_resistance(ME,7,0);
		set_aggression(ME,75);
		}
	if (get_memory_cell(4) == 4) { // arrmoora
		set_name(ME,"Arrmoora");
		set_level(ME,8);
		set_boss_level(ME,1);
		change_max_health(ME,20);
		set_resistance(ME,7,0);
		}
	if (get_memory_cell(4) == 6) { // tamas
		set_name(ME,"Tamas");
		set_level(ME,15);
		set_boss_level(ME,1);
		change_max_health(ME,20);
		}
	if (get_memory_cell(4) == 7) { // Loyalist Sepko
		set_name(ME,"Loyalist Sepko");
		set_level(ME,18);
		set_boss_level(ME,1);
		change_max_health(ME,40);
		}
	if (get_memory_cell(4) == 9) { // Loyalist ambush
		set_resistance(ME,7,30);
		}
	if (get_memory_cell(4) == 10) { // Commander Myranna
		set_name(ME,"Commander Myranna");
		set_level(ME,24);
		set_boss_level(ME,2);
		set_attack_bonus(ME,8);
		set_new_abil(ME,20);
		}
	if (get_memory_cell(4) == 11) { // Veress'Yss
		set_name(ME,"Veress'Yss");
		set_level(ME,24);
		set_boss_level(ME,2);
		set_attack_bonus(ME,8);
		set_new_abil(ME,16);
		change_max_health(ME,50);
		}
	if (get_memory_cell(4) == 14) { // Yahsso of the Spear
		set_name(ME,"Yahsso of the Spear");
		set_level(ME,40);
		set_boss_level(ME,2);
		set_attack_bonus(ME,8);
		set_new_abil(ME,10 + get_ran(1,0,6));
		set_aggression(ME,30);
		alter_stat(ME,20,8);
		alter_stat(ME,28,8);
		alter_stat(ME,29,8);
		}
	if (get_memory_cell(4) == 15) { // Arena Warrior
		set_name(ME,"Arena Warrior");
		set_boss_level(ME,1);
		set_attack_bonus(ME,5);
		set_summon_level(ME,1);
		}
	if (get_memory_cell(4) == 17) { // Tial of the Circle
		set_name(ME,"Tial of the Circle");
		set_level(ME,36);
		set_boss_level(ME,2);
		set_attack_bonus(ME,20);
		set_aggression(ME,60);
		alter_stat(ME,19,6);
		alter_stat(ME,28,6);
		alter_stat(ME,29,6);
		}
	if ((get_memory_cell(4) == 18) || (get_memory_cell(4) == 19)) { // Fort Monastery Warrior
		set_level(ME,30);
		change_max_health(ME,100);
		set_attack_bonus(ME,30);
		}

	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))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);

	if (get_memory_cell(4) == 1) { // koepp
		sf(103,1,1);
		toggle_quest(77,4);
		}
	if (get_memory_cell(4) == 2) { // fahdroth
		sf(161,1,1);
		award_party_xp(200,10);
		begin_talk_mode(70);
		}
	if (get_memory_cell(4) == 3) { // treviass
		sf(161,3,0);
		sf(161,2,1);
		begin_talk_mode(67);
		}
	if (get_memory_cell(4) == 11) { // Veress'Yss
		sf(64,1,1);
		award_party_xp(200,25);
		begin_talk_mode(15);
		}
	if (get_memory_cell(4) == 14) { // Yahsso of the Spear

		begin_talk_mode(6);
		}
	if (get_memory_cell(4) == 17) { // Tial of the Circle
		sf(13,12,1);
		erase_char(30010);
		begin_talk_mode(23);
		}
break;

beginstate START_STATE; 
		if ((get_memory_cell(4) == 1) && (misc_var == 0)) { // koepp
		if (get_nearest_party_char(5) >= 0) {
			misc_var = 1;
			talk_no_exit(10);
			}
		}
	if ((get_memory_cell(4) == 2) && (misc_var == 0)) { // fahdroth
		if (get_nearest_party_char(7) >= 0) {
			misc_var = 1;
			alert_char(17466);
			alert_char(17467);
			talk_no_exit(50);
			}
		}
	if ((get_memory_cell(4) == 4) && (misc_var == 0)) { // arrmoora
		if (get_attitude(ME) >= 10)
			misc_var = 1;
			else if (get_nearest_party_char(7) >= 0) {
				misc_var = 1;
				talk_no_exit(30);
				}
		}
	if ((get_memory_cell(4) == 5) && (misc_var == 0)) { // Kah'Horrr
		if (get_nearest_party_char(7) >= 0) {
			misc_var = 1;
			alert_char(30011);
			talk_no_exit(9);
			}
		}
	if ((get_memory_cell(4) == 6) && (misc_var == 0)) { // Tamas
		if (get_nearest_party_char(5) >= 0) {
			misc_var = 1;
			talk_no_exit(30);
			}
		}
	if ((get_memory_cell(4) == 7) && (gf(100,1) == 0)) { // Sepko
		if ((get_nearest_party_char(6) >= 0) || (get_attitude(ME) >= 10)) {
			sf(100,1,1);
			set_attitude(30031,10);
			alert_char(30031);
			activate_hidden_group(31);
			set_level(10823,15);
			set_level(10824,15);
			set_boss_level(10823,1);
			set_boss_level(10824,1);
			talk_no_exit(30);
			}
		}
	if ((get_memory_cell(4) == 8) && (gf(30,10) == 0)) { // Barghan warrior
		if (get_nearest_party_char(5) >= 0) {		
			change_crime_level(26,5);
			sf(30,10,1);
			begin_talk_mode(14);
			}
		}
	if (get_memory_cell(4) == 10) { // Commander Myranna
		if ((get_attitude(ME) >= 10) && (char_ok(10162)))
			talk_no_exit(110);
		if (get_attitude(ME) < 10) {
			if (dist_to_char(10162) <= 2)
				talk_no_exit(110);
				else approach_char(ME,10162,1);
			}
		
		}
	if ((get_memory_cell(4) == 11) && (misc_var == 0)) { // Veress'Yss
		if (get_nearest_party_char(8) >= 0) {
			misc_var = 1;
			talk_no_exit(30);
			}
		}
		
	// 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_foe_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (get_foe_target(ME,12,0)) {
		if (dist_to_party() > 8)
			approach_char(ME,30000,7);
			else {
				do_attack();
				set_state(3);
				}
		}
		
	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if (get_memory_cell(4) == 9) { // Loyalist ambush
		if (misc_var == 0) {
			if (dist_to_nav_point(ME,0) <= 3)
				misc_var = 1;
				else approach_nav_point(ME,0,2);
			}
			else {
				set_foe_target(ME,random_group_member(0));
				set_state(3);
				}
		}

	if ((get_attitude(ME) >= 10) && ((get_memory_cell(4) == 15) || (get_memory_cell(4) == 16))) { // arena warrior, undead slith
				set_foe_target(ME,random_group_member(0));
				set_state(3);
		}
		
	if ((get_memory_cell(4) == 12) || (get_memory_cell(4) == 13)) { // go to solberg
		approach_nav_point(ME,2,1);
		}

	if (get_memory_cell(4) == 18) { // Fort Monastery Warrior
		if (gf(6,13) == 3) {
			if (dist_to_nav_point(ME,1) <= 3)
				fidget(ME,25);
				else approach_nav_point(ME,1,2);
			}
			else if (my_dist_from_start() >= 6) {
				if (get_ran(1,1,100) < 40) 
					return_to_start(ME,1);
				}
		}
	if (get_memory_cell(4) == 19) { // Fort Monastery commander
		if (gf(6,13) == 2) {
			if ((dist_to_nav_point(ME,2) <= 3) && (dist_to_party() <= 6)) {
				sf(6,13,3);
				sf(6,10,1);
				
				set_act_at_dist(30001,1);
				set_act_at_dist(30002,1);
				set_act_at_dist(30003,1);
				
				set_act_at_dist(30004,1);
				set_act_at_dist(696,1);
				set_act_at_dist(697,1);
				set_act_at_dist(698,1);
				
				begin_talk_mode(18);
				}
				else if (dist_to_nav_point(ME,2) > 3)
					approach_nav_point(ME,2,2);
			}
		if (gf(6,13) == 3) {
			if (dist_to_nav_point(ME,2) <= 3)
				fidget(ME,25);
				else approach_nav_point(ME,2,2);
			}
			else if (my_dist_from_start() >= 6) {
				if (get_ran(1,1,100) < 40) 
					return_to_start(ME,1);
				}
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if (get_memory_cell(0) != 3) {
		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) {
				tried_return = 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);

	 // fahdroth
	 if ((get_memory_cell(4) == 2) && (misc_var == 0)) { // fahdroth
			misc_var = 1;
			alert_char(17466);
			alert_char(17467);
			talk_no_exit(50);
		}
	if ((get_memory_cell(4) == 2) && (misc_var == 1) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) { // fahdroth
		misc_var = 2;
		alert_char(30045);
		talk_no_exit(65);
		}
	if ((get_memory_cell(4) == 2) && (misc_var == 2) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) { // fahdroth
		misc_var = 3;
		sf(161,15,1);
		give_object_message(29090,1);
		activate_hidden_group(45);
		talk_no_exit(66);
		}
	if ((get_memory_cell(4) == 2) && (misc_var == 3) && (get_health(ME) < (get_max_health(ME) * 1) / 4)) { // fahdroth
		misc_var = 4;
		alert_char(30045);
		alert_char(30046);
		talk_no_exit(65);
		}

		
	// arrmoora
	if ((get_memory_cell(4) == 4) && (misc_var == 0)) { // arrmoora
			misc_var = 1;
			talk_no_exit(30);
		}

	// Kah'Horrr
	if ((get_memory_cell(4) == 5) && (misc_var == 0)) { 
			misc_var = 1;
			alert_char(30011);
			talk_no_exit(9);
		}

	// Tamas make sure alerted
	if ((get_memory_cell(4) == 5) && (get_attitude(ME) >= 10) && (misc_var < 5)) { 
			misc_var = 5;
		set_attitude(30005,10);
		alert_char(30005);
			}
		
	if ((get_memory_cell(4) == 5) && (misc_var2 == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) { // Kah'Horrr
		misc_var2 = 1;
		run_char_animation(2,1,35);	
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"releases a cloud of acid.");
			status_nearby((get_level(ME) + get_attack_bonus(ME)) / 4,7,6,4);
	  		create_missile_spiral(142,40,7,2);
			pc_heard_sound_delay(177,100);						

		talk_no_exit(10);
		}
	if ((get_memory_cell(4) == 5) && (misc_var2 == 1) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) { // Kah'Horrr
		misc_var2 = 2;
		run_char_animation(2,1,35);	
		print_named_str(ME,"creates several vicious allies.");
		summon_creature(13766);
		summon_creature(13767);
		summon_creature(13768);
		summon_creature(13769);
		place_particle_num(13766,1,1,8);
		place_particle_num(13767,1,1,8);
		place_particle_num(13768,1,1,8);
		place_particle_num(13769,1,1,8);
		
		pc_heard_sound_delay(136,100);						

		talk_no_exit(11);
		}
	if ((get_memory_cell(4) == 5) && (misc_var2 == 2) && (get_health(ME) < (get_max_health(ME) * 1) / 4)) { // Kah'Horrr
		misc_var2 = 3;
		run_char_animation(2,1,35);	
		  	place_particle_num(ME,3,1,8);
			print_named_str(ME,"releases a cloud of dizzying mist.");
			status_nearby(30,7,17,4);
			pc_heard_sound_delay(195,100);						

		talk_no_exit(10);
		}

	if ((get_memory_cell(4) == 7) && (misc_var == 0)) { // Sepko
		if (get_health(ME) < get_max_health(ME) / 2) {
			misc_var = 1;
			set_act_at_dist(30032,1);
			alert_char(30032);
			talk_no_exit(8);
			}
		}
	if ((get_memory_cell(4) == 8) && (gf(30,10) == 0)) { // Barghan warrior
		if (get_nearest_party_char(8) >= 0) {		
			change_crime_level(26,5);
			sf(30,10,1);
			begin_talk_mode(14);
			}
		}
		
	if ((get_memory_cell(4) == 11) && (misc_var2 == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) { // Veress'Yss
		misc_var2 = 1;
		run_char_animation(2,1,35);	
		  	place_particle_num(ME,3,1,8);
			print_named_str(ME,"breaks another glass globe. Mist flies out.");
			status_nearby(-12,7,1,4);
			status_nearby(-12,7,0,4);
			status_nearby(-12,7,8,4);
			status_nearby(12,7,6,4);
			pc_heard_sound_delay(195,100);						

		}
		
	if ((get_memory_cell(4) == 11) && (misc_var2 == 1) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) { // Veress'Yss
		misc_var2 = 2;
		run_char_animation(2,1,35);	
		  	place_particle_num(ME,3,1,8);
			print_named_str(ME,"breaks another glass globe. He speeds up!");
			set_char_status(ME,24,12);
			set_char_status(ME,0,12);
			set_char_status(ME,1,12);
			set_char_status(ME,8,12);
			pc_heard_sound_delay(195,100);						

		}

	if ((get_memory_cell(4) == 13) && (get_ran(1,0,100) < 25) && (is_combat()) && (get_nearest_hate_char(3) >= 0) && (tick_difference(last_abil,get_current_tick()) > 1)) { // solberg hunter
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

			print_named_str(ME,"spins around, cleaving the air with a mighty blow.");
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,5),3,0,0);
			pc_heard_sound_delay(3,100);				
			spray_missiles(15,8,3);
			spray_missiles(20,8,3);
		end();

		
		}

	 if ((get_memory_cell(4) == 14) && (misc_var == 0)) { // Yahsso of the Spear
			misc_var = 1;

			talk_no_exit(5);
		}
	if ((get_memory_cell(4) == 14) && (get_ran(1,0,100) < 75) && (is_combat()) && (get_nearest_hate_char(3) >= 0) && (tick_difference(last_abil,get_current_tick()) > 1)) { // Yahsso of the Spear
		set_new_abil(ME,10 + get_ran(1,0,6));
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();
		set_char_status(ME,1,8);
		set_char_status(ME,8,8);
		set_char_status(ME,20,5);
		set_char_status(ME,21,5);
	
			print_named_str(ME,"spins around, cleaving the air with a mighty blow.");
			damage_nearby(get_ran(1,300,400),3,0,0);

			status_nearby(-12,3,0,0);
			status_nearby(-12,3,1,0);
			status_nearby(-12,3,8,0);
			status_nearby(-8,3,24,0);
			status_nearby(-8,3,28,0);

			pc_heard_sound_delay(3,100);				
			spray_missiles(15,8,3);
			spray_missiles(20,8,3);
		end();

		
		}

	if ((get_memory_cell(4) == 17) && (is_combat()) && (get_nearest_hate_char(8) >= 0) && (tick_difference(last_abil,get_current_tick()) > 1)) { //  Tial of the Circle
			pc_heard_sound_delay(3,100);				
			run_char_animation(2,1,35);	
			
			last_abil = get_current_tick();
			
			print_str_color("Tial lets out a deafening battle cry.",3);
			print_str_color("  A hot wind blows through the chamber.",3);
			
			if (gf(12,25) <= 7) {
				if ((char_ok(1462 + gf(12,25))) && (get_attitude(1462 + gf(12,25)) < 10)) {
					print_str_color("  A spectral slithzerikai begins to move.",3);
					set_attitude(1462 + gf(12,25),10);
					}
				}
			force_char_status(30010,27,0);
				
			inc_flag(12,25,1);
			
			last_abil = get_current_tick();
		
			}
	
	// REGULAR CODE

	if ((my_dist_from_start() >= 25) && (tried_return == 0) && (get_memory_cell(0) != 3)) {
		tried_return = 1;
		set_foe_target(ME,-1);
		return_to_start(ME,2);
		set_state(4);
		}

	disc_level = (get_level(ME) / 4) + 1;
	// use a disc?
	if ((is_combat()) && (get_nearest_hate_char(2) >= 0) && (get_char_status(ME,27) == 0) && (get_ran(1,0,100) < 75)) {
		used_disc = 0;
		
		// heal bad effects
		if ((used_disc == 0) && (get_char_status(ME,1) < 0) && (disc_level >= 4)) {
			use_discipline(ME,4);
			used_disc = 1;
			}
		if ((used_disc == 0) && ((get_char_status(ME,0) < 0) || (get_char_status(ME,8) < 0)) && (get_ran(1,0,100) < 60) && (disc_level >= 3)) {
			use_discipline(ME,3);
			used_disc = 1;
			}
		
		// go down the list and pick somethying good otherwise
		if ((used_disc == 0) && (disc_level >= 9) && (get_ran(1,0,100) < 60)) {
			use_discipline(ME,9);
			used_disc = 1;
			}
		if ((used_disc == 0) && (disc_level >= 8) && (get_ran(1,0,100) < 60)) {
			use_discipline(ME,8);
			used_disc = 1;
			}
		if ((used_disc == 0) && (disc_level >= 7) && (get_ran(1,0,100) < 60)) {
			use_discipline(ME,7);
			used_disc = 1;
			}
		if ((used_disc == 0) && (disc_level >= 6) && (get_ran(1,0,100) < 60)) {
			use_discipline(ME,6);
			used_disc = 1;
			}
		if ((used_disc == 0) && (disc_level >= 5)) {
			use_discipline(ME,5); // dont devove below mighty blow
			used_disc = 1;
			}
		if ((used_disc == 0) && (disc_level >= 2) && (get_ran(1,0,100) < 60)) {
			use_discipline(ME,2);
			used_disc = 1;
			}
		if ((used_disc == 0) && (disc_level >= 1) && (get_ran(1,0,100) < 60)) {
			use_discipline(ME,1);
			used_disc = 1;
			}
		if (used_disc == 0)
			use_discipline(ME,0);
		
		}

	if (((get_memory_cell(4) == 12) || (get_memory_cell(4) == 13)) && (can_see_char(7364)) && (dist_to_char(7364) <= 6) && (get_ran(1,0,100) < 50))
		set_foe_target(ME,7364);
	
	do_attack();
break;

beginstate 4; // returning to start
	if (my_dist_from_start() < 10)
		set_state(START_STATE);

	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	return_to_start(ME,2);
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}

	if (get_memory_cell(4) == 18) { // Fort Monastery Warrior
		if (gf(6,13) == 3)
			begin_talk_mode(17);
		if (my_dist_from_start() > 7) {
			print_str_color("This soldier quickly runs past you.",2);
			end();
			}
		}
	if (get_memory_cell(4) == 19) { // Fort Monastery cmdr
		if (gf(6,13) == 3)
			begin_talk_mode(15);
		if (my_dist_from_start() > 7) {
			begin_talk_mode(16);
			end();
			}
		}
	begin_talk_mode(get_memory_cell(3));
break;