Page 1 of 1

Pirate quest with tracking set up

Posted: Fri Feb 10, 2012 4:38 am
by dragon slayer
Okay here it is. I think i got it right
var NPCPause = 6000;
var questTrackString = "";
var tempString = pUser.GetTag( "QuestTracker" );
var questNumber = 1; //Is the number of quest
var questStatus = "1"; //changes the postion of the number

function onCharDoubleClick( pUser, targChar )
{
   if( !targChar.InRange( pUser, 4 ) )
   {
      pUser.SysMessage( "Not close enough" );
      return false;
   }
   else
            targChar.SetTimer( 11, NPCPause );
            pUser.SetTag( "PQ_START", 1 );
            var pirateGump = new Gump;

            pirateGump.AddPage(0);
            pirateGump.AddTiledGump(54, 33, 369, 400, 2624);
            pirateGump.AddCheckerTrans(54, 33, 369, 400);
            pirateGump.AddTiledGump(416, 39, 44, 389, 203);
            pirateGump.AddGump(97, 49, 9005);
            pirateGump.AddTiledGump(58, 39, 29, 390, 10460);
            pirateGump.AddTiledGump(412, 37, 31, 389, 10460);
            pirateGump.AddText(140, 60, 0x34, "Pirate Quest");
            pirateGump.AddHTMLGump(107, 140, 300, 230, true, false, "Hail "+pUser.name+". Could you so kindly help me with a problem i am having with all this pirates they are coming up to the harbor and stealing all my goods that we get in i need them stopped. Will you help me?");
            pirateGump.AddGump(430, 9, 10441);
            pirateGump.AddTiledGump(40, 38, 17, 391, 9263);
            pirateGump.AddGump(6, 25, 10421);
            pirateGump.AddGump(34, 12, 10420);
            pirateGump.AddTiledGump(94, 25, 342, 15, 10304);
            pirateGump.AddTiledGump(40, 427, 415, 16, 10304);
            pirateGump.AddGump(-10, 314, 10402);
            pirateGump.AddGump(56, 150, 10411);
            pirateGump.AddGump(155, 120, 2103);
            pirateGump.AddGump(136, 84, 96);
            pirateGump.AddButton(225, 390, 0xF7,0, 0,0);
            pirateGump.Send( pUser );
            pirateGump.Free();
            return false;
}

