QuestState.java 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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.Calendar;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.cache.HtmCache;
  26. import com.l2jserver.gameserver.instancemanager.QuestManager;
  27. import com.l2jserver.gameserver.model.actor.L2Character;
  28. import com.l2jserver.gameserver.model.actor.L2Npc;
  29. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  32. import com.l2jserver.gameserver.model.quest.Quest.QuestSound;
  33. import com.l2jserver.gameserver.network.serverpackets.ExShowQuestMark;
  34. import com.l2jserver.gameserver.network.serverpackets.PlaySound;
  35. import com.l2jserver.gameserver.network.serverpackets.QuestList;
  36. import com.l2jserver.gameserver.network.serverpackets.TutorialCloseHtml;
  37. import com.l2jserver.gameserver.network.serverpackets.TutorialEnableClientEvent;
  38. import com.l2jserver.gameserver.network.serverpackets.TutorialShowHtml;
  39. import com.l2jserver.gameserver.network.serverpackets.TutorialShowQuestionMark;
  40. import com.l2jserver.gameserver.util.Util;
  41. /**
  42. * Quest state class.
  43. * @author Luis Arias
  44. */
  45. public final class QuestState
  46. {
  47. protected static final Logger _log = Logger.getLogger(QuestState.class.getName());
  48. /** The name of the quest of this QuestState */
  49. private final String _questName;
  50. /** The "owner" of this QuestState object */
  51. private final L2PcInstance _player;
  52. /** The current state of the quest */
  53. private byte _state;
  54. /** A map of key->value pairs containing the quest state variables and their values */
  55. private Map<String, String> _vars;
  56. /**
  57. * boolean flag letting QuestStateManager know to exit quest when cleaning up
  58. */
  59. private boolean _isExitQuestOnCleanUp = false;
  60. /**
  61. * This enumerate represent the different quest types.
  62. */
  63. public static enum QuestType
  64. {
  65. REPEATABLE,
  66. ONE_TIME,
  67. DAILY
  68. }
  69. /**
  70. * Constructor of the QuestState. Creates the QuestState object and sets the player's progress of the quest to this QuestState.
  71. * @param quest the {@link Quest} object associated with the QuestState
  72. * @param player the owner of this {@link QuestState} object
  73. * @param state the initial state of the quest
  74. */
  75. public QuestState(Quest quest, L2PcInstance player, byte state)
  76. {
  77. _questName = quest.getName();
  78. _player = player;
  79. _state = state;
  80. player.setQuestState(this);
  81. }
  82. /**
  83. * @return the name of the quest of this QuestState
  84. */
  85. public String getQuestName()
  86. {
  87. return _questName;
  88. }
  89. /**
  90. * @return the {@link Quest} object of this QuestState
  91. */
  92. public Quest getQuest()
  93. {
  94. return QuestManager.getInstance().getQuest(_questName);
  95. }
  96. /**
  97. * @return the {@link L2PcInstance} object of the owner of this QuestState
  98. */
  99. public L2PcInstance getPlayer()
  100. {
  101. return _player;
  102. }
  103. /**
  104. * @return the current State of this QuestState
  105. * @see com.l2jserver.gameserver.model.quest.State
  106. */
  107. public byte getState()
  108. {
  109. return _state;
  110. }
  111. /**
  112. * @return {@code true} if the State of this QuestState is CREATED, {@code false} otherwise
  113. * @see com.l2jserver.gameserver.model.quest.State
  114. */
  115. public boolean isCreated()
  116. {
  117. return (_state == State.CREATED);
  118. }
  119. /**
  120. * @return {@code true} if the State of this QuestState is STARTED, {@code false} otherwise
  121. * @see com.l2jserver.gameserver.model.quest.State
  122. */
  123. public boolean isStarted()
  124. {
  125. return (_state == State.STARTED);
  126. }
  127. /**
  128. * @return {@code true} if the State of this QuestState is COMPLETED, {@code false} otherwise
  129. * @see com.l2jserver.gameserver.model.quest.State
  130. */
  131. public boolean isCompleted()
  132. {
  133. return (_state == State.COMPLETED);
  134. }
  135. /**
  136. * @param state the new state of the quest to set
  137. * @return {@code true} if state was changed, {@code false} otherwise
  138. * @see #setState(byte state, boolean saveInDb)
  139. * @see com.l2jserver.gameserver.model.quest.State
  140. */
  141. public boolean setState(byte state)
  142. {
  143. return setState(state, true);
  144. }
  145. /**
  146. * Change the state of this quest to the specified value.
  147. * @param state the new state of the quest to set
  148. * @param saveInDb if {@code true}, will save the state change in the database
  149. * @return {@code true} if state was changed, {@code false} otherwise
  150. * @see com.l2jserver.gameserver.model.quest.State
  151. */
  152. public boolean setState(byte state, boolean saveInDb)
  153. {
  154. if (_state == state)
  155. {
  156. return false;
  157. }
  158. final boolean newQuest = isCreated();
  159. _state = state;
  160. if (saveInDb)
  161. {
  162. if (newQuest)
  163. {
  164. Quest.createQuestInDb(this);
  165. }
  166. else
  167. {
  168. Quest.updateQuestInDb(this);
  169. }
  170. }
  171. _player.sendPacket(new QuestList());
  172. return true;
  173. }
  174. /**
  175. * Add parameter used in quests.
  176. * @param var String pointing out the name of the variable for quest
  177. * @param val String pointing out the value of the variable for quest
  178. * @return String (equal to parameter "val")
  179. */
  180. public String setInternal(String var, String val)
  181. {
  182. if (_vars == null)
  183. {
  184. _vars = new HashMap<>();
  185. }
  186. if (val == null)
  187. {
  188. val = "";
  189. }
  190. _vars.put(var, val);
  191. return val;
  192. }
  193. /**
  194. * Return value of parameter "val" after adding the couple (var,val) in class variable "vars".<br>
  195. * Actions:<br>
  196. * <ul>
  197. * <li>Initialize class variable "vars" if is null.</li>
  198. * <li>Initialize parameter "val" if is null</li>
  199. * <li>Add/Update couple (var,val) in class variable FastMap "vars"</li>
  200. * <li>If the key represented by "var" exists in FastMap "vars", the couple (var,val) is updated in the database.<br>
  201. * The key is known as existing if the preceding value of the key (given as result of function put()) is not null.<br>
  202. * If the key doesn't exist, the couple is added/created in the database</li>
  203. * <ul>
  204. * @param var String indicating the name of the variable for quest
  205. * @param val String indicating the value of the variable for quest
  206. * @return String (equal to parameter "val")
  207. */
  208. public String set(String var, String val)
  209. {
  210. if (_vars == null)
  211. {
  212. _vars = new HashMap<>();
  213. }
  214. if (val == null)
  215. {
  216. val = "";
  217. }
  218. // FastMap.put() returns previous value associated with specified key, or null if there was no mapping for key.
  219. String old = _vars.put(var, val);
  220. if (old != null)
  221. {
  222. Quest.updateQuestVarInDb(this, var, val);
  223. }
  224. else
  225. {
  226. Quest.createQuestVarInDb(this, var, val);
  227. }
  228. if ("cond".equals(var))
  229. {
  230. try
  231. {
  232. int previousVal = 0;
  233. try
  234. {
  235. previousVal = Integer.parseInt(old);
  236. }
  237. catch (Exception ex)
  238. {
  239. previousVal = 0;
  240. }
  241. setCond(Integer.parseInt(val), previousVal);
  242. }
  243. catch (Exception e)
  244. {
  245. _log.log(Level.WARNING, _player.getName() + ", " + getQuestName() + " cond [" + val + "] is not an integer. Value stored, but no packet was sent: " + e.getMessage(), e);
  246. }
  247. }
  248. return val;
  249. }
  250. /**
  251. * Internally handles the progression of the quest so that it is ready for sending appropriate packets to the client.<br>
  252. * <u><i>Actions :</i></u><br>
  253. * <ul>
  254. * <li>Check if the new progress number resets the quest to a previous (smaller) step.</li>
  255. * <li>If not, check if quest progress steps have been skipped.</li>
  256. * <li>If skipped, prepare the variable completedStateFlags appropriately to be ready for sending to clients.</li>
  257. * <li>If no steps were skipped, flags do not need to be prepared...</li>
  258. * <li>If the passed step resets the quest to a previous step, reset such that steps after the parameter are not considered, while skipped steps before the parameter, if any, maintain their info.</li>
  259. * </ul>
  260. * @param cond the current quest progress condition (0 - 31 including)
  261. * @param old the previous quest progress condition to check against
  262. */
  263. private void setCond(int cond, int old)
  264. {
  265. if (cond == old)
  266. {
  267. return;
  268. }
  269. int completedStateFlags = 0;
  270. // cond 0 and 1 do not need completedStateFlags. Also, if cond > 1, the 1st step must
  271. // always exist (i.e. it can never be skipped). So if cond is 2, we can still safely
  272. // assume no steps have been skipped.
  273. // Finally, more than 31 steps CANNOT be supported in any way with skipping.
  274. if ((cond < 3) || (cond > 31))
  275. {
  276. unset("__compltdStateFlags");
  277. }
  278. else
  279. {
  280. completedStateFlags = getInt("__compltdStateFlags");
  281. }
  282. // case 1: No steps have been skipped so far...
  283. if (completedStateFlags == 0)
  284. {
  285. // check if this step also doesn't skip anything. If so, no further work is needed
  286. // also, in this case, no work is needed if the state is being reset to a smaller value
  287. // in those cases, skip forward to informing the client about the change...
  288. // ELSE, if we just now skipped for the first time...prepare the flags!!!
  289. if (cond > (old + 1))
  290. {
  291. // set the most significant bit to 1 (indicates that there exist skipped states)
  292. // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
  293. // what the cond says)
  294. completedStateFlags = 0x80000001;
  295. // since no flag had been skipped until now, the least significant bits must all
  296. // be set to 1, up until "old" number of bits.
  297. completedStateFlags |= ((1 << old) - 1);
  298. // now, just set the bit corresponding to the passed cond to 1 (current step)
  299. completedStateFlags |= (1 << (cond - 1));
  300. set("__compltdStateFlags", String.valueOf(completedStateFlags));
  301. }
  302. }
  303. // case 2: There were exist previously skipped steps
  304. // if this is a push back to a previous step, clear all completion flags ahead
  305. else if (cond < old)
  306. {
  307. // note, this also unsets the flag indicating that there exist skips
  308. completedStateFlags &= ((1 << cond) - 1);
  309. // now, check if this resulted in no steps being skipped any more
  310. if (completedStateFlags == ((1 << cond) - 1))
  311. {
  312. unset("__compltdStateFlags");
  313. }
  314. else
  315. {
  316. // set the most significant bit back to 1 again, to correctly indicate that this skips states.
  317. // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
  318. // what the cond says)
  319. completedStateFlags |= 0x80000001;
  320. set("__compltdStateFlags", String.valueOf(completedStateFlags));
  321. }
  322. }
  323. // If this moves forward, it changes nothing on previously skipped steps.
  324. // Just mark this state and we are done.
  325. else
  326. {
  327. completedStateFlags |= (1 << (cond - 1));
  328. set("__compltdStateFlags", String.valueOf(completedStateFlags));
  329. }
  330. // send a packet to the client to inform it of the quest progress (step change)
  331. _player.sendPacket(new QuestList());
  332. final Quest q = getQuest();
  333. if (!q.isCustomQuest() && (cond > 0))
  334. {
  335. _player.sendPacket(new ExShowQuestMark(q.getQuestIntId()));
  336. }
  337. }
  338. /**
  339. * Removes a quest variable from the list of existing quest variables.
  340. * @param var the name of the variable to remove
  341. * @return the previous value of the variable or {@code null} if none were found
  342. */
  343. public String unset(String var)
  344. {
  345. if (_vars == null)
  346. {
  347. return null;
  348. }
  349. String old = _vars.remove(var);
  350. if (old != null)
  351. {
  352. Quest.deleteQuestVarInDb(this, var);
  353. }
  354. return old;
  355. }
  356. /**
  357. * Insert (or update) in the database variables that need to stay persistent for this player after a reboot. This function is for storage of values that are not related to a specific quest but are global instead, i.e. can be used by any script.
  358. * @param var the name of the variable to save
  359. * @param value the value of the variable
  360. */
  361. // TODO: these methods should not be here, they could be used by other classes to save some variables, but they can't because they require to create a QuestState first.
  362. public final void saveGlobalQuestVar(String var, String value)
  363. {
  364. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  365. PreparedStatement statement = con.prepareStatement("REPLACE INTO character_quest_global_data (charId, var, value) VALUES (?, ?, ?)"))
  366. {
  367. statement.setInt(1, _player.getObjectId());
  368. statement.setString(2, var);
  369. statement.setString(3, value);
  370. statement.executeUpdate();
  371. }
  372. catch (Exception e)
  373. {
  374. _log.log(Level.WARNING, "Could not insert player's global quest variable: " + e.getMessage(), e);
  375. }
  376. }
  377. /**
  378. * Read from the database a previously saved variable for this quest.<br>
  379. * Due to performance considerations, this function should best be used only when the quest is first loaded.<br>
  380. * Subclasses of this class can define structures into which these loaded values can be saved.<br>
  381. * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.<br>
  382. * Values read from this function were entered by calls to "saveGlobalQuestVar".
  383. * @param var the name of the variable whose value to get
  384. * @return the value of the variable or an empty string if the variable does not exist in the database
  385. */
  386. // TODO: these methods should not be here, they could be used by other classes to save some variables, but they can't because they require to create a QuestState first.
  387. public final String getGlobalQuestVar(String var)
  388. {
  389. String result = "";
  390. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  391. PreparedStatement ps = con.prepareStatement("SELECT value FROM character_quest_global_data WHERE charId = ? AND var = ?"))
  392. {
  393. ps.setInt(1, _player.getObjectId());
  394. ps.setString(2, var);
  395. try (ResultSet rs = ps.executeQuery())
  396. {
  397. if (rs.first())
  398. {
  399. result = rs.getString(1);
  400. }
  401. }
  402. }
  403. catch (Exception e)
  404. {
  405. _log.log(Level.WARNING, "Could not load player's global quest variable: " + e.getMessage(), e);
  406. }
  407. return result;
  408. }
  409. /**
  410. * Permanently delete a global quest variable from the database.
  411. * @param var the name of the variable to delete
  412. */
  413. public final void deleteGlobalQuestVar(String var)
  414. {
  415. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  416. PreparedStatement statement = con.prepareStatement("DELETE FROM character_quest_global_data WHERE charId = ? AND var = ?"))
  417. {
  418. statement.setInt(1, _player.getObjectId());
  419. statement.setString(2, var);
  420. statement.executeUpdate();
  421. }
  422. catch (Exception e)
  423. {
  424. _log.log(Level.WARNING, "could not delete player's global quest variable; charId = " + _player.getObjectId() + ", variable name = " + var + ". Exception: " + e.getMessage(), e);
  425. }
  426. }
  427. /**
  428. * @param var the name of the variable to get
  429. * @return the value of the variable from the list of quest variables
  430. */
  431. public String get(String var)
  432. {
  433. if (_vars == null)
  434. {
  435. return null;
  436. }
  437. return _vars.get(var);
  438. }
  439. /**
  440. * @param var the name of the variable to get
  441. * @return the integer value of the variable or 0 if the variable does not exist or its value is not an integer
  442. */
  443. public int getInt(String var)
  444. {
  445. if (_vars == null)
  446. {
  447. return 0;
  448. }
  449. final String variable = _vars.get(var);
  450. if ((variable == null) || (variable.length() == 0))
  451. {
  452. return 0;
  453. }
  454. int varint = 0;
  455. try
  456. {
  457. varint = Integer.parseInt(variable);
  458. }
  459. catch (NumberFormatException nfe)
  460. {
  461. _log.log(Level.INFO, "Quest " + getQuestName() + ", method getInt(" + var + "), tried to parse a non-integer value (" + variable + "). Char Id: " + _player.getObjectId(), nfe);
  462. }
  463. return varint;
  464. }
  465. /**
  466. * Checks if the quest state progress ({@code cond}) is at the specified step.
  467. * @param condition the condition to check against
  468. * @return {@code true} if the quest condition is equal to {@code condition}, {@code false} otherwise
  469. * @see #getInt(String var)
  470. */
  471. public boolean isCond(int condition)
  472. {
  473. return (getInt("cond") == condition);
  474. }
  475. /**
  476. * Sets the quest state progress ({@code cond}) to the specified step.
  477. * @param value the new value of the quest state progress
  478. * @return this {@link QuestState} object
  479. * @see #set(String var, String val)
  480. * @see #setCond(int, boolean)
  481. */
  482. public QuestState setCond(int value)
  483. {
  484. if (isStarted())
  485. {
  486. set("cond", String.valueOf(value));
  487. }
  488. return this;
  489. }
  490. /**
  491. * @return the current quest progress ({@code cond})
  492. */
  493. public int getCond()
  494. {
  495. if (isStarted())
  496. {
  497. return getInt("cond");
  498. }
  499. return 0;
  500. }
  501. /**
  502. * Check if a given variable is set for this quest.
  503. * @param variable the variable to check
  504. * @return {@code true} if the variable is set, {@code false} otherwise
  505. * @see #get(String)
  506. * @see #getInt(String)
  507. * @see #getCond()
  508. */
  509. public boolean isSet(String variable)
  510. {
  511. return (get(variable) != null);
  512. }
  513. /**
  514. * Sets the quest state progress ({@code cond}) to the specified step.
  515. * @param value the new value of the quest state progress
  516. * @param playQuestMiddle if {@code true}, plays "ItemSound.quest_middle"
  517. * @return this {@link QuestState} object
  518. * @see #setCond(int value)
  519. * @see #set(String var, String val)
  520. */
  521. public QuestState setCond(int value, boolean playQuestMiddle)
  522. {
  523. if (!isStarted())
  524. {
  525. return this;
  526. }
  527. set("cond", String.valueOf(value));
  528. if (playQuestMiddle)
  529. {
  530. playSound(QuestSound.ITEMSOUND_QUEST_MIDDLE);
  531. }
  532. return this;
  533. }
  534. /**
  535. * Add player to get notification of characters death
  536. * @param character the {@link L2Character} object of the character to get notification of death
  537. */
  538. public void addNotifyOfDeath(L2Character character)
  539. {
  540. if (!(character instanceof L2PcInstance))
  541. {
  542. return;
  543. }
  544. ((L2PcInstance) character).addNotifyQuestOfDeath(this);
  545. }
  546. // TODO: This all remains because of backward compatibility, should be cleared when all scripts are rewritten in java
  547. /**
  548. * Return the quantity of one sort of item hold by the player
  549. * @param itemId the Id of the item wanted to be count
  550. * @return long
  551. */
  552. public long getQuestItemsCount(int itemId)
  553. {
  554. return getQuest().getQuestItemsCount(getPlayer(), itemId);
  555. }
  556. /**
  557. * @param itemId the Id of the item required
  558. * @return true if item exists in player's inventory, false - if not
  559. */
  560. public boolean hasQuestItems(int itemId)
  561. {
  562. return getQuest().hasQuestItems(getPlayer(), itemId);
  563. }
  564. /**
  565. * Return the level of enchantment on the weapon of the player(Done specifically for weapon SA's)
  566. * @param itemId Id of the item to check enchantment
  567. * @return int
  568. */
  569. public int getEnchantLevel(int itemId)
  570. {
  571. return getQuest().getEnchantLevel(getPlayer(), itemId);
  572. }
  573. /**
  574. * Give adena to the player
  575. * @param count
  576. * @param applyRates
  577. */
  578. public void giveAdena(long count, boolean applyRates)
  579. {
  580. giveItems(PcInventory.ADENA_ID, count, applyRates ? 0 : 1);
  581. }
  582. /**
  583. * Give reward to player using multiplier's
  584. * @param itemId
  585. * @param count
  586. */
  587. public void rewardItems(int itemId, long count)
  588. {
  589. getQuest().rewardItems(getPlayer(), itemId, count);
  590. }
  591. /**
  592. * Give item/reward to the player
  593. * @param itemId
  594. * @param count
  595. */
  596. public void giveItems(int itemId, long count)
  597. {
  598. giveItems(itemId, count, 0);
  599. }
  600. public void giveItems(int itemId, long count, int enchantlevel)
  601. {
  602. getQuest().giveItems(getPlayer(), itemId, count, enchantlevel);
  603. }
  604. public void giveItems(int itemId, long count, byte attributeId, int attributeLevel)
  605. {
  606. getQuest().giveItems(getPlayer(), itemId, count, attributeId, attributeLevel);
  607. }
  608. /**
  609. * Drop Quest item using Config.RATE_QUEST_DROP
  610. * @param itemId int Item Identifier of the item to be dropped
  611. * @param count (minCount, maxCount) long Quantity of items to be dropped
  612. * @param neededCount Quantity of items needed for quest
  613. * @param dropChance int Base chance of drop, same as in droplist
  614. * @param sound boolean indicating whether to play sound
  615. * @return boolean indicating whether player has requested number of items
  616. */
  617. public boolean dropQuestItems(int itemId, int count, long neededCount, int dropChance, boolean sound)
  618. {
  619. return dropQuestItems(itemId, count, count, neededCount, dropChance, sound);
  620. }
  621. public boolean dropQuestItems(int itemId, int minCount, int maxCount, long neededCount, int dropChance, boolean sound)
  622. {
  623. return getQuest().dropQuestItems(getPlayer(), itemId, minCount, maxCount, neededCount, dropChance, sound);
  624. }
  625. // TODO: More radar functions need to be added when the radar class is complete.
  626. // BEGIN STUFF THAT WILL PROBABLY BE CHANGED
  627. public void addRadar(int x, int y, int z)
  628. {
  629. _player.getRadar().addMarker(x, y, z);
  630. }
  631. public void removeRadar(int x, int y, int z)
  632. {
  633. _player.getRadar().removeMarker(x, y, z);
  634. }
  635. public void clearRadar()
  636. {
  637. _player.getRadar().removeAllMarkers();
  638. }
  639. // END STUFF THAT WILL PROBABLY BE CHANGED
  640. /**
  641. * Remove items from player's inventory when talking to NPC in order to have rewards.<br>
  642. * Actions:<br>
  643. * <ul>
  644. * <li>Destroy quantity of items wanted</li>
  645. * <li>Send new inventory list to player</li>
  646. * </ul>
  647. * @param itemId Identifier of the item
  648. * @param count Quantity of items to destroy
  649. */
  650. public void takeItems(int itemId, long count)
  651. {
  652. getQuest().takeItems(getPlayer(), itemId, count);
  653. }
  654. /**
  655. * Send a packet in order to play a sound to the player.
  656. * @param sound the name of the sound to play
  657. */
  658. public void playSound(String sound)
  659. {
  660. getQuest().playSound(getPlayer(), sound);
  661. }
  662. /**
  663. * Send a packet in order to play a sound to the player.
  664. * @param sound the {@link QuestSound} object of the sound to play
  665. */
  666. public void playSound(QuestSound sound)
  667. {
  668. getQuest().playSound(getPlayer(), sound);
  669. }
  670. /**
  671. * Add XP and SP as quest reward
  672. * @param exp
  673. * @param sp
  674. */
  675. public void addExpAndSp(int exp, int sp)
  676. {
  677. getQuest().addExpAndSp(getPlayer(), exp, sp);
  678. }
  679. /**
  680. * @param loc
  681. * @return number of ticks from GameTimeController
  682. */
  683. public int getItemEquipped(int loc)
  684. {
  685. return getQuest().getItemEquipped(getPlayer(), loc);
  686. }
  687. /**
  688. * Return the number of ticks from the GameTimeController
  689. * @return int
  690. */
  691. public int getGameTicks()
  692. {
  693. return getQuest().getGameTicks();
  694. }
  695. /**
  696. * @return {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
  697. */
  698. public final boolean isExitQuestOnCleanUp()
  699. {
  700. return _isExitQuestOnCleanUp;
  701. }
  702. /**
  703. * @param isExitQuestOnCleanUp {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
  704. */
  705. public void setIsExitQuestOnCleanUp(boolean isExitQuestOnCleanUp)
  706. {
  707. _isExitQuestOnCleanUp = isExitQuestOnCleanUp;
  708. }
  709. /**
  710. * Start a timed event for a quest.<br>
  711. * Will call an event in onEvent/onAdvEvent.
  712. * @param name the name of the timer/event
  713. * @param time time in milliseconds till the event is executed
  714. */
  715. public void startQuestTimer(String name, long time)
  716. {
  717. getQuest().startQuestTimer(name, time, null, getPlayer(), false);
  718. }
  719. /**
  720. * Start a timed event for a quest.<br>
  721. * Will call an event in onEvent/onAdvEvent.
  722. * @param name the name of the timer/event
  723. * @param time time in milliseconds till the event is executed
  724. * @param npc the L2Npc associated with this event
  725. */
  726. public void startQuestTimer(String name, long time, L2Npc npc)
  727. {
  728. getQuest().startQuestTimer(name, time, npc, getPlayer(), false);
  729. }
  730. /**
  731. * Start a repeating timed event for a quest.<br>
  732. * Will call an event in onEvent/onAdvEvent.
  733. * @param name the name of the timer/event
  734. * @param time time in milliseconds till the event is executed/repeated
  735. */
  736. public void startRepeatingQuestTimer(String name, long time)
  737. {
  738. getQuest().startQuestTimer(name, time, null, getPlayer(), true);
  739. }
  740. /**
  741. * Start a repeating timed event for a quest.<br>
  742. * Will call an event in onEvent/onAdvEvent.
  743. * @param name the name of the timer/event
  744. * @param time time in milliseconds till the event is executed/repeated
  745. * @param npc the L2Npc associated with this event
  746. */
  747. public void startRepeatingQuestTimer(String name, long time, L2Npc npc)
  748. {
  749. getQuest().startQuestTimer(name, time, npc, getPlayer(), true);
  750. }
  751. /**
  752. * @param name the name of the QuestTimer required
  753. * @return the {@link QuestTimer} object with the specified name or {@code null} if it doesn't exist
  754. */
  755. public final QuestTimer getQuestTimer(String name)
  756. {
  757. return getQuest().getQuestTimer(name, null, getPlayer());
  758. }
  759. // --- Spawn methods ---
  760. /**
  761. * Add a temporary spawn of the specified npc.<br>
  762. * Player's coordinates will be used for the spawn.
  763. * @param npcId the Id of the npc to spawn
  764. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  765. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  766. */
  767. public L2Npc addSpawn(int npcId)
  768. {
  769. return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, 0, false);
  770. }
  771. /**
  772. * Add a temporary spawn of the specified npc.<br>
  773. * Player's coordinates will be used for the spawn.
  774. * @param npcId the Id of the npc to spawn
  775. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  776. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  777. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  778. */
  779. public L2Npc addSpawn(int npcId, int despawnDelay)
  780. {
  781. return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, despawnDelay, false);
  782. }
  783. /**
  784. * Add a temporary spawn of the specified npc.
  785. * @param npcId the Id of the npc to spawn
  786. * @param x the X coordinate of the npc spawn location
  787. * @param y the Y coordinate of the npc spawn location
  788. * @param z the Z coordinate (height) of the npc spawn location
  789. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  790. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  791. */
  792. public L2Npc addSpawn(int npcId, int x, int y, int z)
  793. {
  794. return addSpawn(npcId, x, y, z, 0, false, 0, false);
  795. }
  796. /**
  797. * Add a temporary spawn of the specified npc.
  798. * @param npcId the Id of the npc to spawn
  799. * @param x the X coordinate of the npc spawn location
  800. * @param y the Y coordinate of the npc spawn location
  801. * @param z the Z coordinate (height) of the npc spawn location
  802. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  803. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  804. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  805. */
  806. public L2Npc addSpawn(int npcId, int x, int y, int z, int despawnDelay)
  807. {
  808. return addSpawn(npcId, x, y, z, 0, false, despawnDelay, false);
  809. }
  810. /**
  811. * Add a temporary spawn of the specified npc.
  812. * @param npcId the Id of the npc to spawn
  813. * @param cha the character whose coordinates will be used for the npc spawn
  814. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  815. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  816. */
  817. public L2Npc addSpawn(int npcId, L2Character cha)
  818. {
  819. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, 0, false);
  820. }
  821. /**
  822. * Add a temporary spawn of the specified npc.
  823. * @param npcId the Id of the npc to spawn
  824. * @param cha the character whose coordinates will be used for the npc spawn
  825. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  826. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  827. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  828. */
  829. public L2Npc addSpawn(int npcId, L2Character cha, int despawnDelay)
  830. {
  831. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, despawnDelay, false);
  832. }
  833. /**
  834. * Add a temporary spawn of the specified npc.
  835. * @param npcId the Id of the npc to spawn
  836. * @param cha the character whose coordinates will be used for the npc spawn
  837. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  838. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  839. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  840. * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  841. */
  842. public L2Npc addSpawn(int npcId, L2Character cha, boolean randomOffset, int despawnDelay)
  843. {
  844. return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), randomOffset, despawnDelay, false);
  845. }
  846. /**
  847. * Add a temporary spawn of the specified npc.
  848. * @param npcId the Id of the npc to spawn
  849. * @param x the X coordinate of the npc spawn location
  850. * @param y the Y coordinate of the npc spawn location
  851. * @param z the Z coordinate (height) of the npc spawn location
  852. * @param heading the heading of the npc
  853. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  854. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  855. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  856. * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
  857. */
  858. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay)
  859. {
  860. return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, false);
  861. }
  862. /**
  863. * Add a temporary spawn of the specified npc.
  864. * @param npcId the Id of the npc to spawn
  865. * @param x the X coordinate of the npc spawn location
  866. * @param y the Y coordinate of the npc spawn location
  867. * @param z the Z coordinate (height) of the npc spawn location
  868. * @param heading the heading of the npc
  869. * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
  870. * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
  871. * @param isSummonSpawn if {@code true}, displays a summon animation on npc spawn (default: {@code false})
  872. * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
  873. * @see #addSpawn(int)
  874. * @see #addSpawn(int, int)
  875. * @see #addSpawn(int, L2Character)
  876. * @see #addSpawn(int, L2Character, int)
  877. * @see #addSpawn(int, int, int, int)
  878. * @see #addSpawn(int, L2Character, boolean, int)
  879. * @see #addSpawn(int, int, int, int, int)
  880. * @see #addSpawn(int, int, int, int, int, boolean, int)
  881. * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
  882. */
  883. public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
  884. {
  885. return getQuest().addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn);
  886. }
  887. /**
  888. * @param fileName the name of the file you want to show. Must be in the same folder (or subfolder) as script
  889. * @return a String containing the contents of the specified HTML file
  890. */
  891. public String showHtmlFile(String fileName)
  892. {
  893. return getQuest().showHtmlFile(getPlayer(), fileName);
  894. }
  895. /**
  896. * Set condition to 1, state to STARTED and play the "ItemSound.quest_accept".<br>
  897. * Works only if state is CREATED and the quest is not a custom quest.
  898. * @return the newly created {@code QuestState} object
  899. */
  900. public QuestState startQuest()
  901. {
  902. if (isCreated() && !getQuest().isCustomQuest())
  903. {
  904. set("cond", "1");
  905. setState(State.STARTED);
  906. playSound(QuestSound.ITEMSOUND_QUEST_ACCEPT);
  907. }
  908. return this;
  909. }
  910. /**
  911. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  912. * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
  913. * @param type the {@link QuestType} of the quest
  914. * @return this {@link QuestState} object
  915. * @see #exitQuest(QuestType type, boolean playExitQuest)
  916. * @see #exitQuest(boolean repeatable)
  917. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  918. */
  919. public QuestState exitQuest(QuestType type)
  920. {
  921. switch (type)
  922. {
  923. case DAILY:
  924. {
  925. exitQuest(false);
  926. setRestartTime();
  927. break;
  928. }
  929. // case ONE_TIME:
  930. // case REPEATABLE:
  931. default:
  932. {
  933. exitQuest(type == QuestType.REPEATABLE);
  934. break;
  935. }
  936. }
  937. return this;
  938. }
  939. /**
  940. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  941. * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
  942. * @param type the {@link QuestType} of the quest
  943. * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
  944. * @return this {@link QuestState} object
  945. * @see #exitQuest(QuestType type)
  946. * @see #exitQuest(boolean repeatable)
  947. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  948. */
  949. public QuestState exitQuest(QuestType type, boolean playExitQuest)
  950. {
  951. exitQuest(type);
  952. if (playExitQuest)
  953. {
  954. playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
  955. }
  956. return this;
  957. }
  958. /**
  959. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  960. * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
  961. * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
  962. * @return this {@link QuestState} object
  963. * @see #exitQuest(QuestType type)
  964. * @see #exitQuest(QuestType type, boolean playExitQuest)
  965. * @see #exitQuest(boolean repeatable, boolean playExitQuest)
  966. */
  967. public QuestState exitQuest(boolean repeatable)
  968. {
  969. _player.removeNotifyQuestOfDeath(this);
  970. if (!isStarted())
  971. {
  972. return this;
  973. }
  974. // Clean registered quest items
  975. getQuest().removeRegisteredQuestItems(_player);
  976. Quest.deleteQuestInDb(this, repeatable);
  977. if (repeatable)
  978. {
  979. _player.delQuestState(getQuestName());
  980. }
  981. else
  982. {
  983. setState(State.COMPLETED);
  984. }
  985. _vars = null;
  986. return this;
  987. }
  988. /**
  989. * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
  990. * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
  991. * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
  992. * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
  993. * @return this {@link QuestState} object
  994. * @see #exitQuest(QuestType type)
  995. * @see #exitQuest(QuestType type, boolean playExitQuest)
  996. * @see #exitQuest(boolean repeatable)
  997. */
  998. public QuestState exitQuest(boolean repeatable, boolean playExitQuest)
  999. {
  1000. exitQuest(repeatable);
  1001. if (playExitQuest)
  1002. {
  1003. playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
  1004. }
  1005. return this;
  1006. }
  1007. public void showQuestionMark(int number)
  1008. {
  1009. _player.sendPacket(new TutorialShowQuestionMark(number));
  1010. }
  1011. // TODO make tutorial voices the same as quest sounds
  1012. public void playTutorialVoice(String voice)
  1013. {
  1014. _player.sendPacket(new PlaySound(2, voice, 0, 0, _player.getX(), _player.getY(), _player.getZ()));
  1015. }
  1016. public void showTutorialHTML(String html)
  1017. {
  1018. String text = HtmCache.getInstance().getHtm(_player.getHtmlPrefix(), "data/scripts/quests/255_Tutorial/" + html);
  1019. if (text == null)
  1020. {
  1021. _log.warning("missing html page data/scripts/quests/255_Tutorial/" + html);
  1022. text = "<html><body>File data/scripts/quests/255_Tutorial/" + html + " not found or file is empty.</body></html>";
  1023. }
  1024. _player.sendPacket(new TutorialShowHtml(text));
  1025. }
  1026. public void closeTutorialHtml()
  1027. {
  1028. _player.sendPacket(TutorialCloseHtml.STATIC_PACKET);
  1029. }
  1030. public void onTutorialClientEvent(int number)
  1031. {
  1032. _player.sendPacket(new TutorialEnableClientEvent(number));
  1033. }
  1034. public void dropItem(L2MonsterInstance npc, L2PcInstance player, int itemId, int count)
  1035. {
  1036. npc.dropItem(player, itemId, count);
  1037. }
  1038. /**
  1039. * Set the restart time for the daily quests.<br>
  1040. * The time is hardcoded at {@link Quest#getResetHour()} hours, {@link Quest#getResetMinutes()} minutes of the following day.<br>
  1041. * It can be overridden in scripts (quests).
  1042. */
  1043. public void setRestartTime()
  1044. {
  1045. final Calendar reDo = Calendar.getInstance();
  1046. if (reDo.get(Calendar.HOUR_OF_DAY) >= getQuest().getResetHour())
  1047. {
  1048. reDo.add(Calendar.DATE, 1);
  1049. }
  1050. reDo.set(Calendar.HOUR_OF_DAY, getQuest().getResetHour());
  1051. reDo.set(Calendar.MINUTE, getQuest().getResetMinutes());
  1052. set("restartTime", String.valueOf(reDo.getTimeInMillis()));
  1053. }
  1054. /**
  1055. * Check if a daily quest is available to be started over.
  1056. * @return {@code true} if the quest is available, {@code false} otherwise.
  1057. */
  1058. public boolean isNowAvailable()
  1059. {
  1060. final String val = get("restartTime");
  1061. return ((val == null) || !Util.isDigit(val)) || (Long.parseLong(val) <= System.currentTimeMillis());
  1062. }
  1063. }