QuestState.java 38 KB

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