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>
104 lines
6.3 KiB
Python
104 lines
6.3 KiB
Python
#!/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)
|