function onSpeech( myString, myPlayer, myNPC, pSock )
{
   if( !myNPC.InRange( myPlayer, 2 ) )
      return;
   var startQuest = myPlayer.GetTag( "PQ_START" );
   var Speech_Array = myString.split(" ");
   var i = 0, currObj = 0;

   for( i = 1; i <= Speech_Array.length; i++ )
   {
      if( Speech_Array[currObj].match( /\bHail\b/i ) || Speech_Array[currObj].match( /\bHi\b/i ) || Speech_Array[currObj].match( /\bHello\b/i ) )
      {
         myNPC.TurnToward( myPlayer );
         myNPC.TextMessage( "Hail "+myPlayer.name+". You seem like a brave sort, I may have a quest for you." );
      }
      else if( Speech_Array[currObj].match( /\bQuest\b/i ) || Speech_Array[currObj].match( /\bAdventure\b/i ) || Speech_Array[currObj].match( /\bTask\b/i ) )    
      {
         myNPC.TurnToward( myPlayer );
         if( myPlayer.GetTag( "PQ_LEVEL" ) )
            myNPC.TextMessage( "I have already given you a quest, you may request your reward when you have completed it" );
         else
         {
            myPlayer.SetTag( "PQ_START", 1 );
            var pirateGump = new Gump; // create a new gump  

            pirateGump.AddPage(1);
            pirateGump.AddBackground( 0, 0, 400, 350, 0xA28 );
            pirateGump.AddBackground( 30, 115, 350, 170, 0x0bb8 ); // Tile White Bacground
            pirateGump.AddGump( 130, 20, 0x64 ); // Sign Image
            pirateGump.AddText( 160, 60, 0, "Pirate Quest" );
            pirateGump.AddText( 50, 120, 0, "I am in need of your services to kill some pirates," );
            pirateGump.AddText( 40, 140, 0, "I can offer you great treasures as a reward." );
            pirateGump.AddText( 60, 300, 0, "Accept" );
            pirateGump.AddText( 280, 300, 0, "Cancel" );
            pirateGump.AddButton( 320, 300, 0x0fa5, 1, 0, 0 ); // cancel
            pirateGump.AddButton( 100, 300, 0x0fa5, 1, 0, 3 ); // accept
            pirateGump.Send( myPlayer );
            pirateGump.Free();
            return false;
         }
      }
      else if( Speech_Array[currObj].match( /\bReward\b/i ) )
      {
         myNPC.TurnToward( myPlayer );
         var taskLevel = myPlayer.GetTag( "PQ_LEVEL" );
         if( taskLevel )
         {
            var numToKill = myPlayer.GetTag( "PQ_NUMTOKILL" );
            if( numToKill > 0 )
            {
            var pirateGump = new Gump; // create a new gump  

               pirateGump.AddPage(0);
               pirateGump.AddBackground( 0, 0, 400, 350, 0xA28 );
               pirateGump.AddBackground( 30, 115, 350, 170, 0x0bb8 );
               pirateGump.AddGump( 130, 20, 0x64 ); // Sign Image
               pirateGump.AddText( 150, 30, 0, "Pirate Quest" );
               pirateGump.AddText( 180, 60, 0, "Unfinished" );
               pirateGump.AddText( 82, 120, 0, "Alas, you still have "+NumToString( numToKill )+" more pirates to slay" );
               pirateGump.AddText( 30, 140, 0, "before you receive your reward!" );
               pirateGump.AddText( 35, 300, 0, "Keep Going" );
               pirateGump.AddText( 160, 300, 0, "Quite" );
               pirateGump.AddButton( 200, 300, 0x0fa5, 1, 0, 2 ); //quite
               pirateGump.AddButton( 100, 300, 0x0fa5, 1, 0, 3 ); // keep going
               pirateGump.Send( myPlayer ); // send this gump to client now
               pirateGump.Free(); // clear this gump from uox-memory
               return false;
            }
            else
            {
               myNPC.TextMessage( "Wonderful work my friend, here is your reward." );
               questTrackString = tempString.substr(0, questNumber - 1) + questStatus + tempString.substr(questNumber);
               var goldToGive = 0;
               switch( taskLevel )
               {
               case 1:      goldToGive = 500;      break;
               case 2:      goldToGive = 1000;      break;
               case 3:      goldToGive = 2000;      break;
               case 4:      goldToGive = 4000;      break;
               }
               CreateDFNItem( myPlayer.socket, myPlayer, "0x0EED", goldToGive, "ITEM", true );
               myPlayer.SetTag( "QuestTracker", questtrackerstring );
               myPlayer.SoundEffect( 0x0037, false );
               myPlayer.SetTag( "PQ_NUMTOKILL", 0 );
               myPlayer.SetTag( "PQ_IDTOKILL", 0 );
               myPlayer.SetTag( "PQ_LEVEL", 0 );
            }
          }
          else
            myNPC.TextMessage( "You must first accept my quest to receive a reward." );
        }
        else if( Speech_Array[currObj].match( /\bQuite\b/i ) )
        {
               myNPC.TurnToward( myPlayer );
               myNPC.TextMessage( "I'll find some other brave soul to take on this nasty pirates!" );
               myPlayer.SetTag( "PQ_NUMTOKILL", -1 );
               myPlayer.SetTag( "PQ_IDTOKILL", 0 );
               myPlayer.SetTag( "PQ_LEVEL", 0 );
        }
        currObj++;
   }
}

