Add attribute lock (AVA2 ENABLE_LOCK_ATTR port)
Up to two normal attribute slots (addon pair = one group) keep their type and value through CItem::ChangeAttribute, so scroll rerolls, the switchbot and GM rerolls all respect it. The mask lives in the new player.mt_item_attr_lock table (created at boot), cached per CItem and written synchronously; CopyAttributeTo carries it to refined items. No item struct, db cache or client packet change. Toggled with /mt_switchbot 1 lock CELL SLOT; MTSB1 lock line follows attrs_end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
400e44fae4
commit
dc7159338e
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Attribute lock: compile the production CItem::ChangeAttribute body against a stub item and check the lock rules."""
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
ROOT=Path(__file__).resolve().parents[2]
|
||||
SRC=ROOT/'src/server/game/src'
|
||||
def text(name):return (SRC/name).read_bytes().decode('latin1')
|
||||
attribute=text('item_attribute.cpp');glue=text('port_attr_lock.cpp');main=text('main.cpp')
|
||||
body=re.search(r'void CItem::ChangeAttribute\(const int\* aiChangeProb\)\n\{.*?\n\}\n',attribute,re.S).group(0)
|
||||
copy=re.search(r'void CItem::CopyAttributeTo\(LPITEM pItem\)\n\{.*?\n\}\n',attribute,re.S).group(0)
|
||||
assert 'PortAttrLockSet(GetOwner(), pItem, lock)' in copy # refine / replacement keeps the lock
|
||||
assert 'port_attr_lock.cpp' in text('Makefile') and '#include "port_attr_lock.h"' in attribute
|
||||
assert 'if (!g_bAuthServer)\n\t\tPortAttrLockBoot();' in main.split('config_init(st_localeServiceName);',1)[1][:120]
|
||||
assert 'CREATE TABLE IF NOT EXISTS mt_item_attr_lock' in glue and 'item->m_portAttrLockLoaded=true' in glue
|
||||
assert 'm_portAttrLockLoaded = false;' in text('item.cpp')
|
||||
code=r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
typedef unsigned char BYTE;
|
||||
enum {ITEM_ATTRIBUTE_MAX_LEVEL=5,ITEM_ATTRIBUTE_MAX_NUM=7,SKILL=71,NORMAL=72};
|
||||
const int MAX_NORM_ATTR_NUM=5;
|
||||
struct TPlayerItemAttribute{BYTE bType;short sValue;};
|
||||
struct TItemTable{short sAddonType;};
|
||||
class CHARACTER;
|
||||
#include "port_attr_lock.h"
|
||||
static unsigned g_lock=0;static int g_lookups=0;static BYTE g_next=10;
|
||||
class CItem {
|
||||
public:
|
||||
TPlayerItemAttribute m_aAttr[ITEM_ATTRIBUTE_MAX_NUM];TItemTable proto;
|
||||
CItem(){memset(m_aAttr,0,sizeof(m_aAttr));proto.sAddonType=0;}
|
||||
BYTE GetAttributeType(int i){return m_aAttr[i].bType;}
|
||||
int GetAttributeCount(){int i=0;while(i<MAX_NORM_ATTR_NUM && m_aAttr[i].bType)++i;return i;}
|
||||
void ClearAttribute(){for(int i=0;i<MAX_NORM_ATTR_NUM;++i)m_aAttr[i].bType=0,m_aAttr[i].sValue=0;}
|
||||
const TItemTable* GetProto(){return &proto;}
|
||||
bool HasAttr(BYTE t){for(int i=0;i<MAX_NORM_ATTR_NUM;++i)if(m_aAttr[i].bType==t)return true;return false;}
|
||||
// CItem::AddAttribute / AddAttr: write at GetAttributeCount(), the first empty slot.
|
||||
void Put(BYTE t,short v){int i=GetAttributeCount();assert(i<MAX_NORM_ATTR_NUM && !HasAttr(t));m_aAttr[i].bType=t;m_aAttr[i].sValue=v;}
|
||||
void ApplyAddon(int){Put(NORMAL,-5);Put(SKILL,3);}
|
||||
void PutAttribute(const int*){while(HasAttr(g_next))++g_next;Put(g_next,g_next*10);++g_next;}
|
||||
void ChangeAttribute(const int* aiChangeProb=NULL);
|
||||
};
|
||||
unsigned PortAttrLockGet(CItem*){++g_lookups;return g_lock;}
|
||||
@BODY@
|
||||
static CItem Item(const std::vector<BYTE>& types,bool addon){
|
||||
CItem item;item.proto.sAddonType=addon;
|
||||
for(size_t i=0;i<types.size();++i)item.m_aAttr[i].bType=types[i],item.m_aAttr[i].sValue=types[i]*10+1;
|
||||
item.m_aAttr[5].bType=90;item.m_aAttr[5].sValue=9; // rare slot untouched
|
||||
return item;
|
||||
}
|
||||
static void Check(CItem& item,unsigned keep,const CItem& before,int count){
|
||||
assert(item.GetAttributeCount()==count); // contiguous, same count
|
||||
for(int i=0;i<MAX_NORM_ATTR_NUM;++i){
|
||||
if(keep>>i&1)assert(item.m_aAttr[i].bType==before.m_aAttr[i].bType && item.m_aAttr[i].sValue==before.m_aAttr[i].sValue);
|
||||
for(int j=0;j<i;++j)assert(!item.m_aAttr[i].bType || item.m_aAttr[i].bType!=item.m_aAttr[j].bType);
|
||||
}
|
||||
assert(item.m_aAttr[5].bType==90 && item.m_aAttr[5].sValue==9);
|
||||
}
|
||||
int main(){
|
||||
// No lock: identical to the original (addon first, then rerolls), all values change.
|
||||
CItem a=Item({1,2,3,4,5},false),b=a;g_lock=0;a.ChangeAttribute();Check(a,0,b,5);
|
||||
for(int i=0;i<5;++i)assert(a.m_aAttr[i].sValue!=b.m_aAttr[i].sValue);
|
||||
// Slots 1 and 3 locked: kept in place, gaps refilled in order, no duplicate types.
|
||||
a=Item({1,2,3,4,5},false);b=a;g_lock=10;a.ChangeAttribute();Check(a,10,b,5);
|
||||
assert(a.m_aAttr[0].sValue!=b.m_aAttr[0].sValue && a.m_aAttr[4].sValue!=b.m_aAttr[4].sValue);
|
||||
// Locked type is never rolled again (PutAttribute skips existing types).
|
||||
g_next=2;a=Item({1,2,3},false);b=a;g_lock=2;a.ChangeAttribute();Check(a,2,b,3);assert(a.m_aAttr[0].bType!=2&&a.m_aAttr[2].bType!=2);
|
||||
// Lock on an empty slot is ignored; count kept.
|
||||
a=Item({1,2},false);b=a;g_lock=16;a.ChangeAttribute();Check(a,0,b,2);
|
||||
// Addon item: lock on slot 1 keeps the whole pair and skips ApplyAddon; otherwise the addon is rerolled.
|
||||
a=Item({NORMAL,SKILL,3,4},true);a.m_aAttr[0].sValue=-7;b=a;g_lock=2;a.ChangeAttribute();Check(a,3,b,4);
|
||||
a=Item({NORMAL,SKILL,3,4},true);a.m_aAttr[0].sValue=-7;b=a;g_lock=8;a.ChangeAttribute();Check(a,8,b,4);
|
||||
assert(a.m_aAttr[0].bType==NORMAL && a.m_aAttr[0].sValue==-5 && a.m_aAttr[1].bType==SKILL && a.m_aAttr[1].sValue==3);
|
||||
// No attributes: cleared, no lookup.
|
||||
g_lookups=0;a=Item({},false);a.ChangeAttribute();assert(a.GetAttributeCount()==0 && g_lookups==0);
|
||||
|
||||
// Toggle rules.
|
||||
unsigned out=0;const unsigned five=31,three=7;
|
||||
assert(!PortAttrLockToggle(0,4,five,false,out) && out==16);
|
||||
assert(!PortAttrLockToggle(16,0,five,false,out) && out==17);
|
||||
assert(!strcmp(PortAttrLockToggle(17,1,five,false,out),"max")); // third group
|
||||
assert(!PortAttrLockToggle(17,4,five,false,out) && out==1); // unlock
|
||||
assert(!strcmp(PortAttrLockToggle(0,3,three,false,out),"slot")); // empty slot
|
||||
assert(!strcmp(PortAttrLockToggle(0,5,five,false,out),"slot"));
|
||||
assert(!strcmp(PortAttrLockToggle(1,1,3,false,out),"max")); // would lock every filled slot
|
||||
assert(!PortAttrLockToggle(0,0,five,true,out) && out==3); // addon pair = one group
|
||||
assert(!PortAttrLockToggle(3,4,five,true,out) && out==19);
|
||||
assert(!strcmp(PortAttrLockToggle(19,2,five,true,out),"max"));
|
||||
assert(!PortAttrLockToggle(19,1,five,true,out) && out==16);
|
||||
assert(!strcmp(PortAttrLockToggle(0,0,3,true,out),"max")); // pair is all there is
|
||||
assert(PortAttrLockEffective(1,five,true)==3 && PortAttrLockEffective(0xff,three,false)==7 && PortAttrLockEffective(24,three,false)==0);
|
||||
assert(PortAttrLockGroupCount(31,true)==4 && PortAttrLockGroupCount(5,true)==2);
|
||||
puts("PASS: ChangeAttribute keeps locked slots (plain/addon/empty), no duplicates, rare untouched; toggle rules");
|
||||
}
|
||||
'''.replace('@BODY@',body)
|
||||
with tempfile.TemporaryDirectory(prefix='mt-attrlock-') as folder:
|
||||
path=Path(folder);(path/'test.cpp').write_text(code)
|
||||
subprocess.run(['c++','-std=c++17','-Wall','-I'+str(SRC),str(path/'test.cpp'),'-o',str(path/'test')],check=True)
|
||||
subprocess.run([str(path/'test')],check=True)
|
||||
@@ -41,13 +41,13 @@ void event_cancel(LPEVENT* e){if(*e)(*e)->cancelled=true;*e=nullptr;}
|
||||
#define EVENTINFO(name) struct name : public event_info_data
|
||||
#define PASSES_PER_SEC(s) ((s)*4)
|
||||
static DWORD clockms=1000;DWORD get_dword_time(){return clockms;}
|
||||
struct CHARACTER;
|
||||
struct CItem {
|
||||
class CHARACTER;
|
||||
class CItem {public:
|
||||
DWORD id,vnum;BYTE type,subtype;int set=0;unsigned cell;BYTE window=INVENTORY;bool equipped=false,exchanging=false;int count=1;
|
||||
CHARACTER* owner=nullptr;TItemTable proto{};TPlayerItemAttribute attrs[7];
|
||||
DWORD GetID(){return id;}DWORD GetVnum(){return vnum;}BYTE GetType(){return type;}BYTE GetSubType(){return subtype;}
|
||||
int GetAttributeSetIndex(){return set;}const TItemTable* GetProto(){return &proto;}
|
||||
const TPlayerItemAttribute& GetAttribute(int i){return attrs[i];}
|
||||
const TPlayerItemAttribute& GetAttribute(int i){return attrs[i];}BYTE GetAttributeType(int i){return attrs[i].bType;}
|
||||
int GetAttributeCount(){int n=0;for(int i=0;i<5;++i)n+=attrs[i].bType!=0;return n;}
|
||||
CHARACTER* GetOwner(){return owner;}BYTE GetWindow(){return window;}unsigned GetCell(){return cell;}
|
||||
bool IsEquipped(){return equipped;}bool IsExchanging(){return exchanging;}
|
||||
@@ -58,7 +58,7 @@ struct ITEM_MANAGER{
|
||||
static ITEM_MANAGER& instance(){static ITEM_MANAGER m;return m;}
|
||||
LPITEM Find(DWORD id){for(auto* i:items)if(i->id==id)return i;return nullptr;}
|
||||
};
|
||||
struct CHARACTER {
|
||||
class CHARACTER {public:
|
||||
DWORD m_portSwitchbotLastCommand=0;bool m_portQuietInfo=false;std::string m_portLastInfo;
|
||||
DWORD pid=7;VID vid;bool desc=true,busy=false,dead=false,reject=false;int uses=0;
|
||||
std::vector<std::string> messages,info;std::vector<CItem*> inventory;
|
||||
@@ -92,6 +92,10 @@ struct CHARACTER_MANAGER{
|
||||
};
|
||||
#define ACMD(name) void name(LPCHARACTER ch,const char* argument)
|
||||
#include "port_switchbot.h"
|
||||
// Lock storage stub: the real one is a player.mt_item_attr_lock row (port_attr_lock.cpp).
|
||||
static std::map<DWORD,unsigned> locks;static bool lockFail=false;
|
||||
unsigned PortAttrLockGet(CItem* item){return locks[item->id];}
|
||||
bool PortAttrLockSet(CHARACTER*,CItem* item,unsigned mask){if(lockFail)return false;locks[item->id]=mask;return true;}
|
||||
static CHARACTER ch;
|
||||
static void cmd(const char* text){clockms+=300;ch.messages.clear();do_port_switchbot(&ch,text);}
|
||||
static long tick(){for(auto& e:events)if(!e->cancelled&&e->func){long r=e->func(e.get(),0);if(!r)e->func=nullptr;return r;}return -1;}
|
||||
@@ -110,8 +114,9 @@ int main(){
|
||||
|
||||
// Allowed list: proto applies excluded, set filter, level cap picks lValues[level-1].
|
||||
cmd("1 attrs 10");
|
||||
assert(ch.messages.size()==4 && ch.messages[0]=="MTSB1 attrs 10 11209 2");
|
||||
assert(ch.messages[1]=="MTSB1 attr 0 3 12" && ch.messages[2]=="MTSB1 attr 1 63 6" && last()=="MTSB1 attrs_end 10 2");
|
||||
assert(ch.messages.size()==5 && ch.messages[0]=="MTSB1 attrs 10 11209 2");
|
||||
assert(ch.messages[1]=="MTSB1 attr 0 3 12" && ch.messages[2]=="MTSB1 attr 1 63 6" && ch.messages[3]=="MTSB1 attrs_end 10 2");
|
||||
assert(last()=="MTSB1 lock 10 11209 0");
|
||||
armor.proto.sAddonType=1;cmd("1 attrs 10");assert(ch.messages[3]=="MTSB1 attr 2 71 30" && ch.messages[4]=="MTSB1 attr 3 72 65");armor.proto.sAddonType=0;
|
||||
cmd("1 attrs 30");assert(last()=="MTSB1 error attrs no_attr");
|
||||
cmd("1 attrs 99");assert(last()=="MTSB1 error attrs item");
|
||||
@@ -161,6 +166,30 @@ int main(){
|
||||
ch.vid.id=100;ch.messages.clear();assert(tick()==0 && ch.messages.empty() && g_portSwitchbots.empty());
|
||||
assert(ch.uses==6);
|
||||
|
||||
// Lock toggle: filled slots only, at most 2 groups and never every filled slot, not on the rolling item.
|
||||
armor.attrs[0]={APPLY_STR,2};armor.attrs[1]={APPLY_CRITICAL,1};armor.attrs[2]={APPLY_ATTBONUS_MONSTER,2};
|
||||
cmd("1 lock 10 0");assert(last()=="MTSB1 lock 10 11209 1");
|
||||
cmd("1 lock 10 2");assert(last()=="MTSB1 lock 10 11209 5");
|
||||
cmd("1 lock 10 1");assert(last()=="MTSB1 error lock max");
|
||||
cmd("1 lock 10 3");assert(last()=="MTSB1 error lock slot");
|
||||
cmd("1 lock 10 5");assert(last()=="MTSB1 error lock slot");
|
||||
cmd("1 lock 10 0");assert(last()=="MTSB1 lock 10 11209 4" && locks[1001]==4);
|
||||
cmd("1 lock 10 1");assert(last()=="MTSB1 lock 10 11209 6");
|
||||
cmd("1 attrs 10");assert(last()=="MTSB1 lock 10 11209 6");
|
||||
cmd("1 lock 30 0");assert(last()=="MTSB1 error lock no_attr");
|
||||
cmd("1 lock 99 0");assert(last()=="MTSB1 error lock item");
|
||||
cmd("1 lock 10");assert(last()=="MTSB1 error lock invalid");
|
||||
lockFail=true;cmd("1 lock 10 1");assert(last()=="MTSB1 error lock db" && locks[1001]==6);lockFail=false;
|
||||
ch.rolls={{{APPLY_STR,2}}};cmd("1 start 10 71084 5 1 3 12");cmd("1 lock 10 1");assert(last()=="MTSB1 error lock running");
|
||||
cmd("1 stop");
|
||||
// Addon weapon: slots 0 and 1 lock together and count as one group.
|
||||
armor.proto.sAddonType=1;locks[1001]=0;armor.attrs[3]={APPLY_MAX_HP,500};
|
||||
cmd("1 lock 10 1");assert(last()=="MTSB1 lock 10 11209 3");
|
||||
cmd("1 lock 10 3");assert(last()=="MTSB1 lock 10 11209 11");
|
||||
cmd("1 lock 10 2");assert(last()=="MTSB1 error lock max");
|
||||
cmd("1 lock 10 0");assert(last()=="MTSB1 lock 10 11209 8");
|
||||
armor.proto.sAddonType=0;armor.attrs[2]=armor.attrs[3]=TPlayerItemAttribute{};locks.clear();
|
||||
|
||||
// Malformed or foreign input is ignored or rejected without side effects.
|
||||
cmd("2 status");assert(ch.messages.empty());
|
||||
cmd("1 explode");assert(last()=="MTSB1 error explode invalid");
|
||||
|
||||
Reference in New Issue
Block a user