Add batch gift box opening (mt_chest)

AVA2 ENABLE_MULTI_CHEST port: /mt_chest 1 open CELL COUNT opens up to 200
gift boxes of one vnum across bag cells through the original UseItem path,
stops at 3 free cells, skips monster/debuff groups and reports the gains
as MTCH1 lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-15 23:24:11 +09:00
co-authored by Claude Opus 5.5
parent bcd318791e
commit 4b4f32ed58
5 changed files with 278 additions and 0 deletions
+2
View File
@@ -12,6 +12,7 @@ ACMD(do_port_biolog);
ACMD(do_port_features);
ACMD(do_port_switchbot);
ACMD(do_port_pickup);
ACMD(do_port_chest);
ACMD(do_user_horse_ride);
ACMD(do_user_horse_back);
ACMD(do_user_horse_feed);
@@ -289,6 +290,7 @@ struct command_info cmd_info[] =
{ "mt_features", do_port_features, 0, POS_DEAD, GM_PLAYER },
{ "mt_switchbot", do_port_switchbot, 0, POS_DEAD, GM_PLAYER },
{ "mt_pickup", do_port_pickup, 0, POS_DEAD, GM_PLAYER },
{ "mt_chest", do_port_chest, 0, POS_DEAD, GM_PLAYER },
{ "bio", do_port_biolog, 0, POS_DEAD, GM_PLAYER },
{ "biolog", do_port_biolog, 0, POS_DEAD, GM_PLAYER },
{ "server_bonuses", do_server_bonuses, 0, POS_DEAD, GM_PLAYER },
+1
View File
@@ -45,6 +45,7 @@
#include "port_features.h"
#include "port_switchbot.h"
#include "port_pickup.h"
#include "port_chest.h"
extern int g_server_id;
+115
View File
@@ -0,0 +1,115 @@
#ifndef METIN2_PORT_CHEST_H
#define METIN2_PORT_CHEST_H
// PORT: AVA2-style batch chest opening (ENABLE_MULTI_CHEST /chest_drop). AVA2 rewrote the drop loop
// (GiveItemFromSpecialItemGroupNew); here every box goes through the original CHARACTER::UseItem gift-box path,
// so its checks, special_item_group roll, gold/exp, ItemLog and consumption are unchanged. Boxes of one vnum are
// opened from the chosen cell first, then from any other cell (most gift boxes do not stack). Per-box INFO lines
// are held back and the gains are reported once as MTCH1 lines. Protocol: docs/FEATURE-CHEST.md.
#include <map>
#include <sstream>
#include <string>
#include <vector>
static const unsigned kPortChestMax=200, kPortChestReportVnums=40, kPortChestFreeCells=3;
static const DWORD kPortChestGapMs=500;
static std::map<DWORD,DWORD> g_portChestLastCommand;
static bool PortChestNumber(const std::string& text,unsigned& value) {
if(text.empty() || text.size()>5)return false;
value=0;
for(char c:text){if(c<'0'||c>'9')return false;value=value*10+unsigned(c-'0');}
return true;
}
// Gift boxes whose group only hands out items, gold or exp. Groups that spawn monsters, slow, poison or drain
// HP stay single-use: opening a stack of them at once would spawn every group around the player in one tick.
static bool PortChestBatchable(LPITEM item) {
if(item->GetType()!=ITEM_GIFTBOX)return false;
const CSpecialItemGroup* group=ITEM_MANAGER::instance().GetSpecialItemGroup(item->GetVnum());
if(!group || group->IsEmpty())return false;
const DWORD unsafe[]={CSpecialItemGroup::MOB,CSpecialItemGroup::SLOW,CSpecialItemGroup::DRAIN_HP,
CSpecialItemGroup::POISON,CSpecialItemGroup::MOB_GROUP};
for(DWORD kind:unsafe)if(group->Contains(kind))return false;
return true;
}
static bool PortChestBusy(LPCHARACTER ch) {
return ch->IsDead() || ch->IsStun() || ch->IsWarping() || ch->IsObserverMode() || !ch->CanHandleItem() ||
ch->GetExchange() || ch->GetMyShop() || ch->GetShopOwner() || ch->IsOpenSafebox() || ch->IsCubeOpen();
}
// Item counts by vnum over the bag and the dragon soul bag, where AutoGiveItem puts what a box gives.
static std::map<DWORD,long long> PortChestSnapshot(LPCHARACTER ch) {
std::map<DWORD,long long> counts;
for(int i=0;i<INVENTORY_MAX_NUM;++i)if(LPITEM item=ch->GetInventoryItem(i))counts[item->GetVnum()]+=item->GetCount();
for(int i=0;i<DRAGON_SOUL_INVENTORY_MAX_NUM;++i)
if(LPITEM item=ch->GetItem(TItemPos(DRAGON_SOUL_INVENTORY,i)))counts[item->GetVnum()]+=item->GetCount();
return counts;
}
// The box to open next: the chosen cell while it still holds this vnum, otherwise any bag cell that does.
static LPITEM PortChestNext(LPCHARACTER ch,unsigned cell,DWORD vnum) {
LPITEM item=ch->GetInventoryItem(cell);
if(item && item->GetVnum()==vnum && !item->IsExchanging())return item;
for(int i=0;i<INVENTORY_MAX_NUM;++i) {
item=ch->GetInventoryItem(i);
if(item && item->GetVnum()==vnum && !item->IsExchanging())return item;
}
return NULL;
}
static const char* PortChestOpen(LPCHARACTER ch,unsigned cell,unsigned asked) {
if(cell>=unsigned(INVENTORY_MAX_NUM) || !asked || asked>kPortChestMax)return "invalid";
if(PortChestBusy(ch))return "busy";
LPITEM first=ch->GetInventoryItem(cell);
if(!first || first->IsExchanging())return "item";
if(!PortChestBatchable(first))return "kind";
if(ch->GetEmptyInventory(kPortChestFreeCells)==-1)return "full";
const DWORD vnum=first->GetVnum();
const std::map<DWORD,long long> before=PortChestSnapshot(ch);
const long long goldBefore=ch->GetGold();
unsigned opened=0;const char* reason="done";std::string rejection;
while(opened<asked) {
if(PortChestBusy(ch)){reason="busy";break;}
// Stop while a whole weapon still fits, so AutoGiveItem never has to drop a reward on the ground.
if(ch->GetEmptyInventory(kPortChestFreeCells)==-1){reason="full";break;}
LPITEM box=PortChestNext(ch,cell,vnum);
if(!box){reason="empty";break;}
const int count=ch->CountSpecifyItem(vnum);
ch->m_portQuietInfo=true;ch->m_portLastInfo.clear();
ch->UseItem(TItemPos(INVENTORY,box->GetCell()));
ch->m_portQuietInfo=false;
// Success is judged by the original path consuming exactly one box, not by its return value.
if(ch->CountSpecifyItem(vnum)!=count-1){reason="rejected";rejection=ch->m_portLastInfo;break;}
++opened;
}
if(!rejection.empty())ch->ChatPacket(CHAT_TYPE_INFO,"%s",rejection.c_str());
const std::map<DWORD,long long> after=PortChestSnapshot(ch);
unsigned reported=0;
for(const auto& entry:after) {
if(entry.first==vnum)continue;
auto it=before.find(entry.first);
const long long gained=entry.second-(it==before.end()?0:it->second);
if(gained<=0)continue;
if(++reported>kPortChestReportVnums)break;
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTCH1 got %u %lld",unsigned(entry.first),gained);
}
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTCH1 done %u %u %u %s %lld",unsigned(vnum),asked,opened,reason,
(long long)ch->GetGold()-goldBefore);
return NULL;
}
ACMD(do_port_chest) {
if(!ch || !ch->IsPC() || !ch->GetDesc())return;
std::istringstream input(argument);std::vector<std::string> args;std::string token;
while(input>>token){if(token.size()>10 || args.size()>=5)return;args.push_back(token);}
if(args.size()<2 || args[0]!="1")return;
DWORD& last=g_portChestLastCommand[ch->GetPlayerID()];
if(last && get_dword_time()-last<kPortChestGapMs){ch->ChatPacket(CHAT_TYPE_COMMAND,"MTCH1 error rate");return;}
last=get_dword_time();
unsigned cell=0,count=0;const char* error="invalid";
if(args[1]=="open" && args.size()==4 && PortChestNumber(args[2],cell) && PortChestNumber(args[3],count))
error=PortChestOpen(ch,cell,count);
if(error)ch->ChatPacket(CHAT_TYPE_COMMAND,"MTCH1 error %s",error);
}
#endif