// minibossnpc.txt
// Like basicnpc, but character automatically talks to party when they get 
// within 6 spaces or combat starts. The dialogue is meant to only
// be said once, and not again. This script is meant to be for
// foes of the party.
// 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 the character talks from.
//   Cell 4,5 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     character talks, is set to 1.
//   Cell 6 - Potion use?
//     0 - none
//     1 - healing
//     2 - haste
//     3 - invuln
//     4 - acid cloud
//     5 - fear cloud
//     6 - charm cloud
//     7 - summon an add (summons next numbered monster up)
//     8 - summons 4 adds (summons next 4 numbered monster up)
//     9 - lightning cloud
//   Cell 7 - which char.
//     0 - gelbart

begincreaturescript;

variables;

short i,target;
short talk_done = 0;
short speed_used = 0;
short heal_used = 0;
short last_abil;
short r1;

body;

beginstate INIT_STATE;
	set_courage(ME,100);
	set_resistance(ME,7,90);
	change_max_health(ME,80);
	alter_stat(ME,19,5);
	alter_stat(ME,28,10);
	set_attack_bonus(ME,20);
	
	if (get_memory_cell(7) == 0) {
		set_name(ME,"Lieutenant Gelbart");
		set_boss_level(ME,2);
		}
		else {
			change_max_health(ME,100);
			if (get_memory_cell(7) == 1) 
				set_name(ME,"Raider Rumson");
			if (get_memory_cell(7) == 2) 
				set_name(ME,"Raider Stirling");
			if (get_memory_cell(7) == 3) 
				set_name(ME,"Raider Halloway");
			if (get_memory_cell(7) == 4) 
				set_name(ME,"Raider Easss'Kes");
			if (get_memory_cell(7) == 5) 
				set_name(ME,"Raider Olssson");
			}

	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	inc_flag(23,14,1);
	
	if (gf(23,16) > 0)
		end();
		
	if (gf(23,14) >= 6) {
		sf(23,12,0);
		sf(23,13,1);
		sf(23,10,1);
		begin_talk_mode(18);
		}
	if (get_memory_cell(7) == 0) {
		sf(23,9,1);
		begin_talk_mode(19);
		}
		
break;

