#Aldorik's house
from eventAPI import getTag, setTag, playSpecAudio, showPicture, removeItem, optionInput, removeItem, modifyGold, modifyXP, getPartyCount, combat
from APIUtils import issetall, isclearall, showMessage, firstHasItem, firstPC
from SDLKeys import SDLK_y, SDLK_n, SDLK_l, SDLK_h, SDLK_r
import strangersHouse

RingPlt1 = 0

def fireEvent(e):
    global RingPlt1
    RingPlt1 = getTag(e, "RingPlt1")
    if (not issetall(RingPlt1, 0x01000000)):
        strangersHouse.fireEvent(e)
        setTag(e, "RingPlt1", RingPlt1)
        return 0
    if (not isclearall(RingPlt1, 0x1E000000)):
        returnToAldorik(e)
        setTag(e, "RingPlt1", RingPlt1)
        return 0
    playSpecAudio(e, "House")
    showPicture(e, "Aldorik")
    if (optionInput(e,
        ["\"I'm glad you came to see me - I was beginning to think that I would never find Heroes suitable to the task.",
         "For many years, an heirloom of my family has been lost in a mystical dungeon located beneath this part of Rennibister.",
         "Once, three adventurers, brothers all of my great-grandfather, tried to obtain the Cloak of the True Hero from the powerful sorcerer who had created it.",
         "The sorcerer deemed them worthy of a trial, and set them each three tests.",
         "The brother who passed all three tests would be granted the Cloak.",
         "I know little in the way of details, as my great-grandfather preferred not to discuss the matter, but I do know that none of the three brothers returned.",
         "The three trials are contained in a mystical dungeon, the entrance to which is located in the house neighboring mine to the North.",
         "The dungeon is beneath a locked trap door, and I have the key.",
         "If you're interested, I'm willing to pay you 10,000 Gold pieces if you will retrieve the Cloak for me.\"",
         ""
         "\"Do you accept?\""],
        ["(Y)es, we accept", "(N)o, we refuse"], [SDLK_y, SDLK_n]) == SDLK_y):
        RingPlt1 |= 0x04000000
        acceptCloakQuest(e)
    else:
        RingPlt1 |= 0x02000000
        refuseCloakQuest(e)
    setTag(e, "RingPlt1", RingPlt1)
    return 0

def acceptCloakQuest(e): # section 2
    optionInput(e,
        ["\"Oh, thank you!  You have no idea how long I've waited for someone brave enough to undertake this challenge for me.",
         "Please, return here as soon as you have the Cloak, and I will have your reward waiting!\""],
        ["(L)eave the House"], [SDLK_l])

def refuseCloakQuest(e): # section 3
    optionInput(e,
        ["\"I'm very sorry to hear that.  But since there is no one else in town worth of the challenge, I will wait here in case you reconsider.\""],
        ["(L)eave the House"], [SDLK_l])

def returnToAldorik(e): # section 4
    global RingPlt1
    if (not isclearall(RingPlt1, 0x1C000000)):
        returnAfterAccepted(e)
        return 0
    playSpecAudio(e, "House")
    showPicture(e, "Aldorik")
    if (optionInput(e,
        ["\"You have returned!  Does this mean you are willing to accept my offer?\""],
        ["(Y)es, we accept", "(N)o, we refuse"], [SDLK_y, SDLK_n]) == SDLK_y):
        RingPlt1 |= 0x04000000
        acceptCloakQuest(e)
    else:
        RingPlt1 |= 0x02000000
        refuseCloakQuest(e)

def returnAfterAccepted(e): # section 5
    global RingPlt1
    if (not isclearall(RingPlt1, 0x18000000)):
        alreadyCompleted(e)
        return 0
    playSpecAudio(e, "House")
    showPicture(e, "Aldorik")
    if (optionInput(e,
        ["\"Finally, you have emerged from the horrid pit!  Did you bring the Cloak?\""],
        ["(Y)es, it's here", "(N)ot yet"], [SDLK_y, SDLK_n]) == SDLK_y):
        checkForCloak(e)
    else:
        impatientAldorik(e)

