2
0

Quest.java 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  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 statement;
  784. PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId=? and name=?");
  785. PreparedStatement invalidQuestDataVar = con.prepareStatement("delete FROM character_quests WHERE charId=? and name=? and var=?");
  786. statement = con.prepareStatement("SELECT name,value FROM character_quests WHERE charId=? AND var=?");
  787. statement.setInt(1, player.getObjectId());
  788. statement.setString(2, "<state>");
  789. ResultSet rs = statement.executeQuery();
  790. while (rs.next())
  791. {
  792. // Get ID of the quest and ID of its state
  793. String questId = rs.getString("name");
  794. String statename = rs.getString("value");
  795. // Search quest associated with the ID
  796. Quest q = QuestManager.getInstance().getQuest(questId);
  797. if (q == null)
  798. {
  799. _log.finer("Unknown quest " + questId + " for player " + player.getName());
  800. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  801. {
  802. invalidQuestData.setInt(1, player.getObjectId());
  803. invalidQuestData.setString(2, questId);
  804. invalidQuestData.executeUpdate();
  805. }
  806. continue;
  807. }
  808. // Create a new QuestState for the player that will be added to the player's list of quests
  809. new QuestState(q, player, State.getStateId(statename));
  810. }
  811. rs.close();
  812. invalidQuestData.close();
  813. statement.close();
  814. // Get list of quests owned by the player from the DB in order to add variables used in the quest.
  815. statement = con.prepareStatement("SELECT name,var,value FROM character_quests WHERE charId=? AND var<>?");
  816. statement.setInt(1, player.getObjectId());
  817. statement.setString(2, "<state>");
  818. rs = statement.executeQuery();
  819. while (rs.next())
  820. {
  821. String questId = rs.getString("name");
  822. String var = rs.getString("var");
  823. String value = rs.getString("value");
  824. // Get the QuestState saved in the loop before
  825. QuestState qs = player.getQuestState(questId);
  826. if (qs == null)
  827. {
  828. _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
  829. if (Config.AUTODELETE_INVALID_QUEST_DATA)
  830. {
  831. invalidQuestDataVar.setInt(1, player.getObjectId());
  832. invalidQuestDataVar.setString(2, questId);
  833. invalidQuestDataVar.setString(3, var);
  834. invalidQuestDataVar.executeUpdate();
  835. }
  836. continue;
  837. }
  838. // Add parameter to the quest
  839. qs.setInternal(var, value);
  840. }
  841. rs.close();
  842. invalidQuestDataVar.close();
  843. statement.close();
  844. }
  845. catch (Exception e)
  846. {
  847. _log.log(Level.WARNING, "could not insert char quest:", e);
  848. }
  849. finally
  850. {
  851. L2DatabaseFactory.close(con);
  852. }
  853. // events
  854. for (String name : _allEventsS.keySet())
  855. {
  856. player.processQuestEvent(name, "enter");
  857. }
  858. }
  859. /**
  860. * Insert (or Update) in the database variables that need to stay persistant for this quest after a reboot.
  861. * This function is for storage of values that do not related to a specific player but are
  862. * global for all characters. For example, if we need to disable a quest-gatekeeper until
  863. * a certain time (as is done with some grand-boss gatekeepers), we can save that time in the DB.
  864. * @param var : String designating the name of the variable for the quest
  865. * @param value : String designating the value of the variable for the quest
  866. */
  867. public final void saveGlobalQuestVar(String var, String value)
  868. {
  869. Connection con = null;
  870. try
  871. {
  872. con = L2DatabaseFactory.getInstance().getConnection();
  873. PreparedStatement statement;
  874. statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)");
  875. statement.setString(1, getName());
  876. statement.setString(2, var);
  877. statement.setString(3, value);
  878. statement.executeUpdate();
  879. statement.close();
  880. }
  881. catch (Exception e)
  882. {
  883. _log.log(Level.WARNING, "could not insert global quest variable:", e);
  884. }
  885. finally
  886. {
  887. L2DatabaseFactory.close(con);
  888. }
  889. }
  890. /**
  891. * Read from the database a previously saved variable for this quest.
  892. * Due to performance considerations, this function should best be used only when the quest is first loaded.
  893. * Subclasses of this class can define structures into which these loaded values can be saved.
  894. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.
  895. * Values read from this function were entered by calls to "saveGlobalQuestVar"
  896. * @param var : String designating the name of the variable for the quest
  897. * @return String : String representing the loaded value for the passed var, or an empty string if the var was invalid
  898. */
  899. public final String loadGlobalQuestVar(String var)
  900. {
  901. String result = "";
  902. Connection con = null;
  903. try
  904. {
  905. con = L2DatabaseFactory.getInstance().getConnection();
  906. PreparedStatement statement;
  907. statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?");
  908. statement.setString(1, getName());
  909. statement.setString(2, var);
  910. ResultSet rs = statement.executeQuery();
  911. if (rs.first())
  912. result = rs.getString(1);
  913. rs.close();
  914. statement.close();
  915. }
  916. catch (Exception e)
  917. {
  918. _log.log(Level.WARNING, "could not load global quest variable:", e);
  919. }
  920. finally
  921. {
  922. L2DatabaseFactory.close(con);
  923. }
  924. return result;
  925. }
  926. /**
  927. * Permanently delete from the database a global quest variable that was previously saved for this quest.
  928. * @param var : String designating the name of the variable for the quest
  929. */
  930. public final void deleteGlobalQuestVar(String var)
  931. {
  932. Connection con = null;
  933. try
  934. {
  935. con = L2DatabaseFactory.getInstance().getConnection();
  936. PreparedStatement statement;
  937. statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?");
  938. statement.setString(1, getName());
  939. statement.setString(2, var);
  940. statement.executeUpdate();
  941. statement.close();
  942. }
  943. catch (Exception e)
  944. {
  945. _log.log(Level.WARNING, "could not delete global quest variable:", e);
  946. }
  947. finally
  948. {
  949. L2DatabaseFactory.close(con);
  950. }
  951. }
  952. /**
  953. * Permanently delete from the database all global quest variables that was previously saved for this quest.
  954. */
  955. public final void deleteAllGlobalQuestVars()
  956. {
  957. Connection con = null;
  958. try
  959. {
  960. con = L2DatabaseFactory.getInstance().getConnection();
  961. PreparedStatement statement;
  962. statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?");
  963. statement.setString(1, getName());
  964. statement.executeUpdate();
  965. statement.close();
  966. }
  967. catch (Exception e)
  968. {
  969. _log.log(Level.WARNING, "could not delete global quest variables:", e);
  970. }
  971. finally
  972. {
  973. L2DatabaseFactory.close(con);
  974. }
  975. }
  976. /**
  977. * Insert in the database the quest for the player.
  978. * @param qs : QuestState pointing out the state of the quest
  979. * @param var : String designating the name of the variable for the quest
  980. * @param value : String designating the value of the variable for the quest
  981. */
  982. public static void createQuestVarInDb(QuestState qs, String var, String value)
  983. {
  984. Connection con = null;
  985. try
  986. {
  987. con = L2DatabaseFactory.getInstance().getConnection();
  988. PreparedStatement statement;
  989. statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE value=?");
  990. statement.setInt(1, qs.getPlayer().getObjectId());
  991. statement.setString(2, qs.getQuestName());
  992. statement.setString(3, var);
  993. statement.setString(4, value);
  994. statement.setString(5, value);
  995. statement.executeUpdate();
  996. statement.close();
  997. }
  998. catch (Exception e)
  999. {
  1000. _log.log(Level.WARNING, "could not insert char quest:", e);
  1001. }
  1002. finally
  1003. {
  1004. L2DatabaseFactory.close(con);
  1005. }
  1006. }
  1007. /**
  1008. * Update the value of the variable "var" for the quest.<BR><BR>
  1009. * <U><I>Actions :</I></U><BR>
  1010. * The selection of the right record is made with :
  1011. * <LI>charId = qs.getPlayer().getObjectID()</LI>
  1012. * <LI>name = qs.getQuest().getName()</LI>
  1013. * <LI>var = var</LI>
  1014. * <BR><BR>
  1015. * The modification made is :
  1016. * <LI>value = parameter value</LI>
  1017. * @param qs : Quest State
  1018. * @param var : String designating the name of the variable for quest
  1019. * @param value : String designating the value of the variable for quest
  1020. */
  1021. public static void updateQuestVarInDb(QuestState qs, String var, String value)
  1022. {
  1023. Connection con = null;
  1024. try
  1025. {
  1026. con = L2DatabaseFactory.getInstance().getConnection();
  1027. PreparedStatement statement;
  1028. statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?");
  1029. statement.setString(1, value);
  1030. statement.setInt(2, qs.getPlayer().getObjectId());
  1031. statement.setString(3, qs.getQuestName());
  1032. statement.setString(4, var);
  1033. statement.executeUpdate();
  1034. statement.close();
  1035. }
  1036. catch (Exception e)
  1037. {
  1038. _log.log(Level.WARNING, "could not update char quest:", e);
  1039. }
  1040. finally
  1041. {
  1042. L2DatabaseFactory.close(con);
  1043. }
  1044. }
  1045. /**
  1046. * Delete a variable of player's quest from the database.
  1047. * @param qs : object QuestState pointing out the player's quest
  1048. * @param var : String designating the variable characterizing the quest
  1049. */
  1050. public static void deleteQuestVarInDb(QuestState qs, String var)
  1051. {
  1052. Connection con = null;
  1053. try
  1054. {
  1055. con = L2DatabaseFactory.getInstance().getConnection();
  1056. PreparedStatement statement;
  1057. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?");
  1058. statement.setInt(1, qs.getPlayer().getObjectId());
  1059. statement.setString(2, qs.getQuestName());
  1060. statement.setString(3, var);
  1061. statement.executeUpdate();
  1062. statement.close();
  1063. }
  1064. catch (Exception e)
  1065. {
  1066. _log.log(Level.WARNING, "could not delete char quest:", e);
  1067. }
  1068. finally
  1069. {
  1070. L2DatabaseFactory.close(con);
  1071. }
  1072. }
  1073. /**
  1074. * Delete the player's quest from database.
  1075. * @param qs : QuestState pointing out the player's quest
  1076. */
  1077. public static void deleteQuestInDb(QuestState qs)
  1078. {
  1079. Connection con = null;
  1080. try
  1081. {
  1082. con = L2DatabaseFactory.getInstance().getConnection();
  1083. PreparedStatement statement;
  1084. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=?");
  1085. statement.setInt(1, qs.getPlayer().getObjectId());
  1086. statement.setString(2, qs.getQuestName());
  1087. statement.executeUpdate();
  1088. statement.close();
  1089. }
  1090. catch (Exception e)
  1091. {
  1092. _log.log(Level.WARNING, "could not delete char quest:", e);
  1093. }
  1094. finally
  1095. {
  1096. L2DatabaseFactory.close(con);
  1097. }
  1098. }
  1099. /**
  1100. * Create a record in database for quest.<BR><BR>
  1101. * <U><I>Actions :</I></U><BR>
  1102. * Use fucntion createQuestVarInDb() with following parameters :<BR>
  1103. * <LI>QuestState : parameter sq that puts in fields of database :
  1104. * <UL type="square">
  1105. * <LI>charId : ID of the player</LI>
  1106. * <LI>name : name of the quest</LI>
  1107. * </UL>
  1108. * </LI>
  1109. * <LI>var : string "&lt;state&gt;" as the name of the variable for the quest</LI>
  1110. * <LI>val : string corresponding at the ID of the state (in fact, initial state)</LI>
  1111. * @param qs : QuestState
  1112. */
  1113. public static void createQuestInDb(QuestState qs)
  1114. {
  1115. createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
  1116. }
  1117. /**
  1118. * Update informations regarding quest in database.<BR>
  1119. * <U><I>Actions :</I></U><BR>
  1120. * <LI>Get ID state of the quest recorded in object qs</LI>
  1121. * <LI>Test if quest is completed. If true, add a star (*) before the ID state</LI>
  1122. * <LI>Save in database the ID state (with or without the star) for the variable called "&lt;state&gt;" of the quest</LI>
  1123. * @param qs : QuestState
  1124. */
  1125. public static void updateQuestInDb(QuestState qs)
  1126. {
  1127. String val = State.getStateName(qs.getState());
  1128. updateQuestVarInDb(qs, "<state>", val);
  1129. }
  1130. /**
  1131. * Return default html page "You are either not on a quest that involves this NPC.."
  1132. * @param player
  1133. * @return
  1134. */
  1135. public static String getNoQuestMsg(L2PcInstance player)
  1136. {
  1137. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/noquest.htm");
  1138. if (result != null && result.length() > 0)
  1139. return result;
  1140. return DEFAULT_NO_QUEST_MSG;
  1141. }
  1142. /**
  1143. * Return default html page "This quest has already been completed."
  1144. * @param player
  1145. * @return
  1146. */
  1147. public static String getAlreadyCompletedMsg(L2PcInstance player)
  1148. {
  1149. final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/alreadycompleted.htm");
  1150. if (result != null && result.length() > 0)
  1151. return result;
  1152. return DEFAULT_ALREADY_COMPLETED_MSG;
  1153. }
  1154. /**
  1155. * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.<BR><BR>
  1156. * @param npcId : id of the NPC to register
  1157. * @param eventType : type of event being registered
  1158. * @return L2NpcTemplate : Npc Template corresponding to the npcId, or null if the id is invalid
  1159. */
  1160. public L2NpcTemplate addEventId(int npcId, QuestEventType eventType)
  1161. {
  1162. try
  1163. {
  1164. L2NpcTemplate t = NpcTable.getInstance().getTemplate(npcId);
  1165. if (t != null)
  1166. {
  1167. t.addQuestEvent(eventType, this);
  1168. }
  1169. return t;
  1170. }
  1171. catch (Exception e)
  1172. {
  1173. _log.log(Level.WARNING, "Exception on addEventId(): " + e.getMessage(), e);
  1174. return null;
  1175. }
  1176. }
  1177. /**
  1178. * Add the quest to the NPC's startQuest
  1179. * @param npcId
  1180. * @return L2NpcTemplate : Start NPC
  1181. */
  1182. public L2NpcTemplate addStartNpc(int npcId)
  1183. {
  1184. return addEventId(npcId, Quest.QuestEventType.QUEST_START);
  1185. }
  1186. /**
  1187. * Add the quest to the NPC's first-talk (default action dialog)
  1188. * @param npcId
  1189. * @return L2NpcTemplate : Start NPC
  1190. */
  1191. public L2NpcTemplate addFirstTalkId(int npcId)
  1192. {
  1193. return addEventId(npcId, Quest.QuestEventType.ON_FIRST_TALK);
  1194. }
  1195. /**
  1196. * Add the NPC to the AcquireSkill dialog
  1197. * @param npcId
  1198. * @return L2NpcTemplate : NPC
  1199. */
  1200. public L2NpcTemplate addAcquireSkillId(int npcId)
  1201. {
  1202. return addEventId(npcId, Quest.QuestEventType.ON_SKILL_LEARN);
  1203. }
  1204. /**
  1205. * Add this quest to the list of quests that the passed mob will respond to for Attack Events.<BR><BR>
  1206. * @param attackId
  1207. * @return int : attackId
  1208. */
  1209. public L2NpcTemplate addAttackId(int attackId)
  1210. {
  1211. return addEventId(attackId, Quest.QuestEventType.ON_ATTACK);
  1212. }
  1213. /**
  1214. * Add this quest to the list of quests that the passed mob will respond to for Kill Events.<BR><BR>
  1215. * @param killId
  1216. * @return int : killId
  1217. */
  1218. public L2NpcTemplate addKillId(int killId)
  1219. {
  1220. return addEventId(killId, Quest.QuestEventType.ON_KILL);
  1221. }
  1222. /**
  1223. * Add this quest to the list of quests that the passed npc will respond to for Talk Events.<BR><BR>
  1224. * @param talkId : ID of the NPC
  1225. * @return int : ID of the NPC
  1226. */
  1227. public L2NpcTemplate addTalkId(int talkId)
  1228. {
  1229. return addEventId(talkId, Quest.QuestEventType.ON_TALK);
  1230. }
  1231. /**
  1232. * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.<BR><BR>
  1233. * @param talkId : ID of the NPC
  1234. * @return int : ID of the NPC
  1235. */
  1236. public L2NpcTemplate addSpawnId(int npcId)
  1237. {
  1238. return addEventId(npcId, Quest.QuestEventType.ON_SPAWN);
  1239. }
  1240. /**
  1241. * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.<BR><BR>
  1242. * @param talkId : ID of the NPC
  1243. * @return int : ID of the NPC
  1244. */
  1245. public L2NpcTemplate addSkillSeeId(int npcId)
  1246. {
  1247. return addEventId(npcId, Quest.QuestEventType.ON_SKILL_SEE);
  1248. }
  1249. public L2NpcTemplate addSpellFinishedId(int npcId)
  1250. {
  1251. return addEventId(npcId, Quest.QuestEventType.ON_SPELL_FINISHED);
  1252. }
  1253. public L2NpcTemplate addTrapActionId(int trapId)
  1254. {
  1255. return addEventId(trapId, Quest.QuestEventType.ON_TRAP_ACTION);
  1256. }
  1257. /**
  1258. * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.<BR><BR>
  1259. * @param talkId : ID of the NPC
  1260. * @return int : ID of the NPC
  1261. */
  1262. public L2NpcTemplate addFactionCallId(int npcId)
  1263. {
  1264. return addEventId(npcId, Quest.QuestEventType.ON_FACTION_CALL);
  1265. }
  1266. /**
  1267. * Add this quest to the list of quests that the passed npc will respond to for Character See Events.<BR><BR>
  1268. * @param talkId : ID of the NPC
  1269. * @return int : ID of the NPC
  1270. */
  1271. public L2NpcTemplate addAggroRangeEnterId(int npcId)
  1272. {
  1273. return addEventId(npcId, Quest.QuestEventType.ON_AGGRO_RANGE_ENTER);
  1274. }
  1275. public L2ZoneType addEnterZoneId(int zoneId)
  1276. {
  1277. try
  1278. {
  1279. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1280. if (zone != null)
  1281. {
  1282. zone.addQuestEvent(Quest.QuestEventType.ON_ENTER_ZONE, this);
  1283. }
  1284. return zone;
  1285. }
  1286. catch (Exception e)
  1287. {
  1288. _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
  1289. return null;
  1290. }
  1291. }
  1292. public L2ZoneType addExitZoneId(int zoneId)
  1293. {
  1294. try
  1295. {
  1296. L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
  1297. if (zone != null)
  1298. {
  1299. zone.addQuestEvent(Quest.QuestEventType.ON_EXIT_ZONE, this);
  1300. }
  1301. return zone;
  1302. }
  1303. catch (Exception e)
  1304. {
  1305. _log.log(Level.WARNING, "Exception on addExitZoneId(): " + e.getMessage(), e);
  1306. return null;
  1307. }
  1308. }
  1309. // returns a random party member's L2PcInstance for the passed player's party
  1310. // returns the passed player if he has no party.
  1311. public L2PcInstance getRandomPartyMember(L2PcInstance player)
  1312. {
  1313. // NPE prevention. If the player is null, there is nothing to return
  1314. if (player == null)
  1315. return null;
  1316. if ((player.getParty() == null) || (player.getParty().getPartyMembers().isEmpty()))
  1317. return player;
  1318. L2Party party = player.getParty();
  1319. return party.getPartyMembers().get(Rnd.get(party.getPartyMembers().size()));
  1320. }
  1321. /**
  1322. * Auxilary function for party quests.
  1323. * Note: This function is only here because of how commonly it may be used by quest developers.
  1324. * For any variations on this function, the quest script can always handle things on its own
  1325. * @param player: the instance of a player whose party is to be searched
  1326. * @param value: the value of the "cond" variable that must be matched
  1327. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1328. * condition, or null if no match.
  1329. */
  1330. public L2PcInstance getRandomPartyMember(L2PcInstance player, String value)
  1331. {
  1332. return getRandomPartyMember(player, "cond", value);
  1333. }
  1334. /**
  1335. * Auxilary function for party quests.
  1336. * Note: This function is only here because of how commonly it may be used by quest developers.
  1337. * For any variations on this function, the quest script can always handle things on its own
  1338. * @param player: the instance of a player whose party is to be searched
  1339. * @param var/value: a tuple specifying a quest condition that must be satisfied for
  1340. * a party member to be considered.
  1341. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1342. * condition, or null if no match. If the var is null, any random party
  1343. * member is returned (i.e. no condition is applied).
  1344. * The party member must be within 1500 distance from the target of the reference
  1345. * player, or if no target exists, 1500 distance from the player itself.
  1346. */
  1347. public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
  1348. {
  1349. // if no valid player instance is passed, there is nothing to check...
  1350. if (player == null)
  1351. return null;
  1352. // for null var condition, return any random party member.
  1353. if (var == null)
  1354. return getRandomPartyMember(player);
  1355. // normal cases...if the player is not in a party, check the player's state
  1356. QuestState temp = null;
  1357. L2Party party = player.getParty();
  1358. // if this player is not in a party, just check if this player instance matches the conditions itself
  1359. if ((party == null) || (party.getPartyMembers().isEmpty()))
  1360. {
  1361. temp = player.getQuestState(getName());
  1362. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value))
  1363. return player; // match
  1364. return null; // no match
  1365. }
  1366. // if the player is in a party, gather a list of all matching party members (possibly
  1367. // including this player)
  1368. FastList<L2PcInstance> candidates = new FastList<L2PcInstance>();
  1369. // get the target for enforcing distance limitations.
  1370. L2Object target = player.getTarget();
  1371. if (target == null)
  1372. target = player;
  1373. for (L2PcInstance partyMember : party.getPartyMembers())
  1374. {
  1375. if (partyMember == null)
  1376. continue;
  1377. temp = partyMember.getQuestState(getName());
  1378. if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
  1379. candidates.add(partyMember);
  1380. }
  1381. // if there was no match, return null...
  1382. if (candidates.isEmpty())
  1383. return null;
  1384. // if a match was found from the party, return one of them at random.
  1385. return candidates.get(Rnd.get(candidates.size()));
  1386. }
  1387. /**
  1388. * Auxilary function for party quests.
  1389. * Note: This function is only here because of how commonly it may be used by quest developers.
  1390. * For any variations on this function, the quest script can always handle things on its own
  1391. * @param player: the instance of a player whose party is to be searched
  1392. * @param state: the state in which the party member's queststate must be in order to be considered.
  1393. * @return L2PcInstance: L2PcInstance for a random party member that matches the specified
  1394. * condition, or null if no match. If the var is null, any random party
  1395. * member is returned (i.e. no condition is applied).
  1396. */
  1397. public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
  1398. {
  1399. // if no valid player instance is passed, there is nothing to check...
  1400. if (player == null)
  1401. return null;
  1402. // normal cases...if the player is not in a partym check the player's state
  1403. QuestState temp = null;
  1404. L2Party party = player.getParty();
  1405. // if this player is not in a party, just check if this player instance matches the conditions itself
  1406. if ((party == null) || (party.getPartyMembers().isEmpty()))
  1407. {
  1408. temp = player.getQuestState(getName());
  1409. if ((temp != null) && (temp.getState() == state))
  1410. return player; // match
  1411. return null; // no match
  1412. }
  1413. // if the player is in a party, gather a list of all matching party members (possibly
  1414. // including this player)
  1415. FastList<L2PcInstance> candidates = new FastList<L2PcInstance>();
  1416. // get the target for enforcing distance limitations.
  1417. L2Object target = player.getTarget();
  1418. if (target == null)
  1419. target = player;
  1420. for (L2PcInstance partyMember : party.getPartyMembers())
  1421. {
  1422. if (partyMember == null)
  1423. continue;
  1424. temp = partyMember.getQuestState(getName());
  1425. if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
  1426. candidates.add(partyMember);
  1427. }
  1428. // if there was no match, return null...
  1429. if (candidates.isEmpty())
  1430. return null;
  1431. // if a match was found from the party, return one of them at random.
  1432. return candidates.get(Rnd.get(candidates.size()));
  1433. }
  1434. /**
  1435. * Show HTML file to client
  1436. * @param fileName
  1437. * @return String : message sent to client
  1438. */
  1439. public String showHtmlFile(L2PcInstance player, String fileName)
  1440. {
  1441. boolean questwindow = true;
  1442. if (fileName.endsWith(".html"))
  1443. questwindow = false;
  1444. int questId = getQuestIntId();
  1445. //Create handler to file linked to the quest
  1446. String content = getHtm(player.getHtmlPrefix(), fileName);
  1447. if (player.getTarget() != null)
  1448. content = content.replaceAll("%objectId%", String.valueOf(player.getTarget().getObjectId()));
  1449. //Send message to client if message not empty
  1450. if (content != null)
  1451. {
  1452. if (questwindow && questId > 0 && questId < 20000 && questId != 999)
  1453. {
  1454. NpcQuestHtmlMessage npcReply = new NpcQuestHtmlMessage(5,questId);
  1455. npcReply.setHtml(content);
  1456. npcReply.replace("%playername%", player.getName());
  1457. player.sendPacket(npcReply);
  1458. }
  1459. else
  1460. {
  1461. NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
  1462. npcReply.setHtml(content);
  1463. npcReply.replace("%playername%", player.getName());
  1464. player.sendPacket(npcReply);
  1465. }
  1466. player.sendPacket(ActionFailed.STATIC_PACKET);
  1467. }
  1468. return content;
  1469. }
  1470. /**
  1471. * Return HTML file contents
  1472. * @param player
  1473. * @param fileName
  1474. * @return
  1475. */
  1476. public String getHtm(String prefix, String fileName)
  1477. {
  1478. String content = HtmCache.getInstance().getHtm(prefix, "data/scripts/" + getDescr().toLowerCase() + "/" + getName() + "/" + fileName);
  1479. if (content == null)
  1480. {
  1481. content = HtmCache.getInstance().getHtm(prefix, "data/scripts/quests/Q" + getName() + "/" + fileName);
  1482. if (content == null)
  1483. content = HtmCache.getInstance().getHtmForce(prefix, "data/scripts/quests/" + getName() + "/" + fileName);
  1484. }
  1485. return content;
  1486. }
  1487. // Method - Public
  1488. /**
  1489. * Add a temporary (quest) spawn
  1490. * Return instance of newly spawned npc
  1491. */
  1492. public L2Npc addSpawn(int npcId, L2Character cha)
  1493. {
  1494. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, false);
  1495. }
  1496. /**
  1497. * Add a temporary (quest) spawn
  1498. * Return instance of newly spawned npc
  1499. * with summon animation
  1500. */
  1501. public L2Npc addSpawn(int npcId, L2Character cha, boolean isSummonSpawn)
  1502. {
  1503. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, isSummonSpawn);
  1504. }
  1505. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  1506. {
  1507. return addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay, false);
  1508. }
  1509. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
  1510. {
  1511. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn, 0);
  1512. }
  1513. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
  1514. {
  1515. L2Npc result = null;
  1516. try
  1517. {
  1518. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  1519. if (template != null)
  1520. {
  1521. // Sometimes, even if the quest script specifies some xyz (for example npc.getX() etc) by the time the code
  1522. // reaches here, xyz have become 0! Also, a questdev might have purposely set xy to 0,0...however,
  1523. // the spawn code is coded such that if x=y=0, it looks into location for the spawn loc! This will NOT work
  1524. // with quest spawns! For both of the above cases, we need a fail-safe spawn. For this, we use the
  1525. // default spawn location, which is at the player's loc.
  1526. if ((x == 0) && (y == 0))
  1527. {
  1528. _log.log(Level.SEVERE, "Failed to adjust bad locks for quest spawn! Spawn aborted!");
  1529. return null;
  1530. }
  1531. if (randomOffset)
  1532. {
  1533. int offset;
  1534. offset = Rnd.get(2); // Get the direction of the offset
  1535. if (offset == 0)
  1536. {
  1537. offset = -1;
  1538. } // make offset negative
  1539. offset *= Rnd.get(50, 100);
  1540. x += offset;
  1541. offset = Rnd.get(2); // Get the direction of the offset
  1542. if (offset == 0)
  1543. {
  1544. offset = -1;
  1545. } // make offset negative
  1546. offset *= Rnd.get(50, 100);
  1547. y += offset;
  1548. }
  1549. L2Spawn spawn = new L2Spawn(template);
  1550. spawn.setInstanceId(instanceId);
  1551. spawn.setHeading(heading);
  1552. spawn.setLocx(x);
  1553. spawn.setLocy(y);
  1554. spawn.setLocz(z + 20);
  1555. spawn.stopRespawn();
  1556. result = spawn.spawnOne(isSummonSpawn);
  1557. if (despawnDelay > 0)
  1558. result.scheduleDespawn(despawnDelay);
  1559. return result;
  1560. }
  1561. }
  1562. catch (Exception e1)
  1563. {
  1564. _log.warning("Could not spawn Npc " + npcId);
  1565. }
  1566. return null;
  1567. }
  1568. public L2Trap addTrap(int trapId, int x, int y, int z, int heading, L2Skill skill, int instanceId)
  1569. {
  1570. L2NpcTemplate TrapTemplate = NpcTable.getInstance().getTemplate(trapId);
  1571. L2Trap trap = new L2TrapInstance(IdFactory.getInstance().getNextId(), TrapTemplate, instanceId, -1, skill);
  1572. trap.setCurrentHp(trap.getMaxHp());
  1573. trap.setCurrentMp(trap.getMaxMp());
  1574. trap.setIsInvul(true);
  1575. trap.setHeading(heading);
  1576. //L2World.getInstance().storeObject(trap);
  1577. trap.spawnMe(x, y, z);
  1578. return trap;
  1579. }
  1580. public L2Npc addMinion(L2MonsterInstance master, int minionId)
  1581. {
  1582. return MinionList.spawnMinion(master, minionId);
  1583. }
  1584. public int[] getRegisteredItemIds()
  1585. {
  1586. return questItemIds;
  1587. }
  1588. @Override
  1589. public String getScriptName()
  1590. {
  1591. return this.getName();
  1592. }
  1593. @Override
  1594. public void setActive(boolean status)
  1595. {
  1596. // TODO implement me
  1597. }
  1598. @Override
  1599. public boolean reload()
  1600. {
  1601. unload();
  1602. return super.reload();
  1603. }
  1604. @Override
  1605. public boolean unload()
  1606. {
  1607. return unload(true);
  1608. }
  1609. public boolean unload(boolean removeFromList)
  1610. {
  1611. this.saveGlobalData();
  1612. // cancel all pending timers before reloading.
  1613. // if timers ought to be restarted, the quest can take care of it
  1614. // with its code (example: save global data indicating what timer must
  1615. // be restarted).
  1616. for (FastList<QuestTimer> timers : _allEventTimers.values())
  1617. for (QuestTimer timer : timers)
  1618. timer.cancel();
  1619. _allEventTimers.clear();
  1620. if (removeFromList)
  1621. return QuestManager.getInstance().removeQuest(this);
  1622. else
  1623. return true;
  1624. }
  1625. @Override
  1626. public ScriptManager<?> getScriptManager()
  1627. {
  1628. return QuestManager.getInstance();
  1629. }
  1630. public void setOnEnterWorld(boolean val)
  1631. {
  1632. _onEnterWorld = val;
  1633. }
  1634. public boolean getOnEnterWorld()
  1635. {
  1636. return _onEnterWorld;
  1637. }
  1638. }