// 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 - Dialogue node played when character dies.

begincreaturescript;

variables;

short i,target;
short tried_return = 0;
short talk_done = 0;
short used_potion = 0;
short pot_type;
short last_abil;

body;

beginstate INIT_STATE;
	set_courage(ME,100);
	set_resistance(ME,7,90);
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0))
		if (get_flag(get_memory_cell(4),get_memory_cell(5)) > 0)
			talk_done = 1;
	if (get_memory_cell(3) == 0)
		talk_done = 1;
	pot_type = get_memory_cell(6);
	last_abil = get_current_tick();
	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(7) != 0)
		begin_talk_mode(get_memory_cell(7));
break;

beginstate START_STATE; 
	if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0)) {
		if (gf(get_memory_cell(4),get_memory_cell(5)) > 0)
			talk_done = 1;
		}

	if ((get_nearest_party_char(4) >= 0) && (talk_done == 0) && (get_attitude(ME) < 10) && (in_boat() == FALSE)) {
		talk_done = 1;
		if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0)) {
			if (get_flag(get_memory_cell(4),get_memory_cell(5)) > 0)
				end();
			set_flag(get_memory_cell(4),get_memory_cell(5),1);
			}
		talk_no_exit(get_memory_cell(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_foe_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (get_foe_target(ME,8,0)) {
		if ((talk_done == 0) && (get_memory_cell(3) > 0)) {
			talk_done = 1;
			if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0))
				set_flag(get_memory_cell(4),get_memory_cell(5),1);
			talk_no_exit(get_memory_cell(3));
			}
		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());
		if (talk_done == 0) {
			talk_done = 1;
			if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0))
				set_flag(get_memory_cell(4),get_memory_cell(5),1);
			talk_no_exit(get_memory_cell(3));
			}
		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) {
			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 ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0)) {
		if (gf(get_memory_cell(4),get_memory_cell(5)) > 0)
			talk_done = 1;
		}

	if (target_ok() == FALSE)
		set_state(START_STATE);
	if ((talk_done == 0) && (get_nearest_party_char(10) >= 0)) {
		talk_done = 1;
		if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0))
			set_flag(get_memory_cell(4),get_memory_cell(5),1);
		talk_no_exit(get_memory_cell(3));
		end();
		}

	if (tick_difference(last_abil,get_current_tick()) > 1) {
		last_abil = get_current_tick();
		if ((pot_type == 1) && (used_potion < 2)) {
			if (get_health(ME) < get_max_health(ME) / 2) {
				print_named_str(ME,"drinks a healing potion.");
				used_potion = used_potion + 1;
				//place_particle_num(ME,3,1,8);
				heal_char(ME,get_max_health(ME) / 2);
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(120,100);						
				end();
				}
			}
		if ((pot_type == 2) && (used_potion < 2)) {
			if ((get_char_status(ME,1) <= 0) && (get_ran(1,0,100) < 70)) {
				print_named_str(ME,"drinks a speed potion.");
				used_potion = used_potion + 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);						
				
				if (used_potion >= 2) {
					used_potion = 0;
					pot_type = 1;
					}
				end();
				}
			
			}
		if ((pot_type == 3) && (used_potion < 2)) {
			if ((get_char_status(ME,18) <= 0) && (get_ran(1,0,100) < 40)) {
				print_named_str(ME,"drinks an invulnerability potion.");
				used_potion = used_potion + 1;
				//place_particle_num(ME,3,0,8);
				set_char_status(ME,18,4);
				run_char_animation(2,1,35);	
				pc_heard_sound_delay(131,100);						
	
				if (used_potion >= 2) {
					used_potion = 0;
					pot_type = 2;
					}
				end();
				}
			}
		if ((pot_type == 4) && (used_potion < 1)) {
			if (get_health(ME) < get_max_health(ME) / 2) {
				print_named_str(ME,"breaks a green glass globe. Noxious gas pours out.");
				used_potion = used_potion + 1;
				status_nearby(get_ran(get_level(ME),1,3),5,6,4);
				spray_missiles(52,8,7);
				pc_heard_sound_delay(177,100);						
				run_char_animation(2,1,35);	

				used_potion = 0;
				pot_type = 2;
					end_combat_turn();
				end();
				}
			}
		if ((pot_type == 5) && (used_potion < 1)) {
			if (get_health(ME) < (get_max_health(ME) * 2) / 3) {
				print_named_str(ME,"breaks a red glass globe. Gas pours out. You start");
				print_str_color("  to feel terrified.",2);
				used_potion = used_potion + 1;
				status_nearby(get_level(ME) + 2,5,7,4);
				particle_nearby(7,8,0,4,8);
				pc_heard_sound_delay(177,100);						
				run_char_animation(2,1,35);	
	
				used_potion = 0;
				pot_type = 4;
					end_combat_turn();
				end();
				}
			}
		if ((pot_type == 6) && (used_potion < 1)) {
			if (get_health(ME) < (get_max_health(ME) * 2) / 3) {
				print_named_str(ME,"breaks a purple glass globe. Gas pours out. You start");
				print_str_color("  to feel strangely peaceful.",2);
				used_potion = used_potion + 1;
				status_nearby(get_level(ME) + 2,5,5,4);
				particle_nearby(7,10,0,4,8);
				pc_heard_sound_delay(177,100);						
				run_char_animation(2,1,35);	
	
				used_potion = 0;
				pot_type = 4;
				end_combat_turn();
				end();
				}
			}
		if ((pot_type == 7) && (used_potion < 1)) {
			if (get_health(ME) < (get_max_health(ME) * 3) / 4) {
				if (summon_creature(my_id() + 1)) {
					used_potion = used_potion + 1;
					print_named_str(ME,"calls for help!");
					place_particle_num(my_id() + 1,1,1,8);
					set_summon_level(my_id() + 1,1);
					if (get_level(my_id() + 1) < get_level(ME) - 4)
						set_level(my_id() + 1,get_level(ME) - 4);
					pc_heard_sound_delay(136,100);						
					run_char_animation(2,1,35);	
					end_combat_turn();
					used_potion = 0;
					pot_type = 4;
					end();
					}
				}
			}
		if ((pot_type == 8) && (used_potion < 4)) {
					used_potion = used_potion + 1;
			if ((get_ran(1,0,100) < 75) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
				if (summon_creature(my_id() + used_potion)) {
					print_named_str(ME,"calls for help!");
					set_summon_level(my_id() + used_potion,1);
					place_particle_num(my_id() + used_potion,1,1,8);
					if (get_level(my_id() + used_potion) < get_level(ME) - 4)
						set_level(my_id() + used_potion,get_level(ME) - 4);
					pc_heard_sound_delay(136,100);						
					run_char_animation(2,1,35);	
					end_combat_turn();
					end();
					}
				}
			}
		}
		if ((pot_type == 9) && (used_potion < 1)) {
			if (get_health(ME) < (get_max_health(ME) * 2) / 3) {
				print_named_str(ME,"breaks a glowing glass globe. Lightning flashes");
				print_str_color("  out in all directions.",2);
				used_potion = used_potion + 1;
				status_nearby(8,6,22,4);
				spray_missiles(57,8,8);
				pc_heard_sound_delay(179,250);						
				run_char_animation(2,1,35);	
	
				used_potion = 0;
				pot_type = 4;
				end_combat_turn();
				end();
				}
			}
				
	if ((my_dist_from_start() >= 25) && (tried_return == 0)) {
		tried_return = 1;
		set_foe_target(ME,-1);
		return_to_start(ME,2);
		set_state(4);
		}

	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_named_str(ME,"doesn't respond.");
		end();
		}
	talk_done = 1;
	if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0))
		set_flag(get_memory_cell(4),get_memory_cell(5),1);
	begin_talk_mode(get_memory_cell(3));
break;