L2Npc.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model.actor;
  16. import static com.l2jserver.gameserver.ai.CtrlIntention.AI_INTENTION_ACTIVE;
  17. import java.util.Collection;
  18. import java.util.logging.Level;
  19. import javolution.util.FastList;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.SevenSigns;
  22. import com.l2jserver.gameserver.SevenSignsFestival;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.cache.HtmCache;
  25. import com.l2jserver.gameserver.datatables.ItemTable;
  26. import com.l2jserver.gameserver.handler.BypassHandler;
  27. import com.l2jserver.gameserver.handler.IBypassHandler;
  28. import com.l2jserver.gameserver.instancemanager.CastleManager;
  29. import com.l2jserver.gameserver.instancemanager.FortManager;
  30. import com.l2jserver.gameserver.instancemanager.TownManager;
  31. import com.l2jserver.gameserver.model.L2ItemInstance;
  32. import com.l2jserver.gameserver.model.L2NpcAIData;
  33. import com.l2jserver.gameserver.model.L2Object;
  34. import com.l2jserver.gameserver.model.L2Skill;
  35. import com.l2jserver.gameserver.model.L2Spawn;
  36. import com.l2jserver.gameserver.model.L2World;
  37. import com.l2jserver.gameserver.model.L2WorldRegion;
  38. import com.l2jserver.gameserver.model.actor.instance.L2ClanHallManagerInstance;
  39. import com.l2jserver.gameserver.model.actor.instance.L2DoormenInstance;
  40. import com.l2jserver.gameserver.model.actor.instance.L2FestivalGuideInstance;
  41. import com.l2jserver.gameserver.model.actor.instance.L2FishermanInstance;
  42. import com.l2jserver.gameserver.model.actor.instance.L2MerchantInstance;
  43. import com.l2jserver.gameserver.model.actor.instance.L2NpcInstance;
  44. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  45. import com.l2jserver.gameserver.model.actor.instance.L2TeleporterInstance;
  46. import com.l2jserver.gameserver.model.actor.instance.L2TrainerInstance;
  47. import com.l2jserver.gameserver.model.actor.instance.L2WarehouseInstance;
  48. import com.l2jserver.gameserver.model.actor.knownlist.NpcKnownList;
  49. import com.l2jserver.gameserver.model.actor.stat.NpcStat;
  50. import com.l2jserver.gameserver.model.actor.status.NpcStatus;
  51. import com.l2jserver.gameserver.model.entity.Castle;
  52. import com.l2jserver.gameserver.model.entity.Fort;
  53. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  54. import com.l2jserver.gameserver.model.quest.Quest;
  55. import com.l2jserver.gameserver.model.zone.type.L2TownZone;
  56. import com.l2jserver.gameserver.network.SystemMessageId;
  57. import com.l2jserver.gameserver.network.serverpackets.AbstractNpcInfo;
  58. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  59. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  60. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  61. import com.l2jserver.gameserver.network.serverpackets.ServerObjectInfo;
  62. import com.l2jserver.gameserver.network.serverpackets.SocialAction;
  63. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  64. import com.l2jserver.gameserver.skills.Stats;
  65. import com.l2jserver.gameserver.taskmanager.DecayTaskManager;
  66. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  67. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate.AIType;
  68. import com.l2jserver.gameserver.templates.item.L2Item;
  69. import com.l2jserver.gameserver.templates.item.L2Weapon;
  70. import com.l2jserver.gameserver.util.Broadcast;
  71. import com.l2jserver.util.Rnd;
  72. import com.l2jserver.util.StringUtil;
  73. /**
  74. * This class represents a Non-Player-Character in the world. It can be a monster or a friendly character.
  75. * It also uses a template to fetch some static values. The templates are hardcoded in the client, so we can rely on them.<BR><BR>
  76. *
  77. * L2Character :<BR><BR>
  78. * <li>L2Attackable</li>
  79. * <li>L2BoxInstance</li>
  80. * <li>L2FolkInstance</li>
  81. *
  82. * @version $Revision: 1.32.2.7.2.24 $ $Date: 2005/04/11 10:06:09 $
  83. */
  84. public class L2Npc extends L2Character
  85. {
  86. //private static Logger _log = Logger.getLogger(L2NpcInstance.class.getName());
  87. /** The interaction distance of the L2NpcInstance(is used as offset in MovetoLocation method) */
  88. public static final int INTERACTION_DISTANCE = 150;
  89. /** The L2Spawn object that manage this L2NpcInstance */
  90. private L2Spawn _spawn;
  91. /** The flag to specify if this L2NpcInstance is busy */
  92. private boolean _isBusy = false;
  93. /** The busy message for this L2NpcInstance */
  94. private String _busyMessage = "";
  95. /** True if endDecayTask has already been called */
  96. volatile boolean _isDecayed = false;
  97. /** The castle index in the array of L2Castle this L2NpcInstance belongs to */
  98. private int _castleIndex = -2;
  99. /** The fortress index in the array of L2Fort this L2NpcInstance belongs to */
  100. private int _fortIndex = -2;
  101. public boolean isEventMob = false;
  102. private boolean _isInTown = false;
  103. /** True if this L2Npc is autoattackable **/
  104. private boolean _isAutoAttackable = false;
  105. /** Time of last social packet broadcast*/
  106. private long _lastSocialBroadcast = 0;
  107. /** Minimum interval between social packets*/
  108. private int _minimalSocialInterval = 6000;
  109. protected RandomAnimationTask _rAniTask = null;
  110. private int _currentLHandId; // normally this shouldn't change from the template, but there exist exceptions
  111. private int _currentRHandId; // normally this shouldn't change from the template, but there exist exceptions
  112. private int _currentEnchant; // normally this shouldn't change from the template, but there exist exceptions
  113. private double _currentCollisionHeight; // used for npc grow effect skills
  114. private double _currentCollisionRadius; // used for npc grow effect skills
  115. public boolean _soulshotcharged = false;
  116. public boolean _spiritshotcharged = false;
  117. private int _soulshotamount = 0;
  118. private int _spiritshotamount = 0;
  119. public boolean _ssrecharged = true;
  120. public boolean _spsrecharged = true;
  121. //AI Recall
  122. public int getSoulShot()
  123. {
  124. L2NpcAIData AI = getTemplate().getAIDataStatic();
  125. return AI.getSoulShot();
  126. }
  127. public int getSpiritShot()
  128. {
  129. L2NpcAIData AI = getTemplate().getAIDataStatic();
  130. return AI.getSpiritShot();
  131. }
  132. public int getSoulShotChance()
  133. {
  134. L2NpcAIData AI = getTemplate().getAIDataStatic();
  135. return AI.getSoulShotChance();
  136. }
  137. public int getSpiritShotChance()
  138. {
  139. L2NpcAIData AI = getTemplate().getAIDataStatic();
  140. return AI.getSpiritShotChance();
  141. }
  142. public boolean useSoulShot()
  143. {
  144. if(_soulshotcharged)
  145. return true;
  146. if(_ssrecharged)
  147. {
  148. _soulshotamount = getSoulShot();
  149. _ssrecharged = false;
  150. }
  151. else if (_soulshotamount>0)
  152. {
  153. if (Rnd.get(100) <= getSoulShotChance())
  154. {
  155. _soulshotamount = _soulshotamount - 1;
  156. Broadcast.toSelfAndKnownPlayersInRadius(this, new MagicSkillUse(this, this, 2154, 1, 0, 0), 360000);
  157. _soulshotcharged = true;
  158. }
  159. }
  160. else return false;
  161. return _soulshotcharged;
  162. }
  163. public boolean useSpiritShot()
  164. {
  165. if(_spiritshotcharged)
  166. return true;
  167. else
  168. {
  169. //_spiritshotcharged = false;
  170. if(_spsrecharged)
  171. {
  172. _spiritshotamount = getSpiritShot();
  173. _spsrecharged = false;
  174. }
  175. else if (_spiritshotamount>0)
  176. {
  177. if (Rnd.get(100) <= getSpiritShotChance())
  178. {
  179. _spiritshotamount = _spiritshotamount - 1;
  180. Broadcast.toSelfAndKnownPlayersInRadius(this, new MagicSkillUse(this, this, 2061, 1, 0, 0), 360000);
  181. _spiritshotcharged = true;
  182. }
  183. }
  184. else return false;
  185. }
  186. return _spiritshotcharged;
  187. }
  188. public int getEnemyRange()
  189. {
  190. L2NpcAIData AI = getTemplate().getAIDataStatic();
  191. return AI.getEnemyRange();
  192. }
  193. public String getEnemyClan()
  194. {
  195. L2NpcAIData AI = getTemplate().getAIDataStatic();
  196. return AI.getEnemyClan();
  197. }
  198. public int getClanRange()
  199. {
  200. L2NpcAIData AI = getTemplate().getAIDataStatic();
  201. return AI.getClanRange();
  202. }
  203. public String getClan()
  204. {
  205. L2NpcAIData AI = getTemplate().getAIDataStatic();
  206. return AI.getClan();
  207. }
  208. // GET THE PRIMARY ATTACK
  209. public int getPrimaryAttack()
  210. {
  211. L2NpcAIData AI = getTemplate().getAIDataStatic();
  212. return AI.getPrimaryAttack();
  213. }
  214. public int getSkillChance()
  215. {
  216. L2NpcAIData AI = getTemplate().getAIDataStatic();
  217. return AI.getSkillChance();
  218. }
  219. public int getCanMove()
  220. {
  221. L2NpcAIData AI = getTemplate().getAIDataStatic();
  222. return AI.getCanMove();
  223. }
  224. public int getIsChaos()
  225. {
  226. L2NpcAIData AI = getTemplate().getAIDataStatic();
  227. return AI.getIsChaos();
  228. }
  229. public int getCanDodge()
  230. {
  231. L2NpcAIData AI = getTemplate().getAIDataStatic();
  232. return AI.getDodge();
  233. }
  234. public int getSSkillChance()
  235. {
  236. L2NpcAIData AI = getTemplate().getAIDataStatic();
  237. return AI.getShortRangeChance();
  238. }
  239. public int getLSkillChance()
  240. {
  241. L2NpcAIData AI = getTemplate().getAIDataStatic();
  242. return AI.getLongRangeChance();
  243. }
  244. public int getSwitchRangeChance()
  245. {
  246. L2NpcAIData AI = getTemplate().getAIDataStatic();
  247. return AI.getSwitchRangeChance();
  248. }
  249. public boolean hasLSkill()
  250. {
  251. L2NpcAIData AI = getTemplate().getAIDataStatic();
  252. if (AI.getLongRangeSkill() == 0)
  253. return false;
  254. else
  255. return true;
  256. }
  257. public boolean hasSSkill()
  258. {
  259. L2NpcAIData AI = getTemplate().getAIDataStatic();
  260. if (AI.getShortRangeSkill() == 0)
  261. return false;
  262. else
  263. return true;
  264. }
  265. public FastList<L2Skill> getLrangeSkill()
  266. {
  267. FastList<L2Skill> skilldata = new FastList <L2Skill>();
  268. boolean hasLrange = false;
  269. L2NpcAIData AI = getTemplate().getAIDataStatic();
  270. if (AI == null || AI.getLongRangeSkill() == 0)
  271. return null;
  272. switch (AI.getLongRangeSkill())
  273. {
  274. case -1:
  275. {
  276. L2Skill[] skills = null;
  277. skills = getAllSkills();
  278. if (skills != null)
  279. {
  280. for (L2Skill sk: skills)
  281. {
  282. if (sk == null || sk.isPassive()
  283. || sk.getTargetType() == L2Skill.SkillTargetType.TARGET_SELF)
  284. continue;
  285. if (sk.getCastRange() >= 200)
  286. {
  287. skilldata.add(sk);
  288. hasLrange = true;
  289. }
  290. }
  291. }
  292. break;
  293. }
  294. case 1:
  295. {
  296. if (getTemplate()._universalskills != null)
  297. {
  298. for (L2Skill sk: getTemplate()._universalskills)
  299. {
  300. if (sk.getCastRange() >= 200)
  301. {
  302. skilldata.add(sk);
  303. hasLrange = true;
  304. }
  305. }
  306. }
  307. break;
  308. }
  309. default:
  310. {
  311. for (L2Skill sk: getAllSkills())
  312. {
  313. if (sk.getId() == AI.getLongRangeSkill())
  314. {
  315. skilldata.add(sk);
  316. hasLrange = true;
  317. }
  318. }
  319. }
  320. }
  321. return (hasLrange ? skilldata : null);
  322. }
  323. public FastList<L2Skill> getSrangeSkill()
  324. {
  325. FastList<L2Skill> skilldata = new FastList <L2Skill>();
  326. boolean hasSrange = false;
  327. L2NpcAIData AI = getTemplate().getAIDataStatic();
  328. if (AI == null || AI.getShortRangeSkill() == 0)
  329. return null;
  330. switch (AI.getShortRangeSkill())
  331. {
  332. case -1:
  333. {
  334. L2Skill[] skills = null;
  335. skills = getAllSkills();
  336. if (skills != null)
  337. {
  338. for (L2Skill sk: skills)
  339. {
  340. if (sk == null || sk.isPassive()
  341. || sk.getTargetType() == L2Skill.SkillTargetType.TARGET_SELF)
  342. continue;
  343. if (sk.getCastRange() <= 200)
  344. {
  345. skilldata.add(sk);
  346. hasSrange = true;
  347. }
  348. }
  349. }
  350. break;
  351. }
  352. case 1:
  353. {
  354. if (getTemplate()._universalskills != null)
  355. {
  356. for (L2Skill sk: getTemplate()._universalskills)
  357. {
  358. if (sk.getCastRange() <= 200)
  359. {
  360. skilldata.add(sk);
  361. hasSrange = true;
  362. }
  363. }
  364. }
  365. break;
  366. }
  367. default:
  368. {
  369. for (L2Skill sk: getAllSkills())
  370. {
  371. if (sk.getId() == AI.getShortRangeSkill())
  372. {
  373. skilldata.add(sk);
  374. hasSrange = true;
  375. }
  376. }
  377. }
  378. }
  379. return (hasSrange ? skilldata : null);
  380. }
  381. /** Task launching the function onRandomAnimation() */
  382. protected class RandomAnimationTask implements Runnable
  383. {
  384. public void run()
  385. {
  386. try
  387. {
  388. if (this != _rAniTask)
  389. return; // Shouldn't happen, but who knows... just to make sure every active npc has only one timer.
  390. if (isMob())
  391. {
  392. // Cancel further animation timers until intention is changed to ACTIVE again.
  393. if (getAI().getIntention() != AI_INTENTION_ACTIVE)
  394. return;
  395. }
  396. else
  397. {
  398. if (!isInActiveRegion()) // NPCs in inactive region don't run this task
  399. return;
  400. }
  401. if (!(isDead() || isStunned() || isSleeping() || isParalyzed()))
  402. onRandomAnimation(Rnd.get(2, 3));
  403. startRandomAnimationTimer();
  404. }
  405. catch (Exception e)
  406. {
  407. _log.log(Level.SEVERE, "", e);
  408. }
  409. }
  410. }
  411. /**
  412. * Send a packet SocialAction to all L2PcInstance in the _KnownPlayers of the L2NpcInstance and create a new RandomAnimation Task.<BR><BR>
  413. */
  414. public void onRandomAnimation(int animationId)
  415. {
  416. // Send a packet SocialAction to all L2PcInstance in the _KnownPlayers of the L2NpcInstance
  417. long now = System.currentTimeMillis();
  418. if (now - _lastSocialBroadcast > _minimalSocialInterval)
  419. {
  420. _lastSocialBroadcast = now;
  421. broadcastPacket(new SocialAction(getObjectId(), animationId));
  422. }
  423. }
  424. /**
  425. * Create a RandomAnimation Task that will be launched after the calculated delay.<BR><BR>
  426. */
  427. public void startRandomAnimationTimer()
  428. {
  429. if (!hasRandomAnimation())
  430. return;
  431. int minWait = isMob() ? Config.MIN_MONSTER_ANIMATION : Config.MIN_NPC_ANIMATION;
  432. int maxWait = isMob() ? Config.MAX_MONSTER_ANIMATION : Config.MAX_NPC_ANIMATION;
  433. // Calculate the delay before the next animation
  434. int interval = Rnd.get(minWait, maxWait) * 1000;
  435. // Create a RandomAnimation Task that will be launched after the calculated delay
  436. _rAniTask = new RandomAnimationTask();
  437. ThreadPoolManager.getInstance().scheduleGeneral(_rAniTask, interval);
  438. }
  439. /**
  440. * Check if the server allows Random Animation.<BR><BR>
  441. */
  442. public boolean hasRandomAnimation()
  443. {
  444. return (Config.MAX_NPC_ANIMATION > 0 && !getAiType().equals(AIType.CORPSE));
  445. }
  446. /**
  447. * Constructor of L2NpcInstance (use L2Character constructor).<BR><BR>
  448. *
  449. * <B><U> Actions</U> :</B><BR><BR>
  450. * <li>Call the L2Character constructor to set the _template of the L2Character (copy skills from template to object and link _calculators to NPC_STD_CALCULATOR) </li>
  451. * <li>Set the name of the L2Character</li>
  452. * <li>Create a RandomAnimation Task that will be launched after the calculated delay if the server allow it </li><BR><BR>
  453. *
  454. * @param objectId Identifier of the object to initialized
  455. * @param template The L2NpcTemplate to apply to the NPC
  456. *
  457. */
  458. public L2Npc(int objectId, L2NpcTemplate template)
  459. {
  460. // Call the L2Character constructor to set the _template of the L2Character, copy skills from template to object
  461. // and link _calculators to NPC_STD_CALCULATOR
  462. super(objectId, template);
  463. setInstanceType(InstanceType.L2Npc);
  464. initCharStatusUpdateValues();
  465. // initialize the "current" equipment
  466. _currentLHandId = getTemplate().lhand;
  467. _currentRHandId = getTemplate().rhand;
  468. _currentEnchant = Config.ENABLE_RANDOM_ENCHANT_EFFECT ? Rnd.get(4,21) : getTemplate().enchantEffect;
  469. // initialize the "current" collisions
  470. _currentCollisionHeight = getTemplate().fCollisionHeight;
  471. _currentCollisionRadius = getTemplate().fCollisionRadius;
  472. if (template == null)
  473. {
  474. _log.severe("No template for Npc. Please check your datapack is setup correctly.");
  475. return;
  476. }
  477. // Set the name of the L2Character
  478. setName(template.name);
  479. }
  480. @Override
  481. public NpcKnownList getKnownList()
  482. {
  483. return (NpcKnownList) super.getKnownList();
  484. }
  485. @Override
  486. public void initKnownList()
  487. {
  488. setKnownList(new NpcKnownList(this));
  489. }
  490. @Override
  491. public NpcStat getStat()
  492. {
  493. return (NpcStat) super.getStat();
  494. }
  495. @Override
  496. public void initCharStat()
  497. {
  498. setStat(new NpcStat(this));
  499. }
  500. @Override
  501. public NpcStatus getStatus()
  502. {
  503. return (NpcStatus) super.getStatus();
  504. }
  505. @Override
  506. public void initCharStatus()
  507. {
  508. setStatus(new NpcStatus(this));
  509. }
  510. /** Return the L2NpcTemplate of the L2NpcInstance. */
  511. @Override
  512. public final L2NpcTemplate getTemplate()
  513. {
  514. return (L2NpcTemplate) super.getTemplate();
  515. }
  516. /**
  517. * Return the generic Identifier of this L2NpcInstance contained in the L2NpcTemplate.<BR><BR>
  518. */
  519. public int getNpcId()
  520. {
  521. return getTemplate().npcId;
  522. }
  523. @Override
  524. public boolean isAttackable()
  525. {
  526. return Config.ALT_ATTACKABLE_NPCS;
  527. }
  528. /**
  529. * Return the faction Identifier of this L2NpcInstance contained in the L2NpcTemplate.<BR><BR>
  530. *
  531. * <B><U> Concept</U> :</B><BR><BR>
  532. * If a NPC belows to a Faction, other NPC of the faction inside the Faction range will help it if it's attacked<BR><BR>
  533. *
  534. */
  535. //@Deprecated
  536. public final String getFactionId()
  537. {
  538. return getClan();
  539. }
  540. /**
  541. * Return the Level of this L2NpcInstance contained in the L2NpcTemplate.<BR><BR>
  542. */
  543. @Override
  544. public final int getLevel()
  545. {
  546. return getTemplate().level;
  547. }
  548. /**
  549. * Return True if the L2NpcInstance is agressive (ex : L2MonsterInstance in function of aggroRange).<BR><BR>
  550. */
  551. public boolean isAggressive()
  552. {
  553. return false;
  554. }
  555. /**
  556. * Return the Aggro Range of this L2NpcInstance contained in the L2NpcTemplate.<BR><BR>
  557. */
  558. public int getAggroRange()
  559. {
  560. return getTemplate().aggroRange;
  561. }
  562. /**
  563. * Return the Faction Range of this L2NpcInstance contained in the L2NpcTemplate.<BR><BR>
  564. */
  565. //@Deprecated
  566. public int getFactionRange()
  567. {
  568. return getClanRange();
  569. }
  570. /**
  571. * Return True if this L2NpcInstance is undead in function of the L2NpcTemplate.<BR><BR>
  572. */
  573. @Override
  574. public boolean isUndead()
  575. {
  576. return getTemplate().isUndead;
  577. }
  578. /**
  579. * Send a packet NpcInfo with state of abnormal effect to all L2PcInstance in the _KnownPlayers of the L2NpcInstance.<BR><BR>
  580. */
  581. @Override
  582. public void updateAbnormalEffect()
  583. {
  584. // Send a Server->Client packet NpcInfo with state of abnormal effect to all L2PcInstance in the _KnownPlayers of the L2NpcInstance
  585. Collection<L2PcInstance> plrs = getKnownList().getKnownPlayers().values();
  586. //synchronized (getKnownList().getKnownPlayers())
  587. {
  588. for (L2PcInstance player : plrs)
  589. {
  590. if (getRunSpeed() == 0)
  591. player.sendPacket(new ServerObjectInfo(this, player));
  592. else
  593. player.sendPacket(new AbstractNpcInfo.NpcInfo(this, player));
  594. }
  595. }
  596. }
  597. /**
  598. * Return the distance under which the object must be add to _knownObject in
  599. * function of the object type.<BR>
  600. * <BR>
  601. *
  602. * <B><U> Values </U> :</B><BR>
  603. * <BR>
  604. * <li> object is a L2FolkInstance : 0 (don't remember it) </li>
  605. * <li> object is a L2Character : 0 (don't remember it) </li>
  606. * <li> object is a L2PlayableInstance : 1500 </li>
  607. * <li> others : 500 </li>
  608. * <BR>
  609. * <BR>
  610. *
  611. * <B><U> Override in </U> :</B><BR>
  612. * <BR>
  613. * <li> L2Attackable</li>
  614. * <BR>
  615. * <BR>
  616. *
  617. * @param object
  618. * The Object to add to _knownObject
  619. *
  620. */
  621. public int getDistanceToWatchObject(L2Object object)
  622. {
  623. if (object instanceof L2FestivalGuideInstance)
  624. return 10000;
  625. if (object instanceof L2NpcInstance || !(object instanceof L2Character))
  626. return 0;
  627. if (object instanceof L2Playable)
  628. return 1500;
  629. return 500;
  630. }
  631. /**
  632. * Return the distance after which the object must be remove from _knownObject in function of the object type.<BR><BR>
  633. *
  634. * <B><U> Values </U> :</B><BR><BR>
  635. * <li> object is not a L2Character : 0 (don't remember it) </li>
  636. * <li> object is a L2FolkInstance : 0 (don't remember it)</li>
  637. * <li> object is a L2PlayableInstance : 3000 </li>
  638. * <li> others : 1000 </li><BR><BR>
  639. *
  640. * <B><U> Overridden in </U> :</B><BR><BR>
  641. * <li> L2Attackable</li><BR><BR>
  642. *
  643. * @param object The Object to remove from _knownObject
  644. *
  645. */
  646. public int getDistanceToForgetObject(L2Object object)
  647. {
  648. return 2 * getDistanceToWatchObject(object);
  649. }
  650. /**
  651. * Return False.<BR><BR>
  652. *
  653. * <B><U> Overridden in </U> :</B><BR><BR>
  654. * <li> L2MonsterInstance : Check if the attacker is not another L2MonsterInstance</li>
  655. * <li> L2PcInstance</li><BR><BR>
  656. */
  657. @Override
  658. public boolean isAutoAttackable(L2Character attacker)
  659. {
  660. return _isAutoAttackable;
  661. }
  662. public void setAutoAttackable(boolean flag)
  663. {
  664. _isAutoAttackable = flag;
  665. }
  666. /**
  667. * Return the Identifier of the item in the left hand of this L2NpcInstance contained in the L2NpcTemplate.<BR><BR>
  668. */
  669. public int getLeftHandItem()
  670. {
  671. return _currentLHandId;
  672. }
  673. /**
  674. * Return the Identifier of the item in the right hand of this L2NpcInstance contained in the L2NpcTemplate.<BR><BR>
  675. */
  676. public int getRightHandItem()
  677. {
  678. return _currentRHandId;
  679. }
  680. public int getEnchantEffect()
  681. {
  682. return _currentEnchant;
  683. }
  684. /**
  685. * Return the busy status of this L2NpcInstance.<BR><BR>
  686. */
  687. public final boolean isBusy()
  688. {
  689. return _isBusy;
  690. }
  691. /**
  692. * Set the busy status of this L2NpcInstance.<BR><BR>
  693. */
  694. public void setBusy(boolean isBusy)
  695. {
  696. _isBusy = isBusy;
  697. }
  698. /**
  699. * Return the busy message of this L2NpcInstance.<BR><BR>
  700. */
  701. public final String getBusyMessage()
  702. {
  703. return _busyMessage;
  704. }
  705. /**
  706. * Set the busy message of this L2NpcInstance.<BR><BR>
  707. */
  708. public void setBusyMessage(String message)
  709. {
  710. _busyMessage = message;
  711. }
  712. /**
  713. * Return true if this L2Npc instance can be warehouse manager.<BR><BR>
  714. */
  715. public boolean isWarehouse()
  716. {
  717. return false;
  718. }
  719. public boolean canTarget(L2PcInstance player)
  720. {
  721. if (player.isOutOfControl())
  722. {
  723. player.sendPacket(ActionFailed.STATIC_PACKET);
  724. return false;
  725. }
  726. if (player.isLockedTarget() && player.getLockedTarget() != this)
  727. {
  728. player.sendPacket(new SystemMessage(SystemMessageId.FAILED_CHANGE_TARGET));
  729. player.sendPacket(ActionFailed.STATIC_PACKET);
  730. return false;
  731. }
  732. // TODO: More checks...
  733. return true;
  734. }
  735. public boolean canInteract(L2PcInstance player)
  736. {
  737. // TODO: NPC busy check etc...
  738. if (player.isCastingNow() || player.isCastingSimultaneouslyNow())
  739. return false;
  740. if (player.isDead() || player.isFakeDeath())
  741. return false;
  742. if (player.isSitting())
  743. return false;
  744. if (player.getPrivateStoreType() != 0)
  745. return false;
  746. if (!isInsideRadius(player, INTERACTION_DISTANCE, true, false))
  747. return false;
  748. if (player.getInstanceId() != getInstanceId()
  749. && player.getInstanceId() != -1)
  750. return false;
  751. return true;
  752. }
  753. /** Return the L2Castle this L2NpcInstance belongs to. */
  754. public final Castle getCastle()
  755. {
  756. // Get castle this NPC belongs to (excluding L2Attackable)
  757. if (_castleIndex < 0)
  758. {
  759. L2TownZone town = TownManager.getTown(getX(), getY(), getZ());
  760. if (town != null)
  761. _castleIndex = CastleManager.getInstance().getCastleIndex(town.getTaxById());
  762. if (_castleIndex < 0)
  763. {
  764. _castleIndex = CastleManager.getInstance().findNearestCastleIndex(this);
  765. }
  766. else
  767. _isInTown = true; // Npc was spawned in town
  768. }
  769. if (_castleIndex < 0)
  770. return null;
  771. return CastleManager.getInstance().getCastles().get(_castleIndex);
  772. }
  773. /** Return the L2Fort this L2NpcInstance belongs to. */
  774. public final Fort getFort()
  775. {
  776. // Get Fort this NPC belongs to (excluding L2Attackable)
  777. if (_fortIndex < 0)
  778. {
  779. Fort fort = FortManager.getInstance().getFort(getX(), getY(), getZ());
  780. if (fort != null)
  781. _fortIndex = FortManager.getInstance().getFortIndex(fort.getFortId());
  782. if (_fortIndex < 0)
  783. _fortIndex = FortManager.getInstance().findNearestFortIndex(this);
  784. }
  785. if (_fortIndex < 0)
  786. return null;
  787. return FortManager.getInstance().getForts().get(_fortIndex);
  788. }
  789. public final boolean getIsInTown()
  790. {
  791. if (_castleIndex < 0)
  792. getCastle();
  793. return _isInTown;
  794. }
  795. /**
  796. * Open a quest or chat window on client with the text of the L2NpcInstance in function of the command.<BR><BR>
  797. *
  798. * <B><U> Example of use </U> :</B><BR><BR>
  799. * <li> Client packet : RequestBypassToServer</li><BR><BR>
  800. *
  801. * @param command The command string received from client
  802. *
  803. */
  804. public void onBypassFeedback(L2PcInstance player, String command)
  805. {
  806. //if (canInteract(player))
  807. {
  808. if (isBusy() && getBusyMessage().length() > 0)
  809. {
  810. player.sendPacket(ActionFailed.STATIC_PACKET);
  811. NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
  812. html.setFile(player.getHtmlPrefix(), "data/html/npcbusy.htm");
  813. html.replace("%busymessage%", getBusyMessage());
  814. html.replace("%npcname%", getName());
  815. html.replace("%playername%", player.getName());
  816. player.sendPacket(html);
  817. }
  818. else
  819. {
  820. IBypassHandler handler = BypassHandler.getInstance().getBypassHandler(command);
  821. if (handler != null)
  822. handler.useBypass(command, player, this);
  823. else
  824. _log.info(getClass().getSimpleName()+": Unknown NPC bypass: \""+command+"\" NpcId: "+getNpcId());
  825. }
  826. }
  827. }
  828. /**
  829. * Return null (regular NPCs don't have weapons instancies).<BR><BR>
  830. */
  831. @Override
  832. public L2ItemInstance getActiveWeaponInstance()
  833. {
  834. // regular NPCs dont have weapons instancies
  835. return null;
  836. }
  837. /**
  838. * Return the weapon item equiped in the right hand of the L2NpcInstance or null.<BR><BR>
  839. */
  840. @Override
  841. public L2Weapon getActiveWeaponItem()
  842. {
  843. // Get the weapon identifier equiped in the right hand of the L2NpcInstance
  844. int weaponId = getTemplate().rhand;
  845. if (weaponId < 1)
  846. return null;
  847. // Get the weapon item equiped in the right hand of the L2NpcInstance
  848. L2Item item = ItemTable.getInstance().getTemplate(getTemplate().rhand);
  849. if (!(item instanceof L2Weapon))
  850. return null;
  851. return (L2Weapon) item;
  852. }
  853. /**
  854. * Return null (regular NPCs don't have weapons instancies).<BR><BR>
  855. */
  856. @Override
  857. public L2ItemInstance getSecondaryWeaponInstance()
  858. {
  859. // regular NPCs dont have weapons instancies
  860. return null;
  861. }
  862. /**
  863. * Return the weapon item equiped in the left hand of the L2NpcInstance or null.<BR><BR>
  864. */
  865. @Override
  866. public L2Weapon getSecondaryWeaponItem()
  867. {
  868. // Get the weapon identifier equiped in the right hand of the L2NpcInstance
  869. int weaponId = getTemplate().lhand;
  870. if (weaponId < 1)
  871. return null;
  872. // Get the weapon item equiped in the right hand of the L2NpcInstance
  873. L2Item item = ItemTable.getInstance().getTemplate(getTemplate().lhand);
  874. if (!(item instanceof L2Weapon))
  875. return null;
  876. return (L2Weapon) item;
  877. }
  878. /**
  879. * Send a Server->Client packet NpcHtmlMessage to the L2PcInstance in order to display the message of the L2NpcInstance.<BR><BR>
  880. *
  881. * @param player The L2PcInstance who talks with the L2NpcInstance
  882. * @param content The text of the L2NpcMessage
  883. *
  884. */
  885. public void insertObjectIdAndShowChatWindow(L2PcInstance player, String content)
  886. {
  887. // Send a Server->Client packet NpcHtmlMessage to the L2PcInstance in order to display the message of the L2NpcInstance
  888. content = content.replaceAll("%objectId%", String.valueOf(getObjectId()));
  889. NpcHtmlMessage npcReply = new NpcHtmlMessage(getObjectId());
  890. npcReply.setHtml(content);
  891. player.sendPacket(npcReply);
  892. }
  893. /**
  894. * Return the pathfile of the selected HTML file in function of the npcId and of the page number.<BR><BR>
  895. *
  896. * <B><U> Format of the pathfile </U> :</B><BR><BR>
  897. * <li> if the file exists on the server (page number = 0) : <B>data/html/default/12006.htm</B> (npcId-page number)</li>
  898. * <li> if the file exists on the server (page number > 0) : <B>data/html/default/12006-1.htm</B> (npcId-page number)</li>
  899. * <li> if the file doesn't exist on the server : <B>data/html/npcdefault.htm</B> (message : "I have nothing to say to you")</li><BR><BR>
  900. *
  901. * <B><U> Overridden in </U> :</B><BR><BR>
  902. * <li> L2GuardInstance : Set the pathfile to data/html/guard/12006-1.htm (npcId-page number)</li><BR><BR>
  903. *
  904. * @param npcId The Identifier of the L2NpcInstance whose text must be display
  905. * @param val The number of the page to display
  906. *
  907. */
  908. public String getHtmlPath(int npcId, int val)
  909. {
  910. String pom = "";
  911. if (val == 0)
  912. pom = "" + npcId;
  913. else
  914. pom = npcId + "-" + val;
  915. String temp = "data/html/default/" + pom + ".htm";
  916. if (!Config.LAZY_CACHE)
  917. {
  918. // If not running lazy cache the file must be in the cache or it doesnt exist
  919. if (HtmCache.getInstance().contains(temp))
  920. return temp;
  921. }
  922. else
  923. {
  924. if (HtmCache.getInstance().isLoadable(temp))
  925. return temp;
  926. }
  927. // If the file is not found, the standard message "I have nothing to say to you" is returned
  928. return "data/html/npcdefault.htm";
  929. }
  930. public void showChatWindow(L2PcInstance player)
  931. {
  932. showChatWindow(player, 0);
  933. }
  934. /**
  935. * Returns true if html exists
  936. * @param player
  937. * @param type
  938. * @return boolean
  939. */
  940. private boolean showPkDenyChatWindow(L2PcInstance player, String type)
  941. {
  942. String html = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/" + type + "/" + getNpcId() + "-pk.htm");
  943. if (html != null)
  944. {
  945. NpcHtmlMessage pkDenyMsg = new NpcHtmlMessage(getObjectId());
  946. pkDenyMsg.setHtml(html);
  947. player.sendPacket(pkDenyMsg);
  948. player.sendPacket(ActionFailed.STATIC_PACKET);
  949. return true;
  950. }
  951. return false;
  952. }
  953. /**
  954. * Open a chat window on client with the text of the L2NpcInstance.<BR><BR>
  955. *
  956. * <B><U> Actions</U> :</B><BR><BR>
  957. * <li>Get the text of the selected HTML file in function of the npcId and of the page number </li>
  958. * <li>Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance </li>
  959. * <li>Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet </li><BR>
  960. *
  961. * @param player The L2PcInstance that talk with the L2NpcInstance
  962. * @param val The number of the page of the L2NpcInstance to display
  963. *
  964. */
  965. public void showChatWindow(L2PcInstance player, int val)
  966. {
  967. if (player.isCursedWeaponEquipped() && (!(player.getTarget() instanceof L2ClanHallManagerInstance) || !(player.getTarget() instanceof L2DoormenInstance)))
  968. {
  969. player.setTarget(player);
  970. return;
  971. }
  972. if (player.getKarma() > 0)
  973. {
  974. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && this instanceof L2MerchantInstance)
  975. {
  976. if (showPkDenyChatWindow(player, "merchant"))
  977. return;
  978. }
  979. else if (!Config.ALT_GAME_KARMA_PLAYER_CAN_USE_GK && this instanceof L2TeleporterInstance)
  980. {
  981. if (showPkDenyChatWindow(player, "teleporter"))
  982. return;
  983. }
  984. else if (!Config.ALT_GAME_KARMA_PLAYER_CAN_USE_WAREHOUSE && this instanceof L2WarehouseInstance)
  985. {
  986. if (showPkDenyChatWindow(player, "warehouse"))
  987. return;
  988. }
  989. else if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && this instanceof L2FishermanInstance)
  990. {
  991. if (showPkDenyChatWindow(player, "fisherman"))
  992. return;
  993. }
  994. }
  995. if ("L2Auctioneer".equals(getTemplate().type) && val == 0)
  996. return;
  997. int npcId = getTemplate().npcId;
  998. /* For use with Seven Signs implementation */
  999. String filename = SevenSigns.SEVEN_SIGNS_HTML_PATH;
  1000. int sealAvariceOwner = SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_AVARICE);
  1001. int sealGnosisOwner = SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_GNOSIS);
  1002. int playerCabal = SevenSigns.getInstance().getPlayerCabal(player.getObjectId());
  1003. int compWinner = SevenSigns.getInstance().getCabalHighestScore();
  1004. switch (npcId)
  1005. {
  1006. case 31127: //
  1007. case 31128: //
  1008. case 31129: // Dawn Festival Guides
  1009. case 31130: //
  1010. case 31131: //
  1011. filename += "festival/dawn_guide.htm";
  1012. break;
  1013. case 31137: //
  1014. case 31138: //
  1015. case 31139: // Dusk Festival Guides
  1016. case 31140: //
  1017. case 31141: //
  1018. filename += "festival/dusk_guide.htm";
  1019. break;
  1020. case 31092: // Black Marketeer of Mammon
  1021. filename += "blkmrkt_1.htm";
  1022. break;
  1023. case 31113: // Merchant of Mammon
  1024. if (Config.ALT_STRICT_SEVENSIGNS)
  1025. {
  1026. switch (compWinner)
  1027. {
  1028. case SevenSigns.CABAL_DAWN:
  1029. if (playerCabal != compWinner || playerCabal != sealAvariceOwner)
  1030. {
  1031. player.sendPacket(new SystemMessage(SystemMessageId.CAN_BE_USED_BY_DAWN));
  1032. player.sendPacket(ActionFailed.STATIC_PACKET);
  1033. return;
  1034. }
  1035. break;
  1036. case SevenSigns.CABAL_DUSK:
  1037. if (playerCabal != compWinner || playerCabal != sealAvariceOwner)
  1038. {
  1039. player.sendPacket(new SystemMessage(SystemMessageId.CAN_BE_USED_BY_DUSK));
  1040. player.sendPacket(ActionFailed.STATIC_PACKET);
  1041. return;
  1042. }
  1043. break;
  1044. default:
  1045. player.sendPacket(new SystemMessage(SystemMessageId.QUEST_EVENT_PERIOD));
  1046. return;
  1047. }
  1048. }
  1049. filename += "mammmerch_1.htm";
  1050. break;
  1051. case 31126: // Blacksmith of Mammon
  1052. if (Config.ALT_STRICT_SEVENSIGNS)
  1053. {
  1054. switch (compWinner)
  1055. {
  1056. case SevenSigns.CABAL_DAWN:
  1057. if (playerCabal != compWinner || playerCabal != sealGnosisOwner)
  1058. {
  1059. player.sendPacket(new SystemMessage(SystemMessageId.CAN_BE_USED_BY_DAWN));
  1060. player.sendPacket(ActionFailed.STATIC_PACKET);
  1061. return;
  1062. }
  1063. break;
  1064. case SevenSigns.CABAL_DUSK:
  1065. if (playerCabal != compWinner || playerCabal != sealGnosisOwner)
  1066. {
  1067. player.sendPacket(new SystemMessage(SystemMessageId.CAN_BE_USED_BY_DUSK));
  1068. player.sendPacket(ActionFailed.STATIC_PACKET);
  1069. return;
  1070. }
  1071. break;
  1072. default:
  1073. player.sendPacket(new SystemMessage(SystemMessageId.QUEST_EVENT_PERIOD));
  1074. return;
  1075. }
  1076. }
  1077. filename += "mammblack_1.htm";
  1078. break;
  1079. case 31132:
  1080. case 31133:
  1081. case 31134:
  1082. case 31135:
  1083. case 31136: // Festival Witches
  1084. case 31142:
  1085. case 31143:
  1086. case 31144:
  1087. case 31145:
  1088. case 31146:
  1089. filename += "festival/festival_witch.htm";
  1090. break;
  1091. case 31688:
  1092. if (player.isNoble())
  1093. filename = Olympiad.OLYMPIAD_HTML_PATH + "noble_main.htm";
  1094. else
  1095. filename = (getHtmlPath(npcId, val));
  1096. break;
  1097. case 31690:
  1098. case 31769:
  1099. case 31770:
  1100. case 31771:
  1101. case 31772:
  1102. if (player.isHero() || player.isNoble())
  1103. filename = Olympiad.OLYMPIAD_HTML_PATH + "hero_main.htm";
  1104. else
  1105. filename = (getHtmlPath(npcId, val));
  1106. break;
  1107. case 36402:
  1108. if (player.olyBuff > 0)
  1109. filename = (player.olyBuff == 5 ? Olympiad.OLYMPIAD_HTML_PATH + "olympiad_buffs.htm" : Olympiad.OLYMPIAD_HTML_PATH + "olympiad_5buffs.htm");
  1110. else
  1111. filename = Olympiad.OLYMPIAD_HTML_PATH + "olympiad_nobuffs.htm";
  1112. break;
  1113. default:
  1114. if (npcId >= 31865 && npcId <= 31918)
  1115. {
  1116. if (val == 0 )
  1117. filename += "rift/GuardianOfBorder.htm";
  1118. else
  1119. filename += "rift/GuardianOfBorder-" + val + ".htm";
  1120. break;
  1121. }
  1122. if ((npcId >= 31093 && npcId <= 31094) || (npcId >= 31172 && npcId <= 31201) || (npcId >= 31239 && npcId <= 31254))
  1123. return;
  1124. // Get the text of the selected HTML file in function of the npcId and of the page number
  1125. filename = (getHtmlPath(npcId, val));
  1126. break;
  1127. }
  1128. // Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance
  1129. NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
  1130. html.setFile(player.getHtmlPrefix(), filename);
  1131. if (this instanceof L2MerchantInstance)
  1132. {
  1133. if (Config.LIST_PET_RENT_NPC.contains(npcId))
  1134. html.replace("_Quest", "_RentPet\">Rent Pet</a><br><a action=\"bypass -h npc_%objectId%_Quest");
  1135. }
  1136. html.replace("%objectId%", String.valueOf(getObjectId()));
  1137. html.replace("%festivalMins%", SevenSignsFestival.getInstance().getTimeToNextFestivalStr());
  1138. player.sendPacket(html);
  1139. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  1140. player.sendPacket(ActionFailed.STATIC_PACKET);
  1141. }
  1142. /**
  1143. * Open a chat window on client with the text specified by the given file name and path,<BR>
  1144. * relative to the datapack root.
  1145. * <BR><BR>
  1146. * Added by Tempy
  1147. * @param player The L2PcInstance that talk with the L2NpcInstance
  1148. * @param filename The filename that contains the text to send
  1149. *
  1150. */
  1151. public void showChatWindow(L2PcInstance player, String filename)
  1152. {
  1153. // Send a Server->Client NpcHtmlMessage containing the text of the L2NpcInstance to the L2PcInstance
  1154. NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
  1155. html.setFile(player.getHtmlPrefix(), filename);
  1156. html.replace("%objectId%", String.valueOf(getObjectId()));
  1157. player.sendPacket(html);
  1158. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  1159. player.sendPacket(ActionFailed.STATIC_PACKET);
  1160. }
  1161. /**
  1162. * Return the Exp Reward of this L2NpcInstance contained in the L2NpcTemplate (modified by RATE_XP).<BR><BR>
  1163. */
  1164. public int getExpReward()
  1165. {
  1166. double rateXp = getStat().calcStat(Stats.MAX_HP, 1, this, null);
  1167. return (int) (getTemplate().rewardExp * rateXp * Config.RATE_XP);
  1168. }
  1169. /**
  1170. * Return the SP Reward of this L2NpcInstance contained in the L2NpcTemplate (modified by RATE_SP).<BR><BR>
  1171. */
  1172. public int getSpReward()
  1173. {
  1174. double rateSp = getStat().calcStat(Stats.MAX_HP, 1, this, null);
  1175. return (int) (getTemplate().rewardSp * rateSp * Config.RATE_SP);
  1176. }
  1177. /**
  1178. * Kill the L2NpcInstance (the corpse disappeared after 7 seconds).<BR><BR>
  1179. *
  1180. * <B><U> Actions</U> :</B><BR><BR>
  1181. * <li>Create a DecayTask to remove the corpse of the L2NpcInstance after 7 seconds </li>
  1182. * <li>Set target to null and cancel Attack or Cast </li>
  1183. * <li>Stop movement </li>
  1184. * <li>Stop HP/MP/CP Regeneration task </li>
  1185. * <li>Stop all active skills effects in progress on the L2Character </li>
  1186. * <li>Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform </li>
  1187. * <li>Notify L2Character AI </li><BR><BR>
  1188. *
  1189. * <B><U> Overridden in </U> :</B><BR><BR>
  1190. * <li> L2Attackable </li><BR><BR>
  1191. *
  1192. * @param killer The L2Character who killed it
  1193. *
  1194. */
  1195. @Override
  1196. public boolean doDie(L2Character killer)
  1197. {
  1198. if (!super.doDie(killer))
  1199. return false;
  1200. // normally this wouldn't really be needed, but for those few exceptions,
  1201. // we do need to reset the weapons back to the initial templated weapon.
  1202. _currentLHandId = getTemplate().lhand;
  1203. _currentRHandId = getTemplate().rhand;
  1204. _currentCollisionHeight = getTemplate().fCollisionHeight;
  1205. _currentCollisionRadius = getTemplate().fCollisionRadius;
  1206. DecayTaskManager.getInstance().addDecayTask(this);
  1207. return true;
  1208. }
  1209. /**
  1210. * Set the spawn of the L2NpcInstance.<BR><BR>
  1211. *
  1212. * @param spawn The L2Spawn that manage the L2NpcInstance
  1213. *
  1214. */
  1215. public void setSpawn(L2Spawn spawn)
  1216. {
  1217. _spawn = spawn;
  1218. }
  1219. @Override
  1220. public void onSpawn()
  1221. {
  1222. super.onSpawn();
  1223. if (getTemplate().getEventQuests(Quest.QuestEventType.ON_SPAWN) != null)
  1224. for (Quest quest : getTemplate().getEventQuests(Quest.QuestEventType.ON_SPAWN))
  1225. quest.notifySpawn(this);
  1226. }
  1227. /**
  1228. * Remove the L2NpcInstance from the world and update its spawn object (for a complete removal use the deleteMe method).<BR><BR>
  1229. *
  1230. * <B><U> Actions</U> :</B><BR><BR>
  1231. * <li>Remove the L2NpcInstance from the world when the decay task is launched </li>
  1232. * <li>Decrease its spawn counter </li>
  1233. * <li>Manage Siege task (killFlag, killCT) </li><BR><BR>
  1234. *
  1235. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World </B></FONT><BR>
  1236. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packets to players</B></FONT><BR><BR>
  1237. *
  1238. */
  1239. @Override
  1240. public void onDecay()
  1241. {
  1242. if (isDecayed())
  1243. return;
  1244. setDecayed(true);
  1245. // Remove the L2NpcInstance from the world when the decay task is launched
  1246. super.onDecay();
  1247. // Decrease its spawn counter
  1248. if (_spawn != null)
  1249. _spawn.decreaseCount(this);
  1250. }
  1251. /**
  1252. * Remove PROPERLY the L2NpcInstance from the world.<BR><BR>
  1253. *
  1254. * <B><U> Actions</U> :</B><BR><BR>
  1255. * <li>Remove the L2NpcInstance from the world and update its spawn object </li>
  1256. * <li>Remove all L2Object from _knownObjects and _knownPlayer of the L2NpcInstance then cancel Attack or Cast and notify AI </li>
  1257. * <li>Remove L2Object object from _allObjects of L2World </li><BR><BR>
  1258. *
  1259. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packets to players</B></FONT><BR><BR>
  1260. *
  1261. */
  1262. public void deleteMe()
  1263. {
  1264. L2WorldRegion oldRegion = getWorldRegion();
  1265. try
  1266. {
  1267. decayMe();
  1268. }
  1269. catch (Exception e)
  1270. {
  1271. _log.log(Level.SEVERE, "Failed decayMe().", e);
  1272. }
  1273. try
  1274. {
  1275. if (_fusionSkill != null)
  1276. abortCast();
  1277. for (L2Character character : getKnownList().getKnownCharacters())
  1278. if (character.getFusionSkill() != null && character.getFusionSkill().getTarget() == this)
  1279. character.abortCast();
  1280. }
  1281. catch (Exception e)
  1282. {
  1283. _log.log(Level.SEVERE, "deleteMe()", e);
  1284. }
  1285. if (oldRegion != null)
  1286. oldRegion.removeFromZones(this);
  1287. // Remove all L2Object from _knownObjects and _knownPlayer of the L2Character then cancel Attak or Cast and notify AI
  1288. try
  1289. {
  1290. getKnownList().removeAllKnownObjects();
  1291. }
  1292. catch (Exception e)
  1293. {
  1294. _log.log(Level.SEVERE, "Failed removing cleaning knownlist.", e);
  1295. }
  1296. // Remove L2Object object from _allObjects of L2World
  1297. L2World.getInstance().removeObject(this);
  1298. }
  1299. /**
  1300. * Return the L2Spawn object that manage this L2NpcInstance.<BR><BR>
  1301. */
  1302. public L2Spawn getSpawn()
  1303. {
  1304. return _spawn;
  1305. }
  1306. @Override
  1307. public String toString()
  1308. {
  1309. return getClass().getSimpleName()+":"+getTemplate().name+"("+getNpcId()+")"+"["+getObjectId()+"]";
  1310. }
  1311. public boolean isDecayed()
  1312. {
  1313. return _isDecayed;
  1314. }
  1315. public void setDecayed(boolean decayed)
  1316. {
  1317. _isDecayed = decayed;
  1318. }
  1319. public void endDecayTask()
  1320. {
  1321. if (!isDecayed())
  1322. {
  1323. DecayTaskManager.getInstance().cancelDecayTask(this);
  1324. onDecay();
  1325. }
  1326. }
  1327. public boolean isMob() // rather delete this check
  1328. {
  1329. return false; // This means we use MAX_NPC_ANIMATION instead of MAX_MONSTER_ANIMATION
  1330. }
  1331. // Two functions to change the appearance of the equipped weapons on the NPC
  1332. // This is only useful for a few NPCs and is most likely going to be called from AI
  1333. public void setLHandId(int newWeaponId)
  1334. {
  1335. _currentLHandId = newWeaponId;
  1336. updateAbnormalEffect();
  1337. }
  1338. public void setRHandId(int newWeaponId)
  1339. {
  1340. _currentRHandId = newWeaponId;
  1341. updateAbnormalEffect();
  1342. }
  1343. public void setLRHandId(int newLWeaponId, int newRWeaponId)
  1344. {
  1345. _currentRHandId = newRWeaponId;
  1346. _currentLHandId = newLWeaponId;
  1347. updateAbnormalEffect();
  1348. }
  1349. public void setEnchant(int newEnchantValue)
  1350. {
  1351. _currentEnchant = newEnchantValue;
  1352. updateAbnormalEffect();
  1353. }
  1354. public void setCollisionHeight(double height)
  1355. {
  1356. _currentCollisionHeight = height;
  1357. }
  1358. public void setCollisionRadius(double radius)
  1359. {
  1360. _currentCollisionRadius = radius;
  1361. }
  1362. public double getCollisionHeight()
  1363. {
  1364. return _currentCollisionHeight;
  1365. }
  1366. public double getCollisionRadius()
  1367. {
  1368. return _currentCollisionRadius;
  1369. }
  1370. @Override
  1371. public void sendInfo(L2PcInstance activeChar)
  1372. {
  1373. if (Config.CHECK_KNOWN)
  1374. activeChar.sendMessage("Added NPC: "+getName());
  1375. if (getRunSpeed() == 0)
  1376. activeChar.sendPacket(new ServerObjectInfo(this, activeChar));
  1377. else
  1378. activeChar.sendPacket(new AbstractNpcInfo.NpcInfo(this, activeChar));
  1379. }
  1380. public void showNoTeachHtml(L2PcInstance player)
  1381. {
  1382. int npcId = getNpcId();
  1383. String html = "";
  1384. if (this instanceof L2WarehouseInstance)
  1385. html = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/warehouse/" + npcId + "-noteach.htm");
  1386. else if (this instanceof L2TrainerInstance)
  1387. html = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/trainer/" + npcId + "-noteach.htm");
  1388. if (html == null)
  1389. {
  1390. _log.warning("Npc " + npcId + " missing noTeach html!");
  1391. NpcHtmlMessage msg = new NpcHtmlMessage(getObjectId());
  1392. final String sb = StringUtil.concat(
  1393. "<html><body>" +
  1394. "I cannot teach you any skills.<br>You must find your current class teachers.",
  1395. "</body></html>"
  1396. );
  1397. msg.setHtml(sb);
  1398. player.sendPacket(msg);
  1399. return;
  1400. }
  1401. else
  1402. {
  1403. NpcHtmlMessage noTeachMsg = new NpcHtmlMessage(getObjectId());
  1404. noTeachMsg.setHtml(html);
  1405. noTeachMsg.replace("%objectId%", String.valueOf(getObjectId()));
  1406. player.sendPacket(noTeachMsg);
  1407. }
  1408. }
  1409. public L2Npc scheduleDespawn(long delay)
  1410. {
  1411. ThreadPoolManager.getInstance().scheduleGeneral(this.new DespawnTask(this), delay);
  1412. return this;
  1413. }
  1414. public class DespawnTask implements Runnable
  1415. {
  1416. L2Npc _npc;
  1417. public DespawnTask(L2Npc npc)
  1418. {
  1419. _npc = npc;
  1420. }
  1421. @Override
  1422. public void run()
  1423. {
  1424. if (_npc != null)
  1425. _npc.deleteMe();
  1426. }
  1427. }
  1428. @Override
  1429. protected final void notifyQuestEventSkillFinished(L2Skill skill, L2Object target)
  1430. {
  1431. try
  1432. {
  1433. if (getTemplate().getEventQuests(Quest.QuestEventType.ON_SPELL_FINISHED) != null)
  1434. {
  1435. L2PcInstance player = target.getActingPlayer();
  1436. for (Quest quest : getTemplate().getEventQuests(Quest.QuestEventType.ON_SPELL_FINISHED))
  1437. {
  1438. quest.notifySpellFinished(this, player, skill);
  1439. }
  1440. }
  1441. }
  1442. catch (Exception e)
  1443. {
  1444. _log.log(Level.SEVERE, "", e);
  1445. }
  1446. }
  1447. /* (non-Javadoc)
  1448. * @see com.l2jserver.gameserver.model.actor.L2Character#isMovementDisabled()
  1449. */
  1450. @Override
  1451. public boolean isMovementDisabled()
  1452. {
  1453. return super.isMovementDisabled() || getCanMove() > 0 || getAiType().equals(AIType.CORPSE);
  1454. }
  1455. public AIType getAiType()
  1456. {
  1457. return getTemplate().getAIDataStatic().getAiType();
  1458. }
  1459. }