// Script made 10/6 2005 by -Seeker- for the Alangara server.

// Purpose of script: Allowing characters to fish

// This script was inspired heavily from Nouny's fishing script
// but was rewritten from ground up, rather than having to
// customize the existing script heavily.

// Possible catches with this script: (d100)
// Lootbag    : 100
// Fish       :  20 - 99
// Bottle     :  17 - 19
// Skeleton   :  15 - 16
// Redfish    :  12 - 14
// Rags       :   8 - 11
// Skull      :   7 -  7
// ibis       :   6 -  6
// Bottle     :   5 -  5
// moorberry  :   4 -  4
// Stein      :   1 -  3

const int BREAKING_CHANCE = 0;   // The percentage that the fishing rod will break
                                 // with use - can be 0
const int XP_REWARD = 2;         // XP per attempt
const int XP_LEVEL_LIMIT = 35;   // The last level in which XP is gained from fishing
const int FISHING_DC = 16;       // The DC of catching anything
const float DELAY_LENGTH = 6.0f; // How long do the character sit down while fishing

void message( string msg, object oPC )
{
   FloatingTextStringOnCreature( msg, oPC, FALSE );
}

void fish( object oPC, object oFishingRod )
{
   // Make a fishing check for the player
   // Presently, that is a simple d20 roll without a modifier
   int iFishingCheck = d20();
   string sResult;
   if (iFishingCheck < FISHING_DC)
   {
      // It did not suceed
      sResult = "You caught nothing, better luck next time!";
   }
   else
   {
      // Check for breakage
      int iBreak = d100();
      if ( iBreak <= BREAKING_CHANCE )
      {
         sResult = "You almost got one but it got away and your fishing rod broke!";
         DestroyObject(oFishingRod, 0.0);
         PlaySound("as_na_branchsnp3");
      }
      else
      {
         // Find out what was caught
         // Catch list borrowed from Nouny ;-)

         int nCatch= d100();
         location lLoc = GetLocation( oPC );

         if (nCatch == 100) //PC gets a loot bag!
         {
            CreateObject(OBJECT_TYPE_PLACEABLE, "plc_lootbag2", lLoc, TRUE);
            sResult = "You caught a bag!";
         }

         if ( (nCatch >= 20) && (nCatch <= 99) ) //PC gets a fish
         {
            CreateItemOnObject("ke_it_msmlmisc20", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "ke_it_msmlmisc20", lLoc, TRUE);
            int nFishLong = d20() + 9; //The length of the fish
            string sFish = IntToString(nFishLong);
            sResult = "You caught a " + sFish + " inches fish!";
         }

         if ( (nCatch >= 17) && (nCatch <=19) ) //PC gets a bottle
         {
            CreateItemOnObject("nw_it_thnmisc001", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_thnmisc001", lLoc, TRUE);
            sResult = "You caught an empty bottle!";
            }

     //    if ( (nCatch >= 15) && (nCatch <=16) ) //PC gets a skeleton and fight
     //    {
     //       CreateObject(OBJECT_TYPE_PLACEABLE, "nw_pl_skeleton", lLoc, TRUE);
     //       sResult = "You caught a skeleton!";
     //    }

         if ( (nCatch >= 15) && (nCatch <=16) ) //PC gets a skeleton and fight
         {
            CreateItemOnObject("it_mpotion011", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_thnmisc001", lLoc, TRUE);
            sResult = "You caught a potion of Hurricane!";
         }


         if ( (nCatch >= 11) && (nCatch <=14) ) //PC gets a redfish
         {
            CreateItemOnObject("redfish", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "redfish", lLoc, TRUE);
            sResult = "You caught a Redfish!";
         }

         if ( (nCatch >= 08) && (nCatch <=10) ) //PC gets rags
         {
            CreateItemOnObject("nw_it_msmlmisc21", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_msmlmisc21", lLoc, TRUE);
            sResult = "You caught some smelly rags!";
         }

         if ( (nCatch >= 07) && (nCatch <=07) ) //PC gets a gargoyle skull
         {
            CreateItemOnObject("nw_it_msmlmisc14", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_msmlmisc14", lLoc, TRUE);
            sResult = "You caught a skull!";
         }

         if ( (nCatch >= 06) && (nCatch <=06) ) //PC gets a ibis flower
         {
            CreateItemOnObject("ibisflower001", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_msmlmisc14", lLoc, TRUE);
            sResult = "A wet Ibis Flower gets cought by your hook as you pull it up";
         }

         if ( (nCatch >= 05) && (nCatch <=05) ) //PC gets a Mysterious Bottle
         {
            CreateItemOnObject("efreetibottlex1", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_msmlmisc14", lLoc, TRUE);
            sResult = "Your hook gets stuck in the cork of an old mysterious bottle";
         }

          if ( (nCatch >= 04) && (nCatch <=04) ) //PC gets a ibis flower
         {
            CreateItemOnObject("moorberry", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_msmlmisc14", lLoc, TRUE);
            sResult = "A Moorberry gets cought by your hook as you pull it up";
         }

         if ( (nCatch >= 01) && (nCatch <=03) ) //PC gets stein
         {
            CreateItemOnObject("nw_it_thnmisc002", oPC);
            // CreateObject(OBJECT_TYPE_ITEM, "nw_it_thnmisc002", lLoc, TRUE);
            sResult = "You caught an empty ale stein!";
         }
      }
   }
   message ( sResult, oPC );

   // Award XP for fishing attempt
   int iLevel = GetHitDice( oPC );
   if ( iLevel <= XP_LEVEL_LIMIT )
   {
      GiveXPToCreature( oPC, XP_REWARD );
   }

}

void main()
{
   object oPC = GetClickingObject();
   if (!GetIsPC(oPC)) return;

   object oHasFishingRod = GetItemPossessedBy(oPC, "FishingRod");
   location lLoc = GetLocation(oPC);

   if (oHasFishingRod == OBJECT_INVALID)
   {
      message("You cannot fish without a fishing rod.", oPC);
      AssignCommand(oPC, ActionPlayAnimation(ANIMATION_FIREFORGET_PAUSE_SCRATCH_HEAD , 1.0, 2.0));
   }
   else
   {
      // Make sure that Fishing Rod is equipped
      object oEquipped = GetItemInSlot( INVENTORY_SLOT_RIGHTHAND, oPC );
      if ( GetTag( oEquipped ) != "FishingRod" )
      {
         message ("You must equip your fishing rod, before starting to fish.", oPC );
         AssignCommand(oPC, ActionPlayAnimation(ANIMATION_FIREFORGET_PAUSE_SCRATCH_HEAD , 1.0, 2.0));
      }
      else
      {
         // Play "fishing animation" and sound
         ClearAllActions();
         AssignCommand( oPC, ActionPlayAnimation (ANIMATION_LOOPING_SIT_CROSS, 1.0f, DELAY_LENGTH) );
         PlaySound("as_na_splash2");
         DelayCommand (0.1f, SetCommandable (FALSE, oPC));
         DelayCommand (DELAY_LENGTH, SetCommandable (TRUE, oPC));
         float fDelay = DELAY_LENGTH + 0.1f;
         DelayCommand (fDelay, fish( oPC, oHasFishingRod) );
      }
   }



}