content: /**
   * This file is part of Aion-Lightning <aion-lightning.org>.
   *
   *  Aion-Lightning is free software: you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation, either version 3 of the License, or
   *  (at your option) any later version.
   *
   *  Aion-Lightning is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details. *
   *  You should have received a copy of the GNU General Public License
   *  along with Aion-Lightning.
   *  If not, see <http://www.gnu.org/licenses/>.
   */
  package ai.instance.tallocsHollow;
  
  import java.util.List;
  import java.util.concurrent.Future;
  import java.util.concurrent.atomic.AtomicBoolean;
  
  import com.aionemu.gameserver.utils.ThreadPoolManager;
  import com.aionemu.gameserver.ai2.AIName;
  import com.aionemu.gameserver.ai2.NpcAI2;
  import com.aionemu.gameserver.ai2.manager.WalkManager;
  import com.aionemu.gameserver.model.EmotionType;
  import com.aionemu.gameserver.model.gameobjects.Creature;
  import com.aionemu.gameserver.model.gameobjects.Npc;
  import com.aionemu.gameserver.model.templates.spawns.SpawnTemplate;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_EMOTION;
  import com.aionemu.gameserver.skillengine.SkillEngine;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  import com.aionemu.gameserver.world.WorldMapInstance;
  import com.aionemu.gameserver.world.WorldPosition;
  
  import ai.AggressiveNpcAI2;
  
  /**
   * @author xTz
   */
  @AIName("celestius")
  public class CelestiusAI2 extends AggressiveNpcAI2 {
  
  	private AtomicBoolean isHome = new AtomicBoolean(true);
  	private Future<?> helpersTask;
  
  	@Override
  	protected void handleAttack(Creature creature) {
  		super.handleAttack(creature);
  		if (isHome.compareAndSet(true, false)) {
  			startHelpersCall();
  
  		}
  	}
  
  	private void cancelHelpersTask() {
  		if (helpersTask != null && !helpersTask.isDone()) {
  			helpersTask.cancel(true);
  		}
  	}
  
  	private void startHelpersCall() {
  		helpersTask = ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable() {
  
  			@Override
  			public void run() {
  				if (isAlreadyDead() && getLifeStats().getHpPercentage() < 90) {
  					deleteHelpers();
  					cancelHelpersTask();
  				}
  				else {
  					deleteHelpers();
  					SkillEngine.getInstance().getSkill(getOwner(), 18981, 44, getOwner()).useNoAnimationSkill();
  					startRun((Npc) spawn(281514, 518, 813, 1378, (byte) 0), "3001900001");
  					startRun((Npc) spawn(281514, 551, 795, 1376, (byte) 0), "3001900002");
  					startRun((Npc) spawn(281514, 574, 854, 1375, (byte) 0), "3001900003");
  				}
  			}
  		}, 1000, 25000);
  	}
  
  	private void startRun(Npc npc, String walkId) {
  		npc.getSpawn().setWalkerId(walkId);
  		WalkManager.startWalking((NpcAI2) npc.getAi2());
  		npc.setState(1);
  		PacketSendUtility.broadcastPacket(npc, new SM_EMOTION(npc, EmotionType.START_EMOTE2, 0, npc.getObjectId()));
  	}
  
  	private void deleteHelpers() {
  		WorldPosition p = getPosition();
  		if (p != null) {
  			WorldMapInstance instance = p.getWorldMapInstance();
  			if (instance != null) {
  				List<Npc> npcs = instance.getNpcs(281514);
  				for (Npc npc : npcs) {
  					SpawnTemplate template = npc.getSpawn();
  					if (npc != null && (template.getX() == 518 || template.getX() == 551 || template.getX() == 574)) {
  						npc.getController().onDelete();
  					}
  				}
  			}
  		}
  	}
  
  	@Override
  	protected void handleBackHome() {
  		cancelHelpersTask();
  		deleteHelpers();
  		isHome.set(true);
  		super.handleBackHome();
  	}
  
  	@Override
  	protected void handleDespawned() {
  		cancelHelpersTask();
  		deleteHelpers();
  		super.handleDespawned();
  	}
  
  	@Override
  	protected void handleDied() {
  		cancelHelpersTask();
  		deleteHelpers();
  		super.handleDied();
  	}
  }
  