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

rtmelvinFlag = 0

def fireEvent(e):
   global rtmelvinFlag
   rtmelvinFlag = getTag(e, "rtmelvinFlag")
   if (rtmelvinFlag == 3):
      helloMelvin(e)
   elif (rtmelvinFlag < 3):
      treasureRoom(e)
   else:
      return 1
   return 1

def helloMelvin(e):
   global rtmelvinFlag
   showMessage(e, ["As you enter the room you see a figure hunched over a large, opened steel chest.  Quickly finishing what it was doing, the figure turns towards you."])
   showPicture(e, "Gnome_Male02")
   showMessage(e, ["It's Melvin.", "", "\"Oh, hello again.  Just doing a little more exploring.\""])
   showMessage(e, ["\"So, err...  anything new?\""])
   showMessage(e, ["\"Umm, me?  Just finishing up here I think.\"", "", "He casts a quick glance at the chest behind him."])
   if (poseYesNo(e, ["\"Kinda crazy how people will just leave things lying around.  I'm just lucky enough to find it, I guess.  Speaking of luck, 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, ["Melvin stretches a bit and gives you a small wave."])
      showMessage(e, ["\"Time to leave you once again.\"", "", "\"I think that I have about all that I can carry for now.  Good luck to you.  I'm heading out to a more civilized place.  One with a tavern.\""])
      showMessage(e, ["With a final bow, Melvin takes his leave.", "", "You wonder if you will ever see him again.  You wonder if such a meeting would even be advisible."])
   rtmelvinFlag = 4
   setTag(e, "rtmelvinFlag", rtmelvinFlag)

def tellStory(e):
   showMessage(e, ["\"Let me tell you about the time that I was captured by pirates.\""])
   showMessage(e, ["\"I was taking a short journey to one of the outer islands in order to visit a counsin of mine, Mikal.  Now during this journey the ship was attacked by pirates!  It was Unlucky Jack the Pirate and boy did he ever catch everyone by surprise.\""])
   showMessage(e, ["\"Now, Unlucky Jack was a good pirate.  I would even rate him a great pirate.  You see in all his years of sailing he almost always came upon his prey unobserved.  He was said to have so much captured treasure that he didn't even bother to hide most of it anymore.  Those who sailed with him just one year would often retire to comfortable lives.  I heard that no matter where you were on Jack's island you could dig and find treasure.  So as to why he was named Unlucky Jack?  You see, he was a lucky pirate, but a very unlucky man.\""])
   showMessage(e, ["\"Now, his crew boarded us and easily overpowered the sailors and guards.  When Jack himself boarded, he tripped over a slightly loosened plank and fell into a water barrel.  That's the kind of luck that Jack had.\"", "", "\"So the pirates proceed to empty the hold and raid the cabins.  They found a fair amount or coin on that ship.  They even managed to liberate me of most of my, um, aquisitions. \"", "", "Melvin gives you a wink.", "", "\"Hey, pirates aren't the only ones who can raid a cabin.\""])
   showMessage(e, ["\"As I was waiting with the rest of the passengers, the ships captain just lost his mind and pulled a knife when Unlucky Jack had his back turned.  Not wanting to see that poor fool thrown into the sea, I decided that I would stop his foolish attack by tripping him.\"", "", "\"So I saved the pirate's life.  No big deal.  I help people all the time you know becuase I'm a nice guy.  But Unlucky Jack...  he decided that I was his new good luck charm.\"", "", "\"I was placed in a cage out on the deck so that my luck would be available to all of the pirate crew.  It was cramped and to be honest, I would have preferred to spend the time in a nice dungeon since most dungeons are at least roomy.\""])
   showMessage(e, ["\"After a few days of this suffering, Unlucky Jack stumbled across a large merchant ship and they proceeded to attack.  So I say to Jack, 'Hey, you should take me across to the other ship, otherwise I won't be close enough to provide you with any luck.'  He thinks that this is an excellent idea and he yanks me out of the cage and carries me across to the other ship.\"", "", "\"Upon boarding, Jack immediately tripped over rope and stumbled into one of his crew, accidently stabbing him.  I think he felt kinda bad about that.\"", "", "\"He then cut the rope to free himself, but the rope was actually part of the rigging.  Cutting the rope released one of the sails which fell on several of his crew, trapping them.  Of course the yardarm then swung violently, knocking two of his men overboard.  He yelled and gestured frantically to try and get things back under control, but he knocked over a lamp, setting the previously fallen sail on fire and, of course, dooming those trapped underneath.  He tried casting a basic douse spell to put out the fire before it consumed the ship, but ended up unleashing a rather large torrent that swept his remaining crew overboard.\""])
   showMessage(e, ["\"During this entire episode, the merchant crew remained untouched.  They just watched in disbelief.\"", "", "\"Seeing as how the pirate crew was lost, we hoped back on Jack's ship and left the merchant behind.  He didn't bother to put me back in the cage.  Instead, we sailed to port so that Jack could hire a new crew.\"", "", "\"When we arrived, he kindly escorted me off his ship, gave me a small bag of gold and asked me to never come within a thousand spans of him again.\"", "", "\"Quite a nice guy, huh?\""])
   showMessage(e, ["\"Of course, he didn't need to give me the gold since I had already raided his cabin on the journey to port.  Unlucky for him that he didn't put me back in the cage.\"", "", "\"Good to know that my luck doesn't extend to others.  It's something I prefer to keep for myself.\""])
   showMessage(e, ["\"Time to leave you once again.\"", "", "\"I think that I have about all that I can carry for now.\""])
   showMessage(e, ["\"You're quite the good sport you know.  Here take this for being patient with my rambling.  No worries, I found a good dozen of them anyway.\""])
   showMessage(e, ["Melvin hands you a bright red robe.  It does appear to be quite nice looking.", "", "\"Good luck to you.  I'm heading out to a more civilized place.  One with a tavern.\""])
   showMessage(e, ["With a final bow, Melvin takes his leave.", "", "You wonder if you will ever see him again.  You wonder if such a meeting would even be advisible."])
   giveItems(e, ["Red Robe"])
   rtmelvinFlag = 4
   setTag(e, "rtmelvinFlag", rtmelvinFlag)
   return 1

def treasureRoom(e):
   showMessage(e, ["A treasure room!", "", "There are several old chests scattered about this room."])
   showMessage(e, ["Most of the chests appear to be in decent shape, but some have fallen apart and their contents have been spilled onto the floor."])
   return 1
