// beamnpc.txt
// A monster with a special ability scripted in. The ability is an beam, used 
// 50% of the time.

// 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.
//   Cell 4 - The ability.

// The abilities:
// 0 - Beam Slow
// 1 - Beam Curse
// 2 - Beam Fire (1-6 dam per level)
// 3 - Beam Acid
// 4 - Beam Terror
// 5 - Beam Daze
// 6 - Beam Debuff
// 7 - Beam Cold (1-8 dam per level)
// 8 - Beam Energy (1-10 dam per level)
// 9 - Beam Charm
// 10 - Beam Freeze
// 11 - lightning
// 12 poison

begincreaturescript;

variables;

short i,target;
short last_abil;
short last_abil2;
short used_abil = 0;
short talky = 0;
short talky2 = 0;
short char_got = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Mother Salamander");
	set_level(ME,40);
	set_boss_level(ME,2);
	set_courage(ME,100);
	set_resistance(ME,7,90);
	set_strategy(ME,0);
	set_coloradj(ME,4);
	

	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	break;

beginstate DEAD_STATE;
	begin_talk_mode(5);
break;

beginstate START_STATE; 
	if (talky == 0) {
		if ((get_nearest_party_char(4) >= 0) || (who_shot_me() >= 0)) {
			talky = 1;
			begin_talk_mode(7);
			}
		end();
		}
	// 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 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
	set_attack_bonus(ME,5 * gf(11,9));
	if ((talky2 == 0) && (gf(11,9) > 2)) {
		talky2 = 1;
		begin_talk_mode(6);
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;

	if ((get_ran(1,0,100) < 70) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	

			print_named_str(ME,"spits out a massive glob of flaming goo.");
			//run_sparkles_on_char(get_target(),173,1,4);
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,12),2);
			set_char_status(get_target(),1,-12);
			set_char_status(get_target(),27,8);
			pc_heard_sound_delay(178,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),70);



		last_abil = get_current_tick();
		end();
		}

	char_got = -1;
	if (tick_difference(last_abil2,get_current_tick()) > 0) {
		i = get_ran(1,0,7);
		if (char_ok(1222 + i) == FALSE) {
			if (spawn_creature(1222 + i))
				char_got = 1222 + i;
		
			}

			
		}
	if (char_got > 0) {
		print_str_color("One of the salamander's eggs hatches.",2);
		set_name(char_got,"Salamander Wyrmling");
		place_particle_num(char_got,1,1,8);
		set_level(char_got,25);
		set_summon_level(char_got,1);
		pc_heard_sound_delay(136,100);						
		last_abil2 = get_current_tick();
		set_attack_bonus(char_got,50);
		set_boss_level(char_got,-1);
		alert_char(char_got);
		run_char_animation(2,1,35);	
		
		alert_char(1222);
		alert_char(1223);
		alert_char(1224);
		alert_char(1225);
		alert_char(1226);
		alert_char(1227);
		alert_char(1228);
		alert_char(1229);
		
		end();
		}
	


	if (used_abil == 0)
		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;