EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Archive::Quests (https://www.eqemulator.org/forums/forumdisplay.php?f=624)
-   -   NPC and Player Flags Code Fix for Perl (https://www.eqemulator.org/forums/showthread.php?t=11320)

Valdain 01-11-2004 06:22 AM

NPC and Player Flags Code Fix for Perl
 
Here are the code snippets for getting both player and npc flag functions working correctly. Since it looks like our perl functions can't return a value (or at least I don't know how to!) we need to add a new exported variable: returnflag. This will allow flagcheck to retrieve the flag's value.

In parser.cpp replace the "flagnpc" if-block with:
Code:

                        else if (!strcmp(strlwr(command),"flagnpc")) {
                                int8 tmpFlagNum = atoi(arglist[0]);
                                int8 tmpFlagVal = atoi(arglist[1]);
                                if(tmpFlagNum >= 0 && tmpFlagNum < 60)                                                               
                                {
                                        if (other)
                                          other->flag[tmpFlagNum] = tmpFlagVal;
                                }
                                else {
                                        cout << "[SCRIPT ERROR](flagnpc): A flag was requested that was not in the correct range." << endl;
                                }
                        }

Replace the "flagclient" if-block with:
Code:

                        else if (!strcmp(strlwr(command),"flagclient")) {//Valdain - fixed command name
                                int8 tmpFlagNum = atoi(arglist[0]);
                                int8 tmpFlagVal = atoi(arglist[1]);
                                if(tmpFlagNum >= 0 && tmpFlagNum < 60)                                                               
                                {
                                        if (mob && mob->IsClient())
                                                mob->CastToClient()->flag[tmpFlagNum] = tmpFlagVal;
                                }
                                else{
                                        cout << "[SCRIPT ERROR](flagclient): A flag was requested that was not in the correct range." << endl;
                                }
                        }

Replace the "flagcheck" if-block with:
Code:

                        else if (!strcmp(strlwr(command),"flagcheck")) {
                                int8 tmpFlagNum1 = atoi(arglist[0]);                        //Valdain
                                int8 tmpFlagNum2 = atoi(arglist[1]);
                                if(tmpFlagNum1 >= 0 && tmpFlagNum1 < 60)                                                               
                                {
                                        if(tmpFlagNum2==0)        //Client Flag
                                        {
                        if(other && mob && mob->IsClient())
                                                        other->returnflag = mob->CastToClient()->flag[tmpFlagNum1];
                                        }
                                        else if(tmpFlagNum2==1) //NPC Flag
                                        {
                        if(other)
                                                        other->returnflag = other->flag[tmpFlagNum1];
                                        }
                                        else
                                                cout << "[SCRIPT ERROR](flagcheck): tmpFlagNum2 wasn't 0 or 1!" << endl;
                                }
                                else {
                                        cout << "[SCRIPT ERROR(]flagcheck): A flag was requested that was not in the correct range." << endl;
                                }
                        }

Next, in mob.h we need to add:
Code:

        bool                        CombatRange(Mob* other);
        int8                        flag[60];
--->>>        int8                        returnflag; //Valdain - Flag used to store value for flagcheck

Those take care of getting the core functions working. Now for the additions to the perl stuff.

In embparser.cpp in PerlembParser::Event:
Code:

        if (npcmob)
        {
                ExportVar(packagename.c_str(), "mname", npcmob->GetName());
// MYRA - added vars $mobid & $mlevel per Eglin
                ExportVar(packagename.c_str(), "mobid", itoa(npcmob->GetID()));
                ExportVar(packagename.c_str(), "mlevel", itoa(npcmob->GetLevel()));
//end Myra
//Valdain - added $returnflag
--->>>                ExportVar(packagename.c_str(), "returnflag", itoa(npcmob->returnflag));
        }

Usage

flagnpc and flagclient work the same:
Code:

quest::flagnpc(flag_number, flag_value);
quest::flagclient(flag_number, flag_value);

Where:
flag_number can be from 0 to 59
flag_value can be from 0 to 255

flagcheck now works!
Code:

quest::flagcheck(flag_number, flag_type);
Where:
flag_number can be 0 to 59
flag_type = 0 to check the player's flag or 1 to check the NPC's flags

Getting the results
Now that flagcheck works, we can get the results by getting the $returnflag variable.

Example:
Code:

sub EVENT_SAY() {
  if ($text=~/hail/i){
          quest::say("Greetings kind sir. Would you like to buy a [ring]?");
          quest::flagnpc(1,1);
          quest::flagcheck(1,1);  #check flag 1, on the NPC so we can use it next time
  }

  if ($text=~/ring/i){
          if($returnflag == 1)
          {
                quest::say("It's a special ring they say.  Worth fair amount of money too.");
                quest::flagnpc(1,2);
                quest::flagcheck(1,1); #check flag for next time
          }
          else
          {
                quest::say("I don't know what you're talking about.");
          }
  }
}

Limitations
One limitation I've noticed in testing is that you can't set or check a flag early in the event, then use flagcheck or returnflag to get that value later on in the event. You must follow the example and pre-get the value for next time.

Incorrect Example:
Code:

sub EVENT_SAY() {
  if ($text=~/hail/i){
        quest::say("Greetings kind sir. Would you like to buy a [ring]?");
        quest::flagnpc(1,1); #set the flag to 1
  }

quest::flagcheck(1,1);
quest::say("Flag 1: $returnflag");
}

$returnflag will be the value of the flag the LAST time the event was called. So Flag 1 may not return 1! Be sure to code your events to only check for flags on the next firing of the event like in the first example!

Update
flagcheck isn't working as well as I would have hoped. It appears that the perl-side variables aren't being update as quickly as they should be. I'm still working on a fix for it, but I'm still learning how the script system works. flagnpc and flagclient are working just as they should.

Hope that helps all you QuestBuilders out there!
-Valdain

Lurker_005 01-11-2004 09:58 PM

Nice work! I just added a link to this thread from my perl howto.

when I get some free time I'll test this out.

Trumpcard 01-12-2004 01:24 AM

Valdain, you should go into IRC and chat with Lethal/devn00b about developer access... You're code fixes are starting to get to long for my simple mind to merge in...

Scorpious2k 01-12-2004 02:44 AM

Quote:

Originally Posted by Trumpcard
to long for my simple mind to merge in...

Heh... "simple mind".... some of us have gotten to know you better than that. Although for a moment I did suffer a bit of envy about your humility (I got over it quickly)

BTW, I have lastname in NPC_types working and NPCs behaving better (less cases of it being to far away to attack when you are standing over it and/or vanishing).

I am going to sit on the NPC one until its right, but if you're interested I can post the lastname fix.

Trumpcard 01-12-2004 04:09 AM

You're another one that needs to just suck it up and request developer access!

Merge your own damn changes!

Scorpious2k 01-12-2004 05:03 AM

Quote:

Originally Posted by Trumpcard
Merge your own damn changes!

OK, I'll take that as a "not at this time, thank you" :-)


All times are GMT -4. The time now is 06:39 AM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.