Quest.java 45 KB

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