Quest.java 49 KB

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