PDA

View Full Version : getting item names


sheck
04-04-2004, 09:58 AM
Is there a quest function that will return an item's name given it's id number? Not that it would be that difficult to write one, just wondering if one is already available...

Just an example, but rather than typing "rusty warhammer" I'd like to replace it with someFunction($item1), where someFunction fetches the item's name from the DB:
sub EVENT_ITEM{
if($item1==6016){
quest::say("Ooh, a rusty warhammer - here's your reward!");
quest::givecash(5,0,0,0);
quest::exp(100);
}
return 1;
}

Thanks,

Jason

m0oni9
04-05-2004, 06:23 AM
The best way to accomplish this would be to call a function on the server, and have it returned to perl. I was working on a good way to do this, but it kind of petered out. The way I have things now, I can get something like this to work:

sub EVENT_ITEM{
if($item1==6016){
$iname = sc::getItemName($item1);
quest::say("Ooh, $iname - here's your reward!");
}
}

The only problem is that there is a module.pm file that needs to be present, and there really should be another way to do this. I made some progress (no shared library, no "use module"). Eglin mentioned looking at how pickle does this (never got around to this, sadly). I'm going to take another whack at this in the next day or two, and see if I can come up with anything.

m0oni9
04-06-2004, 04:17 PM
This is more of a pre-submission. I think I have this worked out. I'll spare the details to create the code, and just give it to you. I've trimmed this down to the least amount of code to keep it workable, I think.. These changes all occur within the zone directory.

First, we are going to create a new file. Name it cperl.cpp, and paste into it the following:
#ifdef EMBPERL

#include "masterentity.h"

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "cperl.h"

extern Database database;

const char *getItemName(unsigned itemid)
{
const Item_Struct* item = NULL;
item = database.GetItem(itemid);

if (item)
return item->Name;
else
return NULL;
}

XS(XS_qc_getItemName); /* prototype to pass -Wmissing-prototypes */
XS(XS_qc_getItemName)
{
dXSARGS;
if (items != 1)
Perl_croak(aTHX_ "Usage: quest::getItemName(itemid)");
{
unsigned itemid = (unsigned)SvUV(ST(0));
const char * RETVAL;
dXSTARG;
RETVAL = getItemName(itemid);
sv_setpv(TARG, RETVAL); XSprePUSH; PUSHTARG;
}
XSRETURN(1);
}


EXTERN_C XS(boot_qc); /* prototype to pass -Wmissing-prototypes */
EXTERN_C XS(boot_qc)
{
dXSARGS;
char* file = __FILE__;

XS_VERSION_BOOTCHECK ;

newXS("quest::getItemName", XS_qc_getItemName, file);

XSRETURN_YES;
}

#endif // EMBPERL
Now, create a file named cperl.h. Paste this into it:

#ifndef CPERL_H
#define CPERL_H
const char *getItemName(unsigned itemid);
XS(XS_qc_getItemName); /* prototype to pass -Wmissing-prototypes */
EXTERN_C XS(boot_qc); /* prototype to pass -Wmissing-prototypes */
#endif // CPERL_H

Now, open up embperl.cpp. At about line 17, you will have:
#include "embperl.h"
Below this, insert:
#include "cperl.h"
At about line 30, you will see the following:
EXTERN_C void xs_init(pTHX) { char *file = __FILE__; newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); }

We're going to add a new line to it (and put in some line breaks for readability sake):
EXTERN_C void xs_init(pTHX) {
char *file = __FILE__;
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
newXS("quest::boot_qc", boot_qc, file);
}

Now, open up embparser.cpp. About line 380 you will have this:
perl->eval(
"{"
"package quest;"
"@cmd_queue = ();"
Between the "package" and "@cmd_queue" lines we want to paste in "&boot_qc;". It will look like this:
perl->eval(
"{"
"package quest;"
"&boot_qc;"
"@cmd_queue = ();"
Finally, we need to change makefile.perl -- I don't know what you windows users do to add a file to the project. :) At the veerrrryyy end of SF=.... (line 14) we are going to add cperl.o. It will then look like this:
../common/EMuShareMem.o ../common/EQEMuError.o beacon.o \
.obj/debug.o .obj/database.o .obj/Item.o .obj/misc.o \
doors.o command.o cperl.o
Note, if you don't have beacon.o in there, that's because I added it to my makefile so that it would compile. You should do the same if you don't have it included in the SF=... line. :D Recompile, and you should now be able to use your script:

sub EVENT_ITEM {
quest::say("Ooh, a " . quest::getItemName($item1) . "! Here's your reward!");
quest::givecash(5,0,0,0);
quest::exp(100);
}

Have not been able to get anyone besides myself to test this code yet, so please let me know how you fare.

(last update 4/7/04)

Scorpious2k
04-25-2004, 01:22 AM
I've put this on our server with the intention of moving it to cvs from there. This seems to be the answer to the one problem with perl - the inability to get dynamic values back from the server.

I think we have the start of a solution for that here. Thanks for the great work m0oni9!