beginstate START_STATE; 

	if (gf(23,15) == 9) {
		if (num_chars_in_group(20) == 0) {
			sf(23,15,10);
			force_char_status(30024,19,0);
			sf(22,21,1);
			begin_talk_mode(26);
			}
		force_char_status(ME,19,10);
		end();
		}
	if (gf(23,15) == 12) {
		force_char_status(ME,5,10);
		}
		
	// 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,8,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 (gf(23,16) == 0) {
		if (gf(23,15) == 2) {
			if (dist_to_loc(ME,611,86) > 4)
				move_to_loc(ME,611,86);
				else {
					if (char_ok(2492) == FALSE)
						spawn_creature(2492);
					
					}
			}
		else if (gf(23,15) == 3) {
			if (dist_to_loc(ME,615,108) > 4)
				move_to_loc(ME,615,108);
				else {
					if (get_nearest_party_char(6) >= 0) {
						sf(23,15,4);
						sf(23,11,1);
						begin_talk_mode(20);
						}
					}
			}
		else if ((gf(23,15) == 4) || (gf(23,15) == 5)) {
			if (dist_to_loc(ME,624,111) > 2)
				move_to_loc(ME,624,111);
				else if (get_nearest_party_char(6) >= 0) {
					sf(23,15,5);
					}
			}
		else if (gf(23,15) == 6) { // waiting for pty
			if (get_nearest_party_char(6) >= 0) {
				sf(23,15,7);
				begin_talk_mode(21);
				}
				
			}
		else if (gf(23,15) == 7) { // to shade
			if (dist_to_loc(ME,623,92) > 2)
				move_to_loc(ME,623,92);
				else {
					if ((get_nearest_party_char(2) >= 0) && (char_ok(2493) == FALSE)) {
						spawn_creature(2493);
						begin_talk_mode(22);
						}
					}
			}
		else if (gf(23,15) == 8) { // to dogs
			if (dist_to_loc(ME,601,92) > 2)
				move_to_loc(ME,601,92);
				else {
					if (get_nearest_party_char(2) >= 0) {
						sf(23,15,9);
						activate_hidden_group(20);
						add_range_to_group(2498,2505,20);
						set_strategy(30020,10);
						set_level(30020,30);
						
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2498,2526 + r1);
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2499,2526 + r1);
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2500,2526 + r1);
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2501,2526 + r1);
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2502,2526 + r1);
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2503,2526 + r1);
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2504,2526 + r1);
						r1 = get_ran(1,0,5);
						if (char_ok(2526 + r1))
							set_foe_target(2505,2526 + r1);
							
						begin_talk_mode(25);
						}
					}
			}
		else if (gf(23,15) == 10) { // to dragon
			if (dist_to_loc(ME,589,74) > 2)
				move_to_loc(ME,589,74);
				else {
					if ((get_nearest_party_char(2) >= 0) && (char_ok(2506) == FALSE)) {
						spawn_creature(2506);
						begin_talk_mode(27);
						}
					}
			}
		else if (gf(23,15) == 11) { // to charm
			if (dist_to_loc(ME,589,106) > 4)
				move_to_loc(ME,589,106);
				else {
					if (get_nearest_party_char(3) >= 0) {
						sf(22,23,1);
						sf(23,15,12);
						set_new_abil(30024,16);
						set_attitude(30024,3);
						force_char_status(30024,5,10);
						begin_talk_mode(38);
						}
					}
			}
		else if (gf(23,15) == 13) { // to stores
			if (dist_to_loc(ME,587,115) > 2)
				move_to_loc(ME,587,115);
				else {
					if ((get_nearest_party_char(2) >= 0) && (gf(23,6) > 0) && (is_talking() == FALSE)) {
						sf(23,15,14);
						begin_talk_mode(39);
						}
					}
			}
		else if (gf(23,15) == 14) { // out 1
			if (dist_to_loc(ME,589,106) > 4)
				move_to_loc(ME,589,106);
				else if (get_nearest_party_char(4) >= 0)
					sf(23,15,15);
			}
		else if (gf(23,15) == 15) { // out 2
			if (dist_to_loc(ME,593,92) > 4)
				move_to_loc(ME,593,92);
				else if (get_nearest_party_char(4) >= 0)
					sf(23,15,16);
			}
		else if (gf(23,15) == 16) { // out 3
			if (dist_to_loc(ME,602,92) > 4)
				move_to_loc(ME,602,92);
				else if (get_nearest_party_char(4) >= 0)
					sf(23,15,17);
			}
		else if (gf(23,15) == 17) { // out 4
			if (dist_to_loc(ME,622,91) > 4)
				move_to_loc(ME,622,91);
				else if (get_nearest_party_char(4) >= 0)
					sf(23,15,18);
			}
		else if (gf(23,15) == 18) { // out 5
			if (dist_to_loc(ME,624,107) > 4)
				move_to_loc(ME,624,107);
				else if (get_nearest_party_char(4) >= 0)
					sf(23,15,19);
			}
		else if (gf(23,15) == 20) { // waiting to go
			if (get_nearest_party_char(4) >= 0) {
				sf(23,15,21);
				begin_talk_mode(40);
				}
			}
		else if (gf(23,15) == 21) { // to final fight
			if (dist_to_loc(ME,614,95) > 2)
				move_to_loc(ME,614,95);
				else {
					if ((get_nearest_party_char(2) >= 0) && (char_ok(2515) == FALSE)) {
						activate_hidden_group(21);
						begin_talk_mode(41);
						}
					}
			}
		else if (gf(23,15) == 22) { // fleeee
			if (dist_to_loc(ME,604,76) > 2)
				move_to_loc(ME,604,76);
				else {
					award_party_xp(300,30);
					print_named_str(ME,"uses the portal pylon and is whisked away to safety.");
					erase_char(ME);
					sf(23,10,1);
					if (gf(23,12) == 0) {
						sf(23,12,1);
						begin_talk_mode(43);
						}
					}
			}
		}

	// 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 (gf(23,15) == 9)
		set_state(START_STATE);
	
	if (target_ok() == FALSE)
		set_state(START_STATE);

//short speed_used = 0;
//short heal_used = 0;

	if (tick_difference(last_abil,get_current_tick()) > 1) {
		last_abil = get_current_tick();
		if ((heal_used < 3) && (get_health(ME) < get_max_health(ME) / 2) && (get_ran(1,0,100) < 33)) {
				print_named_str(ME,"drinks a healing potion.");
				heal_used = heal_used + 1;
				//place_particle_num(ME,3,1,8);
				heal_char(ME,get_max_health(ME) / 4);
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(120,100);						
				heal_used = heal_used + 1;
				end();
			}
		if ((speed_used < 3) && (get_char_status(ME,24) <= 0) && (get_ran(1,0,100) < 33)) {
				print_named_str(ME,"drinks a speed potion.");
				speed_used = speed_used + 1;
				//place_particle_num(ME,3,0,8);
				set_char_status(ME,24,8);
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(101,100);						
				
				speed_used = speed_used + 1;
				end();
				}
		}
				


	do_attack();
break;



beginstate TALKING_STATE;
	if (gf(23,15) == 9) {
		print_named_str(ME,"is frozen and can't respond.");
		end();
		}
		
	if ((get_memory_cell(7) == 0) && (gf(23,15) > 1))
		begin_talk_mode(16);
		
	if (get_memory_cell(7) == 0)
		begin_talk_mode(50);

		begin_talk_mode(17);
break;