# Made by Vice - cleanup by DrLecter
# this script is part of the Official L2J Datapack Project.
# Visit http://forum.l2jdp.com for more details.
import sys
from com.festina.gameserver.model.quest import State
from com.festina.gameserver.model.quest import QuestState
from com.festina.gameserver.model.quest.jython import QuestJython as JQuest

qn = "618_IntoTheFlame"
#NPCs
KLEIN = 8540
HILDA = 8271

#QUEST ITEMS
VACUALITE_ORE,VACUALITE,FLOATING_STONE = range(7265,7268)

#CHANCE
CHANCE_FOR_QUEST_ITEMS = 50

class Quest (JQuest) :
   def __init__(self,id,name,descr): JQuest.__init__(self,id,name,descr)

   def onEvent (self,event,st) :
      htmltext = event
      cond = st.getInt("cond")
      if event == "31540-03.htm" and cond == 0 :
         st.setState(STARTED)
         st.set("cond","1")
         st.playSound("ItemSound.quest_accept")
      elif event == "31540-05.htm" :
         if st.getQuestItemsCount(VACUALITE) and cond == 4 :
            st.takeItems(VACUALITE,1)
            st.giveItems(FLOATING_STONE,1)
            st.playSound("ItemSound.quest_finish")
            st.exitQuest(1)
         else :
            htmltext = "31540-03.htm"
      elif event == "31271-02.htm" and cond == 1 :
         st.set("cond","2")
         st.playSound("ItemSound.quest_middle")
      elif event == "31271-05.htm" :
         if cond == 3 and st.getQuestItemsCount(VACUALITE_ORE) >= 50 :
            st.takeItems(VACUALITE_ORE,-1)
            st.giveItems(VACUALITE,1)
            st.set("cond","4")
            st.playSound("ItemSound.quest_middle")
         else :
            htmltext = "31271-03.htm"
      return htmltext

   def onTalk (self,npc,st) :
      htmltext = "<html><body>I have nothing to say you</body></html>"
      if not st : return htmltext
      npcId = npc.getNpcId()
      cond = st.getInt("cond")
      id = st.getState()
      if npcId == KLEIN :
         if cond == 0 :
            if st.getPlayer().getLevel() < 60 :
               htmltext = "31540-01.htm"
               st.exitQuest(1)
            else :
               htmltext = "31540-02.htm"
         elif cond == 4 and st.getQuestItemsCount(VACUALITE) :
            htmltext = "31540-04.htm"
         else :
            htmltext = "31540-03.htm"
      elif npcId == HILDA :
         if cond == 1 :
            htmltext = "31271-01.htm"
         elif cond == 3 and st.getQuestItemsCount(VACUALITE_ORE) == 50 :
            htmltext = "31271-04.htm"
         elif cond == 4 :
            htmltext = "31271-06.htm"
         else :
            htmltext = "31271-03.htm"
      return htmltext

   def onKill (self,npc,st) :
      if not st : return
      chance = st.getRandom(100)
      count = st.getQuestItemsCount(VACUALITE_ORE)
      if CHANCE_FOR_QUEST_ITEMS > chance and count < 50 :
         st.giveItems(VACUALITE_ORE,1)
         if count == 49 :
            st.set("cond","3")
            st.playSound("ItemSound.quest_middle")
         else :
            st.playSound("ItemSound.quest_itemget")
      return

QUEST       = Quest(618,qn,"Into the Flame")
CREATED   = State('Start',     QUEST) 
STARTING  = State('Starting',  QUEST) 
STARTED   = State('Started',   QUEST) 
COMPLETED = State('Completed', QUEST)

QUEST.setInitialState(CREATED)
QUEST.addStartNpc(KLEIN)
CREATED.addTalkId(KLEIN) 
STARTED.addTalkId(KLEIN) 
STARTING.addTalkId(KLEIN) 
CREATED.addTalkId(HILDA) 
STARTED.addTalkId(HILDA) 
STARTING.addTalkId(HILDA) 

for mob in range(1274,1278)+range(1282,1286)+range(1290,1294) :
    STARTED.addKillId(mob)
    STARTING.addKillId(mob)
    CREATED.addKillId(mob)

for item in range(7265,7267) :
    STARTED.addQuestDrop(KLEIN,item,1)
    STARTING.addQuestDrop(KLEIN,item,1)
    CREATED.addQuestDrop(KLEIN,item,1)
      