function onGumpPress(pSock, myButtonID)
{
  var myPlayer = pSock.currentChar;
   switch( myButtonID )
   {
      case 0: // no
         myPlayer.SysMessage("I'll find some other brave soul to take on this nasty pirates!");
      break;
      case 1: // yes
         var pirateGump = new Gump; // create a new gump
         var numToKill = RandomNumber( 7, 14 );
         var typeToKill = CreateGenericQuest( myPlayer, 1, numToKill );

            pirateGump.AddPage(0);
            pirateGump.AddBackground( 0, 0, 400, 350, 0xA28 );
            pirateGump.AddBackground( 30, 115, 350, 170, 0x0bb8 ); // Tile White Bacground
            pirateGump.AddGump( 130, 20, 0x64 ); // Sign Image
            pirateGump.AddText( 150, 30, 0, "Pirate Quest" );
            pirateGump.AddText( 180, 60, 0, "Accepted" );
            pirateGump.AddText( 82, 120, 0, "Excellent, please return to me after you" );
            pirateGump.AddText( 30, 140, 0, "have slain "+NumToString( numToKill )+" "+typeToKill+"." );
            pirateGump.AddText( 35, 300, 0, "Keep Going" );
            pirateGump.AddText( 160, 300, 0, "Quite" );
            pirateGump.AddButton( 200, 300, 0x0fa5, 1, 0, 2 ); //quite
            pirateGump.AddButton( 100, 300, 0x0fa5, 1, 0, 3 ); // keep going
            pirateGump.Send( myPlayer ); // send this gump to client now
            pirateGump.Free(); // clear this gump from uox-memory
            return false;
      break;
      case 2: // Quite
         myPlayer.SetTag( "PQ_NUMTOKILL", 0 );
         myPlayer.SetTag( "PQ_IDTOKILL", 0 );
         myPlayer.SetTag( "PQ_LEVEL", 0 );  
         myPlayer.SysMessage("Thanks for quiting.");
      break;
      case 3: // keep going
         myPlayer.SysMessage("Thanks for not quiting.");
      break;
    }
}

function CreateGenericQuest( myPlayer, questLevel, numToKill )
{
   var typeToKill = GetMonsterType( myPlayer, questLevel );
   myPlayer.SetTag( "PQ_NUMTOKILL", numToKill );
   myPlayer.SetTag( "PQ_LEVEL", questLevel );
   myPlayer.SetTag( "PQ_START", 0 );
   return typeToKill;
}

function GetMonsterType( myPlayer, questLevel )
{
   var retVal = "";
   switch( questLevel )
   {
   case 1:
      switch( RandomNumber( 0, 2 ) )
      {
      case 0:
         myPlayer.SetTag( "PQ_IDTOKILL", 0x0191 );
         retVal = "Female Pirates";
         break;
      case 1:
         myPlayer.SetTag( "PQ_IDTOKILL", 0x0190 );
         retVal = "Male Pirates";
         break;
      }
      break;
   }
   return retVal;
}

Re: Pirate quest with tracking set up

Posted: Fri Feb 10, 2012 4:47 am
by Xuri
That should work, but you should try to avoid to declare any variables outside your functions, as they become global across all instances of the script that are running!. This means that if player A executes the script, and his questtracker-string is loaded into the global variable, it will be overwritten if player B executes the same script shortly after. Try to keep global variables restricted to ones that are constant and will never change. questTrackString and tempString you should declare in the scripts where they're going to be used, as they'll need to change for every instance of the script that runs.

Also:
myPlayer.SetTag( "PQ_NUMTOKILL", 0 );
This will still keep the tag PQ_NUMTOKILL present on the player. To remove the tag from the player completely (if he abandons quest, for instance), use:
myPlayer.SetTag( "PQ_NUMTOKILL", null );

Re: Pirate quest with tracking set up

Posted: Fri Feb 10, 2012 4:52 am
by dragon slayer
thank you :)

