Example #1
0
bool LootStrategyAction::Execute(Event event)
{
    string strategy = event.getParam();
    
    LootObjectStack* lootItems = AI_VALUE(LootObjectStack*, "available loot");
    set<uint32>& alwaysLootItems = AI_VALUE(set<uint32>&, "always loot list");
    Value<LootStrategy>* lootStrategy = context->GetValue<LootStrategy>("loot strategy");

    if (strategy == "?")
    {
        ostringstream out;
        out << "Loot strategy: ";
        out << LootStrategy2string(lootStrategy->Get());
        out << ", always loot items: ";

        for (set<uint32>::iterator i = alwaysLootItems.begin(); i != alwaysLootItems.end(); i++)
        {
            ItemPrototype const *proto = sItemStorage.LookupEntry<ItemPrototype>(*i);
            if (!proto)
                continue;

            out << chat->formatItem(proto);
        }
        ai->TellMaster(out);
    }
    else
    {
        ItemIds items = chat->parseItems(strategy);

        if (items.size() == 0)
        {
            lootStrategy->Set(String2LootStrategy(strategy));
            ostringstream out;
            out << "Loot strategy set to " << LootStrategy2string(lootStrategy->Get());
            ai->TellMaster(out);
            return true;
        }

        bool remove = strategy.size() > 1 && strategy.substr(0, 1) == "-";
        for (ItemIds::iterator i = items.begin(); i != items.end(); i++)
        {
            uint32 itemid = *i;
            if (remove)
            {
                set<uint32>::iterator j = alwaysLootItems.find(itemid);
                if (j != alwaysLootItems.end())
                    alwaysLootItems.erase(j);
                
                ai->TellMaster("Item(s) removed from always loot list");
            }
            else
            {
                alwaysLootItems.insert(itemid);
                ai->TellMaster("Item(s) added to always loot list");
            }
        }
    }    

    return true;
}