// turret.txt - Only exists to create creatures. Never moves or attacks. 
// Cell 4,5 - Stuff done flag to set when spawner is killed. If both are 0, no flag set.
// Cell 6 - talking cell

begincreaturescript;

variables;

short i,target;
short last_abil;
short last_abil2;
short used_abil = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Apprentice Summoner");
	set_level(ME,20);
	set_boss_level(ME,1);

	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	
	set_walk_speed(ME,0);
	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(4) != 0) || (get_memory_cell(5) != 0))
		inc_flag(get_memory_cell(4),get_memory_cell(5),1);
break;

beginstate START_STATE; 
	if ((get_nearest_party_char(8) >= 0) || (who_shot_me() >= 0)) {
		if (gf(62,23) == 0) {
			sf(62,23,1);
			begin_talk_mode(15);
			}
		}
		
	if (who_shot_me() >= 0) {


		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}


		
	if (get_foe_target(ME,10,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_nearest_party_char(8) >= 0) || (who_shot_me() >= 0)) {
		if (gf(62,23) == 0) {
			sf(62,23,1);
			begin_talk_mode(15);
			}
		}

	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
			set_foe_target(ME,-1);
			set_state(START_STATE);
		}
		
	used_abil = 0;

	if (tick_difference(last_abil2,get_current_tick()) > 0) {
		last_abil2 = get_current_tick();
		inc_flag(62,24,1);
		print_str_color("An apprentice works on summoning a new defender.",2);
		}
		
	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,"flings a lance of fire.");
			//run_sparkles_on_char(get_target(),173,1,4);
			damage_char(get_target(),get_ran(get_level(ME) + get_attack_bonus(ME),1,6),2);
			pc_heard_sound_delay(178,100);						
			shoot_projectile(ME,char_loc_x(get_target()),char_loc_y(get_target()),70);
			used_abil = 1;

		last_abil = get_current_tick();
		}

	if (used_abil == 0)
		do_attack();
	turret_heal();
break;


beginstate TALKING_STATE;
	if (get_memory_cell(6) == 0) {
		print_str("Talking: It doesn't respond. It can't talk.");
		end();
		}
	begin_talk_mode(get_memory_cell(6));
break;