123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059 |
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.model.quest;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import com.l2jserver.Config;
- import com.l2jserver.L2DatabaseFactory;
- import com.l2jserver.gameserver.GameTimeController;
- import com.l2jserver.gameserver.cache.HtmCache;
- import com.l2jserver.gameserver.instancemanager.QuestManager;
- import com.l2jserver.gameserver.model.L2DropData;
- import com.l2jserver.gameserver.model.L2ItemInstance;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.model.actor.L2Npc;
- import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.network.SystemMessageId;
- import com.l2jserver.gameserver.network.serverpackets.ExShowQuestMark;
- import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
- import com.l2jserver.gameserver.network.serverpackets.PlaySound;
- import com.l2jserver.gameserver.network.serverpackets.QuestList;
- import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
- import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
- import com.l2jserver.gameserver.network.serverpackets.TutorialCloseHtml;
- import com.l2jserver.gameserver.network.serverpackets.TutorialEnableClientEvent;
- import com.l2jserver.gameserver.network.serverpackets.TutorialShowHtml;
- import com.l2jserver.gameserver.network.serverpackets.TutorialShowQuestionMark;
- import com.l2jserver.gameserver.skills.Stats;
- import com.l2jserver.util.Rnd;
- import javolution.util.FastMap;
- /**
- * @author Luis Arias
- */
- public final class QuestState
- {
- protected static final Logger _log = Logger.getLogger(Quest.class.getName());
- /** Quest associated to the QuestState */
- private final String _questName;
- /** Player who engaged the quest */
- private final L2PcInstance _player;
- /** State of the quest */
- private byte _state;
- /** List of couples (variable for quest,value of the variable for quest) */
- private Map<String, String> _vars;
- /** Boolean flag letting QuestStateManager know to exit quest when cleaning up */
- private boolean _isExitQuestOnCleanUp = false;
- /**
- * Constructor of the QuestState : save the quest in the list of quests of the player.<BR/><BR/>
- *
- * <U><I>Actions :</U></I><BR/>
- * <LI>Save informations in the object QuestState created (Quest, Player, Completion, State)</LI>
- * <LI>Add the QuestState in the player's list of quests by using setQuestState()</LI>
- * <LI>Add drops gotten by the quest</LI>
- * <BR/>
- * @param quest : quest associated with the QuestState
- * @param player : L2PcInstance pointing out the player
- * @param state : state of the quest
- * @param completed : boolean for completion of the quest
- */
- QuestState(Quest quest, L2PcInstance player, byte state)
- {
- _questName = quest.getName();
- _player = player;
-
- // Save the state of the quest for the player in the player's list of quest onwed
- getPlayer().setQuestState(this);
-
- // set the state of the quest
- _state = state;
- }
- public String getQuestName()
- {
- return _questName;
- }
- /**
- * Return the quest
- * @return Quest
- */
- public Quest getQuest()
- {
- return QuestManager.getInstance().getQuest(_questName);
- }
- /**
- * Return the L2PcInstance
- * @return L2PcInstance
- */
- public L2PcInstance getPlayer()
- {
- return _player;
- }
- /**
- * Return the state of the quest
- * @return State
- */
- public byte getState()
- {
- return _state;
- }
- /**
- * Return true if quest completed, false otherwise
- * @return boolean
- */
- public boolean isCompleted()
- {
- return (getState() == State.COMPLETED);
- }
- /**
- * Return true if quest started, false otherwise
- * @return boolean
- */
- public boolean isStarted()
- {
- return (getState() == State.STARTED);
- }
- /**
- * Return state of the quest after its initialization.<BR><BR>
- * <U><I>Actions :</I></U>
- * <LI>Remove drops from previous state</LI>
- * <LI>Set new state of the quest</LI>
- * <LI>Add drop for new state</LI>
- * <LI>Update information in database</LI>
- * <LI>Send packet QuestList to client</LI>
- * @param state
- * @return object
- */
- public Object setState(byte state)
- {
- // set new state if it is not already in that state
- if (_state != state)
- {
- _state = state;
-
- Quest.updateQuestInDb(this);
- QuestList ql = new QuestList();
-
- getPlayer().sendPacket(ql);
- }
- return state;
- }
- /**
- * Add parameter used in quests.
- * @param var : String pointing out the name of the variable for quest
- * @param val : String pointing out the value of the variable for quest
- * @return String (equal to parameter "val")
- */
- public String setInternal(String var, String val)
- {
- if (_vars == null)
- _vars = new FastMap<String, String>();
-
- if (val == null)
- val = "";
-
- _vars.put(var, val);
- return val;
- }
- /**
- * Return value of parameter "val" after adding the couple (var,val) in class variable "vars".<BR><BR>
- * <U><I>Actions :</I></U><BR>
- * <LI>Initialize class variable "vars" if is null</LI>
- * <LI>Initialize parameter "val" if is null</LI>
- * <LI>Add/Update couple (var,val) in class variable FastMap "vars"</LI>
- * <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
- * existing if the preceding value of the key (given as result of function put()) is not null.<BR>
- * If the key doesn't exist, the couple is added/created in the database</LI>
- * @param var : String indicating the name of the variable for quest
- * @param val : String indicating the value of the variable for quest
- * @return String (equal to parameter "val")
- */
- public String set(String var, String val)
- {
- if (_vars == null)
- _vars = new FastMap<String, String>();
- if (val == null)
- val = "";
- // FastMap.put() returns previous value associated with specified key, or null if there was no mapping for key.
- String old = _vars.put(var, val);
- if (old != null)
- Quest.updateQuestVarInDb(this, var, val);
- else
- Quest.createQuestVarInDb(this, var, val);
- if ("cond".equals(var))
- {
- try
- {
- int previousVal = 0;
- try
- {
- previousVal = Integer.parseInt(old);
- }
- catch (Exception ex)
- {
- previousVal = 0;
- }
- setCond(Integer.parseInt(val), previousVal);
- }
- catch (Exception e)
- {
- _log.finer(getPlayer().getName() + ", " + getQuestName() + " cond [" + val + "] is not an integer. Value stored, but no packet was sent: " + e);
- }
- }
- return val;
- }
- /**
- * Internally handles the progression of the quest so that it is ready for sending
- * appropriate packets to the client<BR><BR>
- * <U><I>Actions :</I></U><BR>
- * <LI>Check if the new progress number resets the quest to a previous (smaller) step</LI>
- * <LI>If not, check if quest progress steps have been skipped</LI>
- * <LI>If skipped, prepare the variable completedStateFlags appropriately to be ready for sending to clients</LI>
- * <LI>If no steps were skipped, flags do not need to be prepared...</LI>
- * <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>
- * @param cond : int indicating the step number for the current quest progress (as will be shown to the client)
- * @param old : int indicating the previously noted step
- *
- * For more info on the variable communicating the progress steps to the client, please see
- * @link com.l2jserver.loginserver.serverpacket.QuestList
- */
- private void setCond(int cond, int old)
- {
- int completedStateFlags = 0; // initializing...
- // if there is no change since last setting, there is nothing to do here
- if (cond == old)
- return;
- // cond 0 and 1 do not need completedStateFlags. Also, if cond > 1, the 1st step must
- // always exist (i.e. it can never be skipped). So if cond is 2, we can still safely
- // assume no steps have been skipped.
- // Finally, more than 31 steps CANNOT be supported in any way with skipping.
- if (cond < 3 || cond > 31)
- {
- unset("__compltdStateFlags");
- }
- else
- completedStateFlags = getInt("__compltdStateFlags");
- // case 1: No steps have been skipped so far...
- if (completedStateFlags == 0)
- {
- // check if this step also doesn't skip anything. If so, no further work is needed
- // also, in this case, no work is needed if the state is being reset to a smaller value
- // in those cases, skip forward to informing the client about the change...
- // ELSE, if we just now skipped for the first time...prepare the flags!!!
- if (cond > (old + 1))
- {
- // set the most significant bit to 1 (indicates that there exist skipped states)
- // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
- // what the cond says)
- completedStateFlags = 0x80000001;
- // since no flag had been skipped until now, the least significant bits must all
- // be set to 1, up until "old" number of bits.
- completedStateFlags |= ((1 << old) - 1);
- // now, just set the bit corresponding to the passed cond to 1 (current step)
- completedStateFlags |= (1 << (cond - 1));
- set("__compltdStateFlags", String.valueOf(completedStateFlags));
- }
- }
- // case 2: There were exist previously skipped steps
- else
- {
- // if this is a push back to a previous step, clear all completion flags ahead
- if (cond < old)
- {
- completedStateFlags &= ((1 << cond) - 1); // note, this also unsets the flag indicating that there exist skips
- //now, check if this resulted in no steps being skipped any more
- if (completedStateFlags == ((1 << cond) - 1))
- unset("__compltdStateFlags");
- else
- {
- // set the most significant bit back to 1 again, to correctly indicate that this skips states.
- // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
- // what the cond says)
- completedStateFlags |= 0x80000001;
- set("__compltdStateFlags", String.valueOf(completedStateFlags));
- }
- }
- // if this moves forward, it changes nothing on previously skipped steps...so just mark this
- // state and we are done
- else
- {
- completedStateFlags |= (1 << (cond - 1));
- set("__compltdStateFlags", String.valueOf(completedStateFlags));
- }
- }
- // send a packet to the client to inform it of the quest progress (step change)
- QuestList ql = new QuestList();
- getPlayer().sendPacket(ql);
- int questId = getQuest().getQuestIntId();
- if (questId > 0 && questId < 19999 && cond > 0)
- getPlayer().sendPacket(new ExShowQuestMark(questId));
- }
- /**
- * Remove the variable of quest from the list of variables for the quest.<BR><BR>
- * <U><I>Concept : </I></U>
- * Remove the variable of quest represented by "var" from the class variable FastMap "vars" and from the database.
- * @param var : String designating the variable for the quest to be deleted
- * @return String pointing out the previous value associated with the variable "var"
- */
- public String unset(String var)
- {
- if (_vars == null)
- return null;
- String old = _vars.remove(var);
- if (old != null)
- Quest.deleteQuestVarInDb(this, var);
- return old;
- }
- /**
- * Insert (or Update) in the database variables that need to stay persistant for this player after a reboot.
- * This function is for storage of values that do not related to a specific quest but are
- * global for all quests. For example, player's can get only once the adena and XP reward for
- * the first class quests, but they can make more than one first class quest.
- * @param var : String designating the name of the variable for the quest
- * @param value : String designating the value of the variable for the quest
- */
- public final void saveGlobalQuestVar(String var, String value)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
- statement = con.prepareStatement("REPLACE INTO character_quest_global_data (charId,var,value) VALUES (?,?,?)");
- statement.setInt(1, _player.getObjectId());
- statement.setString(2, var);
- statement.setString(3, value);
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not insert player's global quest variable:", e);
- }
- finally
- {
- try
- {
- con.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- /**
- * Read from the database a previously saved variable for this quest.
- * Due to performance considerations, this function should best be used only when the quest is first loaded.
- * Subclasses of this class can define structures into which these loaded values can be saved.
- * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.
- * Values read from this function were entered by calls to "saveGlobalQuestVar"
- * @param var : String designating the name of the variable for the quest
- * @return String : String representing the loaded value for the passed var, or an empty string if the var was invalid
- */
- public final String getGlobalQuestVar(String var)
- {
- String result = "";
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
- statement = con.prepareStatement("SELECT value FROM character_quest_global_data WHERE charId = ? AND var = ?");
- statement.setInt(1, _player.getObjectId());
- statement.setString(2, var);
- ResultSet rs = statement.executeQuery();
- if (rs.first())
- result = rs.getString(1);
- rs.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not load player's global quest variable:", e);
- }
- finally
- {
- try
- {
- con.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- return result;
- }
- /**
- * Permanently delete from the database one of the player's global quest variable that was previously saved.
- * @param var : String designating the name of the variable
- */
- public final void deleteGlobalQuestVar(String var)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
- statement = con.prepareStatement("DELETE FROM character_quest_global_data WHERE charId = ? AND var = ?");
- statement.setInt(1, _player.getObjectId());
- statement.setString(2, var);
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not delete player's global quest variable:", e);
- }
- finally
- {
- try
- {
- con.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- /**
- * Return the value of the variable of quest represented by "var"
- * @param var : name of the variable of quest
- * @return Object
- */
- public Object get(String var)
- {
- if (_vars == null)
- return null;
- return _vars.get(var);
- }
- /**
- * Return the value of the variable of quest represented by "var"
- * @param var : String designating the variable for the quest
- * @return int
- */
- public int getInt(String var)
- {
- int varint = 0;
-
- try
- {
- varint = Integer.parseInt(_vars.get(var));
- }
- catch (Exception e)
- {
- _log.finer(getPlayer().getName() + ": variable " + var + " isn't an integer: " + varint + e);
- // if (Config.AUTODELETE_INVALID_QUEST_DATA)
- // exitQuest(true);
- }
- return varint;
- }
-
- /**
- * Add player to get notification of characters death
- * @param character : L2Character of the character to get notification of death
- */
- public void addNotifyOfDeath(L2Character character)
- {
- if (character == null || !(character instanceof L2PcInstance))
- return;
- ((L2PcInstance)character).addNotifyQuestOfDeath(this);
- }
- /**
- * Return the quantity of one sort of item hold by the player
- * @param itemId : ID of the item wanted to be count
- * @return long
- */
- public long getQuestItemsCount(int itemId)
- {
- long count = 0;
- for (L2ItemInstance item : getPlayer().getInventory().getItems())
- if (item != null && item.getItemId() == itemId)
- count += item.getCount();
- return count;
- }
- /**
- * Return the level of enchantment on the weapon of the player(Done specifically for weapon SA's)
- * @param itemId : ID of the item to check enchantment
- * @return int
- */
- public int getEnchantLevel(int itemId)
- {
- L2ItemInstance enchanteditem = getPlayer().getInventory().getItemByItemId(itemId);
- if (enchanteditem == null)
- return 0;
- return enchanteditem.getEnchantLevel();
- }
- /**
- * Give adena to the player
- * @param count
- * @param applyRates
- */
- public void giveAdena(long count, boolean applyRates)
- {
- giveItems(57, count, applyRates ? 0 : 1);
- }
- /**
- * Give item/reward to the player
- * @param itemId
- * @param count
- */
- public void giveItems(int itemId, long count)
- {
- giveItems(itemId, count, 0);
- }
- public void giveItems(int itemId, long count, int enchantlevel)
- {
- if (count <= 0)
- return;
- // If item for reward is adena (ID=57), modify count with rate for quest reward if rates available
- if (itemId == 57 && !(enchantlevel > 0))
- count = (long) (count * Config.RATE_QUESTS_REWARD);
- // Add items to player's inventory
- L2ItemInstance item = getPlayer().getInventory().addItem("Quest", itemId, count, getPlayer(), getPlayer().getTarget());
- if (item == null)
- return;
- // set enchant level for item if that item is not adena
- if (enchantlevel > 0 && itemId != 57)
- item.setEnchantLevel(enchantlevel);
- // If item for reward is gold, send message of gold reward to client
- if (itemId == 57)
- {
- SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_ADENA);
- smsg.addItemNumber(count);
- getPlayer().sendPacket(smsg);
- }
- // Otherwise, send message of object reward to client
- else
- {
- if (count > 1)
- {
- SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
- smsg.addItemName(item);
- smsg.addItemNumber(count);
- getPlayer().sendPacket(smsg);
- }
- else
- {
- SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_ITEM);
- smsg.addItemName(item);
- getPlayer().sendPacket(smsg);
- }
- }
- // send packets
- StatusUpdate su = new StatusUpdate(getPlayer().getObjectId());
- su.addAttribute(StatusUpdate.CUR_LOAD, getPlayer().getCurrentLoad());
- getPlayer().sendPacket(su);
- }
- public void giveItems(int itemId, long count, byte attributeId, int attributeLevel)
- {
- if (count <= 0)
- return;
- // Add items to player's inventory
- L2ItemInstance item = getPlayer().getInventory().addItem("Quest", itemId, count, getPlayer(), getPlayer().getTarget());
- if (item == null)
- return;
- // set enchant level for item if that item is not adena
- if (attributeId >= 0 && attributeLevel > 0)
- {
- item.setElementAttr(attributeId, attributeLevel);
- if (item.isEquipped())
- item.updateElementAttrBonus(getPlayer());
- InventoryUpdate iu = new InventoryUpdate();
- iu.addModifiedItem(item);
- getPlayer().sendPacket(iu);
- }
- // If item for reward is gold, send message of gold reward to client
- if (itemId == 57)
- {
- SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_ADENA);
- smsg.addItemNumber(count);
- getPlayer().sendPacket(smsg);
- }
- // Otherwise, send message of object reward to client
- else
- {
- if (count > 1)
- {
- SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
- smsg.addItemName(item);
- smsg.addItemNumber(count);
- getPlayer().sendPacket(smsg);
- }
- else
- {
- SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_ITEM);
- smsg.addItemName(item);
- getPlayer().sendPacket(smsg);
- }
- }
- // send packets
- StatusUpdate su = new StatusUpdate(getPlayer().getObjectId());
- su.addAttribute(StatusUpdate.CUR_LOAD, getPlayer().getCurrentLoad());
- getPlayer().sendPacket(su);
- }
- /**
- * Drop Quest item using Config.RATE_DROP_QUEST
- * @param itemId : int Item Identifier of the item to be dropped
- * @param count(minCount, maxCount) : long Quantity of items to be dropped
- * @param neededCount : Quantity of items needed for quest
- * @param dropChance : int Base chance of drop, same as in droplist
- * @param sound : boolean indicating whether to play sound
- * @return boolean indicating whether player has requested number of items
- */
- public boolean dropQuestItems(int itemId, int count, long neededCount, int dropChance, boolean sound)
- {
- return dropQuestItems(itemId, count, count, neededCount, dropChance, sound);
- }
- public boolean dropQuestItems(int itemId, int minCount, int maxCount, long neededCount, int dropChance, boolean sound)
- {
- dropChance *= Config.RATE_DROP_QUEST / ((getPlayer().getParty() != null) ? getPlayer().getParty().getMemberCount() : 1);
- long currentCount = getQuestItemsCount(itemId);
- if (neededCount > 0 && currentCount >= neededCount)
- return true;
- if (currentCount >= neededCount)
- return true;
- long itemCount = 0;
- int random = Rnd.get(L2DropData.MAX_CHANCE);
- while (random < dropChance)
- {
- // Get the item quantity dropped
- if (minCount < maxCount)
- itemCount += Rnd.get(minCount, maxCount);
- else if (minCount == maxCount)
- itemCount += minCount;
- else
- itemCount++;
- // Prepare for next iteration if dropChance > L2DropData.MAX_CHANCE
- dropChance -= L2DropData.MAX_CHANCE;
- }
- if (itemCount > 0)
- {
- // if over neededCount, just fill the gap
- if (neededCount > 0 && currentCount + itemCount > neededCount)
- itemCount = neededCount - currentCount;
- // Inventory slot check
- if (!getPlayer().getInventory().validateCapacityByItemId(itemId))
- return false;
- // Give the item to Player
- getPlayer().addItem("Quest", itemId, itemCount, getPlayer().getTarget(), true);
- if (sound)
- playSound((currentCount + itemCount < neededCount) ? "Itemsound.quest_itemget" : "Itemsound.quest_middle");
- }
- return (neededCount > 0 && currentCount + itemCount >= neededCount);
- }
- //TODO: More radar functions need to be added when the radar class is complete.
- // BEGIN STUFF THAT WILL PROBABLY BE CHANGED
- public void addRadar(int x, int y, int z)
- {
- getPlayer().getRadar().addMarker(x, y, z);
- }
- public void removeRadar(int x, int y, int z)
- {
- getPlayer().getRadar().removeMarker(x, y, z);
- }
- public void clearRadar()
- {
- getPlayer().getRadar().removeAllMarkers();
- }
- // END STUFF THAT WILL PROBABLY BE CHANGED
- /**
- * Remove items from player's inventory when talking to NPC in order to have rewards.<BR><BR>
- * <U><I>Actions :</I></U>
- * <LI>Destroy quantity of items wanted</LI>
- * <LI>Send new inventory list to player</LI>
- * @param itemId : Identifier of the item
- * @param count : Quantity of items to destroy
- */
- public void takeItems(int itemId, long count)
- {
- // Get object item from player's inventory list
- L2ItemInstance item = getPlayer().getInventory().getItemByItemId(itemId);
- if (item == null)
- return;
- // Tests on count value in order not to have negative value
- if (count < 0 || count > item.getCount())
- count = item.getCount();
- // Destroy the quantity of items wanted
- if (itemId == 57)
- getPlayer().reduceAdena("Quest", count, getPlayer(), true);
- else
- {
- if (item.isEquipped())
- {
- L2ItemInstance[] unequiped = getPlayer().getInventory().unEquipItemInBodySlotAndRecord(item.getItem().getBodyPart());
- InventoryUpdate iu = new InventoryUpdate();
- for (L2ItemInstance itm: unequiped)
- iu.addModifiedItem(itm);
- getPlayer().sendPacket(iu);
- getPlayer().broadcastUserInfo();
- }
- getPlayer().destroyItemByItemId("Quest", itemId, count, getPlayer(), true);
- }
- }
- /**
- * Send a packet in order to play sound at client terminal
- * @param sound
- */
- public void playSound(String sound)
- {
- getPlayer().sendPacket(new PlaySound(sound));
- }
- /**
- * Add XP and SP as quest reward
- * @param exp
- * @param sp
- */
- public void addExpAndSp(int exp, int sp)
- {
- getPlayer().addExpAndSp((int) getPlayer().calcStat(Stats.EXPSP_RATE, exp * Config.RATE_QUESTS_REWARD, null, null), (int) getPlayer().calcStat(Stats.EXPSP_RATE, sp * Config.RATE_QUESTS_REWARD, null, null));
- }
- /**
- * Return random value
- * @param max : max value for randomisation
- * @return int
- */
- public int getRandom(int max)
- {
- return Rnd.get(max);
- }
- /**
- * Return number of ticks from GameTimeController
- * @return int
- */
- public int getItemEquipped(int loc)
- {
- return getPlayer().getInventory().getPaperdollItemId(loc);
- }
- /**
- * Return the number of ticks from the GameTimeController
- * @return int
- */
- public int getGameTicks()
- {
- return GameTimeController.getGameTicks();
- }
- /**
- * Return true if quest is to exited on clean up by QuestStateManager
- * @return boolean
- */
- public final boolean isExitQuestOnCleanUp()
- {
- return _isExitQuestOnCleanUp;
- }
- /**
- * Return the QuestTimer object with the specified name
- * @return QuestTimer<BR> Return null if name does not exist
- */
- public void setIsExitQuestOnCleanUp(boolean isExitQuestOnCleanUp)
- {
- _isExitQuestOnCleanUp = isExitQuestOnCleanUp;
- }
- /**
- * Start a timer for quest.<BR><BR>
- * @param name<BR> The name of the timer. Will also be the value for event of onEvent
- * @param time<BR> The milisecond value the timer will elapse
- */
- public void startQuestTimer(String name, long time)
- {
- getQuest().startQuestTimer(name, time, null, getPlayer(), false);
- }
- public void startQuestTimer(String name, long time, L2Npc npc)
- {
- getQuest().startQuestTimer(name, time, npc, getPlayer(), false);
- }
- public void startRepeatingQuestTimer(String name, long time)
- {
- getQuest().startQuestTimer(name, time, null, getPlayer(), true);
- }
- public void startRepeatingQuestTimer(String name, long time, L2Npc npc)
- {
- getQuest().startQuestTimer(name, time, npc, getPlayer(), true);
- }
- /**
- * Return the QuestTimer object with the specified name
- * @return QuestTimer<BR> Return null if name does not exist
- */
- public final QuestTimer getQuestTimer(String name)
- {
- return getQuest().getQuestTimer(name, null, getPlayer());
- }
- /**
- * Add spawn for player instance
- * Return object id of newly spawned npc
- */
- public L2Npc addSpawn(int npcId)
- {
- return addSpawn(npcId, getPlayer().getX(), getPlayer().getY(), getPlayer().getZ(), 0, false, 0);
- }
- public L2Npc addSpawn(int npcId, int despawnDelay)
- {
- return addSpawn(npcId, getPlayer().getX(), getPlayer().getY(), getPlayer().getZ(), 0, false, despawnDelay);
- }
- public L2Npc addSpawn(int npcId, int x, int y, int z)
- {
- return addSpawn(npcId, x, y, z, 0, false, 0);
- }
- /**
- * Add spawn for player instance
- * Will despawn after the spawn length expires
- * Uses player's coords and heading.
- * Adds a little randomization in the x y coords
- * Return object id of newly spawned npc
- */
- public L2Npc addSpawn(int npcId, L2Character cha)
- {
- return addSpawn(npcId, cha, true, 0);
- }
- public L2Npc addSpawn(int npcId, L2Character cha, int despawnDelay)
- {
- return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, despawnDelay);
- }
- /**
- * Add spawn for player instance
- * Will despawn after the spawn length expires
- * Return object id of newly spawned npc
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int despawnDelay)
- {
- return addSpawn(npcId, x, y, z, 0, false, despawnDelay);
- }
- /**
- * Add spawn for player instance
- * Inherits coords and heading from specified L2Character instance.
- * It could be either the player, or any killed/attacked mob
- * Return object id of newly spawned npc
- */
- public L2Npc addSpawn(int npcId, L2Character cha, boolean randomOffset, int despawnDelay)
- {
- return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), randomOffset, despawnDelay);
- }
- /**
- * Add spawn for player instance
- * Return object id of newly spawned npc
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay)
- {
- return getQuest().addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, false);
- }
- /**
- * Add spawn for player instance
- * Return object id of newly spawned npc
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- {
- return getQuest().addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn);
- }
- public String showHtmlFile(String fileName)
- {
- return getQuest().showHtmlFile(getPlayer(), fileName);
- }
- /**
- * Destroy element used by quest when quest is exited
- * @param repeatable
- * @return QuestState
- */
- public QuestState exitQuest(boolean repeatable)
- {
- // remove this quest from the notifyDeath list of this character if its on this list
- _player.removeNotifyQuestOfDeath(this);
- if (isCompleted())
- return this;
- // Say quest is completed
- setState(State.COMPLETED);
- // Clean registered quest items
- int[] itemIdList = getQuest().getRegisteredItemIds();
- if (itemIdList != null)
- {
- for (int i = 0; i < itemIdList.length; i++)
- takeItems(itemIdList[i], -1);
- }
- // If quest is repeatable, delete quest from list of quest of the player and from database (quest CAN be created again => repeatable)
- if (repeatable)
- {
- getPlayer().delQuestState(getQuestName());
- Quest.deleteQuestInDb(this);
- _vars = null;
- }
- else
- {
- // Otherwise, delete variables for quest and update database (quest CANNOT be created again => not repeatable)
- if (_vars != null)
- {
- for (String var : _vars.keySet())
- unset(var);
- }
- Quest.updateQuestInDb(this);
- }
- return this;
- }
- public void showQuestionMark(int number)
- {
- getPlayer().sendPacket(new TutorialShowQuestionMark(number));
- }
- public void playTutorialVoice(String voice)
- {
- getPlayer().sendPacket(new PlaySound(2, voice, 0, 0, getPlayer().getX(), getPlayer().getY(), getPlayer().getZ()));
- }
- public void showTutorialHTML(String html)
- {
- String text = HtmCache.getInstance().getHtm("data/scripts/quests/255_Tutorial/" + html);
- if (text == null)
- {
- _log.warning("missing html page data/scripts/quests/255_Tutorial/" + html);
- text = "<html><body>File data/scripts/quests/255_Tutorial/" + html + " not found or file is empty.</body></html>";
- }
- getPlayer().sendPacket(new TutorialShowHtml(text));
- }
- public void closeTutorialHtml()
- {
- getPlayer().sendPacket(new TutorialCloseHtml());
- }
- public void onTutorialClientEvent(int number)
- {
- getPlayer().sendPacket(new TutorialEnableClientEvent(number));
- }
- public void dropItem(L2MonsterInstance npc, L2PcInstance player, int itemId, int count)
- {
- npc.dropItem(player, itemId, count);
- }
- }
|