package com.l2scoria.gameserver.model.quest;
import com.l2scoria.Config;
import com.l2scoria.gameserver.GameTimeController;
import com.l2scoria.gameserver.cache.HtmCache;
import com.l2scoria.gameserver.managers.QuestManager;
import com.l2scoria.gameserver.model.*;
import com.l2scoria.gameserver.model.actor.instance.*;
import com.l2scoria.gameserver.network.SystemMessageId;
import com.l2scoria.gameserver.network.serverpackets.*;
import com.l2scoria.gameserver.skills.Stats;
import com.l2scoria.util.random.Rnd;
import java.util.*;
import java.util.logging.Logger;
import javolution.util.FastList;
import javolution.util.FastMap;
// Referenced classes of package com.l2scoria.gameserver.model.quest:
// Quest, State, QuestTimer
public final class QuestState
{
QuestState(Quest quest, L2PcInstance player, State state, boolean completed)
{
_isExitQuestOnCleanUp = false;
_questName = quest.getName();
_player = player;
getPlayer().setQuestState(this);
_isCompleted = completed;
_state = state;
}
public String getQuestName()
{
return _questName;
}
public Quest getQuest()
{
return QuestManager.getInstance().getQuest(_questName);
}
public L2PcInstance getPlayer()
{
return _player;
}
public State getState()
{
return _state;
}
public boolean isCompleted()
{
return _isCompleted;
}
public boolean isStarted()
{
return !getStateId().equals("Start") && !getStateId().equals("Completed");
}
public Object setState(State state)
{
if(_state != state)
{
if(state == null)
return null;
if(getStateId().equals("Completed"))
_isCompleted = true;
else
_isCompleted = false;
_state = state;
Quest.updateQuestInDb(this);
QuestList ql = new QuestList();
getPlayer().sendPacket(ql);
ql = null;
}
return state;
}
public String getStateId()
{
return getState().getName();
}
String setInternal(String var, String val)
{
if(_vars == null)
_vars = new FastMap();
if(val == null)
val = "";
_vars.put(var, val);
return val;
}
public String set(String var, String val)
{
if(_vars == null)
_vars = new FastMap();
if(val == null)
val = "";
String old = (String)_vars.put(var, val);
if(old != null)
Quest.updateQuestVarInDb(this, var, val);
else
Quest.createQuestVarInDb(this, var, val);
if(var == "cond")
try
{
int previousVal = 0;
try
{
previousVal = Integer.parseInt(old);
}
catch(Exception ex)
{
previousVal = 0;
}
setCond(Integer.parseInt(val), previousVal);
}
catch(Exception e)
{
_log.finer((new StringBuilder()).append(getPlayer().getName()).append(", ").append(getQuestName()).append(" cond [").append(val).append("] is not an integer. Value stored, but no packet was sent: ").append(e).toString());
}
old = null;
return val;
}
private void setCond(int cond, int old)
{
int completedStateFlags = 0;
if(cond == old)
return;
if(cond < 3 || cond > 31)
unset("__compltdStateFlags");
else
completedStateFlags = getInt("__compltdStateFlags");
if(completedStateFlags == 0)
{
if(cond > old + 1)
{
completedStateFlags = 0x80000001;
completedStateFlags |= (1 << old) - 1;
completedStateFlags |= 1 << cond - 1;
set("__compltdStateFlags", String.valueOf(completedStateFlags));
}
} else
if(cond < old)
{
completedStateFlags &= (1 << cond) - 1;
if(completedStateFlags == (1 << cond) - 1)
{
unset("__compltdStateFlags");
} else
{
completedStateFlags |= 0x80000001;
set("__compltdStateFlags", String.valueOf(completedStateFlags));
}
} else
{
completedStateFlags |= 1 << cond - 1;
set("__compltdStateFlags", String.valueOf(completedStateFlags));
}
QuestList ql = new QuestList();
getPlayer().sendPacket(ql);
ql = null;
int questId = getQuest().getQuestIntId();
if(questId > 0 && questId < 999 && cond > 0)
getPlayer().sendPacket(new ExShowQuestMark(questId));
}
public String unset(String var)
{
if(_vars == null)
return null;
String old = (String)_vars.remove(var);
if(old != null)
Quest.deleteQuestVarInDb(this, var);
return old;
}
public Object get(String var)
{
if(_vars == null)
return null;
else
return _vars.get(var);
}
public int getInt(String var)
{
int varint = 0;
try
{
varint = Integer.parseInt((String)_vars.get(var));
}
catch(Exception e)
{
_log.finer((new StringBuilder()).append(getPlayer().getName()).append(": variable ").append(var).append(" isn't an integer: ").append(varint).append(e).toString());
if(Config.AUTODELETE_INVALID_QUEST_DATA)
exitQuest(true);
}
return varint;
}
public void addNotifyOfDeath(L2Character character)
{
if(character == null)
{
return;
} else
{
character.addNotifyQuestOfDeath(this);
return;
}
}
public int getQuestItemsCount(int itemId)
{
int count = 0;
L2ItemInstance arr$[] = getPlayer().getInventory().getItems();
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; i$++)
{
L2ItemInstance item = arr$[i$];
if(item.getItemId() == itemId)
count += item.getCount();
}
return count;
}
public int getEnchantLevel(int itemId)
{
L2ItemInstance enchanteditem = getPlayer().getInventory().getItemByItemId(itemId);
if(enchanteditem == null)
return 0;
else
return enchanteditem.getEnchantLevel();
}
public void giveItems(int itemId, int count)
{
giveItems(itemId, count, 0);
}
public void giveItems(int itemId, int count, int enchantlevel)
{
if(count <= 0)
return;
int questId = getQuest().getQuestIntId();
if(itemId == 57 && (questId < 217 || questId > 233) && (questId < 401 || questId > 418))
count = (int)((float)count * Config.RATE_QUESTS_REWARD);
L2ItemInstance item = getPlayer().getInventory().addItem("Quest", itemId, count, getPlayer(), getPlayer().getTarget());
if(item == null)
return;
if(enchantlevel > 0)
item.setEnchantLevel(enchantlevel);
if(itemId == 57)
{
SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_ADENA);
smsg.addNumber(count);
getPlayer().sendPacket(smsg);
smsg = null;
} else
if(count > 1)
{
SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
smsg.addItemName(item.getItemId());
smsg.addNumber(count);
getPlayer().sendPacket(smsg);
smsg = null;
} else
{
SystemMessage smsg = new SystemMessage(SystemMessageId.EARNED_ITEM);
smsg.addItemName(item.getItemId());
getPlayer().sendPacket(smsg);
smsg = null;
}
getPlayer().sendPacket(new ItemList(getPlayer(), false));
StatusUpdate su = new StatusUpdate(getPlayer().getObjectId());
su.addAttribute(14, getPlayer().getCurrentLoad());
getPlayer().sendPacket(su);
su = null;
}
public boolean dropQuestItems(int itemId, int count, int neededCount, int dropChance, boolean sound)
{
return dropQuestItems(itemId, count, count, neededCount, dropChance, sound);
}
public boolean dropQuestItems(int itemId, int minCount, int maxCount, int neededCount, int dropChance, boolean sound)
{
dropChance = (int)((float)dropChance * (Config.RATE_DROP_QUEST / (float)(getPlayer().getParty() == null ? 1 : getPlayer().getParty().getMemberCount())));
int currentCount = getQuestItemsCount(itemId);
if(neededCount > 0 && currentCount >= neededCount)
return true;
if(currentCount >= neededCount)
return true;
int itemCount = 0;
for(int random = Rnd.get(0xf4240); random < dropChance; dropChance -= 0xf4240)
{
if(minCount < maxCount)
{
itemCount += Rnd.get(minCount, maxCount);
continue;
}
if(minCount == maxCount)
itemCount += minCount;
else
itemCount++;
}
if(itemCount > 0)
{
if(neededCount > 0 && currentCount + itemCount > neededCount)
itemCount = neededCount - currentCount;
if(!getPlayer().getInventory().validateCapacityByItemId(itemId))
return false;
getPlayer().addItem("Quest", itemId, itemCount, getPlayer().getTarget(), true);
if(sound)
playSound(currentCount + itemCount >= neededCount ? "Itemsound.quest_middle" : "Itemsound.quest_itemget");
}
return neededCount > 0 && currentCount + itemCount >= neededCount;
}
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();
}
public void takeItems(int itemId, int count)
{
L2ItemInstance item = getPlayer().getInventory().getItemByItemId(itemId);
if(item == null)
return;
if(count < 0 || count > item.getCount())
count = item.getCount();
if(itemId == 57)
getPlayer().reduceAdena("Quest", count, getPlayer(), true);
else
getPlayer().destroyItemByItemId("Quest", itemId, count, getPlayer(), true);
item = null;
}
public void playSound(String sound)
{
getPlayer().sendPacket(new PlaySound(sound));
}
public void addExpAndSp(int exp, int sp)
{
getPlayer().addExpAndSp((int)getPlayer().calcStat(Stats.EXPSP_RATE, (float)exp * Config.RATE_QUESTS_REWARD, null, null), (int)getPlayer().calcStat(Stats.EXPSP_RATE, (float)sp * Config.RATE_QUESTS_REWARD, null, null));
}
public int getRandom(int max)
{
return Rnd.get(max);
}
public int getItemEquipped(int loc)
{
return getPlayer().getInventory().getPaperdollItemId(loc);
}
public int getGameTicks()
{
return GameTimeController.getGameTicks();
}
public final boolean isExitQuestOnCleanUp()
{
return _isExitQuestOnCleanUp;
}
public void setIsExitQuestOnCleanUp(boolean isExitQuestOnCleanUp)
{
_isExitQuestOnCleanUp = isExitQuestOnCleanUp;
}
public void startQuestTimer(String name, long time)
{
getQuest().startQuestTimer(name, time, null, getPlayer());
}
public void startQuestTimer(String name, long time, L2NpcInstance npc)
{
getQuest().startQuestTimer(name, time, npc, getPlayer());
}
public final QuestTimer getQuestTimer(String name)
{
return getQuest().getQuestTimer(name, null, getPlayer());
}
public L2NpcInstance addSpawn(int npcId)
{
return addSpawn(npcId, getPlayer().getX(), getPlayer().getY(), getPlayer().getZ(), 0, false, 0);
}
public L2NpcInstance addSpawn(int npcId, int despawnDelay)
{
return addSpawn(npcId, getPlayer().getX(), getPlayer().getY(), getPlayer().getZ(), 0, false, despawnDelay);
}
public L2NpcInstance addSpawn(int npcId, int x, int y, int z)
{
return addSpawn(npcId, x, y, z, 0, false, 0);
}
public L2NpcInstance addSpawn(int npcId, L2Character cha)
{
return addSpawn(npcId, cha, true, 0);
}
public L2NpcInstance addSpawn(int npcId, L2Character cha, int despawnDelay)
{
return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, despawnDelay);
}
public L2NpcInstance addSpawn(int npcId, int x, int y, int z, int despawnDelay)
{
return addSpawn(npcId, x, y, z, 0, false, despawnDelay);
}
public L2NpcInstance addSpawn(int npcId, L2Character cha, boolean randomOffset, int despawnDelay)
{
return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), randomOffset, despawnDelay);
}
public L2NpcInstance 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);
}
public String showHtmlFile(String fileName)
{
return getQuest().showHtmlFile(getPlayer(), fileName);
}
public QuestState exitQuest(boolean repeatable)
{
if(isCompleted())
return this;
_isCompleted = true;
FastList itemIdList = getQuest().getRegisteredItemIds();
if(itemIdList != null)
{
javolution.util.FastList.Node n = itemIdList.head();
for(javolution.util.FastList.Node end = itemIdList.tail(); (n = n.getNext()) != end;)
takeItems(((Integer)n.getValue()).intValue(), -1);
}
if(repeatable)
{
getPlayer().delQuestState(getQuestName());
Quest.deleteQuestInDb(this);
_vars = null;
} else
{
if(_vars != null)
{
String var;
for(Iterator i$ = _vars.keySet().iterator(); i$.hasNext(); unset(var))
var = (String)i$.next();
}
Quest.updateQuestInDb(this);
}
itemIdList = null;
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((new StringBuilder()).append("data/scripts/quests/255_Tutorial/").append(html).toString());
if(text == null)
{
_log.warning((new StringBuilder()).append("missing html page data/scripts/quests/255_Tutorial/").append(html).toString());
text = (new StringBuilder()).append("<html><body>File data/scripts/quests/255_Tutorial/").append(html).append(" not found or file is empty.</body></html>").toString();
}
getPlayer().sendPacket(new TutorialShowHtml(text));
text = null;
}
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);
}
protected static final Logger _log = Logger.getLogger(com/l2scoria/gameserver/model/quest/Quest.getName());
private final String _questName;
private final L2PcInstance _player;
private State _state;
private boolean _isCompleted;
private Map _vars;
private boolean _isExitQuestOnCleanUp;