QuestState.java 35 KB

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