#eastern keep melvin the magnificent
from eventAPI import *
from APIUtils import *

rtmelvinFlag = 0
rtmelvinCount = 0

def fireEvent(e):
   global rtmelvinFlag
   global returnValue
   rtmelvinFlag = getTag(e, "rtmelvinFlag")
   if (rtmelvinFlag == 1):
      helloMelvin(e)
   else:
      return 1
   return returnValue

def helloMelvin(e):
   global rtmelvinCount
   global returnValue
   rtmelvinCount = getTag(e, "rtmelvinCount")
   if (rtmelvinCount == 8):
      showMessage(e, ["Someone rushes past you and you recognize Melvin, that sneaky gnome from before."], optionInput)
      showPicture(e, "Gnome_Male02")
      showMessage(e, ["\"Look out ahead!  Best run for it my friends!\""])
      clearPicture(e)
      showMessage(e, ["Melvin continues to run until out of sight."])
      showMessage(e, ["You suddenly realize what it was that Melvin was running from."])
      fightThem(e)
   elif ((rtmelvinCount == 2) or (rtmelvinCount == 4) or (rtmelvinCount == 6)):
      showMessage(e, ["You see movement out of the corner of your eye and turn to catch Melvin sneaking past.", "", "He gives you a small wave before continuing on."])
      rtmelvinCount+=1
      setTag(e, "rtmelvinCount", rtmelvinCount)
      returnValue = 1
   else:
      rtmelvinCount+=1
      setTag(e, "rtmelvinCount", rtmelvinCount)
      returnValue = 1


def fightThem(e):
   combatRet = combat(e, [("Warrior Wight", 4, 0, 10), ("Wizard Wraith", 4, 0, 30)])
   if (combatRet == 1):
      wonFight(e)
   elif (combatRet == 3):
      lostFight(e)
   else:
      ranAwayFight(e)

def wonFight(e):
   global rtmelvinFlag
   global returnValue
   showMessage(e, ["You have emerged victorius no thanks to that worthless gnome."])
   showMessage(e, ["\"Psssst!\""])
   showPicture(e, "Gnome_Male02")
   showMessage(e, ["\"Errrrrr...  Sorry about that.  I did try to warn you.\""])
   showMessage(e, ["Well, he does seem embarassed about the incident."])
   showMessage(e, ["\"Hey, follow me and I'll take you to a little resting place.  I wouldn't want you to go away all tired after that battle.\""])
   showMessage(e, ["Though you are a little uneasy about his intentions, you could use a little rest.  So you dutifully follow Melvin as he leads you to a safer area.", "", "It does feel safer here."])
   showMessage(e, ["\"Just relax here a little.  You'll feel better after.  Trust me!\""])
   if (poseYesNo(e, ["\"That was quite a battle back there.  I've been through similar before.  Let me tell you about the time...", "", "Will you listen to his story?"])):
      tellStory(e)
   else:
      showMessage(e, ["\"That's okay.  I know that you're tired.\""])
      showMessage(e, ["\"You stay here and rest up.  Still plenty to check here, so i'm off.  Good luck!\""])
      rtmelvinFlag = 2
      setTag(e, "rtmelvinFlag", rtmelvinFlag)
      setExit(e, "maps/rtKeep2.cfg", 9, 13, 270)
      returnValue = 0

def lostFight(e):
   showMessage(e, ["Curse that bastard gnome!"])
   return 1

def ranAwayFight(e):
   showMessage(e, ["The dead have surrounded you and have left no passages for escape."], optionInput)
   fightThem(e)

def tellStory(e):
   global rtmelvinStory
   global returnValue
   showPicture(e, "Gnome_Male02")
   showMessage(e, ["\"This is the story of how I slayed a dragon and rescued a princess.\""])
   showMessage(e, ["\"Once, when I was traveling near the city of Collosus, I came across the wreckage of a caravan.  Now, this wasn't no merchant caravan.  It was a ROYAL caravan.  Seems that one of the King's daughters had been traveling to Collosus in order to help settle a dispute between some of the local lords.  Now, I'm thinking to myself 'If this is a royal caravan, then there must be plenty of treasure strewn about for an enterprising gnome such as myself to find.'  Well, no sooner had I begun to look than all of these guards just rode up and interrupted me.  Figuring that it might be bad to be seen taking advantage of such an opportune situation, I quickly explained that I was 'looking fer clues' if you knowwhaddamean.\""])
   showMessage(e, ["\"Well, they right up and recruited me, they did!  Thought I was some kind of warrior gnome I guess.  So, They stuck me up on one of their horses and off we went searching for princess what's-her-face.  Unfortunatly, the guards were able to follow one heck of a trail all the way to this huge cave.  So one of the guards says 'Yep, it's that blasted dragon again.  well, we have it cornered this time, so it can't escape us.'  Dragon, I think?  No, I think.  I won't be fighting any dragon today or any other day if I can help it!  Well these guards, they were all up for it and they was watching me, so I figured I had to at least pretend to help, right?  I let em go in first anyway.  There is a fine line between bravery and stupidity I always say.\""])
   showMessage(e, ["\"So in they go and I can hear em running around making all this horrible noise.  Screaming and yelling and all that.  And this dragon must have been mighty angry cause it made a noise like I had never heard before.  So I creep in far enough to take a peek and see this dragon tossing all these guardsmen about like they was rocks and it was one of those rock-throwing machines.  In fact, the whole thing got a little out of control and the stupid beast caused a bit of a cavein, blocking my immediate exit.  It was an impressive battle, I tell you true.  But still that dragon whooped em all.  Now right about then I notice that princess, you know, what's-her-face, all in danger and everything.  She was in a cage over in an out-of-the-way place.  I don't know why the dragon hadn't eaten her already.  Maybe he was saving her for a late snack, or maybe it was one of those virginal-princess dragon things that you always read about.\""])
   showMessage(e, ["\"Although, now that I think about it, I seriously doubt it could have been that at all if you knowwhaddamean.\""])
   showMessage(e, ["\"So, while the dragon was distracted with chewing on the last couple of guardsmen, I crept on in further and grabbed one of those big halberds that guards always like to use.  Now, it's a little known piece of dragon lore that dragons are *cough* sensitive in the hind region.  But gnomes, being as how we are about bite size, have developed a sizable lore on dragons.  So I sneak in behind this beast and with all of my strength I jab it in the rear with the halberd.  Whoa! Did that thing roar!  It's eyes went as big as tower shields and it raised it's head up so fast that it struck it's crown on the cave ceiling.  Knocked itself out cold it did!  Well, an angry guard-tossing dragon may present a difficult fight, but an unconcious dragon with a halberd in its hind quarters ain't much trouble at all.  So I dispatched the beast with ease.  Hey, you could call me Melvin the Magnificent Dragon Slayer!\""])
   showMessage(e, ["\"So there was me and that princess as the only survivors in that cave.  All alone.  She wasn't very helpful though.  Made me dig out the entrance all by myself and then escort her back to the city.  Never even said two words to me other than to order me around.  Probably should have just left that poor dragon alone with her.  She probably would have had IT following her orders, too.\""])
   showMessage(e, ["\"Well, looks like you've rested enough for now.  Still plenty to check here, so i'm off.  Good luck!\""])
   partyIdx = 0
   while (partyIdx < getPartyCount(e)):
      modifyHP(e, partyIdx, +400)
      modifySP(e, partyIdx, +200)
      partyIdx += 1
   rtmelvinFlag = 2
   setTag(e, "rtmelvinFlag", rtmelvinFlag)
   setExit(e, "maps/rtKeep2.cfg", 9, 13, 270)
   returnValue = 0