reworking it all now :)

Re: Pirate quest with tracking set up

Posted: Sat Feb 11, 2012 5:51 am
by dragon slayer
Okay getting error
Here is code fixed some things to make it work right

Code: Select all

var NPCPause = 6000;
var questNumber = 1; //Is the number of quest
var questStatus = "1"; //changes the postion of the number

function onCharDoubleClick( pUser, targChar )
{
   if( !targChar.InRange( pUser, 4 ) )
   {
      pUser.SysMessage( "Not close enough" ); 
      return false;
   }
   else
            targChar.SetTimer( 11, NPCPause );
            pUser.SetTag( "PQ_START", 1 ); 
            var pirateGump = new Gump;

            pirateGump.AddPage(0);
            pirateGump.AddTiledGump(54, 33, 369, 400, 2624);
            pirateGump.AddCheckerTrans(54, 33, 369, 400);
            pirateGump.AddTiledGump(416, 39, 44, 389, 203);
            pirateGump.AddGump(97, 49, 9005);
            pirateGump.AddTiledGump(58, 39, 29, 390, 10460);
            pirateGump.AddTiledGump(412, 37, 31, 389, 10460);
            pirateGump.AddText(140, 60, 0x34, "Pirate Quest");
            pirateGump.AddHTMLGump(107, 140, 300, 230, true, false, "Hail "+pUser.name+". Could you so kindly help me with a problem i am having with all this pirates they are coming up to the harbor and stealing all my goods that we get in i need them stopped. Will you help me?");
            pirateGump.AddGump(430, 9, 10441);
            pirateGump.AddTiledGump(40, 38, 17, 391, 9263);
            pirateGump.AddGump(6, 25, 10421);
            pirateGump.AddGump(34, 12, 10420);
            pirateGump.AddTiledGump(94, 25, 342, 15, 10304);
            pirateGump.AddTiledGump(40, 427, 415, 16, 10304);
            pirateGump.AddGump(-10, 314, 10402);
            pirateGump.AddGump(56, 150, 10411);
            pirateGump.AddGump(155, 120, 2103);
            pirateGump.AddGump(136, 84, 96);
            pirateGump.AddButton(225, 390, 0xF7,0, 0,0);
            pirateGump.Send( pUser );
            pirateGump.Free();
            return false;
}

