L2CharacterAI.java 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.ai;
  20. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  21. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ATTACK;
  22. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_CAST;
  23. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_FOLLOW;
  24. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE;
  25. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_INTERACT;
  26. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_MOVE_TO;
  27. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_PICK_UP;
  28. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_REST;
  29. import java.util.List;
  30. import javolution.util.FastList;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.gameserver.GameTimeController;
  33. import com.l2jserver.gameserver.GeoData;
  34. import com.l2jserver.gameserver.ThreadPoolManager;
  35. import com.l2jserver.gameserver.enums.ItemLocation;
  36. import com.l2jserver.gameserver.enums.QuestEventType;
  37. import com.l2jserver.gameserver.instancemanager.WalkingManager;
  38. import com.l2jserver.gameserver.model.L2Object;
  39. import com.l2jserver.gameserver.model.Location;
  40. import com.l2jserver.gameserver.model.actor.L2Attackable;
  41. import com.l2jserver.gameserver.model.actor.L2Character;
  42. import com.l2jserver.gameserver.model.actor.L2Npc;
  43. import com.l2jserver.gameserver.model.actor.L2Playable;
  44. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  45. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  46. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  47. import com.l2jserver.gameserver.model.effects.L2EffectType;
  48. import com.l2jserver.gameserver.model.interfaces.ILocational;
  49. import com.l2jserver.gameserver.model.items.L2Weapon;
  50. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  51. import com.l2jserver.gameserver.model.items.type.L2WeaponType;
  52. import com.l2jserver.gameserver.model.quest.Quest;
  53. import com.l2jserver.gameserver.model.skills.L2Skill;
  54. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  55. import com.l2jserver.gameserver.network.SystemMessageId;
  56. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  57. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  58. import com.l2jserver.gameserver.taskmanager.AttackStanceTaskManager;
  59. import com.l2jserver.util.Rnd;
  60. /**
  61. * This class manages AI of L2Character.<br>
  62. * L2CharacterAI :
  63. * <ul>
  64. * <li>L2AttackableAI</li>
  65. * <li>L2DoorAI</li>
  66. * <li>L2PlayerAI</li>
  67. * <li>L2SummonAI</li>
  68. * </ul>
  69. */
  70. public class L2CharacterAI extends AbstractAI
  71. {
  72. public static class IntentionCommand
  73. {
  74. protected final CtrlIntention _crtlIntention;
  75. protected final Object _arg0, _arg1;
  76. protected IntentionCommand(CtrlIntention pIntention, Object pArg0, Object pArg1)
  77. {
  78. _crtlIntention = pIntention;
  79. _arg0 = pArg0;
  80. _arg1 = pArg1;
  81. }
  82. public CtrlIntention getCtrlIntention()
  83. {
  84. return _crtlIntention;
  85. }
  86. }
  87. /**
  88. * Cast Task
  89. * @author Zoey76
  90. */
  91. public static class CastTask implements Runnable
  92. {
  93. private final L2Character _activeChar;
  94. private final L2Object _target;
  95. private final L2Skill _skill;
  96. public CastTask(L2Character actor, L2Skill skill, L2Object target)
  97. {
  98. _activeChar = actor;
  99. _target = target;
  100. _skill = skill;
  101. }
  102. @Override
  103. public void run()
  104. {
  105. if (_activeChar.isAttackingNow())
  106. {
  107. _activeChar.abortAttack();
  108. }
  109. _activeChar.getAI().changeIntentionToCast(_skill, _target);
  110. }
  111. }
  112. /**
  113. * Constructor of L2CharacterAI.
  114. * @param accessor The AI accessor of the L2Character
  115. */
  116. public L2CharacterAI(L2Character.AIAccessor accessor)
  117. {
  118. super(accessor);
  119. }
  120. public IntentionCommand getNextIntention()
  121. {
  122. return null;
  123. }
  124. @Override
  125. protected void onEvtAttacked(L2Character attacker)
  126. {
  127. if ((attacker instanceof L2Attackable) && !((L2Attackable) attacker).isCoreAIDisabled())
  128. {
  129. clientStartAutoAttack();
  130. }
  131. }
  132. /**
  133. * Manage the Idle Intention : Stop Attack, Movement and Stand Up the actor.<br>
  134. * <B><U> Actions</U> :</B>
  135. * <ul>
  136. * <li>Set the AI Intention to AI_INTENTION_IDLE</li>
  137. * <li>Init cast and attack target</li>
  138. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  139. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  140. * <li>Stand up the actor server side AND client side by sending Server->Client packet ChangeWaitType (broadcast)</li>
  141. * </ul>
  142. */
  143. @Override
  144. protected void onIntentionIdle()
  145. {
  146. // Set the AI Intention to AI_INTENTION_IDLE
  147. changeIntention(AI_INTENTION_IDLE, null, null);
  148. // Init cast and attack target
  149. setCastTarget(null);
  150. setAttackTarget(null);
  151. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  152. clientStopMoving(null);
  153. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  154. clientStopAutoAttack();
  155. }
  156. /**
  157. * Manage the Active Intention : Stop Attack, Movement and Launch Think Event.<br>
  158. * <B><U> Actions</U> : <I>if the Intention is not already Active</I></B>
  159. * <ul>
  160. * <li>Set the AI Intention to AI_INTENTION_ACTIVE</li>
  161. * <li>Init cast and attack target</li>
  162. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  163. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  164. * <li>Launch the Think Event</li>
  165. * </ul>
  166. */
  167. @Override
  168. protected void onIntentionActive()
  169. {
  170. // Check if the Intention is not already Active
  171. if (getIntention() != AI_INTENTION_ACTIVE)
  172. {
  173. // Set the AI Intention to AI_INTENTION_ACTIVE
  174. changeIntention(AI_INTENTION_ACTIVE, null, null);
  175. // Init cast and attack target
  176. setCastTarget(null);
  177. setAttackTarget(null);
  178. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  179. clientStopMoving(null);
  180. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  181. clientStopAutoAttack();
  182. // Also enable random animations for this L2Character if allowed
  183. // This is only for mobs - town npcs are handled in their constructor
  184. if (_actor instanceof L2Attackable)
  185. {
  186. ((L2Npc) _actor).startRandomAnimationTimer();
  187. }
  188. // Launch the Think Event
  189. onEvtThink();
  190. }
  191. }
  192. /**
  193. * Manage the Rest Intention.<br>
  194. * <B><U> Actions</U> : </B>
  195. * <ul>
  196. * <li>Set the AI Intention to AI_INTENTION_IDLE</li>
  197. * </ul>
  198. */
  199. @Override
  200. protected void onIntentionRest()
  201. {
  202. // Set the AI Intention to AI_INTENTION_IDLE
  203. setIntention(AI_INTENTION_IDLE);
  204. }
  205. /**
  206. * Manage the Attack Intention : Stop current Attack (if necessary), Start a new Attack and Launch Think Event.<br>
  207. * <B><U> Actions</U> : </B>
  208. * <ul>
  209. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  210. * <li>Set the Intention of this AI to AI_INTENTION_ATTACK</li>
  211. * <li>Set or change the AI attack target</li>
  212. * <li>Start the actor Auto Attack client side by sending Server->Client packet AutoAttackStart (broadcast)</li>
  213. * <li>Launch the Think Event</li>
  214. * </ul>
  215. * <B><U> Overridden in</U> :</B>
  216. * <ul>
  217. * <li>L2AttackableAI : Calculate attack timeout</li>
  218. * </ul>
  219. */
  220. @Override
  221. protected void onIntentionAttack(L2Character target)
  222. {
  223. if (target == null)
  224. {
  225. clientActionFailed();
  226. return;
  227. }
  228. if (getIntention() == AI_INTENTION_REST)
  229. {
  230. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  231. clientActionFailed();
  232. return;
  233. }
  234. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow() || _actor.isAfraid())
  235. {
  236. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  237. clientActionFailed();
  238. return;
  239. }
  240. // Check if the Intention is already AI_INTENTION_ATTACK
  241. if (getIntention() == AI_INTENTION_ATTACK)
  242. {
  243. // Check if the AI already targets the L2Character
  244. if (getAttackTarget() != target)
  245. {
  246. // Set the AI attack target (change target)
  247. setAttackTarget(target);
  248. stopFollow();
  249. // Launch the Think Event
  250. notifyEvent(CtrlEvent.EVT_THINK, null);
  251. }
  252. else
  253. {
  254. clientActionFailed(); // else client freezes until cancel target
  255. }
  256. }
  257. else
  258. {
  259. // Set the Intention of this AbstractAI to AI_INTENTION_ATTACK
  260. changeIntention(AI_INTENTION_ATTACK, target, null);
  261. // Set the AI attack target
  262. setAttackTarget(target);
  263. stopFollow();
  264. // Launch the Think Event
  265. notifyEvent(CtrlEvent.EVT_THINK, null);
  266. }
  267. }
  268. /**
  269. * Manage the Cast Intention : Stop current Attack, Init the AI in order to cast and Launch Think Event.<br>
  270. * <B><U> Actions</U> : </B>
  271. * <ul>
  272. * <li>Set the AI cast target</li>
  273. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  274. * <li>Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor</li>
  275. * <li>Set the AI skill used by INTENTION_CAST</li>
  276. * <li>Set the Intention of this AI to AI_INTENTION_CAST</li>
  277. * <li>Launch the Think Event</li>
  278. * </ul>
  279. */
  280. @Override
  281. protected void onIntentionCast(L2Skill skill, L2Object target)
  282. {
  283. if ((getIntention() == AI_INTENTION_REST) && skill.isMagic())
  284. {
  285. clientActionFailed();
  286. _actor.setIsCastingNow(false);
  287. return;
  288. }
  289. if (_actor.getBowAttackEndTime() > GameTimeController.getInstance().getGameTicks())
  290. {
  291. ThreadPoolManager.getInstance().scheduleGeneral(new CastTask(_actor, skill, target), (_actor.getBowAttackEndTime() - GameTimeController.getInstance().getGameTicks()) * GameTimeController.MILLIS_IN_TICK);
  292. }
  293. else
  294. {
  295. changeIntentionToCast(skill, target);
  296. }
  297. }
  298. protected void changeIntentionToCast(L2Skill skill, L2Object target)
  299. {
  300. // Set the AI cast target
  301. setCastTarget((L2Character) target);
  302. // Set the AI skill used by INTENTION_CAST
  303. _skill = skill;
  304. // Change the Intention of this AbstractAI to AI_INTENTION_CAST
  305. changeIntention(AI_INTENTION_CAST, skill, target);
  306. // Launch the Think Event
  307. notifyEvent(CtrlEvent.EVT_THINK, null);
  308. }
  309. /**
  310. * Manage the Move To Intention : Stop current Attack and Launch a Move to Location Task.<br>
  311. * <B><U> Actions</U> : </B>
  312. * <ul>
  313. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  314. * <li>Set the Intention of this AI to AI_INTENTION_MOVE_TO</li>
  315. * <li>Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)</li>
  316. * </ul>
  317. */
  318. @Override
  319. protected void onIntentionMoveTo(Location loc)
  320. {
  321. if (getIntention() == AI_INTENTION_REST)
  322. {
  323. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  324. clientActionFailed();
  325. return;
  326. }
  327. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  328. {
  329. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  330. clientActionFailed();
  331. return;
  332. }
  333. // Set the Intention of this AbstractAI to AI_INTENTION_MOVE_TO
  334. changeIntention(AI_INTENTION_MOVE_TO, loc, null);
  335. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  336. clientStopAutoAttack();
  337. // Abort the attack of the L2Character and send Server->Client ActionFailed packet
  338. _actor.abortAttack();
  339. // Move the actor to Location (x,y,z) server side AND client side by sending Server->Client packet CharMoveToLocation (broadcast)
  340. moveTo(loc.getX(), loc.getY(), loc.getZ());
  341. }
  342. /**
  343. * Manage the Follow Intention : Stop current Attack and Launch a Follow Task.<br>
  344. * <B><U> Actions</U> : </B>
  345. * <ul>
  346. * <li>Stop the actor auto-attack server side AND client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  347. * <li>Set the Intention of this AI to AI_INTENTION_FOLLOW</li>
  348. * <li>Create and Launch an AI Follow Task to execute every 1s</li>
  349. * </ul>
  350. */
  351. @Override
  352. protected void onIntentionFollow(L2Character target)
  353. {
  354. if (getIntention() == AI_INTENTION_REST)
  355. {
  356. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  357. clientActionFailed();
  358. return;
  359. }
  360. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  361. {
  362. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  363. clientActionFailed();
  364. return;
  365. }
  366. if (_actor.isMovementDisabled())
  367. {
  368. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  369. clientActionFailed();
  370. return;
  371. }
  372. // Dead actors can`t follow
  373. if (_actor.isDead())
  374. {
  375. clientActionFailed();
  376. return;
  377. }
  378. // do not follow yourself
  379. if (_actor == target)
  380. {
  381. clientActionFailed();
  382. return;
  383. }
  384. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  385. clientStopAutoAttack();
  386. // Set the Intention of this AbstractAI to AI_INTENTION_FOLLOW
  387. changeIntention(AI_INTENTION_FOLLOW, target, null);
  388. // Create and Launch an AI Follow Task to execute every 1s
  389. startFollow(target);
  390. }
  391. /**
  392. * Manage the PickUp Intention : Set the pick up target and Launch a Move To Pawn Task (offset=20).<br>
  393. * <B><U> Actions</U> : </B>
  394. * <ul>
  395. * <li>Set the AI pick up target</li>
  396. * <li>Set the Intention of this AI to AI_INTENTION_PICK_UP</li>
  397. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)</li>
  398. * </ul>
  399. */
  400. @Override
  401. protected void onIntentionPickUp(L2Object object)
  402. {
  403. if (getIntention() == AI_INTENTION_REST)
  404. {
  405. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  406. clientActionFailed();
  407. return;
  408. }
  409. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  410. {
  411. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  412. clientActionFailed();
  413. return;
  414. }
  415. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  416. clientStopAutoAttack();
  417. if ((object instanceof L2ItemInstance) && (((L2ItemInstance) object).getItemLocation() != ItemLocation.VOID))
  418. {
  419. return;
  420. }
  421. // Set the Intention of this AbstractAI to AI_INTENTION_PICK_UP
  422. changeIntention(AI_INTENTION_PICK_UP, object, null);
  423. // Set the AI pick up target
  424. setTarget(object);
  425. if ((object.getX() == 0) && (object.getY() == 0)) // TODO: Find the drop&spawn bug
  426. {
  427. _log.warning("Object in coords 0,0 - using a temporary fix");
  428. object.setXYZ(getActor().getX(), getActor().getY(), getActor().getZ() + 5);
  429. }
  430. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  431. moveToPawn(object, 20);
  432. }
  433. /**
  434. * Manage the Interact Intention : Set the interact target and Launch a Move To Pawn Task (offset=60).<br>
  435. * <B><U> Actions</U> : </B>
  436. * <ul>
  437. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  438. * <li>Set the AI interact target</li>
  439. * <li>Set the Intention of this AI to AI_INTENTION_INTERACT</li>
  440. * <li>Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)</li>
  441. * </ul>
  442. */
  443. @Override
  444. protected void onIntentionInteract(L2Object object)
  445. {
  446. if (getIntention() == AI_INTENTION_REST)
  447. {
  448. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  449. clientActionFailed();
  450. return;
  451. }
  452. if (_actor.isAllSkillsDisabled() || _actor.isCastingNow())
  453. {
  454. // Cancel action client side by sending Server->Client packet ActionFailed to the L2PcInstance actor
  455. clientActionFailed();
  456. return;
  457. }
  458. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  459. clientStopAutoAttack();
  460. if (getIntention() != AI_INTENTION_INTERACT)
  461. {
  462. // Set the Intention of this AbstractAI to AI_INTENTION_INTERACT
  463. changeIntention(AI_INTENTION_INTERACT, object, null);
  464. // Set the AI interact target
  465. setTarget(object);
  466. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  467. moveToPawn(object, 60);
  468. }
  469. }
  470. /**
  471. * Do nothing.
  472. */
  473. @Override
  474. protected void onEvtThink()
  475. {
  476. // do nothing
  477. }
  478. /**
  479. * Do nothing.
  480. */
  481. @Override
  482. protected void onEvtAggression(L2Character target, int aggro)
  483. {
  484. // do nothing
  485. }
  486. /**
  487. * Launch actions corresponding to the Event Stunned then onAttacked Event.<br>
  488. * <B><U> Actions</U> :</B>
  489. * <ul>
  490. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  491. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  492. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  493. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  494. * <li>Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)</li>
  495. * </ul>
  496. */
  497. @Override
  498. protected void onEvtStunned(L2Character attacker)
  499. {
  500. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  501. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  502. if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  503. {
  504. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  505. }
  506. // Stop Server AutoAttack also
  507. setAutoAttacking(false);
  508. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  509. clientStopMoving(null);
  510. // Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)
  511. onEvtAttacked(attacker);
  512. }
  513. @Override
  514. protected void onEvtParalyzed(L2Character attacker)
  515. {
  516. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  517. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  518. if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  519. {
  520. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  521. }
  522. // Stop Server AutoAttack also
  523. setAutoAttacking(false);
  524. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  525. clientStopMoving(null);
  526. // Launch actions corresponding to the Event onAttacked (only for L2AttackableAI after the stunning periode)
  527. onEvtAttacked(attacker);
  528. }
  529. /**
  530. * Launch actions corresponding to the Event Sleeping.<br>
  531. * <B><U> Actions</U> :</B>
  532. * <ul>
  533. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  534. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  535. * <li>Break an attack and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  536. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  537. * </ul>
  538. */
  539. @Override
  540. protected void onEvtSleeping(L2Character attacker)
  541. {
  542. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  543. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  544. if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  545. {
  546. AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  547. }
  548. // stop Server AutoAttack also
  549. setAutoAttacking(false);
  550. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  551. clientStopMoving(null);
  552. }
  553. /**
  554. * Launch actions corresponding to the Event Rooted.<br>
  555. * <B><U> Actions</U> :</B>
  556. * <ul>
  557. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  558. * <li>Launch actions corresponding to the Event onAttacked</li>
  559. * </ul>
  560. */
  561. @Override
  562. protected void onEvtRooted(L2Character attacker)
  563. {
  564. // Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)
  565. // _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  566. // if (AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  567. // AttackStanceTaskManager.getInstance().removeAttackStanceTask(_actor);
  568. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  569. clientStopMoving(null);
  570. // Launch actions corresponding to the Event onAttacked
  571. onEvtAttacked(attacker);
  572. }
  573. /**
  574. * Launch actions corresponding to the Event Confused.<br>
  575. * <B><U> Actions</U> :</B>
  576. * <ul>
  577. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  578. * <li>Launch actions corresponding to the Event onAttacked</li>
  579. * </ul>
  580. */
  581. @Override
  582. protected void onEvtConfused(L2Character attacker)
  583. {
  584. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  585. clientStopMoving(null);
  586. // Launch actions corresponding to the Event onAttacked
  587. onEvtAttacked(attacker);
  588. }
  589. /**
  590. * Launch actions corresponding to the Event Muted.<br>
  591. * <B><U> Actions</U> :</B>
  592. * <ul>
  593. * <li>Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character</li>
  594. * </ul>
  595. */
  596. @Override
  597. protected void onEvtMuted(L2Character attacker)
  598. {
  599. // Break a cast and send Server->Client ActionFailed packet and a System Message to the L2Character
  600. onEvtAttacked(attacker);
  601. }
  602. /**
  603. * Do nothing.
  604. */
  605. @Override
  606. protected void onEvtEvaded(L2Character attacker)
  607. {
  608. // do nothing
  609. }
  610. /**
  611. * Launch actions corresponding to the Event ReadyToAct.<br>
  612. * <B><U> Actions</U> :</B>
  613. * <ul>
  614. * <li>Launch actions corresponding to the Event Think</li>
  615. * </ul>
  616. */
  617. @Override
  618. protected void onEvtReadyToAct()
  619. {
  620. // Launch actions corresponding to the Event Think
  621. onEvtThink();
  622. }
  623. /**
  624. * Do nothing.
  625. */
  626. @Override
  627. protected void onEvtUserCmd(Object arg0, Object arg1)
  628. {
  629. // do nothing
  630. }
  631. /**
  632. * Launch actions corresponding to the Event Arrived.<br>
  633. * <B><U> Actions</U> :</B>
  634. * <ul>
  635. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  636. * <li>Launch actions corresponding to the Event Think</li>
  637. * </ul>
  638. */
  639. @Override
  640. protected void onEvtArrived()
  641. {
  642. _accessor.getActor().revalidateZone(true);
  643. if (_accessor.getActor().moveToNextRoutePoint())
  644. {
  645. return;
  646. }
  647. if (_accessor.getActor() instanceof L2Attackable)
  648. {
  649. ((L2Attackable) _accessor.getActor()).setisReturningToSpawnPoint(false);
  650. }
  651. clientStoppedMoving();
  652. if (_actor instanceof L2Npc)
  653. {
  654. L2Npc npc = (L2Npc) _actor;
  655. WalkingManager.getInstance().onArrived(npc); // Walking Manager support
  656. // Notify quest
  657. if (npc.getTemplate().getEventQuests(QuestEventType.ON_MOVE_FINISHED) != null)
  658. {
  659. for (Quest quest : npc.getTemplate().getEventQuests(QuestEventType.ON_MOVE_FINISHED))
  660. {
  661. quest.notifyMoveFinished(npc);
  662. }
  663. }
  664. }
  665. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  666. if (getIntention() == AI_INTENTION_MOVE_TO)
  667. {
  668. setIntention(AI_INTENTION_ACTIVE);
  669. }
  670. // Launch actions corresponding to the Event Think
  671. onEvtThink();
  672. }
  673. /**
  674. * Launch actions corresponding to the Event ArrivedRevalidate.<br>
  675. * <B><U> Actions</U> :</B>
  676. * <ul>
  677. * <li>Launch actions corresponding to the Event Think</li>
  678. * </ul>
  679. */
  680. @Override
  681. protected void onEvtArrivedRevalidate()
  682. {
  683. // Launch actions corresponding to the Event Think
  684. onEvtThink();
  685. }
  686. /**
  687. * Launch actions corresponding to the Event ArrivedBlocked.<br>
  688. * <B><U> Actions</U> :</B>
  689. * <ul>
  690. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  691. * <li>If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE</li>
  692. * <li>Launch actions corresponding to the Event Think</li>
  693. * </ul>
  694. */
  695. @Override
  696. protected void onEvtArrivedBlocked(Location blocked_at_loc)
  697. {
  698. // If the Intention was AI_INTENTION_MOVE_TO, set the Intention to AI_INTENTION_ACTIVE
  699. if ((getIntention() == AI_INTENTION_MOVE_TO) || (getIntention() == AI_INTENTION_CAST))
  700. {
  701. setIntention(AI_INTENTION_ACTIVE);
  702. }
  703. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  704. clientStopMoving(blocked_at_loc);
  705. // Launch actions corresponding to the Event Think
  706. onEvtThink();
  707. }
  708. /**
  709. * Launch actions corresponding to the Event ForgetObject.<br>
  710. * <B><U> Actions</U> :</B>
  711. * <ul>
  712. * <li>If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE</li>
  713. * <li>If the object was targeted to attack, stop the auto-attack, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  714. * <li>If the object was targeted to cast, cancel target and set the Intention to AI_INTENTION_ACTIVE</li>
  715. * <li>If the object was targeted to follow, stop the movement, cancel AI Follow Task and set the Intention to AI_INTENTION_ACTIVE</li>
  716. * <li>If the targeted object was the actor , cancel AI target, stop AI Follow Task, stop the movement and set the Intention to AI_INTENTION_IDLE</li>
  717. * </ul>
  718. */
  719. @Override
  720. protected void onEvtForgetObject(L2Object object)
  721. {
  722. // If the object was targeted and the Intention was AI_INTENTION_INTERACT or AI_INTENTION_PICK_UP, set the Intention to AI_INTENTION_ACTIVE
  723. if (getTarget() == object)
  724. {
  725. setTarget(null);
  726. if ((getIntention() == AI_INTENTION_INTERACT) || (getIntention() == AI_INTENTION_PICK_UP))
  727. {
  728. setIntention(AI_INTENTION_ACTIVE);
  729. }
  730. }
  731. // Check if the object was targeted to attack
  732. if (getAttackTarget() == object)
  733. {
  734. // Cancel attack target
  735. setAttackTarget(null);
  736. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  737. setIntention(AI_INTENTION_ACTIVE);
  738. }
  739. // Check if the object was targeted to cast
  740. if (getCastTarget() == object)
  741. {
  742. // Cancel cast target
  743. setCastTarget(null);
  744. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  745. setIntention(AI_INTENTION_ACTIVE);
  746. }
  747. // Check if the object was targeted to follow
  748. if (getFollowTarget() == object)
  749. {
  750. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  751. clientStopMoving(null);
  752. // Stop an AI Follow Task
  753. stopFollow();
  754. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  755. setIntention(AI_INTENTION_ACTIVE);
  756. }
  757. // Check if the targeted object was the actor
  758. if (_actor == object)
  759. {
  760. // Cancel AI target
  761. setTarget(null);
  762. setAttackTarget(null);
  763. setCastTarget(null);
  764. // Stop an AI Follow Task
  765. stopFollow();
  766. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  767. clientStopMoving(null);
  768. // Set the Intention of this AbstractAI to AI_INTENTION_IDLE
  769. changeIntention(AI_INTENTION_IDLE, null, null);
  770. }
  771. }
  772. /**
  773. * Launch actions corresponding to the Event Cancel.<br>
  774. * <B><U> Actions</U> :</B>
  775. * <ul>
  776. * <li>Stop an AI Follow Task</li>
  777. * <li>Launch actions corresponding to the Event Think</li>
  778. * </ul>
  779. */
  780. @Override
  781. protected void onEvtCancel()
  782. {
  783. _actor.abortCast();
  784. // Stop an AI Follow Task
  785. stopFollow();
  786. if (!AttackStanceTaskManager.getInstance().hasAttackStanceTask(_actor))
  787. {
  788. _actor.broadcastPacket(new AutoAttackStop(_actor.getObjectId()));
  789. }
  790. // Launch actions corresponding to the Event Think
  791. onEvtThink();
  792. }
  793. /**
  794. * Launch actions corresponding to the Event Dead.<br>
  795. * <B><U> Actions</U> :</B>
  796. * <ul>
  797. * <li>Stop an AI Follow Task</li>
  798. * <li>Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)</li>
  799. * </ul>
  800. */
  801. @Override
  802. protected void onEvtDead()
  803. {
  804. // Stop an AI Tasks
  805. stopAITask();
  806. // Kill the actor client side by sending Server->Client packet AutoAttackStop, StopMove/StopRotation, Die (broadcast)
  807. clientNotifyDead();
  808. if (!(_actor instanceof L2Playable))
  809. {
  810. _actor.setWalking();
  811. }
  812. }
  813. /**
  814. * Launch actions corresponding to the Event Fake Death.<br>
  815. * <B><U> Actions</U> :</B>
  816. * <ul>
  817. * <li>Stop an AI Follow Task</li>
  818. * </ul>
  819. */
  820. @Override
  821. protected void onEvtFakeDeath()
  822. {
  823. // Stop an AI Follow Task
  824. stopFollow();
  825. // Stop the actor movement and send Server->Client packet StopMove/StopRotation (broadcast)
  826. clientStopMoving(null);
  827. // Init AI
  828. _intention = AI_INTENTION_IDLE;
  829. setTarget(null);
  830. setCastTarget(null);
  831. setAttackTarget(null);
  832. }
  833. /**
  834. * Do nothing.
  835. */
  836. @Override
  837. protected void onEvtFinishCasting()
  838. {
  839. // do nothing
  840. }
  841. protected boolean maybeMoveToPosition(ILocational worldPosition, int offset)
  842. {
  843. if (worldPosition == null)
  844. {
  845. _log.warning("maybeMoveToPosition: worldPosition == NULL!");
  846. return false;
  847. }
  848. if (offset < 0)
  849. {
  850. return false; // skill radius -1
  851. }
  852. if (!_actor.isInsideRadius(worldPosition, offset + _actor.getTemplate().getCollisionRadius(), false, false))
  853. {
  854. if (_actor.isMovementDisabled())
  855. {
  856. return true;
  857. }
  858. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  859. {
  860. _actor.setRunning();
  861. }
  862. stopFollow();
  863. int x = _actor.getX();
  864. int y = _actor.getY();
  865. double dx = worldPosition.getX() - x;
  866. double dy = worldPosition.getY() - y;
  867. double dist = Math.sqrt((dx * dx) + (dy * dy));
  868. double sin = dy / dist;
  869. double cos = dx / dist;
  870. dist -= offset - 5;
  871. x += (int) (dist * cos);
  872. y += (int) (dist * sin);
  873. moveTo(x, y, worldPosition.getZ());
  874. return true;
  875. }
  876. if (getFollowTarget() != null)
  877. {
  878. stopFollow();
  879. }
  880. return false;
  881. }
  882. /**
  883. * Manage the Move to Pawn action in function of the distance and of the Interact area.<br>
  884. * <B><U> Actions</U> :</B>
  885. * <ul>
  886. * <li>Get the distance between the current position of the L2Character and the target (x,y)</li>
  887. * <li>If the distance > offset+20, move the actor (by running) to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)</li>
  888. * <li>If the distance <= offset+20, Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  889. * </ul>
  890. * <B><U> Example of use </U> :</B>
  891. * <ul>
  892. * <li>L2PLayerAI, L2SummonAI</li>
  893. * </ul>
  894. * @param target The targeted L2Object
  895. * @param offset The Interact area radius
  896. * @return True if a movement must be done
  897. */
  898. protected boolean maybeMoveToPawn(L2Object target, int offset)
  899. {
  900. // Get the distance between the current position of the L2Character and the target (x,y)
  901. if (target == null)
  902. {
  903. _log.warning("maybeMoveToPawn: target == NULL!");
  904. return false;
  905. }
  906. if (offset < 0)
  907. {
  908. return false; // skill radius -1
  909. }
  910. offset += _actor.getTemplate().getCollisionRadius();
  911. if (target instanceof L2Character)
  912. {
  913. offset += ((L2Character) target).getTemplate().getCollisionRadius();
  914. }
  915. if (!_actor.isInsideRadius(target, offset, false, false))
  916. {
  917. // Caller should be L2Playable and thinkAttack/thinkCast/thinkInteract/thinkPickUp
  918. if (getFollowTarget() != null)
  919. {
  920. // allow larger hit range when the target is moving (check is run only once per second)
  921. if (!_actor.isInsideRadius(target, offset + 100, false, false))
  922. {
  923. return true;
  924. }
  925. stopFollow();
  926. return false;
  927. }
  928. if (_actor.isMovementDisabled())
  929. {
  930. // If player is trying attack target but he cannot move to attack target
  931. // change his intention to idle
  932. if (_actor.getAI().getIntention() == CtrlIntention.AI_INTENTION_ATTACK)
  933. {
  934. _actor.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  935. }
  936. return true;
  937. }
  938. // while flying there is no move to cast
  939. if ((_actor.getAI().getIntention() == CtrlIntention.AI_INTENTION_CAST) && (_actor instanceof L2PcInstance) && ((L2PcInstance) _actor).isTransformed())
  940. {
  941. if (!((L2PcInstance) _actor).getTransformation().isCombat())
  942. {
  943. _actor.sendPacket(SystemMessageId.DIST_TOO_FAR_CASTING_STOPPED);
  944. _actor.sendPacket(ActionFailed.STATIC_PACKET);
  945. return true;
  946. }
  947. }
  948. // If not running, set the L2Character movement type to run and send Server->Client packet ChangeMoveType to all others L2PcInstance
  949. if (!_actor.isRunning() && !(this instanceof L2PlayerAI) && !(this instanceof L2SummonAI))
  950. {
  951. _actor.setRunning();
  952. }
  953. stopFollow();
  954. if ((target instanceof L2Character) && !(target instanceof L2DoorInstance))
  955. {
  956. if (((L2Character) target).isMoving())
  957. {
  958. offset -= 100;
  959. }
  960. if (offset < 5)
  961. {
  962. offset = 5;
  963. }
  964. startFollow((L2Character) target, offset);
  965. }
  966. else
  967. {
  968. // Move the actor to Pawn server side AND client side by sending Server->Client packet MoveToPawn (broadcast)
  969. moveToPawn(target, offset);
  970. }
  971. return true;
  972. }
  973. if (getFollowTarget() != null)
  974. {
  975. stopFollow();
  976. }
  977. // Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)
  978. // clientStopMoving(null);
  979. return false;
  980. }
  981. /**
  982. * Modify current Intention and actions if the target is lost or dead.<br>
  983. * <B><U> Actions</U> : <I>If the target is lost or dead</I></B>
  984. * <ul>
  985. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  986. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  987. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li>
  988. * </ul>
  989. * <B><U> Example of use </U> :</B>
  990. * <ul>
  991. * <li>L2PLayerAI, L2SummonAI</li>
  992. * </ul>
  993. * @param target The targeted L2Object
  994. * @return True if the target is lost or dead (false if fakedeath)
  995. */
  996. protected boolean checkTargetLostOrDead(L2Character target)
  997. {
  998. if ((target == null) || target.isAlikeDead())
  999. {
  1000. // check if player is fakedeath
  1001. if ((target instanceof L2PcInstance) && ((L2PcInstance) target).isFakeDeath())
  1002. {
  1003. target.stopFakeDeath(true);
  1004. return false;
  1005. }
  1006. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  1007. setIntention(AI_INTENTION_ACTIVE);
  1008. return true;
  1009. }
  1010. return false;
  1011. }
  1012. /**
  1013. * Modify current Intention and actions if the target is lost.<br>
  1014. * <B><U> Actions</U> : <I>If the target is lost</I></B>
  1015. * <ul>
  1016. * <li>Stop the actor auto-attack client side by sending Server->Client packet AutoAttackStop (broadcast)</li>
  1017. * <li>Stop the actor movement server side AND client side by sending Server->Client packet StopMove/StopRotation (broadcast)</li>
  1018. * <li>Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE</li>
  1019. * </ul>
  1020. * <B><U> Example of use </U> :</B>
  1021. * <ul>
  1022. * <li>L2PLayerAI, L2SummonAI</li>
  1023. * </ul>
  1024. * @param target The targeted L2Object
  1025. * @return True if the target is lost
  1026. */
  1027. protected boolean checkTargetLost(L2Object target)
  1028. {
  1029. // check if player is fakedeath
  1030. if (target instanceof L2PcInstance)
  1031. {
  1032. L2PcInstance target2 = (L2PcInstance) target; // convert object to chara
  1033. if (target2.isFakeDeath())
  1034. {
  1035. target2.stopFakeDeath(true);
  1036. return false;
  1037. }
  1038. }
  1039. if (target == null)
  1040. {
  1041. // Set the Intention of this AbstractAI to AI_INTENTION_ACTIVE
  1042. setIntention(AI_INTENTION_ACTIVE);
  1043. return true;
  1044. }
  1045. if ((_actor != null) && (_skill != null) && _skill.isBad() && (_skill.getAffectRange() > 0) && (Config.GEODATA > 0) && !GeoData.getInstance().canSeeTarget(_actor, target))
  1046. {
  1047. setIntention(AI_INTENTION_ACTIVE);
  1048. return true;
  1049. }
  1050. return false;
  1051. }
  1052. protected class SelfAnalysis
  1053. {
  1054. public boolean isMage = false;
  1055. public boolean isBalanced;
  1056. public boolean isArcher = false;
  1057. public boolean isHealer = false;
  1058. public boolean isFighter = false;
  1059. public boolean cannotMoveOnLand = false;
  1060. public List<L2Skill> generalSkills = new FastList<>();
  1061. public List<L2Skill> buffSkills = new FastList<>();
  1062. public int lastBuffTick = 0;
  1063. public List<L2Skill> debuffSkills = new FastList<>();
  1064. public int lastDebuffTick = 0;
  1065. public List<L2Skill> cancelSkills = new FastList<>();
  1066. public List<L2Skill> healSkills = new FastList<>();
  1067. // public List<L2Skill> trickSkills = new FastList<>();
  1068. public List<L2Skill> generalDisablers = new FastList<>();
  1069. public List<L2Skill> sleepSkills = new FastList<>();
  1070. public List<L2Skill> rootSkills = new FastList<>();
  1071. public List<L2Skill> muteSkills = new FastList<>();
  1072. public List<L2Skill> resurrectSkills = new FastList<>();
  1073. public boolean hasHealOrResurrect = false;
  1074. public boolean hasLongRangeSkills = false;
  1075. public boolean hasLongRangeDamageSkills = false;
  1076. public int maxCastRange = 0;
  1077. public SelfAnalysis()
  1078. {
  1079. }
  1080. public void init()
  1081. {
  1082. switch (((L2NpcTemplate) _actor.getTemplate()).getAIDataStatic().getAiType())
  1083. {
  1084. case FIGHTER:
  1085. isFighter = true;
  1086. break;
  1087. case MAGE:
  1088. isMage = true;
  1089. break;
  1090. case CORPSE:
  1091. case BALANCED:
  1092. isBalanced = true;
  1093. break;
  1094. case ARCHER:
  1095. isArcher = true;
  1096. break;
  1097. case HEALER:
  1098. isHealer = true;
  1099. break;
  1100. default:
  1101. isFighter = true;
  1102. break;
  1103. }
  1104. // water movement analysis
  1105. if (_actor.isNpc())
  1106. {
  1107. switch (_actor.getId())
  1108. {
  1109. case 20314: // great white shark
  1110. case 20849: // Light Worm
  1111. cannotMoveOnLand = true;
  1112. break;
  1113. default:
  1114. cannotMoveOnLand = false;
  1115. break;
  1116. }
  1117. }
  1118. // skill analysis
  1119. for (L2Skill sk : _actor.getAllSkills())
  1120. {
  1121. if (sk.isPassive())
  1122. {
  1123. continue;
  1124. }
  1125. int castRange = sk.getCastRange();
  1126. boolean hasLongRangeDamageSkill = false;
  1127. switch (sk.getSkillType())
  1128. {
  1129. case BUFF:
  1130. buffSkills.add(sk);
  1131. continue; // won't be considered something for fighting
  1132. case NOTDONE:
  1133. case COREDONE:
  1134. continue; // won't be considered something for fighting
  1135. default:
  1136. if (sk.isDebuff())
  1137. {
  1138. debuffSkills.add(sk);
  1139. }
  1140. else if (sk.hasEffectType(L2EffectType.DISPEL))
  1141. {
  1142. cancelSkills.add(sk);
  1143. }
  1144. else if (sk.hasEffectType(L2EffectType.HEAL, L2EffectType.HEAL_PERCENT))
  1145. {
  1146. healSkills.add(sk);
  1147. hasHealOrResurrect = true;
  1148. }
  1149. else if (sk.hasEffectType(L2EffectType.SLEEP))
  1150. {
  1151. sleepSkills.add(sk);
  1152. }
  1153. else if (sk.hasEffectType(L2EffectType.STUN, L2EffectType.PARALYZE))
  1154. {
  1155. // hardcoding petrification until improvements are made to
  1156. // EffectTemplate... petrification is totally different for
  1157. // AI than paralyze
  1158. switch (sk.getId())
  1159. {
  1160. case 367:
  1161. case 4111:
  1162. case 4383:
  1163. case 4616:
  1164. case 4578:
  1165. sleepSkills.add(sk);
  1166. break;
  1167. default:
  1168. generalDisablers.add(sk);
  1169. break;
  1170. }
  1171. }
  1172. else if (sk.hasEffectType(L2EffectType.ROOT))
  1173. {
  1174. rootSkills.add(sk);
  1175. }
  1176. else if (sk.hasEffectType(L2EffectType.FEAR))
  1177. {
  1178. debuffSkills.add(sk);
  1179. }
  1180. else if (sk.hasEffectType(L2EffectType.MUTE))
  1181. {
  1182. muteSkills.add(sk);
  1183. }
  1184. else if (sk.hasEffectType(L2EffectType.RESURRECTION))
  1185. {
  1186. resurrectSkills.add(sk);
  1187. hasHealOrResurrect = true;
  1188. }
  1189. else
  1190. {
  1191. generalSkills.add(sk);
  1192. hasLongRangeDamageSkill = true;
  1193. }
  1194. break;
  1195. }
  1196. if (castRange > 70)
  1197. {
  1198. hasLongRangeSkills = true;
  1199. if (hasLongRangeDamageSkill)
  1200. {
  1201. hasLongRangeDamageSkills = true;
  1202. }
  1203. }
  1204. if (castRange > maxCastRange)
  1205. {
  1206. maxCastRange = castRange;
  1207. }
  1208. }
  1209. // Because of missing skills, some mages/balanced cannot play like mages
  1210. if (!hasLongRangeDamageSkills && isMage)
  1211. {
  1212. isBalanced = true;
  1213. isMage = false;
  1214. isFighter = false;
  1215. }
  1216. if (!hasLongRangeSkills && (isMage || isBalanced))
  1217. {
  1218. isBalanced = false;
  1219. isMage = false;
  1220. isFighter = true;
  1221. }
  1222. if (generalSkills.isEmpty() && isMage)
  1223. {
  1224. isBalanced = true;
  1225. isMage = false;
  1226. }
  1227. }
  1228. }
  1229. protected class TargetAnalysis
  1230. {
  1231. public L2Character character;
  1232. public boolean isMage;
  1233. public boolean isBalanced;
  1234. public boolean isArcher;
  1235. public boolean isFighter;
  1236. public boolean isCanceled;
  1237. public boolean isSlower;
  1238. public boolean isMagicResistant;
  1239. public TargetAnalysis()
  1240. {
  1241. }
  1242. public void update(L2Character target)
  1243. {
  1244. // update status once in 4 seconds
  1245. if ((target == character) && (Rnd.nextInt(100) > 25))
  1246. {
  1247. return;
  1248. }
  1249. character = target;
  1250. if (target == null)
  1251. {
  1252. return;
  1253. }
  1254. isMage = false;
  1255. isBalanced = false;
  1256. isArcher = false;
  1257. isFighter = false;
  1258. isCanceled = false;
  1259. if (target.getMAtk(null, null) > (1.5 * target.getPAtk(null)))
  1260. {
  1261. isMage = true;
  1262. }
  1263. else if (((target.getPAtk(null) * 0.8) < target.getMAtk(null, null)) || ((target.getMAtk(null, null) * 0.8) > target.getPAtk(null)))
  1264. {
  1265. isBalanced = true;
  1266. }
  1267. else
  1268. {
  1269. L2Weapon weapon = target.getActiveWeaponItem();
  1270. if ((weapon != null) && ((weapon.getItemType() == L2WeaponType.BOW) || (weapon.getItemType() == L2WeaponType.CROSSBOW)))
  1271. {
  1272. isArcher = true;
  1273. }
  1274. else
  1275. {
  1276. isFighter = true;
  1277. }
  1278. }
  1279. if (target.getRunSpeed() < (_actor.getRunSpeed() - 3))
  1280. {
  1281. isSlower = true;
  1282. }
  1283. else
  1284. {
  1285. isSlower = false;
  1286. }
  1287. if ((target.getMDef(null, null) * 1.2) > _actor.getMAtk(null, null))
  1288. {
  1289. isMagicResistant = true;
  1290. }
  1291. else
  1292. {
  1293. isMagicResistant = false;
  1294. }
  1295. if (target.getBuffCount() < 4)
  1296. {
  1297. isCanceled = true;
  1298. }
  1299. }
  1300. }
  1301. public boolean canAura(L2Skill sk)
  1302. {
  1303. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1304. {
  1305. for (L2Object target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1306. {
  1307. if (target == getAttackTarget())
  1308. {
  1309. return true;
  1310. }
  1311. }
  1312. }
  1313. return false;
  1314. }
  1315. public boolean canAOE(L2Skill sk)
  1316. {
  1317. if (sk.hasEffectType(L2EffectType.DISPEL))
  1318. {
  1319. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1320. {
  1321. boolean cancast = true;
  1322. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1323. {
  1324. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1325. {
  1326. continue;
  1327. }
  1328. if (target instanceof L2Attackable)
  1329. {
  1330. L2Npc targets = ((L2Npc) target);
  1331. L2Npc actors = ((L2Npc) _actor);
  1332. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1333. {
  1334. continue;
  1335. }
  1336. }
  1337. if (target.isAffectedBySkill(sk.getId()))
  1338. {
  1339. cancast = false;
  1340. }
  1341. }
  1342. if (cancast)
  1343. {
  1344. return true;
  1345. }
  1346. }
  1347. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1348. {
  1349. boolean cancast = true;
  1350. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1351. {
  1352. if (!GeoData.getInstance().canSeeTarget(_actor, target) || (target == null))
  1353. {
  1354. continue;
  1355. }
  1356. if (target instanceof L2Attackable)
  1357. {
  1358. L2Npc targets = ((L2Npc) target);
  1359. L2Npc actors = ((L2Npc) _actor);
  1360. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1361. {
  1362. continue;
  1363. }
  1364. }
  1365. if (!target.getEffectList().isEmpty())
  1366. {
  1367. cancast = true;
  1368. }
  1369. }
  1370. if (cancast)
  1371. {
  1372. return true;
  1373. }
  1374. }
  1375. }
  1376. else
  1377. {
  1378. if ((sk.getTargetType() == L2TargetType.AURA) || (sk.getTargetType() == L2TargetType.BEHIND_AURA) || (sk.getTargetType() == L2TargetType.FRONT_AURA) || (sk.getTargetType() == L2TargetType.AURA_CORPSE_MOB))
  1379. {
  1380. boolean cancast = false;
  1381. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1382. {
  1383. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1384. {
  1385. continue;
  1386. }
  1387. if (target instanceof L2Attackable)
  1388. {
  1389. L2Npc targets = ((L2Npc) target);
  1390. L2Npc actors = ((L2Npc) _actor);
  1391. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1392. {
  1393. continue;
  1394. }
  1395. }
  1396. if (!target.getEffectList().isEmpty())
  1397. {
  1398. cancast = true;
  1399. }
  1400. }
  1401. if (cancast)
  1402. {
  1403. return true;
  1404. }
  1405. }
  1406. else if ((sk.getTargetType() == L2TargetType.AREA) || (sk.getTargetType() == L2TargetType.BEHIND_AREA) || (sk.getTargetType() == L2TargetType.FRONT_AREA))
  1407. {
  1408. boolean cancast = true;
  1409. for (L2Character target : getAttackTarget().getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1410. {
  1411. if (!GeoData.getInstance().canSeeTarget(_actor, target))
  1412. {
  1413. continue;
  1414. }
  1415. if (target instanceof L2Attackable)
  1416. {
  1417. L2Npc targets = ((L2Npc) target);
  1418. L2Npc actors = ((L2Npc) _actor);
  1419. if ((targets.getEnemyClan() == null) || (actors.getClan() == null) || !targets.getEnemyClan().equals(actors.getClan()) || ((actors.getClan() == null) && (actors.getIsChaos() == 0)))
  1420. {
  1421. continue;
  1422. }
  1423. }
  1424. if (target.isAffectedBySkill(sk.getId()))
  1425. {
  1426. cancast = false;
  1427. }
  1428. }
  1429. if (cancast)
  1430. {
  1431. return true;
  1432. }
  1433. }
  1434. }
  1435. return false;
  1436. }
  1437. public boolean canParty(L2Skill sk)
  1438. {
  1439. if (sk.getTargetType() == L2TargetType.PARTY)
  1440. {
  1441. int count = 0;
  1442. int ccount = 0;
  1443. for (L2Character target : _actor.getKnownList().getKnownCharactersInRadius(sk.getAffectRange()))
  1444. {
  1445. if (!(target instanceof L2Attackable) || !GeoData.getInstance().canSeeTarget(_actor, target))
  1446. {
  1447. continue;
  1448. }
  1449. L2Npc targets = ((L2Npc) target);
  1450. L2Npc actors = ((L2Npc) _actor);
  1451. if ((actors.getFactionId() != null) && targets.getFactionId().equals(actors.getFactionId()))
  1452. {
  1453. count++;
  1454. if (target.isAffectedBySkill(sk.getId()))
  1455. {
  1456. ccount++;
  1457. }
  1458. }
  1459. }
  1460. if (ccount < count)
  1461. {
  1462. return true;
  1463. }
  1464. }
  1465. return false;
  1466. }
  1467. public boolean isParty(L2Skill sk)
  1468. {
  1469. return (sk.getTargetType() == L2TargetType.PARTY);
  1470. }
  1471. }