# 011_dor_house.dat*****
# Upon entering the "fellow's house" just SE 
# of her's, 
#
# check to make sure they decided to check for # evidence
# and makes sure that they haven't already
# done this event
# 26 8 2000 1
# 26 9 0 data/scripts/011_dor_house.dat
from eventAPI import getTag, setTag, showPicture, playSpecAudio, giveItems, removeItem, combat, optionInput, getPartyCount
from APIUtils import issetall, isclearall, showMessage, firstHasItem
from SDLKeys import SDLK_r, SDLK_h, SDLK_t, SDLK_l
import strangersHouse

#lFlagU
#          0    Fresh Event
# 0x03A00000    Finished Event
lFlagU = 0
DorPlt1 = 0

def fireEvent(e):
    global DorPlt1, lFlagU
    DorPlt1 = getTag(e, "DorPlt1")
    lFlagU = getTag(e, "lFlagU")
    if (lFlagU == 0x03A00000):
        # did event already
        completelySearched(e)
        return 0
     # Why do this?
#    if (not isclearall(DorPlt1, 0x1)):
#        abandonedHouse(e)
#        return 0
    if (firstHasItem(e, "Dorukan's Key") < 0):
        # shouldn't this be "locked door" or something?
        strangersHouse.fireEvent(e)
        return 0
    showPicture(e, "House")
    playSpecAudio(e, "dorukan")
    optionInput(e,
        ["You rummage around this small shanty for a few minutes and find a strange looking flask of obsidian that must contain some foul liquid. AND you find a note..."],
        ["(R)ead the note"], [SDLK_r])
    giveItems(e, ["Letter to Dorukan"])
    showPicture(e, "011_Note_ToDorukan")
    if (optionInput(e,
        ["Your heart skips as you suddenly hear feverish breathing...",
         "",
         "\"I WILL TAKE THAT BACK NOW. I will teach you a little lesson about meddling in that which you don't understand!\""],
        ["(H)ere it is. Bye!", "(T)ake your best shot!"], [SDLK_h, SDLK_t]) == SDLK_h):
        holder = firstHasItem(e, "Letter to Dorukan")
        if (holder > -1):
            removeItem(e, holder, "Letter to Dorukan")
    else:
        fightDorukan(e)
    setTag(e, "DorPlt1", DorPlt1)
    setTag(e, "lFlagU", lFlagU)
    return 0

def fightDorukan(e):
    global lFlagU
    combatRet = combat(e, [("Senrat", 2, 0, 20), ("Iron Golem", 1, 0, 30), ("Dorukan", 1, 0, 60)])
    if (combatRet == 1):
        #won
        wonFight(e)
    elif (combatRet == 3):
        #lost
        lostFight(e)
    else:
        # else ran
        defeated(e)

def wonFight(e):
    global DorPlt1, lFlagU
    showPicture(e, "Dorukan")
    playSpecAudio(e, "House")
    showMessage(e,
        ["Dorukan grasping his bloody chest races out the door. You try to follow but lose him around the next building!"])
    showPicture(e, "House")
    showMessage(e,
        ["The ratty looking Mage Dorukan may have gotten away, but your job is done.",
         "",
         "...You find a chest stashed away under some empty wooden boxes..."])
    giveItems(e, ["Elven Helm", "Midnight Vial", "Round Shield", "Luck Shield"])
    DorPlt1 |= 0x1
    lFlagU = 0x03A00000
    completelySearched(e)

def completelySearched(e):    
    global DorPlt1
    if (not issetall(DorPlt1, 0x1)):
        strangersHouse.fireEvent(e);
        return 0
    showPicture(e, "House")
    optionInput(e,
        ["Dorukan's house has been thoroughly searched and nothing else remains."],
        ["(L)eave Dorukan's house."], [SDLK_l])

def lostFight(e):
    showPicture(e, "House")
    showMessage(e,
        ["Lying on the floor in a pool of your own blood, you lose hope and realize there is no escape!"
         "",
         "\"I'll take that back now!\" says the tall ratty looking Mage.",
         "",
         "This tall wasted one must be Dorukan and something tells you that you'll meet him again."])
    x, c = 0, getPartyCount(e)
    while (x < c):
        modifyHP(e, x, 7)
        x += 1
    defeated(e)

def defeated(e):
    global DorPlt1, lFlagU
    showPicture(e, "House")
    optionInput(e,
        ["Even though Dorukan's henchmen defeated you, they were smart enough to realize that they are being hunted. They make a hasty retreat, thinking your party to be dead.",
         "",
         "You rummage around and find the letter that must have been left behind due to someone's carelessness. It could be a fake, but it might be real."],
        ["(L)eave Dorukan's house."], [SDLK_l])
    giveItems(e, ["Elven Helm", "Letter to Dorukan", "Midnight Vial", "Round Shield", "Luck Shield"])
    DorPlt1 |= 1
    lFlagU = 0x03A00000
    completelySearched(e)

def abandonedHouse(e):
    showPicture(e, "House")
    showMessage(e,
        ["You have entered an apparently abandoned residence. There is nothing of interest here..."])