function onSpeech( myString, myPlayer, myNPC, pSock ) 
{ 
   if( !myNPC.InRange( myPlayer, 2 ) ) 
      return; 
   var startQuest = myPlayer.GetTag( "PQ_START" ); 
   var Speech_Array = myString.split(" "); 
   var i = 0, currObj = 0; 

   for( i = 1; i <= Speech_Array.length; i++ ) 
   { 
      if( Speech_Array[currObj].match( /\bHail\b/i ) || Speech_Array[currObj].match( /\bHi\b/i ) || Speech_Array[currObj].match( /\bHello\b/i ) ) 
      { 
         myNPC.TurnToward( myPlayer ); 
         myNPC.TextMessage( "Hail "+myPlayer.name+". You seem like a brave sort, I may have a quest for you." ); 
      } 
      else if( Speech_Array[currObj].match( /\bQuest\b/i ) || Speech_Array[currObj].match( /\bAdventure\b/i ) || Speech_Array[currObj].match( /\bTask\b/i ) )    
      { 
         myNPC.TurnToward( myPlayer ); 
         if( myPlayer.GetTag( "PQ_LEVEL" ) ) 
            myNPC.TextMessage( "I have already given you a quest, you may request your reward when you have completed it" ); 
         else 
         { 
            myPlayer.SetTag( "PQ_START", 1 ); 
            myPlayer.SetTag( "QuestTracker", 0000000000 );
            var pirateGump = new Gump; // create a new gump  

            pirateGump.AddPage(1);
            pirateGump.AddBackground( 0, 0, 400, 350, 0xA28 );
            pirateGump.AddBackground( 30, 115, 350, 170, 0x0bb8 ); // Tile White Bacground
            pirateGump.AddGump( 130, 20, 0x64 ); // Sign Image
            pirateGump.AddText( 160, 60, 0, "Pirate Quest" );
            pirateGump.AddText( 50, 120, 0, "I am in need of your services to kill some pirates," );
            pirateGump.AddText( 40, 140, 0, "I can offer you great treasures as a reward." );
            pirateGump.AddText( 60, 300, 0, "Accept" );
            pirateGump.AddText( 280, 300, 0, "Cancel" );
            pirateGump.AddButton( 320, 300, 0x0fa5, 1, 0, 0 ); // cancel
            pirateGump.AddButton( 100, 300, 0x0fa5, 1, 0, 1 ); // accept
            pirateGump.Send( myPlayer );
            pirateGump.Free();
            return false; 
         } 
      } 
      else if( Speech_Array[currObj].match( /\bReward\b/i ) ) 
      { 
         myNPC.TurnToward( myPlayer ); 
         var taskLevel = myPlayer.GetTag( "PQ_LEVEL" ); 
         if( taskLevel ) 
         { 
            var numToKill = myPlayer.GetTag( "PQ_NUMTOKILL" ); 
            if( numToKill > 0 )
            {
            var pirateGump = new Gump; // create a new gump  

               pirateGump.AddPage(0);
               pirateGump.AddBackground( 0, 0, 400, 350, 0xA28 );
               pirateGump.AddBackground( 30, 115, 350, 170, 0x0bb8 );
               pirateGump.AddGump( 130, 20, 0x64 ); // Sign Image
               pirateGump.AddText( 150, 30, 0, "Pirate Quest" );
               pirateGump.AddText( 180, 60, 0, "Unfinished" );
               pirateGump.AddText( 82, 120, 0, "Alas, you still have "+NumToString( numToKill )+" more pirates to slay" );
               pirateGump.AddText( 30, 140, 0, "before you receive your reward!" );
               pirateGump.AddText( 35, 300, 0, "Keep Going" );
               pirateGump.AddText( 160, 300, 0, "Quite" );
               pirateGump.AddButton( 200, 300, 0x0fa5, 1, 0, 2 ); //quite
               pirateGump.AddButton( 100, 300, 0x0fa5, 1, 0, 3 ); // keep going
               pirateGump.Send( myPlayer ); // send this gump to client now 
               pirateGump.Free(); // clear this gump from uox-memory 
               return false; 
            }
            else
            { 
               var questTrackString = "";
               var tempString = pUser.GetTag( "QuestTracker", 0000000000 );
               myNPC.TextMessage( "Wonderful work my friend, here is your reward." );
               questTrackString = tempString.substr(0, questNumber - 1) + questStatus + tempString.substr(questNumber); 
               var goldToGive = 0; 
               switch( taskLevel ) 
               { 
               case 1:      goldToGive = 500;      break; 
               case 2:      goldToGive = 1000;      break; 
               case 3:      goldToGive = 2000;      break; 
               case 4:      goldToGive = 4000;      break; 
               } 
               CreateDFNItem( myPlayer.socket, myPlayer, "0x0EED", goldToGive, "ITEM", true ); 
               myPlayer.SetTag( "QuestTracker", questTrackString );
               myPlayer.SoundEffect( 0x0037, false ); 
               myPlayer.SetTag( "PQ_NUMTOKILL", null ); 
               myPlayer.SetTag( "PQ_IDTOKILL", null ); 
               myPlayer.SetTag( "PQ_LEVEL", null ); 
            } 
          }
          else 
            myNPC.TextMessage( "You must first accept my quest to receive a reward." ); 
        } 
        else if( Speech_Array[currObj].match( /\bQuite\b/i ) ) 
        { 
               myNPC.TurnToward( myPlayer );
               myNPC.TextMessage( "I'll find some other brave soul to take on this nasty pirates!" );
               myPlayer.SetTag( "PQ_NUMTOKILL", null ); 
               myPlayer.SetTag( "PQ_IDTOKILL", null ); 
               myPlayer.SetTag( "PQ_LEVEL", null ); 
        } 
        currObj++; 
   } 
}

