//groupnpc.txt - Like basicnpc.txt. Basically, it fidgets around and attacks anything 
//it sees it hates. Also, if it becomes hostile, it sends an alert message to everyone 
//in its group, if it is in a group.
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnŐt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3 - Number of state when talked to. Plays default text if 0.
// cell 4 - special idtneity, 1 - general 2 3 - warmage 4 - bruiser

begincreaturescript;

variables;

short i,target;
short sent_alert = 0;
short tried_return = 0;
short reached_nav = 0;
short disc_level;
short used_disc;
short last_abil;
short talky = 0;

body;

beginstate INIT_STATE;
	set_aggression(ME,60);
	
	set_name(ME,"Horde Siege Warrior");
	set_level(ME,30);
	if (gf(170,8) > 0)
		set_boss_level(ME,1);
		else set_boss_level(ME,2);
	set_resistance(ME,7,40);
	set_courage(ME,100);
	
	if (get_memory_cell(4) == 1) {
		set_resistance(ME,7,90);
		set_name(ME,"General Gash'Lah");
		set_level(ME,33);
		set_boss_level(ME,2);
		set_attack_bonus(ME,8);
		set_new_abil(ME,16);
		set_strategy(ME,10);
		}
	if (get_memory_cell(4) == 2) {
		set_resistance(ME,7,90);
		set_name(ME,"Trinket Mage Pryss");
		set_level(ME,28);
		set_new_abil(ME,7);
		}
	if (get_memory_cell(4) == 3) {
		set_resistance(ME,7,90);
		set_name(ME,"Trinket Mage Vizsh");
		set_level(ME,28);
		set_new_abil(ME,9);
		}
	if (get_memory_cell(4) == 4) {
		set_name(ME,"Horde Bruiser");
		set_level(ME,32);
		set_resistance(ME,7,100);
		}
	if (get_memory_cell(4) == 5) {
		set_resistance(ME,7,90);
		set_name(ME,"Horde War Lizard");
		set_level(ME,31);
		}
	
	
	last_abil = get_current_tick();
		
	if (gf(6,1) > 0)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
	if ((what_group_in(ME) >= 0) && (sent_alert == 0)) {
		set_attitude(30000 + what_group_in(ME),10);
		alert_char(30000 + what_group_in(ME));
		sent_alert = 1;
		}

break;

beginstate START_STATE; 
	if ((gf(6,12) == 0) && (gf(6,13) < 3) && (get_nearest_party_char(8) >= 0)) {
		sf(6,12,1);
		
		set_act_at_dist(30001,1);
		set_act_at_dist(30002,1);
		set_act_at_dist(30003,1);
		
		begin_talk_mode(12);
		}
		
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 24)
			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 we're hostile, hunt party down
	if ((get_attitude(ME) >= 10) && (gf(6,12) > 0) && (dist_to_party() <= 30)) {
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}

	if (gf(6,13) > 2) {
		set_aggression(ME,100);
		if (reached_nav == 0) {
			if (dist_to_nav_point(ME,1) <= 3)
				reached_nav = 1;
				else approach_nav_point(ME,1,2);
			}
			else {
				if (dist_to_nav_point(ME,1) > 9)
					reached_nav = 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 (gf(6,13) == 0)
		set_attack_bonus(ME,40); // hard to fight before event
		else set_attack_bonus(ME,3);
		
	if ((gf(6,12) == 0) && (gf(6,13) < 3) && (get_nearest_party_char(6) >= 0)) {
		sf(6,12,1);
		
		set_act_at_dist(30001,1);
		set_act_at_dist(30002,1);
		set_act_at_dist(30003,1);
		
		begin_talk_mode(12);
		}


	if ((my_dist_from_start() >= 32) && (gf(6,13) < 3) && (tried_return == 0)) {
		tried_return = 1;
		set_foe_target(ME,-1);
		return_to_start(ME,2);
		set_state(4);
		}

	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((get_memory_cell(4) == 1) && (get_nearest_party_char(8) >= 0) && (talky == 0) && (gf(6,13) > 2)) {
		talky = 1;
		set_foe_target(ME,get_nearest_party_char(8));
		begin_talk_mode(19);
		}
		
	if ((get_memory_cell(4) == 1) && (is_combat()) && (get_nearest_hate_char(2) >= 0) && (tick_difference(last_abil,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

			status_nearby(-12,3,1,4);
			status_nearby(-12,3,0,4);
			status_nearby(-12,3,8,4);


			print_named_str(ME,"spins around, cleaving the air with his mighty spear.");
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),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) == 2) || (get_memory_cell(4) == 3)) && (is_combat()) && (get_nearest_hate_char(2) >= 0) && (tick_difference(last_abil,get_current_tick()) > 2) && (get_ran(1,0,100) < 50)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();



			print_named_str(ME,"removes a charm from around her neck and smashes it.");
		  	place_particle_num(ME,4,6,8);

		i = get_ran(1,0,2);
		if (i == 0) { // ae terror
		  	place_particle_num(ME,4,10,8);
			print_str_color("  The broken orb radiates a wave of pure terror.",2);
			status_nearby(get_level(ME) + 5,5,7,4);
			//particle_nearby(ae_range,23,2,4,2);
			pc_heard_sound_delay(177,100);						
			
			force_char_status(ME,20,3);
			}
		if (i == 1) { // ae acid + damage
			print_str_color("  A cloud of acidic gas spreads out in all directions.",2);
			status_nearby(4,5,6,4);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),5,5,0);
	  		create_missile_spiral(142,40,5,2);
			pc_heard_sound_delay(177,100);						
			
			force_char_status(ME,21,3);
			}
		if (i == 2) { // ae acid + damage
			print_str_color("  A cloud of searing energy rolls out from the slith.",2);
			damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),5,1,0);
			pc_heard_sound_delay(179,100);						
	  		create_missile_spiral(155,40,5,2);
				
			
			force_char_status(ME,24,3);
			}
			
		end();		
		}

		
	disc_level = (get_level(ME) / 4) + 1;
	// use a disc?
	if ((is_combat()) && (get_nearest_hate_char(1) >= 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;
			}

		
		}


	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();
		}
	begin_talk_mode(get_memory_cell(3));
break;