def checkForCloak(e):
    if (firstHasItem(e, "Cloak of the True Hero") < 0):
        liedAboutCloak(e)
        return 0
    if (optionInput(e,
        ["\"Ahh, excellent!  Thank you very much!  Now, if you'll kindly hand it over, I'll happily reward you as agreed...\""],
        ["(H)and it over", "(R)efuse"], [SDLK_h, SDLK_r]) == SDLK_h):
        giveCloakToAldorik(e)
        return 0
    else:
        keepCloakFromAldorik(e)

def impatientAldorik(e): #section 7
    optionInput(e,
        ["\"Well, then, what are you doing here?  Come back when you've got my Cloak!\""],
        ["(L)eave the House"], [SDLK_l])

def liedAboutCloak(e): # section 8
    optionInput(e,
        ["\"Liar!  You have not the Cloak of the True Hero!  Return when you've finished the quest you agreed to!\""],
        ["(L)eave the House"], [SDLK_l])

def giveCloakToAldorik(e):
    global RingPlt1
    showMessage(e,
        ["\"Thank you again!  I am so grateful to you.  Here's the 10,000 Gold, as promised.\"",
         "",
         "(100,000 EXP)"])
    removeItem(e, firstHasItem(e, "Cloak of the True Hero"), "Cloak of the True Hero")
    modifyGold(e, firstPC(e), 10000)
    x, c = 0, getPartyCount(e)
    while (x < c):
        modifyXP(e, x, 100000)
        x += 1
    RingPlt1 |= 0x08000000

def keepCloakFromAldorik(e):
    global RingPlt1
    showMessage(e,
        ["\"You lying scoundrels!  If you will not turn over what is rightfully mine, I will take it!\""])
    RingPlt1 |= 0x10000000
    combatRet = combat(e, [("Black Claw Warlock", 1, 0, 90)])
    if (combatRet == 1):
        defeatedAldorik(e)
    elif (combatRet == 2):
        runAwayAldorik(e)
    else:
        lostToAldorik(e)

def defeatedAldorik(e):
    RingPlt1 &= ~(0x10000000)
    playSpecAudio(e, "House")
    showPicture(e, "House")
    optionInput(e,
        ["Having defeated Aldorik, you search his house, but find nothing.  Either he never intended to pay you, or he had a way of hiding his money that is beyond your means of discovery."],
        ["(L)eave the House"], [SDLK_l])

def runAwayAldorik(e):
    showMessage(e, ["You bid a hasty retreat from Aldorik's Home"])

def lostToAldorik(e):
    RingPlt1 &= ~(0x10000000)
    showPicture(e, "Aldorik")
    showMessage(e,
        ["As you lie dying on the floor of the house, the old man removes the Cloak of the True Hero from you, and laughs at your foolishness."])
    removeItem(e, firstHasItem(e, "Cloak of the True Hero"), "Cloak of the True Hero")

def alreadyCompleted(e):
    global RingPlt1
    if (not isclearall(RingPlt1, 0x10000000)):
        ranFromAldorikCombat(e)
        return 0
    playSpecAudio(e, "House")
    showPicture(e, "House")
    optionInput(e,
        ["Having gotten back his family property, Aldorik appears to have left.  All that's here now is an empty house."],
        ["(L)eave the House"], [SDLK_l])

def ranFromAldorikCombat(e):
    showMessage(e,
        ["\"You dare return here after refusing to give me my property?!  Whether or not you have the Cloak, you will not live to see tomorrow!\""])
    combatRet = combat(e, [("Black Claw Warlock", 1, 0, 90)])
    if (combatRet == 1):
        defeatedAldorik(e)
    elif (combatRet == 2):
        lostToAldorik(e)