function onGumpPress(pSock, myButtonID) 
{ 
  var myPlayer = pSock.currentChar;
   switch( myButtonID ) 
   { 
      case 0: // no
         myPlayer.SysMessage("I'll find some other brave soul to take on this nasty pirates!");
      break; 
      case 1: // yes 
         var pirateGump = new Gump; // create a new gump
         var numToKill = RandomNumber( 7, 14 ); 
         var typeToKill = CreateGenericQuest( myPlayer, 1, numToKill );

            pirateGump.AddPage(0);
            pirateGump.AddBackground( 0, 0, 400, 350, 0xA28 );
            pirateGump.AddBackground( 30, 115, 350, 170, 0x0bb8 ); // Tile White Bacground
            pirateGump.AddGump( 130, 20, 0x64 ); // Sign Image
            pirateGump.AddText( 150, 30, 0, "Pirate Quest" );
            pirateGump.AddText( 180, 60, 0, "Accepted" );
            pirateGump.AddText( 82, 120, 0, "Excellent, please return to me after you" );
            pirateGump.AddText( 30, 140, 0, "have slain "+NumToString( numToKill )+" "+typeToKill+"." );
            pirateGump.AddText( 35, 300, 0, "Keep Going" );
            pirateGump.AddText( 160, 300, 0, "Quite" );
            pirateGump.AddButton( 200, 300, 0x0fa5, 1, 0, 2 ); //quite
            pirateGump.AddButton( 100, 300, 0x0fa5, 1, 0, 3 ); // keep going
            pirateGump.Send( myPlayer ); // send this gump to client now 
            pirateGump.Free(); // clear this gump from uox-memory 
            return false; 
      break;
      case 2: // Quite
         myPlayer.SetTag( "PQ_NUMTOKILL", null ); 
         myPlayer.SetTag( "PQ_IDTOKILL", null ); 
         myPlayer.SetTag( "PQ_LEVEL", null );  
         myPlayer.SysMessage("Thanks for quiting.");
      break;
      case 3: // keep going
         myPlayer.SysMessage("Thanks for not quiting.");
      break; 
    }
}

function CreateGenericQuest( myPlayer, questLevel, numToKill ) 
{ 
   var typeToKill = GetMonsterType( myPlayer, questLevel ); 
   myPlayer.SetTag( "PQ_NUMTOKILL", numToKill ); 
   myPlayer.SetTag( "PQ_LEVEL", questLevel ); 
   myPlayer.SetTag( "PQ_START", 0 ); 
   return typeToKill; 
} 

function GetMonsterType( myPlayer, questLevel ) 
{ 
   var retVal = ""; 
   switch( questLevel ) 
   { 
   case 1: 
      switch( RandomNumber( 0, 2 ) ) 
      { 
      case 0: 
         myPlayer.SetTag( "PQ_IDTOKILL", 0x0191 ); 
         retVal = "Female Pirates"; 
         break; 
      case 1: 
         myPlayer.SetTag( "PQ_IDTOKILL", 0x0190 ); 
         retVal = "Male Pirates"; 
         break; 
      } 
      break; 
   } 
   return retVal; 
} 
Here is error I'm getting

Code: Select all

[February 09 10:43:19 PM] Invalid Count of Parameters: 2, need: 1
[February 09 10:43:19 PM] JS script failure: Script Number (3019) Message (TypeError: tempString.substr is not a function)
[February 09 10:43:19 PM] Filename: ./js/custom/Quests/piratequest.js
| Line Number: 116
[February 09 10:43:19 PM] Erroneous Line: (null)
| Token Ptr: (null)