#Final
from eventAPI import *
from APIUtils import *

def fireEvent(e):
    if (getTag(e, "TotEplot") != 6):
        if (getTag(e, "TotEplot") == 7):
            back(e)
            return 0
        return 1
    showPicture(e,"Savah")
    showMessage(e, ["You approach the altar, and see a young female gnome poring over an open book.",
                    "","She turns to face you, visibly annoyed but showing no surprise at your unexpected presence, and speaks."])
    showMessage(e, ["\"Gnav has sent you, hasn't he? My poor father...\"","","With scorn plain in her expression, she continues.",
                    "","\"He thinks I can be persuaded to forgo my pursuit of knowledge, and return to my former life."])
    showMessage(e, ["The uneventful, pitiful life of a scholar that he chose for himself.\"",
                    "","Now her voice holds undiluted contempt.","","\"He doesn't realize the extent of the powers I gleaned from the Book of Knowledge, else he would have joined me, or at least stayed away from me."])
    showMessage(e, ["The Book of Knowledge has given me strength, and the power to conquer everything in my path. I Savah, will rule the lands of Rennibister and the rest of the realms.",
                    "","It is unfortunate that you chose to listen to the old fool."])
    showMessage(e, ["Now you will pay the price of your meddling.\"","","",
                    "With a few precise gestures, she summons an army of elementals.",
                    "","You have no choice but to ready for battle."])
    fight(e)
    return 0

def back(e):
    showPicture(e,"Savah")
    showMessage(e, ["Savah is astonished that you've returned.","",
                    "Anyway, she summons her elementals once again, and battle is joined."])
    fight(e)
    
def fight(e):
    k = combat(e, [("Earth Elemental", 50, 0, 10),("Air Elemental", 50, 0, 10),("Water Elemental", 50, 0, 10),("Fire Elemental", 50, 0, 10)])
    if (k == 1):
        final(e)
    elif (k == 2):
        flee(e)
    else:
        die(e)
        
def flee(e):
    showMessage(e, ["You barely have room enough to fight, let alone flee!","",
                    "Worse, you find that Savah's replenished the ranks of the elementals."])
    fight(e)

def die(e):
    setTag(e, "TotEplot", 7)
    showMessage(e, ["To Savah's satisfaction, the elementals overwhelm you.","",
                    "You're crushed, soaked, burnt, whipped, drowned, contused, frozen, scorched, asphyxiated and one thing leading to the other, dead."])

def final(e):
    showPicture(e,"Savah")
    showMessage(e, ["Savah stares in shock as the party climbs above the fallen elementals' bodies.",
                    "","\"No! My elemental army could not have been defeated by mere mortals. Not when I infused them with my power.",
                    "","THIS CAN NOT BE!\""])
    showMessage(e, ["Then out of the darkness steps Gnav...","",
                    "\"Savah...My daughter...","I could not overcome your army by myself, but my own meagre powers allowed me to prevent your flux of energy from empowering your elementals. Thus these adventurers were able to slay them."])
    showMessage(e, ["Now it is time to end this madness. Open your eyes, the Book of Knowledge is corrupting your mind. I beg you, relinquish its power and come back home.\""])
    showMessage(e, ["Lunacy and hatred swirling in her gaze, Savah answers.","",
                    "\"My eyes are finally open, and I see the truth now. You wish to take my power and use it for your own purpose. Already you have wrecked what I had accomplished! But I won't let you."])
    showMessage(e, ["IF I CAN'T HAVE IT, NO ONE WILL!\"","",
                    "Savah retrieves the Book of Knowledge, turns it to the last page, and reads an incantation. Immediately her whole body is engulfed in a blinding ball of white fire."])
    showMessage(e, ["When your sight returns, there is nothing left of her but a pile of ashes from which emerge a pair of bracers and the Book of Knowledge, intact.","",
                    "Gnav sighs, \"I feared it might come to this. Blast this accursed book, she was too weak to resist its lure."])
    showMessage(e, ["Perhaps...Perhaps it shouldn't be read by anyone...","","Anyway...\"","",
                    "He turns to you.","","\"Take my daughter's bracers. She will need them no longer.\""])
    giveItems(e, ["Savah's Bracers"])
    clearPicture(e)
    showMessage(e, ["Gnav speaks again.","","\"Leave this place in haste. I will destroy the book, so that no one ever again falls victim to its seductions.\"",
                    "","As he starts weaving a spell, the walls and ceiling shudder, then begin to crumble."])
    showMessage(e, ["You realize he intends to bury the book, the remains of his daughter and himself in the ruins of the underground temple.",
                    "","You run out of the sanctum and make your way to the entrance stairs. Fortunately, the temple is empty of elemental threats."])
    showMessage(e, ["Amidst the groaning of tortured stone you rush up the stairs, and reach fresh air just as the temple collapses, filling the stairwell with debris.",
                    "","","","","","Thus ends the tale of Gnav and Savah..."])
    setTag(e, "TotEplot", 10)
    setExit(e, "maps/renwild.cfg", 48, 37, 0)
    
