QuestState.java 35 KB

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