Quest.java 49 KB

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