Looks like this is where merchants take money for buying items, so some of the code should be usable for GM Trainers once the formula for prices on skills is figured out.
Code:
void Client::Handle_OP_ShopPlayerBuy(const EQApplicationPacket *app)
{
RDTSC_Timer t1;
t1.start();
Merchant_Sell_Struct* mp=(Merchant_Sell_Struct*)app->pBuffer;
#if EQDEBUG >= 5
LogFile->write(EQEMuLog::Debug, "%s, purchase item..", GetName());
DumpPacket(app);
#endif
int merchantid;
bool tmpmer_used = false;
Mob* tmp = entity_list.GetMob(mp->npcid);
if (tmp == 0 || !tmp->IsNPC() || tmp->GetClass() != MERCHANT)
return;
if (mp->quantity < 1) return;
//you have to be somewhat close to them to be properly using them
if(DistNoRoot(*tmp) > USE_NPC_RANGE2)
return;
merchantid=tmp->CastToNPC()->MerchantType;
uint32 item_id = 0;
std::list<MerchantList> merlist = zone->merchanttable[merchantid];
std::list<MerchantList>::const_iterator itr;
for(itr = merlist.begin();itr != merlist.end();itr++){
MerchantList ml = *itr;
if(mp->itemslot == ml.slot){
item_id = ml.item;
break;
}
}
const Item_Struct* item = NULL;
int32 prevcharges = 0;
if (item_id == 0) { //check to see if its on the temporary table
std::list<TempMerchantList> tmp_merlist = zone->tmpmerchanttable[tmp->GetNPCTypeID()];
std::list<TempMerchantList>::const_iterator tmp_itr;
TempMerchantList ml;
for(tmp_itr = tmp_merlist.begin();tmp_itr != tmp_merlist.end();tmp_itr++){
ml = *tmp_itr;
if(mp->itemslot == ml.slot){
item_id = ml.item;
tmpmer_used = true;
prevcharges = ml.charges;
break;
}
}
}
item = database.GetItem(item_id);
if (!item){
//error finding item, client didnt get the update packet for whatever reason, roleplay a tad
Message(15,"%s tells you 'Sorry, that item is for display purposes only.' as they take the item off the shelf.",tmp->GetCleanName());
EQApplicationPacket* delitempacket = new EQApplicationPacket(OP_ShopDelItem, sizeof(Merchant_DelItem_Struct));
Merchant_DelItem_Struct* delitem = (Merchant_DelItem_Struct*)delitempacket->pBuffer;
delitem->itemslot = mp->itemslot;
delitem->npcid = mp->npcid;
delitem->playerid = mp->playerid;
delitempacket->priority = 6;
entity_list.QueueCloseClients(tmp,delitempacket); //que for anyone that could be using the merchant so they see the update
safe_delete(delitempacket);
return;
}
if (CheckLoreConflict(item))
{
Message(15,"You can only have one of a lore item.");
return;
}
if(tmpmer_used && (mp->quantity > prevcharges))
mp->quantity = prevcharges;
EQApplicationPacket* outapp = new EQApplicationPacket(OP_ShopPlayerBuy, sizeof(Merchant_Sell_Struct));
Merchant_Sell_Struct* mpo=(Merchant_Sell_Struct*)outapp->pBuffer;
mpo->quantity = mp->quantity;
mpo->playerid = mp->playerid;
mpo->npcid = mp->npcid;
mpo->itemslot=mp->itemslot;
sint16 freeslotid=0;
ItemInst* inst = database.CreateItem(item, mp->quantity);
bool stacked = TryStacking(inst);
if(!stacked)
freeslotid = m_inv.FindFreeSlot(false, true, item->Size);
//make sure we are not completely full...
if(freeslotid == SLOT_CURSOR) {
if(m_inv.GetItem(SLOT_CURSOR) != NULL) {
Message(13, "You do not have room for any more items.");
safe_delete(outapp);
safe_delete(inst);
return;
}
}
mpo->price = (item->Price*(1/.884)*Client::CalcPriceMod(tmp,false))*mp->quantity;
if(freeslotid == SLOT_INVALID || !TakeMoneyFromPP(mpo->price))
{
safe_delete(outapp);
safe_delete(inst);
return;
}
string packet;
if(mp->quantity==1 && item->MaxCharges>0 && item->MaxCharges<255)
mp->quantity=item->MaxCharges;
if (!stacked && inst) {
PutItemInInventory(freeslotid, *inst);
SendItemPacket(freeslotid, inst, ItemPacketTrade);
}
else if(!stacked){
LogFile->write(EQEMuLog::Error, "OP_ShopPlayerBuy: item->ItemClass Unknown! Type: %i", item->ItemClass);
}
QueuePacket(outapp);
if(inst && tmpmer_used){
sint32 new_charges = prevcharges - mp->quantity;
zone->SaveTempItem(merchantid, tmp->GetNPCTypeID(),item_id,new_charges);
if(new_charges<=0){
EQApplicationPacket* delitempacket = new EQApplicationPacket(OP_ShopDelItem, sizeof(Merchant_DelItem_Struct));
Merchant_DelItem_Struct* delitem = (Merchant_DelItem_Struct*)delitempacket->pBuffer;
delitem->itemslot = mp->itemslot;
delitem->npcid = mp->npcid;
delitem->playerid = mp->playerid;
delitempacket->priority = 6;
entity_list.QueueClients(tmp,delitempacket); //que for anyone that could be using the merchant so they see the update
safe_delete(delitempacket);
}
}
safe_delete(inst);
safe_delete(outapp);
if (zone->merchantvar!=0){
if (zone->merchantvar==7){
LogMerchant(this,tmp,mpo,item,true);
}
else if ((admin>=10) && (admin<20)){
if ((zone->merchantvar<8) && (zone->merchantvar>5))
LogMerchant(this,tmp,mpo,item,true);
}
else if (admin<=20){
if ((zone->merchantvar<8) && (zone->merchantvar>4))
LogMerchant(this,tmp,mpo,item,true);
}
else if (admin<=80){
if ((zone->merchantvar<8) && (zone->merchantvar>3))
LogMerchant(this,tmp,mpo,item,true);
}
else if (admin<=100){
if ((zone->merchantvar<9) && (zone->merchantvar>2))
LogMerchant(this,tmp,mpo,item,true);
}
else if (admin<=150){
if (((zone->merchantvar<8) && (zone->merchantvar>1)) || (zone->merchantvar==9))
LogMerchant(this,tmp,mpo,item,true);
}
else if (admin<=255){
if ((zone->merchantvar<8) && (zone->merchantvar>0))
LogMerchant(this,tmp,mpo,item,true);
}
}
t1.stop();
cout << "At 1: " << t1.getDuration() << endl;
return;
}