Quest.java 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  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.quest;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.Collection;
  20. import java.util.Map;
  21. import java.util.concurrent.locks.ReentrantReadWriteLock;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.cache.HtmCache;
  30. import com.l2jserver.gameserver.datatables.NpcTable;
  31. import com.l2jserver.gameserver.idfactory.IdFactory;
  32. import com.l2jserver.gameserver.instancemanager.QuestManager;
  33. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  34. import com.l2jserver.gameserver.model.L2Object;
  35. import com.l2jserver.gameserver.model.L2Party;
  36. import com.l2jserver.gameserver.model.L2Skill;
  37. import com.l2jserver.gameserver.model.L2Spawn;
  38. import com.l2jserver.gameserver.model.actor.L2Character;
  39. import com.l2jserver.gameserver.model.actor.L2Npc;
  40. import com.l2jserver.gameserver.model.actor.L2Trap;
  41. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  42. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  43. import com.l2jserver.gameserver.model.actor.instance.L2TrapInstance;
  44. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  45. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  46. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  47. import com.l2jserver.gameserver.network.serverpackets.NpcQuestHtmlMessage;
  48. import com.l2jserver.gameserver.scripting.ManagedScript;
  49. import com.l2jserver.gameserver.scripting.ScriptManager;
  50. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  51. import com.l2jserver.gameserver.util.MinionList;
  52. import com.l2jserver.util.Rnd;
  53. import com.l2jserver.util.Util;
  54. /**
  55. * @author Luis Arias
  56. *
  57. */
  58. public class Quest extends ManagedScript
  59. {
  60. protected static final Logger _log = Logger.getLogger(Quest.class.getName());
  61. /** HashMap containing events from String value of the event */
  62. private static Map<String, Quest> _allEventsS = new FastMap<String, Quest>();
  63. /** HashMap containing lists of timers from the name of the timer */
  64. private Map<String, FastList<QuestTimer>> _allEventTimers = new FastMap<String, FastList<QuestTimer>>();
  65. private final ReentrantReadWriteLock _rwLock = new ReentrantReadWriteLock();
  66. private final int _questId;
  67. private final String _name;
  68. private final String _descr;
  69. private final byte _initialState = State.CREATED;
  70. protected boolean _onEnterWorld = false;
  71. // NOTE: questItemIds will be overridden by child classes. Ideally, it should be
  72. // protected instead of public. However, quest scripts written in Jython will
  73. // have trouble with protected, as Jython only knows private and public...
  74. // In fact, protected will typically be considered private thus breaking the scripts.
  75. // Leave this as public as a workaround.
  76. public int[] questItemIds = null;
  77. private static final String DEFAULT_NO_QUEST_MSG =
  78. "<html><body>You are either not on a quest that involves this NPC, or you don't meet this NPC's minimum quest requirements.</body></html>";
  79. private static final String DEFAULT_ALREADY_COMPLETED_MSG =
  80. "<html><body>This quest has already been completed.</body></html>";
  81. /**
  82. * Return collection view of the values contains in the allEventS
  83. * @return Collection<Quest>
  84. */
  85. public static Collection<Quest> findAllEvents()
  86. {
  87. return _allEventsS.values();
  88. }
  89. /**
  90. * (Constructor)Add values to class variables and put the quest in HashMaps.
  91. * @param questId : int pointing out the ID of the quest
  92. * @param name : String corresponding to the name of the quest
  93. * @param descr : String for the description of the quest
  94. */
  95. public Quest(int questId, String name, String descr)
  96. {
  97. _questId = questId;
  98. _name = name;
  99. _descr = descr;
  100. if (questId != 0)
  101. {
  102. QuestManager.getInstance().addQuest(Quest.this);
  103. }
  104. else
  105. {
  106. _allEventsS.put(name, this);
  107. }
  108. init_LoadGlobalData();
  109. }
  110. /**
  111. * The function init_LoadGlobalData is, by default, called by the constructor of all quests.
  112. * Children of this class can implement this function in order to define what variables
  113. * to load and what structures to save them in. By default, nothing is loaded.
  114. */
  115. protected void init_LoadGlobalData()
  116. {
  117. }
  118. /**
  119. * The function saveGlobalData is, by default, called at shutdown, for all quests, by the QuestManager.
  120. * Children of this class can implement this function in order to convert their structures
  121. * into <var, value> tuples and make calls to save them to the database, if needed.
  122. * By default, nothing is saved.
  123. */
  124. public void saveGlobalData()
  125. {
  126. }
  127. public static enum TrapAction
  128. {
  129. TRAP_TRIGGERED,
  130. TRAP_DETECTED,
  131. TRAP_DISARMED
  132. }
  133. public static enum QuestEventType
  134. {
  135. ON_FIRST_TALK(false), // control the first dialog shown by NPCs when they are clicked (some quests must override the default npc action)
  136. QUEST_START(true), // onTalk action from start npcs
  137. ON_TALK(true), // onTalk action from npcs participating in a quest
  138. ON_ATTACK(true), // onAttack action triggered when a mob gets attacked by someone
  139. ON_KILL(true), // onKill action triggered when a mob gets killed.
  140. ON_SPAWN(true), // onSpawn action triggered when an NPC is spawned or respawned.
  141. ON_SKILL_SEE(true), // NPC or Mob saw a person casting a skill (regardless what the target is).
  142. ON_FACTION_CALL(true), // NPC or Mob saw a person casting a skill (regardless what the target is).
  143. ON_AGGRO_RANGE_ENTER(true), // a person came within the Npc/Mob's range
  144. ON_SPELL_FINISHED(true), // on spell finished action when npc finish casting skill
  145. ON_SKILL_LEARN(false), // control the AcquireSkill dialog from quest script
  146. ON_ENTER_ZONE(true), // on zone enter
  147. ON_EXIT_ZONE(true), // on zone exit
  148. ON_TRAP_ACTION(true); // on zone exit
  149. // control whether this event type is allowed for the same npc template in multiple quests
  150. // or if the npc must be registered in at most one quest for the specified event
  151. private boolean _allowMultipleRegistration;
  152. QuestEventType(boolean allowMultipleRegistration)
  153. {
  154. _allowMultipleRegistration = allowMultipleRegistration;
  155. }
  156. public boolean isMultipleRegistrationAllowed()
  157. {
  158. return _allowMultipleRegistration;
  159. }
  160. }
  161. /**
  162. * Return ID of the quest
  163. * @return int
  164. */
  165. public int getQuestIntId()
  166. {
  167. return _questId;
  168. }
  169. /**
  170. * Add a new QuestState to the database and return it.
  171. * @param player
  172. * @return QuestState : QuestState created
  173. */
  174. public QuestState newQuestState(L2PcInstance player)
  175. {
  176. QuestState qs = new QuestState(this, player, getInitialState());
  177. return qs;
  178. }
  179. /**
  180. * Return initial state of the quest
  181. * @return State
  182. */
  183. public byte getInitialState()
  184. {
  185. return _initialState;
  186. }
  187. /**
  188. * Return name of the quest
  189. * @return String
  190. */
  191. public String getName()
  192. {
  193. return _name;
  194. }
  195. /**
  196. * Return description of the quest
  197. * @return String
  198. */
  199. public String getDescr()
  200. {
  201. return _descr;
  202. }
  203. /**
  204. * Add a timer to the quest, if it doesn't exist already
  205. * @param name: name of the timer (also passed back as "event" in onAdvEvent)
  206. * @param time: time in ms for when to fire the timer
  207. * @param npc: npc associated with this timer (can be null)
  208. * @param player: player associated with this timer (can be null)
  209. */
  210. public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player)
  211. {
  212. startQuestTimer(name, time, npc, player, false);
  213. }
  214. /**
  215. * Add a timer to the quest, if it doesn't exist already. If the timer is repeatable,
  216. * it will auto-fire automatically, at a fixed rate, until explicitly canceled.
  217. * @param name: name of the timer (also passed back as "event" in onAdvEvent)
  218. * @param time: time in ms for when to fire the timer
  219. * @param npc: npc associated with this timer (can be null)
  220. * @param player: player associated with this timer (can be null)
  221. * @param repeatable: indicates if the timer is repeatable or one-time.
  222. */
  223. public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player, boolean repeating)
  224. {
  225. // Add quest timer if timer doesn't already exist
  226. FastList<QuestTimer> timers = getQuestTimers(name);
  227. // no timer exists with the same name, at all
  228. if (timers == null)
  229. {
  230. timers = new FastList<QuestTimer>();
  231. timers.add(new QuestTimer(this, name, time, npc, player, repeating));
  232. _allEventTimers.put(name, timers);
  233. }
  234. // a timer with this name exists, but may not be for the same set of npc and player
  235. else
  236. {
  237. // if there exists a timer with this name, allow the timer only if the [npc, player] set is unique
  238. // nulls act as wildcards
  239. if (getQuestTimer(name, npc, player) == null)
  240. {
  241. try
  242. {
  243. _rwLock.writeLock().lock();
  244. timers.add(new QuestTimer(this, name, time, npc, player, repeating));
  245. }
  246. finally
  247. {
  248. _rwLock.writeLock().unlock();
  249. }
  250. }
  251. }
  252. }
  253. public QuestTimer getQuestTimer(String name, L2Npc npc, L2PcInstance player)
  254. {
  255. FastList<QuestTimer> qt = getQuestTimers(name);
  256. if (qt == null || qt.isEmpty())
  257. return null;
  258. try
  259. {
  260. _rwLock.readLock().lock();
  261. for (QuestTimer timer : qt)
  262. {
  263. if (timer != null)
  264. {
  265. if (timer.isMatch(this, name, npc, player))
  266. return timer;
  267. }
  268. }
  269. }
  270. finally
  271. {
  272. _rwLock.readLock().unlock();
  273. }
  274. return null;
  275. }
  276. private FastList<QuestTimer> getQuestTimers(String name)
  277. {
  278. return _allEventTimers.get(name);
  279. }
  280. public void cancelQuestTimers(String name)
  281. {
  282. FastList<QuestTimer> timers = getQuestTimers(name);
  283. if (timers == null)
  284. return;
  285. try
  286. {
  287. _rwLock.writeLock().lock();
  288. for (QuestTimer timer : timers)
  289. {
  290. if (timer != null)
  291. {
  292. timer.cancel();
  293. }
  294. }
  295. }
  296. finally
  297. {
  298. _rwLock.writeLock().unlock();
  299. }
  300. }
  301. public void cancelQuestTimer(String name, L2Npc npc, L2PcInstance player)
  302. {
  303. QuestTimer timer = getQuestTimer(name, npc, player);
  304. if (timer != null)
  305. timer.cancel();
  306. }
  307. public void removeQuestTimer(QuestTimer timer)
  308. {
  309. if (timer == null)
  310. return;
  311. FastList<QuestTimer> timers = getQuestTimers(timer.getName());
  312. if (timers == null)
  313. return;
  314. try
  315. {
  316. _rwLock.writeLock().lock();
  317. timers.remove(timer);
  318. }
  319. finally
  320. {
  321. _rwLock.writeLock().unlock();
  322. }
  323. }
  324. // these are methods to call from java
  325. public final boolean notifyAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet, L2Skill skill)
  326. {
  327. String res = null;
  328. try
  329. {
  330. res = onAttack(npc, attacker, damage, isPet, skill);
  331. }
  332. catch (Exception e)
  333. {
  334. return showError(attacker, e);
  335. }
  336. return showResult(attacker, res);
  337. }
  338. public final boolean notifyDeath(L2Character killer, L2Character victim, QuestState qs)
  339. {
  340. String res = null;
  341. try
  342. {
  343. res = onDeath(killer, victim, qs);
  344. }
  345. catch (Exception e)
  346. {
  347. return showError(qs.getPlayer(), e);
  348. }
  349. return showResult(qs.getPlayer(), res);
  350. }
  351. public final boolean notifySpellFinished(L2Npc instance, L2PcInstance player, L2Skill skill)
  352. {
  353. String res = null;
  354. try
  355. {
  356. res = onSpellFinished(instance, player, skill);
  357. }
  358. catch (Exception e)
  359. {
  360. return showError(player, e);
  361. }
  362. return showResult(player, res);
  363. }
  364. /**
  365. * Notify quest script when something happens with a trap
  366. * @param trap: the trap instance which triggers the notification
  367. * @param trigger: the character which makes effect on the trap
  368. * @param action: 0: trap casting its skill. 1: trigger detects the trap. 2: trigger removes the trap
  369. */
  370. public final boolean notifyTrapAction(L2Trap trap, L2Character trigger, TrapAction action)
  371. {
  372. String res = null;
  373. try
  374. {
  375. res = onTrapAction(trap, trigger, action);
  376. }
  377. catch (Exception e)
  378. {
  379. if (trigger.getActingPlayer() != null)
  380. return showError(trigger.getActingPlayer(), e);
  381. _log.log(Level.WARNING, "Exception on onTrapAction() in notifyTrapAction(): " + e.getMessage(), e);
  382. return true;
  383. }
  384. if (trigger.getActingPlayer() != null)
  385. return showResult(trigger.getActingPlayer(), res);
  386. return false;
  387. }
  388. public final boolean notifySpawn(L2Npc npc)
  389. {
  390. try
  391. {
  392. onSpawn(npc);
  393. }
  394. catch (Exception e)
  395. {
  396. _log.log(Level.WARNING, "Exception on onSpawn() in notifySpawn(): " + e.getMessage(), e);
  397. return true;
  398. }
  399. return false;
  400. }
  401. public final boolean notifyEvent(String event, L2Npc npc, L2PcInstance player)
  402. {
  403. String res = null;
  404. try
  405. {
  406. res = onAdvEvent(event, npc, player);
  407. }
  408. catch (Exception e)
  409. {
  410. return showError(player, e);
  411. }
  412. return showResult(player, res);
  413. }
  414. public final boolean notifyEnterWorld(L2PcInstance player)
  415. {
  416. String res = null;
  417. try
  418. {
  419. res = onEnterWorld(player);
  420. }
  421. catch (Exception e)
  422. {
  423. return showError(player, e);
  424. }
  425. return showResult(player, res);
  426. }
  427. public final boolean notifyKill(L2Npc npc, L2PcInstance killer, boolean isPet)
  428. {
  429. String res = null;
  430. try
  431. {
  432. res = onKill(npc, killer, isPet);
  433. }
  434. catch (Exception e)
  435. {
  436. return showError(killer, e);
  437. }
  438. return showResult(killer, res);
  439. }
  440. public final boolean notifyTalk(L2Npc npc, QuestState qs)
  441. {
  442. String res = null;
  443. try
  444. {
  445. res = onTalk(npc, qs.getPlayer());
  446. }
  447. catch (Exception e)
  448. {
  449. return showError(qs.getPlayer(), e);
  450. }
  451. qs.getPlayer().setLastQuestNpcObject(npc.getObjectId());
  452. return showResult(qs.getPlayer(), res);
  453. }
  454. // override the default NPC dialogs when a quest defines this for the given NPC
  455. public final boolean notifyFirstTalk(L2Npc npc, L2PcInstance player)
  456. {
  457. String res = null;
  458. try
  459. {
  460. res = onFirstTalk(npc, player);
  461. }
  462. catch (Exception e)
  463. {
  464. return showError(player, e);
  465. }
  466. // if the quest returns text to display, display it.
  467. if (res != null && res.length() > 0)
  468. return showResult(player, res);
  469. // else tell the player that
  470. else
  471. player.sendPacket(ActionFailed.STATIC_PACKET);
  472. // note: if the default html for this npc needs to be shown, onFirstTalk should
  473. // call npc.showChatWindow(player) and then return null.
  474. return true;
  475. }
  476. public final boolean notifyAcquireSkillList(L2Npc npc, L2PcInstance player)
  477. {
  478. String res = null;
  479. try
  480. {
  481. res = onAcquireSkillList(npc, player);
  482. }
  483. catch (Exception e)
  484. {
  485. return showError(player, e);
  486. }
  487. return showResult(player, res);
  488. }
  489. public final boolean notifyAcquireSkillInfo(L2Npc npc, L2PcInstance player, L2Skill skill)
  490. {
  491. String res = null;
  492. try
  493. {
  494. res = onAcquireSkillInfo(npc, player, skill);
  495. }
  496. catch (Exception e)
  497. {
  498. return showError(player, e);
  499. }
  500. return showResult(player, res);
  501. }
  502. public final boolean notifyAcquireSkill(L2Npc npc, L2PcInstance player, L2Skill skill)
  503. {
  504. String res = null;
  505. try
  506. {
  507. res = onAcquireSkill(npc, player, skill);
  508. if (res == "true")
  509. return true;
  510. else if (res == "false")
  511. return false;
  512. }
  513. catch (Exception e)
  514. {
  515. return showError(player, e);
  516. }
  517. return showResult(player, res);
  518. }
  519. public class TmpOnSkillSee implements Runnable
  520. {
  521. private L2Npc _npc;
  522. private L2PcInstance _caster;
  523. private L2Skill _skill;
  524. private L2Object[] _targets;
  525. private boolean _isPet;
  526. public TmpOnSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  527. {
  528. _npc = npc;
  529. _caster = caster;
  530. _skill = skill;
  531. _targets = targets;
  532. _isPet = isPet;
  533. }
  534. public void run()
  535. {
  536. String res = null;
  537. try
  538. {
  539. res = onSkillSee(_npc, _caster, _skill, _targets, _isPet);
  540. }
  541. catch (Exception e)
  542. {
  543. showError(_caster, e);
  544. }
  545. showResult(_caster, res);
  546. }
  547. }
  548. public final boolean notifySkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  549. {
  550. ThreadPoolManager.getInstance().executeAi(new TmpOnSkillSee(npc, caster, skill, targets, isPet));
  551. return true;
  552. }
  553. public final boolean notifyFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
  554. {
  555. String res = null;
  556. try
  557. {
  558. res = onFactionCall(npc, caller, attacker, isPet);
  559. }
  560. catch (Exception e)
  561. {
  562. return showError(attacker, e);
  563. }
  564. return showResult(attacker, res);
  565. }
  566. public class TmpOnAggroEnter implements Runnable
  567. {
  568. private L2Npc _npc;
  569. private L2PcInstance _pc;
  570. private boolean _isPet;
  571. public TmpOnAggroEnter(L2Npc npc, L2PcInstance pc, boolean isPet)
  572. {
  573. _npc = npc;
  574. _pc = pc;
  575. _isPet = isPet;
  576. }
  577. public void run()
  578. {
  579. String res = null;
  580. try
  581. {
  582. res = onAggroRangeEnter(_npc, _pc, _isPet);
  583. }
  584. catch (Exception e)
  585. {
  586. showError(_pc, e);
  587. }
  588. showResult(_pc, res);
  589. }
  590. }
  591. public final boolean notifyAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
  592. {
  593. ThreadPoolManager.getInstance().executeAi(new TmpOnAggroEnter(npc, player, isPet));
  594. return true;
  595. }
  596. public final boolean notifyEnterZone(L2Character character, L2ZoneType zone)
  597. {
  598. L2PcInstance player = character.getActingPlayer();
  599. String res = null;
  600. try
  601. {
  602. res = this.onEnterZone(character, zone);
  603. }
  604. catch (Exception e)
  605. {
  606. if (player != null)
  607. return showError(player, e);
  608. }
  609. if (player != null)
  610. return showResult(player, res);
  611. return true;
  612. }
  613. public final boolean notifyExitZone(L2Character character, L2ZoneType zone)
  614. {
  615. L2PcInstance player = character.getActingPlayer();
  616. String res = null;
  617. try
  618. {
  619. res = this.onExitZone(character, zone);
  620. }
  621. catch (Exception e)
  622. {
  623. if (player != null)
  624. return showError(player, e);
  625. }
  626. if (player != null)
  627. return showResult(player, res);
  628. return true;
  629. }
  630. // these are methods that java calls to invoke scripts
  631. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet)
  632. {
  633. return null;
  634. }
  635. public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet, L2Skill skill)
  636. {
  637. return onAttack(npc, attacker, damage, isPet);
  638. }
  639. public String onDeath(L2Character killer, L2Character victim, QuestState qs)
  640. {
  641. if (killer instanceof L2Npc)
  642. return onAdvEvent("", (L2Npc) killer, qs.getPlayer());
  643. else
  644. return onAdvEvent("", null, qs.getPlayer());
  645. }
  646. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  647. {
  648. // if not overridden by a subclass, then default to the returned value of the simpler (and older) onEvent override
  649. // if the player has a state, use it as parameter in the next call, else return null
  650. QuestState qs = player.getQuestState(getName());
  651. if (qs != null)
  652. return onEvent(event, qs);
  653. return null;
  654. }
  655. public String onEvent(String event, QuestState qs)
  656. {
  657. return null;
  658. }
  659. public String onKill(L2Npc npc, L2PcInstance killer, boolean isPet)
  660. {
  661. return null;
  662. }
  663. public String onTalk(L2Npc npc, L2PcInstance talker)
  664. {
  665. return null;
  666. }
  667. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  668. {
  669. return null;
  670. }
  671. public String onAcquireSkillList(L2Npc npc, L2PcInstance player)
  672. {
  673. return null;
  674. }
  675. public String onAcquireSkillInfo(L2Npc npc, L2PcInstance player, L2Skill skill)
  676. {
  677. return null;
  678. }
  679. public String onAcquireSkill(L2Npc npc, L2PcInstance player, L2Skill skill)
  680. {
  681. return null;
  682. }
  683. public String onSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
  684. {
  685. return null;
  686. }
  687. public String onSpellFinished(L2Npc npc, L2PcInstance player, L2Skill skill)
  688. {
  689. return null;
  690. }
  691. public String onTrapAction(L2Trap trap, L2Character trigger, TrapAction action)
  692. {
  693. return null;
  694. }
  695. public String onSpawn(L2Npc npc)
  696. {
  697. return null;
  698. }
  699. public String onFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
  700. {
  701. return null;
  702. }
  703. public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
  704. {
  705. return null;
  706. }
  707. public String onEnterWorld(L2PcInstance player)
  708. {
  709. return null;
  710. }
  711. public String onEnterZone(L2Character character, L2ZoneType zone)
  712. {
  713. return null;
  714. }
  715. public String onExitZone(L2Character character, L2ZoneType zone)
  716. {
  717. return null;
  718. }
  719. /**
  720. * Show message error to player who has an access level greater than 0
  721. * @param player : L2PcInstance
  722. * @param t : Throwable
  723. * @return boolean
  724. */
  725. public boolean showError(L2PcInstance player, Throwable t)
  726. {
  727. _log.log(Level.WARNING, this.getScriptFile().getAbsolutePath(), t);
  728. if (t.getMessage() == null)
  729. t.printStackTrace();
  730. if (player != null && player.getAccessLevel().isGm())
  731. {
  732. String res = "<html><body><title>Script error</title>" + Util.getStackTrace(t) + "</body></html>";
  733. return showResult(player, res);
  734. }
  735. return false;
  736. }
  737. /**
  738. * Show a message to player.<BR><BR>
  739. * <U><I>Concept : </I></U><BR>
  740. * 3 cases are managed according to the value of the parameter "res" :<BR>
  741. * <LI><U>"res" ends with string ".html" :</U> an HTML is opened in order to be shown in a dialog box</LI>
  742. * <LI><U>"res" starts with "<html>" :</U> the message hold in "res" is shown in a dialog box</LI>
  743. * <LI><U>otherwise :</U> the message held in "res" is shown in chat box</LI>
  744. * @param qs : QuestState
  745. * @param res : String pointing out the message to show at the player
  746. * @return boolean
  747. */
  748. public boolean showResult(L2PcInstance player, String res)
  749. {
  750. if (res == null || res.isEmpty() || player == null)
  751. return true;
  752. if (res.endsWith(".htm") || res.endsWith(".html"))
  753. {
  754. showHtmlFile(player, res);
  755. }
  756. else if (res.startsWith("<html>"))
  757. {
  758. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  759. npcReply.setHtml(res);
  760. npcReply.replace("%playername%", player.getName());
  761. player.sendPacket(npcReply);
  762. player.sendPacket(ActionFailed.STATIC_PACKET);
  763. }
  764. else
  765. {
  766. player.sendMessage(res);
  767. }
  768. return false;
  769. }
  770. /**
  771. * Add quests to the L2PCInstance of the player.<BR><BR>
  772. * <U><I>Action : </U></I><BR>
  773. * Add state of quests, drops and variables for quests in the HashMap _quest of L2PcInstance
  774. * @param player : Player who is entering the world
  775. */
  776. public final static void playerEnter(L2PcInstance player)
  777. {
  778. Connection con = null;
  779. try
  780. {
  781. // Get list of quests owned by the player from database
  782. con = L2DatabaseFactory.getInstance().getConnection();
  783. PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId=? and name=?");
  784. PreparedStatement invalidQuestDataVar = con.prepareStatement("delete FROM character_quests WHERE charId=? and name=? and var=?");
  785. PreparedStatement statement = con.prepareStatement("SELECT name,value FROM character_quests WHERE charId=? AND var=?");
  786. statement.setInt(1, player.getObjectId());
  787. statement.setString(2, "<state>");
  788. ResultSet rs = statement.executeQuery();
  789. while (rs.next())
  790. {
  791. // Get ID of the quest and ID of its state
  792. String questId = rs.getString("name");
  793. String statename = rs.getString("value");
  794. // Search quest associated with the ID
  795. Quest q = QuestManager.getInstance().getQuest(questId);
  796. if (q == null)
  797. {
  798. _log.finer("Unknown quest " + questId + " for player " + player.getName());
  799. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  800. {
  801. invalidQuestData.setInt(1, player.getObjectId());
  802. invalidQuestData.setString(2, questId);
  803. invalidQuestData.executeUpdate();
  804. }
  805. continue;
  806. }
  807. // Create a new QuestState for the player that will be added to the player's list of quests
  808. new QuestState(q, player, State.getStateId(statename));
  809. }
  810. rs.close();
  811. invalidQuestData.close();
  812. statement.close();
  813. // Get list of quests owned by the player from the DB in order to add variables used in the quest.
  814. statement = con.prepareStatement("SELECT name,var,value FROM character_quests WHERE charId=? AND var<>?");
  815. statement.setInt(1, player.getObjectId());
  816. statement.setString(2, "<state>");
  817. rs = statement.executeQuery();
  818. while (rs.next())
  819. {
  820. String questId = rs.getString("name");
  821. String var = rs.getString("var");
  822. String value = rs.getString("value");
  823. // Get the QuestState saved in the loop before
  824. QuestState qs = player.getQuestState(questId);
  825. if (qs == null)
  826. {
  827. _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
  828. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  829. {
  830. invalidQuestDataVar.setInt(1, player.getObjectId());
  831. invalidQuestDataVar.setString(2, questId);
  832. invalidQuestDataVar.setString(3, var);
  833. invalidQuestDataVar.executeUpdate();
  834. }
  835. continue;
  836. }
  837. // Add parameter to the quest
  838. qs.setInternal(var, value);
  839. }
  840. rs.close();
  841. invalidQuestDataVar.close();
  842. statement.close();
  843. }
  844. catch (Exception e)
  845. {
  846. _log.log(Level.WARNING, "could not insert char quest:", e);
  847. }
  848. finally
  849. {
  850. L2DatabaseFactory.close(con);
  851. }
  852. // events
  853. for (String name : _allEventsS.keySet())
  854. {
  855. player.processQuestEvent(name, "enter");
  856. }
  857. }
  858. /**
  859. * Insert (or Update) in the database variables that need to stay persistant for this quest after a reboot.
  860. * This function is for storage of values that do not related to a specific player but are
  861. * global for all characters. For example, if we need to disable a quest-gatekeeper until
  862. * a certain time (as is done with some grand-boss gatekeepers), we can save that time in the DB.
  863. * @param var : String designating the name of the variable for the quest
  864. * @param value : String designating the value of the variable for the quest
  865. */
  866. public final void saveGlobalQuestVar(String var, String value)
  867. {
  868. Connection con = null;
  869. try
  870. {
  871. con = L2DatabaseFactory.getInstance().getConnection();
  872. PreparedStatement statement;
  873. statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)");
  874. statement.setString(1, getName());
  875. statement.setString(2, var);
  876. statement.setString(3, value);
  877. statement.executeUpdate();
  878. statement.close();
  879. }
  880. catch (Exception e)
  881. {
  882. _log.log(Level.WARNING, "could not insert global quest variable:", e);
  883. }
  884. finally
  885. {
  886. L2DatabaseFactory.close(con);
  887. }
  888. }
  889. /**
  890. * Read from the database a previously saved variable for this quest.
  891. * Due to performance considerations, this function should best be used only when the quest is first loaded.
  892. * Subclasses of this class can define structures into which these loaded values can be saved.
  893. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.
  894. * Values read from this function were entered by calls to "saveGlobalQuestVar"
  895. * @param var : String designating the name of the variable for the quest
  896. * @return String : String representing the loaded value for the passed var, or an empty string if the var was invalid
  897. */
  898. public final String loadGlobalQuestVar(String var)
  899. {
  900. String result = "";
  901. Connection con = null;
  902. try
  903. {
  904. con = L2DatabaseFactory.getInstance().getConnection();
  905. PreparedStatement statement;
  906. statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?");
  907. statement.setString(1, getName());
  908. statement.setString(2, var);
  909. ResultSet rs = statement.executeQuery();
  910. if (rs.first())
  911. result = rs.getString(1);
  912. rs.close();
  913. statement.close();
  914. }
  915. catch (Exception e)
  916. {
  917. _log.log(Level.WARNING, "could not load global quest variable:", e);
  918. }
  919. finally
  920. {
  921. L2DatabaseFactory.close(con);
  922. }
  923. return result;
  924. }
  925. /**
  926. * Permanently delete from the database a global quest variable that was previously saved for this quest.
  927. * @param var : String designating the name of the variable for the quest
  928. */
  929. public final void deleteGlobalQuestVar(String var)
  930. {
  931. Connection con = null;
  932. try
  933. {
  934. con = L2DatabaseFactory.getInstance().getConnection();
  935. PreparedStatement statement;
  936. statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?");
  937. statement.setString(1, getName());
  938. statement.setString(2, var);
  939. statement.executeUpdate();
  940. statement.close();
  941. }
  942. catch (Exception e)
  943. {
  944. _log.log(Level.WARNING, "could not delete global quest variable:", e);
  945. }
  946. finally
  947. {
  948. L2DatabaseFactory.close(con);
  949. }
  950. }
  951. /**
  952. * Permanently delete from the database all global quest variables that was previously saved for this quest.
  953. */
  954. public final void deleteAllGlobalQuestVars()
  955. {
  956. Connection con = null;
  957. try
  958. {
  959. con = L2DatabaseFactory.getInstance().getConnection();
  960. PreparedStatement statement;
  961. statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?");
  962. statement.setString(1, getName());
  963. statement.executeUpdate();
  964. statement.close();
  965. }
  966. catch (Exception e)
  967. {
  968. _log.log(Level.WARNING, "could not delete global quest variables:", e);
  969. }
  970. finally
  971. {
  972. L2DatabaseFactory.close(con);
  973. }
  974. }
  975. /**
  976. * Insert in the database the quest for the player.
  977. * @param qs : QuestState pointing out the state of the quest
  978. * @param var : String designating the name of the variable for the quest
  979. * @param value : String designating the value of the variable for the quest
  980. */
  981. public static void createQuestVarInDb(QuestState qs, String var, String value)
  982. {
  983. Connection con = null;
  984. try
  985. {
  986. con = L2DatabaseFactory.getInstance().getConnection();
  987. PreparedStatement statement;
  988. statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE value=?");
  989. statement.setInt(1, qs.getPlayer().getObjectId());
  990. statement.setString(2, qs.getQuestName());
  991. statement.setString(3, var);
  992. statement.setString(4, value);
  993. statement.setString(5, value);
  994. statement.executeUpdate();
  995. statement.close();
  996. }
  997. catch (Exception e)
  998. {
  999. _log.log(Level.WARNING, "could not insert char quest:", e);
  1000. }
  1001. finally
  1002. {
  1003. L2DatabaseFactory.close(con);
  1004. }
  1005. }
  1006. /**
  1007. * Update the value of the variable "var" for the quest.<BR><BR>
  1008. * <U><I>Actions :</I></U><BR>
  1009. * The selection of the right record is made with :
  1010. * <LI>charId = qs.getPlayer().getObjectID()</LI>
  1011. * <LI>name = qs.getQuest().getName()</LI>
  1012. * <LI>var = var</LI>
  1013. * <BR><BR>
  1014. * The modification made is :
  1015. * <LI>value = parameter value</LI>
  1016. * @param qs : Quest State
  1017. * @param var : String designating the name of the variable for quest
  1018. * @param value : String designating the value of the variable for quest
  1019. */
  1020. public static void updateQuestVarInDb(QuestState qs, String var, String value)
  1021. {
  1022. Connection con = null;
  1023. try
  1024. {
  1025. con = L2DatabaseFactory.getInstance().getConnection();
  1026. PreparedStatement statement;
  1027. statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?");
  1028. statement.setString(1, value);
  1029. statement.setInt(2, qs.getPlayer().getObjectId());
  1030. statement.setString(3, qs.getQuestName());
  1031. statement.setString(4, var);
  1032. statement.executeUpdate();
  1033. statement.close();
  1034. }
  1035. catch (Exception e)
  1036. {
  1037. _log.log(Level.WARNING, "could not update char quest:", e);
  1038. }
  1039. finally
  1040. {
  1041. L2DatabaseFactory.close(con);
  1042. }
  1043. }
  1044. /**
  1045. * Delete a variable of player's quest from the database.
  1046. * @param qs : object QuestState pointing out the player's quest
  1047. * @param var : String designating the variable characterizing the quest
  1048. */
  1049. public static void deleteQuestVarInDb(QuestState qs, String var)
  1050. {
  1051. Connection con = null;
  1052. try
  1053. {
  1054. con = L2DatabaseFactory.getInstance().getConnection();
  1055. PreparedStatement statement;
  1056. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?");
  1057. statement.setInt(1, qs.getPlayer().getObjectId());
  1058. statement.setString(2, qs.getQuestName());
  1059. statement.setString(3, var);
  1060. statement.executeUpdate();
  1061. statement.close();
  1062. }
  1063. catch (Exception e)
  1064. {
  1065. _log.log(Level.WARNING, "could not delete char quest:", e);
  1066. }
  1067. finally
  1068. {
  1069. L2DatabaseFactory.close(con);
  1070. }
  1071. }
  1072. /**
  1073. * Delete the player's quest from database.
  1074. * @param qs : QuestState pointing out the player's quest
  1075. */
  1076. public static void deleteQuestInDb(QuestState qs)
  1077. {
  1078. Connection con = null;
  1079. try
  1080. {
  1081. con = L2DatabaseFactory.getInstance().getConnection();
  1082. PreparedStatement statement;
  1083. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=?");
  1084. statement.setInt(1, qs.getPlayer().getObjectId());
  1085. statement.setString(2, qs.getQuestName());
  1086. statement.executeUpdate();
  1087. statement.close();
  1088. }
  1089. catch (Exception e)
  1090. {
  1091. _log.log(Level.WARNING, "could not delete char quest:", e);
  1092. }
  1093. finally
  1094. {
  1095. L2DatabaseFactory.close(con);
  1096. }
  1097. }
  1098. /**
  1099. * Create a record in database for quest.<BR><BR>
  1100. * <U><I>Actions :</I></U><BR>
  1101. * Use fucntion createQuestVarInDb() with following parameters :<BR>
  1102. * <LI>QuestState : parameter sq that puts in fields of database :
  1103. * <UL type="square">
  1104. * <LI>charId : ID of the player</LI>
  1105. * <LI>name : name of the quest</LI>
  1106. * </UL>
  1107. * </LI>
  1108. * <LI>var : string "&lt;state&gt;" as the name of the variable for the quest</LI>
  1109. * <LI>val : string corresponding at the ID of the state (in fact, initial state)</LI>
  1110. * @param qs : QuestState
  1111. */
  1112. public static void createQuestInDb(QuestState qs)
  1113. {
  1114. createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1115. }
  1116. /**
  1117. * Update informations regarding quest in database.<BR>
  1118. * <U><I>Actions :</I></U><BR>
  1119. * <LI>Get ID state of the quest recorded in object qs</LI>
  1120. * <LI>Test if quest is completed. If true, add a star (*) before the ID state</LI>
  1121. * <LI>Save in database the ID state (with or without the star) for the variable called "&lt;state&gt;" of the quest</LI>
  1122. * @param qs : QuestState
  1123. */
  1124. public static void updateQuestInDb(QuestState qs)
  1125. {
  1126. String val = State.getStateName(qs.getState());
  1127. updateQuestVarInDb(qs, "<state>", val);
  1128. }
  1129. /**
  1130. * Return default html page "You are either not on a quest that involves this NPC.."
  1131. * @param player
  1132. * @return
  1133. */
  1134. public static String getNoQuestMsg(L2PcInstance player)
  1135. {
  1136. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/noquest.htm");
  1137. if (result != null && result.length() > 0)
  1138. return result;
  1139. return DEFAULT_NO_QUEST_MSG;
  1140. }
  1141. /**
  1142. * Return default html page "This quest has already been completed."
  1143. * @param player
  1144. * @return
  1145. */
  1146. public static String getAlreadyCompletedMsg(L2PcInstance player)
  1147. {
  1148. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/alreadycompleted.htm");
  1149. if (result != null && result.length() > 0)
  1150. return result;
  1151. return DEFAULT_ALREADY_COMPLETED_MSG;
  1152. }
  1153. /**
  1154. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.<BR><BR>
  1155. * @param npcId : id of the NPC to register
  1156. * @param eventType : type of event being registered
  1157. * @return L2NpcTemplate : Npc Template corresponding to the npcId, or null if the id is invalid
  1158. */
  1159. public L2NpcTemplate addEventId(int npcId, QuestEventType eventType)
  1160. {
  1161. try
  1162. {
  1163. L2NpcTemplate t = NpcTable.getInstance().getTemplate(npcId);
  1164. if (t != null)
  1165. {
  1166. t.addQuestEvent(eventType, this);
  1167. }
  1168. return t;
  1169. }
  1170. catch (Exception e)
  1171. {
  1172. _log.log(Level.WARNING, "Exception on addEventId(): " + e.getMessage(), e);
  1173. return null;
  1174. }
  1175. }
  1176. /**
  1177. * Add the quest to the NPC's startQuest
  1178. * @param npcId
  1179. * @return L2NpcTemplate : Start NPC
  1180. */
  1181. public L2NpcTemplate addStartNpc(int npcId)
  1182. {
  1183. return addEventId(npcId, Quest.QuestEventType.QUEST_START);
  1184. }
  1185. /**
  1186. * Add the quest to the NPC's first-talk (default action dialog)
  1187. * @param npcId
  1188. * @return L2NpcTemplate : Start NPC
  1189. */
  1190. public L2NpcTemplate addFirstTalkId(int npcId)
  1191. {
  1192. return addEventId(npcId, Quest.QuestEventType.ON_FIRST_TALK);
  1193. }
  1194. /**
  1195. * Add the NPC to the AcquireSkill dialog
  1196. * @param npcId
  1197. * @return L2NpcTemplate : NPC
  1198. */
  1199. public L2NpcTemplate addAcquireSkillId(int npcId)
  1200. {
  1201. return addEventId(npcId, Quest.QuestEventType.ON_SKILL_LEARN);
  1202. }
  1203. /**
  1204. * Add this quest to the list of quests that the passed mob will respond to for Attack Events.<BR><BR>
  1205. * @param attackId
  1206. * @return int : attackId
  1207. */
  1208. public L2NpcTemplate addAttackId(int attackId)
  1209. {
  1210. return addEventId(attackId, Quest.QuestEventType.ON_ATTACK);
  1211. }
  1212. /**
  1213. * Add this quest to the list of quests that the passed mob will respond to for Kill Events.<BR><BR>
  1214. * @param killId
  1215. * @return int : killId
  1216. */
  1217. public L2NpcTemplate addKillId(int killId)
  1218. {
  1219. return addEventId(killId, Quest.QuestEventType.ON_KILL);
  1220. }
  1221. /**
  1222. * Add this quest to the list of quests that the passed npc will respond to for Talk Events.<BR><BR>
  1223. * @param talkId : ID of the NPC
  1224. * @return int : ID of the NPC
  1225. */
  1226. public L2NpcTemplate addTalkId(int talkId)
  1227. {
  1228. return addEventId(talkId, Quest.QuestEventType.ON_TALK);
  1229. }
  1230. /**
  1231. * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.<BR><BR>
  1232. * @param talkId : ID of the NPC
  1233. * @return int : ID of the NPC
  1234. */
  1235. public L2NpcTemplate addSpawnId(int npcId)
  1236. {
  1237. return addEventId(npcId, Quest.QuestEventType.ON_SPAWN);
  1238. }
  1239. /**
  1240. * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.<BR><BR>
  1241. * @param talkId : ID of the NPC
  1242. * @return int : ID of the NPC
  1243. */
  1244. public L2NpcTemplate addSkillSeeId(int npcId)
  1245. {
  1246. return addEventId(npcId, Quest.QuestEventType.ON_SKILL_SEE);
  1247. }
  1248. public L2NpcTemplate addSpellFinishedId(int npcId)
  1249. {
  1250. return addEventId(npcId, Quest.QuestEventType.ON_SPELL_FINISHED);
  1251. }
  1252. public L2NpcTemplate addTrapActionId(int trapId)
  1253. {
  1254. return addEventId(trapId, Quest.QuestEventType.ON_TRAP_ACTION);
  1255. }
  1256. /**
  1257. * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.<BR><BR>
  1258. * @param talkId : ID of the NPC
  1259. * @return int : ID of the NPC
  1260. */
  1261. public L2NpcTemplate addFactionCallId(int npcId)
  1262. {
  1263. return addEventId(npcId, Quest.QuestEventType.ON_FACTION_CALL);
  1264. }
  1265. /**
  1266. * Add this quest to the list of quests that the passed npc will respond to for Character See Events.<BR><BR>
  1267. * @param talkId : ID of the NPC
  1268. * @return int : ID of the NPC
  1269. */
  1270. public L2NpcTemplate addAggroRangeEnterId(int npcId)
  1271. {
  1272. return addEventId(npcId, Quest.QuestEventType.ON_AGGRO_RANGE_ENTER);
  1273. }
  1274. public L2ZoneType addEnterZoneId(int zoneId)
  1275. {
  1276. try
  1277. {
  1278. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1279. if (zone != null)
  1280. {
  1281. zone.addQuestEvent(Quest.QuestEventType.ON_ENTER_ZONE, this);
  1282. }
  1283. return zone;
  1284. }
  1285. catch (Exception e)
  1286. {
  1287. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1288. return null;
  1289. }
  1290. }
  1291. public L2ZoneType addExitZoneId(int zoneId)
  1292. {
  1293. try
  1294. {
  1295. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1296. if (zone != null)
  1297. {
  1298. zone.addQuestEvent(Quest.QuestEventType.ON_EXIT_ZONE, this);
  1299. }
  1300. return zone;
  1301. }
  1302. catch (Exception e)
  1303. {
  1304. _log.log(Level.WARNING, "Exception on addExitZoneId(): " + e.getMessage(), e);
  1305. return null;
  1306. }
  1307. }
  1308. // returns a random party member's L2PcInstance for the passed player's party
  1309. // returns the passed player if he has no party.
  1310. public L2PcInstance getRandomPartyMember(L2PcInstance player)
  1311. {
  1312. // NPE prevention. If the player is null, there is nothing to return
  1313. if (player == null)
  1314. return null;
  1315. if ((player.getParty() == null) || (player.getParty().getPartyMembers().isEmpty()))
  1316. return player;
  1317. L2Party party = player.getParty();
  1318. return party.getPartyMembers().get(Rnd.get(party.getPartyMembers().size()));
  1319. }
  1320. /**
  1321. * Auxilary function for party quests.
  1322. * Note: This function is only here because of how commonly it may be used by quest developers.
  1323. * For any variations on this function, the quest script can always handle things on its own
  1324. * @param player: the instance of a player whose party is to be searched
  1325. * @param value: the value of the "cond" variable that must be matched
  1326. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1327. * condition, or null if no match.
  1328. */
  1329. public L2PcInstance getRandomPartyMember(L2PcInstance player, String value)
  1330. {
  1331. return getRandomPartyMember(player, "cond", value);
  1332. }
  1333. /**
  1334. * Auxilary function for party quests.
  1335. * Note: This function is only here because of how commonly it may be used by quest developers.
  1336. * For any variations on this function, the quest script can always handle things on its own
  1337. * @param player: the instance of a player whose party is to be searched
  1338. * @param var/value: a tuple specifying a quest condition that must be satisfied for
  1339. * a party member to be considered.
  1340. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1341. * condition, or null if no match. If the var is null, any random party
  1342. * member is returned (i.e. no condition is applied).
  1343. * The party member must be within 1500 distance from the target of the reference
  1344. * player, or if no target exists, 1500 distance from the player itself.
  1345. */
  1346. public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
  1347. {
  1348. // if no valid player instance is passed, there is nothing to check...
  1349. if (player == null)
  1350. return null;
  1351. // for null var condition, return any random party member.
  1352. if (var == null)
  1353. return getRandomPartyMember(player);
  1354. // normal cases...if the player is not in a party, check the player's state
  1355. QuestState temp = null;
  1356. L2Party party = player.getParty();
  1357. // if this player is not in a party, just check if this player instance matches the conditions itself
  1358. if ((party == null) || (party.getPartyMembers().isEmpty()))
  1359. {
  1360. temp = player.getQuestState(getName());
  1361. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value))
  1362. return player; // match
  1363. return null; // no match
  1364. }
  1365. // if the player is in a party, gather a list of all matching party members (possibly
  1366. // including this player)
  1367. FastList<L2PcInstance> candidates = new FastList<L2PcInstance>();
  1368. // get the target for enforcing distance limitations.
  1369. L2Object target = player.getTarget();
  1370. if (target == null)
  1371. target = player;
  1372. for (L2PcInstance partyMember : party.getPartyMembers())
  1373. {
  1374. if (partyMember == null)
  1375. continue;
  1376. temp = partyMember.getQuestState(getName());
  1377. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
  1378. candidates.add(partyMember);
  1379. }
  1380. // if there was no match, return null...
  1381. if (candidates.isEmpty())
  1382. return null;
  1383. // if a match was found from the party, return one of them at random.
  1384. return candidates.get(Rnd.get(candidates.size()));
  1385. }
  1386. /**
  1387. * Auxilary function for party quests.
  1388. * Note: This function is only here because of how commonly it may be used by quest developers.
  1389. * For any variations on this function, the quest script can always handle things on its own
  1390. * @param player: the instance of a player whose party is to be searched
  1391. * @param state: the state in which the party member's queststate must be in order to be considered.
  1392. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1393. * condition, or null if no match. If the var is null, any random party
  1394. * member is returned (i.e. no condition is applied).
  1395. */
  1396. public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
  1397. {
  1398. // if no valid player instance is passed, there is nothing to check...
  1399. if (player == null)
  1400. return null;
  1401. // normal cases...if the player is not in a partym check the player's state
  1402. QuestState temp = null;
  1403. L2Party party = player.getParty();
  1404. // if this player is not in a party, just check if this player instance matches the conditions itself
  1405. if ((party == null) || (party.getPartyMembers().isEmpty()))
  1406. {
  1407. temp = player.getQuestState(getName());
  1408. if ((temp != null) && (temp.getState() == state))
  1409. return player; // match
  1410. return null; // no match
  1411. }
  1412. // if the player is in a party, gather a list of all matching party members (possibly
  1413. // including this player)
  1414. FastList<L2PcInstance> candidates = new FastList<L2PcInstance>();
  1415. // get the target for enforcing distance limitations.
  1416. L2Object target = player.getTarget();
  1417. if (target == null)
  1418. target = player;
  1419. for (L2PcInstance partyMember : party.getPartyMembers())
  1420. {
  1421. if (partyMember == null)
  1422. continue;
  1423. temp = partyMember.getQuestState(getName());
  1424. if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
  1425. candidates.add(partyMember);
  1426. }
  1427. // if there was no match, return null...
  1428. if (candidates.isEmpty())
  1429. return null;
  1430. // if a match was found from the party, return one of them at random.
  1431. return candidates.get(Rnd.get(candidates.size()));
  1432. }
  1433. /**
  1434. * Show HTML file to client
  1435. * @param fileName
  1436. * @return String : message sent to client
  1437. */
  1438. public String showHtmlFile(L2PcInstance player, String fileName)
  1439. {
  1440. boolean questwindow = true;
  1441. if (fileName.endsWith(".html"))
  1442. questwindow = false;
  1443. int questId = getQuestIntId();
  1444. //Create handler to file linked to the quest
  1445. String content = getHtm(player.getHtmlPrefix(), fileName);
  1446. if (player.getTarget() != null)
  1447. content = content.replaceAll("%objectId%", String.valueOf(player.getTarget().getObjectId()));
  1448. //Send message to client if message not empty
  1449. if (content != null)
  1450. {
  1451. if (questwindow && questId > 0 && questId < 20000 && questId != 999)
  1452. {
  1453. NpcQuestHtmlMessage npcReply = new NpcQuestHtmlMessage(5,questId);
  1454. npcReply.setHtml(content);
  1455. npcReply.replace("%playername%", player.getName());
  1456. player.sendPacket(npcReply);
  1457. }
  1458. else
  1459. {
  1460. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  1461. npcReply.setHtml(content);
  1462. npcReply.replace("%playername%", player.getName());
  1463. player.sendPacket(npcReply);
  1464. }
  1465. player.sendPacket(ActionFailed.STATIC_PACKET);
  1466. }
  1467. return content;
  1468. }
  1469. /**
  1470. * Return HTML file contents
  1471. * @param player
  1472. * @param fileName
  1473. * @return
  1474. */
  1475. public String getHtm(String prefix, String fileName)
  1476. {
  1477. String content = HtmCache.getInstance().getHtm(prefix, "data/scripts/" + getDescr().toLowerCase() + "/" + getName() + "/" + fileName);
  1478. if (content == null)
  1479. {
  1480. content = HtmCache.getInstance().getHtm(prefix, "data/scripts/quests/Q" + getName() + "/" + fileName);
  1481. if (content == null)
  1482. content = HtmCache.getInstance().getHtmForce(prefix, "data/scripts/quests/" + getName() + "/" + fileName);
  1483. }
  1484. return content;
  1485. }
  1486. // Method - Public
  1487. /**
  1488. * Add a temporary (quest) spawn
  1489. * Return instance of newly spawned npc
  1490. */
  1491. public L2Npc addSpawn(int npcId, L2Character cha)
  1492. {
  1493. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, false);
  1494. }
  1495. /**
  1496. * Add a temporary (quest) spawn
  1497. * Return instance of newly spawned npc
  1498. * with summon animation
  1499. */
  1500. public L2Npc addSpawn(int npcId, L2Character cha, boolean isSummonSpawn)
  1501. {
  1502. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, isSummonSpawn);
  1503. }
  1504. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  1505. {
  1506. return addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay, false);
  1507. }
  1508. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  1509. {
  1510. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn, 0);
  1511. }
  1512. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  1513. {
  1514. L2Npc result = null;
  1515. try
  1516. {
  1517. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  1518. if (template != null)
  1519. {
  1520. // Sometimes, even if the quest script specifies some xyz (for example npc.getX() etc) by the time the code
  1521. // reaches here, xyz have become 0! Also, a questdev might have purposely set xy to 0,0...however,
  1522. // the spawn code is coded such that if x=y=0, it looks into location for the spawn loc! This will NOT work
  1523. // with quest spawns! For both of the above cases, we need a fail-safe spawn. For this, we use the
  1524. // default spawn location, which is at the player's loc.
  1525. if ((x == 0) && (y == 0))
  1526. {
  1527. _log.log(Level.SEVERE, "Failed to adjust bad locks for quest spawn! Spawn aborted!");
  1528. return null;
  1529. }
  1530. if (randomOffset)
  1531. {
  1532. int offset;
  1533. offset = Rnd.get(2); // Get the direction of the offset
  1534. if (offset == 0)
  1535. {
  1536. offset = -1;
  1537. } // make offset negative
  1538. offset *= Rnd.get(50, 100);
  1539. x += offset;
  1540. offset = Rnd.get(2); // Get the direction of the offset
  1541. if (offset == 0)
  1542. {
  1543. offset = -1;
  1544. } // make offset negative
  1545. offset *= Rnd.get(50, 100);
  1546. y += offset;
  1547. }
  1548. L2Spawn spawn = new L2Spawn(template);
  1549. spawn.setInstanceId(instanceId);
  1550. spawn.setHeading(heading);
  1551. spawn.setLocx(x);
  1552. spawn.setLocy(y);
  1553. spawn.setLocz(z + 20);
  1554. spawn.stopRespawn();
  1555. result = spawn.spawnOne(isSummonSpawn);
  1556. if (despawnDelay > 0)
  1557. result.scheduleDespawn(despawnDelay);
  1558. return result;
  1559. }
  1560. }
  1561. catch (Exception e1)
  1562. {
  1563. _log.warning("Could not spawn Npc " + npcId);
  1564. }
  1565. return null;
  1566. }
  1567. public L2Trap addTrap(int trapId, int x, int y, int z, int heading, L2Skill skill, int instanceId)
  1568. {
  1569. L2NpcTemplate TrapTemplate = NpcTable.getInstance().getTemplate(trapId);
  1570. L2Trap trap = new L2TrapInstance(IdFactory.getInstance().getNextId(), TrapTemplate, instanceId, -1, skill);
  1571. trap.setCurrentHp(trap.getMaxHp());
  1572. trap.setCurrentMp(trap.getMaxMp());
  1573. trap.setIsInvul(true);
  1574. trap.setHeading(heading);
  1575. //L2World.getInstance().storeObject(trap);
  1576. trap.spawnMe(x, y, z);
  1577. return trap;
  1578. }
  1579. public L2Npc addMinion(L2MonsterInstance master, int minionId)
  1580. {
  1581. return MinionList.spawnMinion(master, minionId);
  1582. }
  1583. public int[] getRegisteredItemIds()
  1584. {
  1585. return questItemIds;
  1586. }
  1587. @Override
  1588. public String getScriptName()
  1589. {
  1590. return this.getName();
  1591. }
  1592. @Override
  1593. public void setActive(boolean status)
  1594. {
  1595. // TODO implement me
  1596. }
  1597. @Override
  1598. public boolean reload()
  1599. {
  1600. unload();
  1601. return super.reload();
  1602. }
  1603. @Override
  1604. public boolean unload()
  1605. {
  1606. return unload(true);
  1607. }
  1608. public boolean unload(boolean removeFromList)
  1609. {
  1610. this.saveGlobalData();
  1611. // cancel all pending timers before reloading.
  1612. // if timers ought to be restarted, the quest can take care of it
  1613. // with its code (example: save global data indicating what timer must
  1614. // be restarted).
  1615. for (FastList<QuestTimer> timers : _allEventTimers.values())
  1616. for (QuestTimer timer : timers)
  1617. timer.cancel();
  1618. _allEventTimers.clear();
  1619. if (removeFromList)
  1620. return QuestManager.getInstance().removeQuest(this);
  1621. else
  1622. return true;
  1623. }
  1624. @Override
  1625. public ScriptManager<?> getScriptManager()
  1626. {
  1627. return QuestManager.getInstance();
  1628. }
  1629. public void setOnEnterWorld(boolean val)
  1630. {
  1631. _onEnterWorld = val;
  1632. }
  1633. public boolean getOnEnterWorld()
  1634. {
  1635. return _onEnterWorld;
  1636. }
  1637. }