eqemu-server/zone/perl_questitem.cpp
hg e01ac39887
[Quest API] Send delivered task items in trade events (#2518)
This restores sending items to EVENT_TRADE that are updated by source
controlled delivery tasks which was removed in 7cf96ca2d8.

That patch filtered out items consumed by task updates to fix a few bugs
with items being returned despite incrementing a task:

  - If an npc without a quest trade event handler was the target of a
    delivery task for a NoDrop/non-Quest item, the npc would auto return
    it due to the `ReturnNonQuestNoDropItems` rule.

  - If an npc without a quest trade event handler was the target of a
    delivery task for a non-NoDrop item, the item would be added to the
    npc's loot.

  - If an npc with an EVENT_ITEM/EVENT_TRADE quest handler used the Lua
    or Perl trade plugins, the plugins would return task items unless
    specific checks for the turned in slots existed.

The quest plugin item returns are problematic for this since they just
summon to return items not handled by the script

  e.g. For a task to deliver N Large Fruit Bat Wings (item id 19616),
  if a player turned in 1 Wing in slot 1 and a stack of 20 Wings in slot
  2, the task would be incremented 21 times and the following Lua trade
  handler would return the stack of 20 from the 2nd trade slot:

  ```lua
    function event_trade(e)
      local item_lib = require("items")
      if item_lib.check_turn_in(e.trade, { item1 = 19616 }) then
        eq.debug("Lua consumed 1 slot and will return other slots")
      end
      item_lib.return_items(e.self, e.other, e.trade)
    end
  ```

  This also occured with the perl plugin though slightly differently
  since that plugin returns all slots unless the exact handin slot count
  matches (requiring check_handin conditions for all slots):

  ```perl
    sub EVENT_ITEM {
      if (plugin::check_handin(\%itemcount, 19616 => 1)) {
        # No issue if only one slot used for trade (item not returned)
      }
      # Perl fails handin check if multiple slots not checked and returns all
      plugin::return_items(\%itemcount);
    }
  ```

While that patch solved the issue, it's inconvenient and wrong to not
receive items in trade events used in a source task update. It breaks
existing trade scripts for tasks that aren't quest controlled and it
forces tasks to be changed to quest controlled and manually updated to
script any extra behavior.

This patch stores the task update count on the item instance before
dispatching it to quests. The burden is now on quests and plugins to
use that value in order to prevent returning items consumed by tasks.

`ItemInstance::RemoveTaskDeliveredItems` has been added to simplify
handling this in plugins which is also used for non-quest item returns.
2022-11-06 17:10:30 -05:00

110 lines
3.4 KiB
C++

#include "../common/features.h"
#include "client.h"
#ifdef EMBPERL_XS_CLASSES
#include "../common/global_define.h"
#include "../common/item_instance.h"
#include "embperl.h"
std::string Perl_QuestItem_GetName(EQ::ItemInstance* self) // @categories Inventory and Items
{
return self->GetItem()->Name;
}
void Perl_QuestItem_SetScale(EQ::ItemInstance* self, float scale_multiplier) // @categories Inventory and Items
{
if (self->IsScaling()) {
self->SetExp((int) (scale_multiplier * 10000 + .5));
}
}
void Perl_QuestItem_ItemSay(EQ::ItemInstance* self, const char* text) // @categories Inventory and Items
{
quest_manager.GetInitiator()->ChannelMessageSend(self->GetItem()->Name, 0, 8, 0, 100, text);
}
void Perl_QuestItem_ItemSay(EQ::ItemInstance* self, const char* text, int language_id) // @categories Inventory and Items
{
quest_manager.GetInitiator()->ChannelMessageSend(self->GetItem()->Name, 0, 8, language_id, 100, text);
}
bool Perl_QuestItem_IsType(EQ::ItemInstance* self, int type) // @categories Inventory and Items
{
return self->IsType(static_cast<EQ::item::ItemClass>(type));
}
bool Perl_QuestItem_IsAttuned(EQ::ItemInstance* self) // @categories Inventory and Items
{
return self->IsAttuned();
}
int Perl_QuestItem_GetCharges(EQ::ItemInstance* self) // @categories Inventory and Items
{
return self->GetCharges();
}
EQ::ItemInstance* Perl_QuestItem_GetAugment(EQ::ItemInstance* self, int slot_id) // @categories Inventory and Items
{
return self->GetAugment(slot_id);
}
uint32_t Perl_QuestItem_GetID(EQ::ItemInstance* self) // @categories Inventory and Items
{
return self->GetItem()->ID;
}
bool Perl_QuestItem_ContainsAugmentByID(EQ::ItemInstance* self, uint32_t item_id) // @categories Inventory and Items
{
return self->ContainsAugmentByID(item_id);
}
int Perl_QuestItem_CountAugmentByID(EQ::ItemInstance* self, uint32_t item_id) // @categories Inventory and Items
{
return self->CountAugmentByID(item_id);
}
bool Perl_QuestItem_IsStackable(EQ::ItemInstance* self)
{
return self->IsStackable();
}
void Perl_QuestItem_SetCharges(EQ::ItemInstance* self, int16_t charges)
{
self->SetCharges(charges);
}
int Perl_QuestItem_GetTaskDeliveredCount(EQ::ItemInstance* self)
{
return self->GetTaskDeliveredCount();
}
int Perl_QuestItem_RemoveTaskDeliveredItems(EQ::ItemInstance* self)
{
return self->RemoveTaskDeliveredItems();
}
void perl_register_questitem()
{
perl::interpreter perl(PERL_GET_THX);
auto package = perl.new_class<EQ::ItemInstance>("QuestItem");
package.add("ContainsAugmentByID", &Perl_QuestItem_ContainsAugmentByID);
package.add("CountAugmentByID", &Perl_QuestItem_CountAugmentByID);
package.add("GetAugment", &Perl_QuestItem_GetAugment);
package.add("GetCharges", &Perl_QuestItem_GetCharges);
package.add("GetID", &Perl_QuestItem_GetID);
package.add("GetName", &Perl_QuestItem_GetName);
package.add("GetTaskDeliveredCount", &Perl_QuestItem_GetTaskDeliveredCount);
package.add("IsAttuned", &Perl_QuestItem_IsAttuned);
package.add("IsStackable", &Perl_QuestItem_IsStackable);
package.add("IsType", &Perl_QuestItem_IsType);
package.add("ItemSay", (void(*)(EQ::ItemInstance*, const char*))&Perl_QuestItem_ItemSay);
package.add("ItemSay", (void(*)(EQ::ItemInstance*, const char*, int))&Perl_QuestItem_ItemSay);
package.add("RemoveTaskDeliveredItems", &Perl_QuestItem_RemoveTaskDeliveredItems);
package.add("SetCharges", &Perl_QuestItem_SetCharges);
package.add("SetScale", &Perl_QuestItem_SetScale);
}
#endif //EMBPERL_XS_CLASSES