Add server-authoritative switchbot (automatic attribute change) v1

/mt_switchbot 1 attrs|start|stop|status runs one bot per character that
calls the original UseItem path every 250 ms, so checks, probabilities,
ItemLog CHANGE_ATTRIBUTE and scroll use are the manual path's. Up to two
alternatives of 1-5 targets; stops on matched/limit/no_scroll/item_gone/
rejected/busy/stopped, erased on logout or warp. Per-roll INFO lines are
held back via a quiet flag on ChatPacket. MTF1 cap mask advertises 64.

Verified live on 203 with an extra channel-5 core (port 13040) running the
isolated build: limit, matched, already-matched, move during run, stop and
logout during run; 72 ItemLog rows for 72 rolls. No attribute lock yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-15 19:45:10 +09:00
co-authored by Claude Opus 5.5
parent 1a367197f5
commit afb3dfb1db
10 changed files with 483 additions and 3 deletions
+6
View File
@@ -3940,6 +3940,12 @@ void CHARACTER::ChatPacket(BYTE type, const char * format, ...)
int len = vsnprintf(chatbuf, sizeof(chatbuf), format, args);
va_end(args);
if (m_portQuietInfo && type == CHAT_TYPE_INFO)
{
m_portLastInfo = chatbuf;
return;
}
struct packet_chat pack_chat;
pack_chat.header = HEADER_GC_CHAT;
+3
View File
@@ -518,6 +518,9 @@ class CHARACTER : public CEntity, public CFSM, public CHorseRider
public:
DWORD m_portFeatureLastQuery = 0; // PORT: read-only query throttle, not persisted.
DWORD m_portSwitchbotLastCommand = 0; // PORT: switchbot command throttle.
bool m_portQuietInfo = false; // PORT: switchbot rolls hold back per-roll INFO lines...
std::string m_portLastInfo; // ...keeping the last one to report a rejected roll.
LPCHARACTER FindCharacterInView(const char * name, bool bFindPCOnly);
void UpdatePacket();
+2
View File
@@ -10,6 +10,7 @@ ACMD(do_autohunt_pull);
ACMD(do_server_bonuses);
ACMD(do_port_biolog);
ACMD(do_port_features);
ACMD(do_port_switchbot);
ACMD(do_user_horse_ride);
ACMD(do_user_horse_back);
ACMD(do_user_horse_feed);
@@ -285,6 +286,7 @@ struct command_info cmd_info[] =
{ "!RESERVED!", NULL, 0, POS_DEAD, GM_IMPLEMENTOR }, /* 반드시 이 것이 처음이어야 한다. */
{ "autohunt_pull", do_autohunt_pull, 0, POS_STANDING, GM_PLAYER },
{ "mt_features", do_port_features, 0, POS_DEAD, GM_PLAYER },
{ "mt_switchbot", do_port_switchbot, 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
@@ -43,6 +43,7 @@
#include "port_dungeons.h"
#include "port_calendar_runtime.h"
#include "port_features.h"
#include "port_switchbot.h"
extern int g_server_id;
+1 -1
View File
@@ -43,7 +43,7 @@ ACMD(do_port_features) {
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 error %u rate",request);return;
}
ch->m_portFeatureLastQuery=get_dword_time();
if(kind=="cap" && args.size()==3) {ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 cap %u 1 63 %lld",request,(long long)get_global_time());return;}
if(kind=="cap" && args.size()==3) {ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 cap %u 1 127 %lld",request,(long long)get_global_time());return;}
if(kind=="dungeons" && args.size()==3) {PortDungeonQuery(ch,request);return;}
if(kind=="stats" && args.size()==3) {
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 begin %u stats 0 0 %u 0 %lld",request,unsigned(sizeof(kPortStats)/sizeof(kPortStats[0])),(long long)get_global_time());
+216
View File
@@ -0,0 +1,216 @@
#ifndef METIN2_PORT_SWITCHBOT_H
#define METIN2_PORT_SWITCHBOT_H
// PORT: AVA2-style automatic attribute change. Every roll goes through the original
// CHARACTER::UseItem change-attribute path, so its checks, probabilities, log and consumption apply unchanged.
// One bound item per character; not persisted; stops on logout, warp, busy windows or any rejected roll.
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
static const unsigned kPortSwitchAlternatives=2, kPortSwitchTargets=5, kPortSwitchLimit=1000;
static const unsigned kPortSwitchAddonSkillMax=30, kPortSwitchAddonNormalMax=65; // CItemAddonManager::ApplyAddonTo bounds.
struct PortSwitchTarget {unsigned apply;int value;};
struct PortSwitchbot {
DWORD pid=0,item=0,scroll=0;unsigned limit=0,rolls=0;VID vid;
std::vector<PortSwitchTarget> alternatives[kPortSwitchAlternatives];
LPEVENT event;
};
static std::map<DWORD,PortSwitchbot> g_portSwitchbots;
static bool PortSwitchNumber(const std::string& text,unsigned& value) {
if(text.empty() || text.size()>10)return false;
unsigned long long result=0;
for(char c:text){if(c<'0'||c>'9')return false;result=result*10+(c-'0');}
if(result>0xffffffffull)return false;
value=unsigned(result);return true;
}
// Attribute types a reroll of this item can produce, with the highest value each can reach.
static std::vector<std::pair<unsigned,int> > PortSwitchAllowed(LPITEM item) {
std::vector<std::pair<unsigned,int> > allowed;
const int set=item->GetAttributeSetIndex();
if(set<0)return allowed;
const TItemTable* proto=item->GetProto();
for(unsigned apply=0;apply<MAX_APPLY_NUM;++apply) {
auto it=g_map_itemAttr.find(apply);
if(it==g_map_itemAttr.end())continue;
const unsigned level=it->second.bMaxLevelBySet[set];
if(!level)continue;
bool base=false;
for(int i=0;i<ITEM_APPLY_MAX_NUM;++i)base|=proto && proto->aApplies[i].bType==apply;
const long value=it->second.lValues[(level>5?5:level)-1];
if(!base && value>0)allowed.push_back(std::make_pair(apply,int(value)));
}
if(proto && proto->sAddonType) {
allowed.push_back(std::make_pair(unsigned(APPLY_SKILL_DAMAGE_BONUS),int(kPortSwitchAddonSkillMax)));
allowed.push_back(std::make_pair(unsigned(APPLY_NORMAL_HIT_DAMAGE_BONUS),int(kPortSwitchAddonNormalMax)));
}
return allowed;
}
// AVA2 CheckItem semantics over the five normal slots: any one configured alternative fully satisfied.
static bool PortSwitchMatches(LPITEM item,const PortSwitchbot& bot) {
for(const auto& alternative:bot.alternatives) {
if(alternative.empty())continue;
bool all=true;
for(const auto& target:alternative) {
bool found=false;
for(int i=0;i<ITEM_MANAGER::MAX_NORM_ATTR_NUM && !found;++i) {
const TPlayerItemAttribute& attr=item->GetAttribute(i);
found=attr.bType==target.apply && attr.sValue>=target.value;
}
all&=found;
}
if(all)return true;
}
return false;
}
static bool PortSwitchBusy(LPCHARACTER ch) {
return ch->IsDead() || ch->IsWarping() || !ch->CanHandleItem() || ch->GetExchange() || ch->GetMyShop() ||
ch->GetShopOwner() || ch->IsOpenSafebox() || ch->IsCubeOpen();
}
static LPITEM PortSwitchItem(LPCHARACTER ch,const PortSwitchbot& bot) {
LPITEM item=ITEM_MANAGER::instance().Find(bot.item);
if(!item || item->GetOwner()!=ch || item->GetWindow()!=INVENTORY || item->GetCell()>=INVENTORY_MAX_NUM || item->IsEquipped())return NULL;
return item;
}
static bool PortSwitchScroll(LPITEM scroll) {
return scroll && scroll->GetType()==ITEM_USE && scroll->GetSubType()==USE_CHANGE_ATTRIBUTE;
}
static void PortSwitchState(LPCHARACTER ch,const PortSwitchbot* bot,bool active,const char* reason) {
LPITEM item=bot?PortSwitchItem(ch,*bot):NULL;
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 state %u %u %u %u %u %u %d %s",unsigned(active),item?unsigned(item->GetCell()):0u,
item?unsigned(item->GetVnum()):0u,bot?bot->rolls:0u,bot?bot->limit:0u,bot?unsigned(bot->scroll):0u,
bot?ch->CountSpecifyItem(bot->scroll):0,reason);
}
// One roll. Returns NULL to keep running, otherwise the stop reason.
static const char* PortSwitchStep(LPCHARACTER ch,PortSwitchbot& bot) {
if(PortSwitchBusy(ch))return "busy";
LPITEM item=PortSwitchItem(ch,bot);
if(!item)return "item_gone";
if(PortSwitchMatches(item,bot))return "matched";
if(bot.rolls>=bot.limit)return "limit";
LPITEM scroll=ch->FindSpecifyItem(bot.scroll);
if(!PortSwitchScroll(scroll))return "no_scroll";
const int before=ch->CountSpecifyItem(bot.scroll);
ch->m_portQuietInfo=true;ch->m_portLastInfo.clear();
ch->UseItem(TItemPos(INVENTORY,scroll->GetCell()),TItemPos(INVENTORY,item->GetCell()));
ch->m_portQuietInfo=false;
// Success is judged by the original path consuming exactly one scroll, not by its return value.
if(ch->CountSpecifyItem(bot.scroll)!=before-1) {
if(!ch->m_portLastInfo.empty())ch->ChatPacket(CHAT_TYPE_INFO,"%s",ch->m_portLastInfo.c_str());
return "rejected";
}
++bot.rolls;
item=PortSwitchItem(ch,bot);
if(!item)return "item_gone";
return PortSwitchMatches(item,bot)?"matched":bot.rolls>=bot.limit?"limit":NULL;
}
EVENTINFO(PortSwitchbotInfo){DWORD pid;PortSwitchbotInfo():pid(0){}};
EVENTFUNC(port_switchbot_event) {
PortSwitchbotInfo* info=dynamic_cast<PortSwitchbotInfo*>(event->info);
if(!info)return 0;
auto it=g_portSwitchbots.find(info->pid);
if(it==g_portSwitchbots.end())return 0;
PortSwitchbot& bot=it->second;
LPCHARACTER ch=CHARACTER_MANAGER::instance().Find(bot.vid);
// A new instance after logout or warp carries a new VID; the bot does not follow it.
if(!ch || ch->GetPlayerID()!=bot.pid || !ch->GetDesc()) {bot.event=NULL;g_portSwitchbots.erase(it);return 0;}
const char* reason=PortSwitchStep(ch,bot);
if(!reason) {PortSwitchState(ch,&bot,true,"running");return PASSES_PER_SEC(1)/4;}
PortSwitchState(ch,&bot,false,reason);
bot.event=NULL;g_portSwitchbots.erase(it);
return 0;
}
static void PortSwitchStop(LPCHARACTER ch,const char* reason) {
auto it=g_portSwitchbots.find(ch->GetPlayerID());
if(it==g_portSwitchbots.end()) {PortSwitchState(ch,NULL,false,"idle");return;}
if(it->second.event)event_cancel(&it->second.event);
PortSwitchState(ch,&it->second,false,reason);
g_portSwitchbots.erase(it);
}
static void PortSwitchAttrs(LPCHARACTER ch,unsigned cell) {
LPITEM item=cell<INVENTORY_MAX_NUM?ch->GetItem(TItemPos(INVENTORY,cell)):NULL;
if(!item){ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 error attrs item");return;}
if(item->GetType()==ITEM_COSTUME || item->GetAttributeSetIndex()<0){ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 error attrs no_attr");return;}
auto allowed=PortSwitchAllowed(item);
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 attrs %u %u %u",cell,unsigned(item->GetVnum()),unsigned(allowed.size()));
for(unsigned i=0;i<allowed.size();++i)
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 attr %u %u %d",i,allowed[i].first,allowed[i].second);
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 attrs_end %u %u",cell,unsigned(allowed.size()));
}
// start CELL SCROLL LIMIT N1 (APPLY VALUE)*N1 [N2 (APPLY VALUE)*N2]
static const char* PortSwitchStart(LPCHARACTER ch,const std::vector<std::string>& args) {
unsigned cell=0,scrollVnum=0,limit=0;
if(args.size()<6 || !PortSwitchNumber(args[2],cell) || !PortSwitchNumber(args[3],scrollVnum) ||
!PortSwitchNumber(args[4],limit) || !limit || limit>kPortSwitchLimit)return "invalid";
if(g_portSwitchbots.count(ch->GetPlayerID()))return "running";
if(PortSwitchBusy(ch))return "busy";
LPITEM item=cell<INVENTORY_MAX_NUM?ch->GetItem(TItemPos(INVENTORY,cell)):NULL;
if(!item)return "item";
if(item->IsEquipped() || item->IsExchanging())return "equipped";
if(item->GetType()==ITEM_COSTUME || item->GetAttributeSetIndex()<0 || !item->GetAttributeCount())return "no_attr";
if(!PortSwitchScroll(ch->FindSpecifyItem(scrollVnum)))return "scroll";
PortSwitchbot bot;bot.pid=ch->GetPlayerID();bot.vid=ch->GetVID();bot.item=item->GetID();bot.scroll=scrollVnum;bot.limit=limit;
const auto allowed=PortSwitchAllowed(item);
size_t at=5;
for(unsigned alt=0;alt<kPortSwitchAlternatives && at<args.size();++alt) {
unsigned count=0;
if(!PortSwitchNumber(args[at++],count) || !count || count>kPortSwitchTargets || at+count*2>args.size())return "invalid";
for(unsigned i=0;i<count;++i,at+=2) {
unsigned apply=0,value=0;
if(!PortSwitchNumber(args[at],apply) || !PortSwitchNumber(args[at+1],value) || !value)return "invalid";
int max=0;
for(const auto& a:allowed)if(a.first==apply)max=a.second;
if(!max || value>unsigned(max))return "target";
for(const auto& t:bot.alternatives[alt])if(t.apply==apply)return "target";
bot.alternatives[alt].push_back(PortSwitchTarget{apply,int(value)});
}
}
if(at!=args.size())return "invalid";
if(PortSwitchMatches(item,bot)) {PortSwitchState(ch,&bot,false,"matched");return NULL;}
PortSwitchbot& stored=g_portSwitchbots[bot.pid];stored=bot;
PortSwitchbotInfo* info=AllocEventInfo<PortSwitchbotInfo>();info->pid=bot.pid;
stored.event=event_create(port_switchbot_event,info,PASSES_PER_SEC(1)/4);
PortSwitchState(ch,&stored,true,"running");
return NULL;
}
ACMD(do_port_switchbot) {
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()>=30)return;args.push_back(token);}
if(args.size()<2 || args[0]!="1")return;
const std::string& verb=args[1];
if(get_dword_time()-ch->m_portSwitchbotLastCommand<250 && ch->m_portSwitchbotLastCommand) {
ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 error %s rate",verb.c_str());return;
}
ch->m_portSwitchbotLastCommand=get_dword_time();
unsigned cell=0;
if(verb=="attrs" && args.size()==3 && PortSwitchNumber(args[2],cell)) PortSwitchAttrs(ch,cell);
else if(verb=="stop" && args.size()==2) PortSwitchStop(ch,"stopped");
else if(verb=="status" && args.size()==2) {
auto it=g_portSwitchbots.find(ch->GetPlayerID());
if(it==g_portSwitchbots.end())PortSwitchState(ch,NULL,false,"idle");
else PortSwitchState(ch,&it->second,true,"running");
}
else if(verb=="start") {
const char* error=PortSwitchStart(ch,args);
if(error)ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 error start %s",error);
}
else ch->ChatPacket(CHAT_TYPE_COMMAND,"MTSB1 error %s invalid",verb.size()<=10?verb.c_str():"unknown");
}
#endif