this is a trimmed down version of the plugin i use to give a random item to an npc.
Code:
#!/usr/bin/perl
# select a random itemID and give to npc
# returns itemID added
sub GetRandomItem {
# defult itemid
my $itemID = 0;
# get npc object from calling script
my $npc = plugin::val('$npc');
# bail if npc has no loot table
if ( !$npc->GetLoottableID() ) {
return 0;
}
# bail if missed chance for extra item
# assuming mob level tops at 80, this should max our chance @ 10%
if ( rand(100) > $npc->GetLevel() * .1 + .2 )
{
return 0;
}
# find valid item id to give to npc (attempts until it gets an item name)
# 1001 is min and 132476 is max itemID in database at time of writing
my $itemName = 0;
until ($itemName) {
$itemID = int( rand(131475) + 1001 );
$itemName = quest::getItemName($itemID);
}
# have npc equip item, where possible
$npc->AddItem( $itemID, 1, 1 );
$npc->Heal();
return $itemID;
}
1;
this is a trimmed version of the python script i used to modify all of the existing script files. as it stands, it is set up to run from a folder i use to keep up to date with the latest svn located in the EQEmu folder along with the running Quests directory. it adds EVENT_COMBAT to all existing npc scripts where needed (which is where the loot is added), and simply modifies the event if it already exists.
Code:
import os, sys
written = 0
modified = 0
zone = ''
script = ''
def getScriptList(zone):
script = []
for entry in os.listdir(zone):
if os.path.isdir(entry):
continue
script.append(entry)
return script
def modEventCombat(content):
# modifying or adding EVENT_COMBAT
found = 0
start = 0
for idx, val in enumerate(content):
if found:
break
if val.find('EVENT_COMBAT') > -1:
found = 1
start = idx
if val.find('{') == -1:
start = start+1
break
if found == 1:
content.insert(start+1, '\tif($combat_state) { plugin::GetRandomItem(); }\n')
else:
content.insert(0, 'sub EVENT_COMBAT\n{\n\tif($combat_state) { plugin::GetRandomItem(); }\n\n')
def parseScript(script):
global modified, written
try:
if(os.path.getmtime(script) < os.path.getmtime('..\\Quests\\' + script)):
return
except WindowsError:
pass
content = open(script).readlines()
if os.path.split(script)[-1] not in ('player.pl'):
modified = modified +1
modEventDeath(content)
modEventCombat(content)
modEventItem(content)
# writing new version of script to file
print 'WRITING: ..\\Quests\\%s' % script
written = written +1
script = open('..\\Quests\\' + script, 'w')
script.writelines(content)
script.close()
for entry in os.listdir('.'):
if not os.path.isdir(entry):
print 'SKIPPING: %s [not a directory]' % entry
continue
if entry[0] == '.':
print 'SKIPPING: %s [hidden directory]' % entry
continue
if entry in ('items', 'plugins', 'spells'):
print 'SKIPPING: %s [excluded directory]' % entry
continue
zone = entry
newz = os.path.join('..\\Quests', zone)
try:
os.stat(newz)
except:
os.mkdir(newz)
print 'CURRENT ZONE: %s' % zone
for script in getScriptList(zone):
parseScript(os.path.join(zone, script))
print
print 'modified: %d' % modified
print 'written : %d' % written