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:
shenlei
2026-10-15 23:06:00 +09:00
co-authored by Claude Opus 5.5
parent 400e44fae4
commit dc7159338e
12 changed files with 367 additions and 13 deletions
+71
View File
@@ -0,0 +1,71 @@
# PORT attribute lock
AVA2 `ENABLE_LOCK_ATTR`, rebuilt for this server's item layout. Up to two of the five normal attribute slots keep their type and value when the item is rerolled. Source: `src/server/game/src/port_attr_lock.{h,cpp}`, `CItem::ChangeAttribute` / `CopyAttributeTo` (item_attribute.cpp), the cache fields in item.h/item.cpp, the boot hook in main.cpp, and the `lock` verb of `mt_switchbot` (port_switchbot.h).
## Storage
AVA2 keeps up to two 1-based slot indices as attribute values at indices 7 and 8 of its 15-slot attribute array. This server has `ITEM_ATTRIBUTE_MAX_NUM = 7` (5 normal + 2 rare) and `ITEM_SOCKET_MAX_NUM = 3`, which metin stones use. The `item` table does have `socket3..socket5`, but `TPlayerItem` and the client packets only carry 3 sockets, so using them would change both the db↔game and client wire formats. The `item` row is also rewritten with `REPLACE INTO`, so a column written behind the db cache would be lost.
The lock therefore lives in its own table, which no packet or db-cache structure carries:
```sql
CREATE TABLE IF NOT EXISTS mt_item_attr_lock (item_id INT UNSIGNED NOT NULL PRIMARY KEY, mask TINYINT UNSIGNED NOT NULL) ENGINE=MyISAM
```
- Each game core creates it at boot (`PortAttrLockBoot`, auth cores skip it). No manual migration is needed.
- Bit i of `mask` is normal slot i (0..4). A mask of 0 deletes the row.
- `PortAttrLockGet` reads the mask once per `CItem` with a synchronous `DBManager::DirectQuery` by primary key and caches it on the item (`m_portAttrLock`, `m_portAttrLockLoaded`). Exchanges on the same core keep the object and its cache. A warp or relog creates a new object, which reads the row again.
- `PortAttrLockSet` writes synchronously, so the next core always sees the new value, and writes an `ATTR_LOCK` item log (hint = new mask).
- A failed lookup logs a syserr, returns 0 and is retried next time. A failed write leaves the cache unchanged and returns the `db` error.
- `CopyAttributeTo` (refine, upgrade, over9 refine, quest item swaps) copies the lock to the new item ID together with the attributes.
- Rows of destroyed items stay behind. They are harmless because item IDs are not reused.
## Rules
- Only the five normal slots can be locked; the rare slots 5/6 are not affected. The item must sit in the inventory, not be equipped or in an exchange, not be a costume, and have an attribute set. The slot must hold an attribute.
- At most 2 lock groups, and at least one filled slot must stay unlocked. On items with an addon type (`sAddonType`, weapons whose slots 0/1 are the skill / normal hit bonus pair), slots 0 and 1 lock and unlock together and count as one group.
- The bot's own item cannot be locked or unlocked while the bot is rolling it (`running`).
- Locking is free. AVA2 consumes 50348/50349 per lock or unlock; those vnums do not exist in 203 `item_proto`.
- `CItem::ChangeAttribute` (scroll rerolls, the switchbot, the GM command):
1. take the effective mask: stored mask ∩ filled slots, with the addon pair kept whole;
2. clear the normal slots;
3. apply the addon only if the addon pair is not locked;
4. put back the locked slots at their indices;
5. reroll the remaining count with the original `PutAttribute`. It writes to the first empty slot and skips types already present, so the gaps fill in order without duplicating a locked type.
With no lock, the effective mask is 0 and the sequence equals the original. The attribute count is kept, and the rare slots are untouched.
- Adding an attribute (71085) and rare-attribute scrolls do not look at the lock.
## Protocol (part of mt_switchbot v1)
```
/mt_switchbot 1 lock CELL SLOT toggle the lock on normal slot SLOT (0..4)
MTSB1 lock CELL VNUM MASK effective mask; also sent after every attrs_end
MTSB1 error lock CODE item equipped no_attr busy running slot max db rate invalid
```
`slot` means out of range or empty. `max` means a third group, or every filled slot locked. `db` means the write failed. Older servers never send a `lock` line, so the client shows no lock buttons there.
## Verification
- `tests/features/test_attr_lock.py` cuts the production `CItem::ChangeAttribute` body out of item_attribute.cpp and compiles it against a stub item. It checks that:
- with no lock every slot changes;
- locked slots 1+3 are kept in place and the gaps are refilled;
- a locked type is not rolled again;
- a lock on an empty slot is ignored;
- a locked addon pair skips `ApplyAddon`, while an unlocked one is rerolled;
- the rare slot is untouched and the count is kept;
- there is no DB lookup when there are no attributes;
- every toggle, effective-mask and group-count rule holds.
- It also checks the boot hook, the Makefile, the cache initialisation and that `CopyAttributeTo` copies the lock.
- `tests/features/test_switchbot.py` covers the `lock` verb (toggle, `slot`, `max`, `db`, `running`, `no_attr`, `item`, `invalid`, the addon pair) and the lock line after `attrs_end`.
- FreeBSD: full rebuild of game in `/root/metin2-features-dev` (log `attrlock-build.log`; `item.h` changed, so every .cpp was touched).
- Live on 2026-10-15, channel-5 test core (13040) with the isolated binary, macOS client, [SA]Admin, armour 11209 (item 20000178, 5 attributes), scroll 71084:
- the first boot created the table;
- locking slot 1 → mask 2, then slot 3 → mask 10, then slot 0 → `error lock max`;
- 10 switchbot rolls (`limit`, scrolls 143 → 133): (23,10) in slot 1 and (39,10) in slot 3 stayed the same on every roll, slots 0/2/4 changed every roll, and there were no duplicate types;
- the row read `20000178 10`;
- a new client session showed `lock 19 11209 10` (reloaded from the table);
- unlocking → 8 → 0, and the row was deleted;
- `log` has four `ATTR_LOCK` rows (hints 2, 10, 8, 0).
- Not done: Android device test and production deploy.
+3 -1
View File
@@ -27,6 +27,7 @@ Requests (250 ms per-character throttle; tokens <= 10 chars, <= 30 tokens):
/mt_switchbot 1 start CELL SCROLL_VNUM LIMIT N1 (APPLY MIN)*N1 [N2 (APPLY MIN)*N2]
/mt_switchbot 1 stop
/mt_switchbot 1 status
/mt_switchbot 1 lock CELL SLOT (attribute lock, see FEATURE-ATTR-LOCK.md)
```
Replies:
@@ -36,6 +37,7 @@ MTSB1 attrs CELL VNUM COUNT
MTSB1 attr INDEX APPLY MAX (COUNT rows, INDEX 0..COUNT-1)
MTSB1 attrs_end CELL COUNT
MTSB1 state ACTIVE CELL VNUM ROLLS LIMIT SCROLL SCROLLS REASON
MTSB1 lock CELL VNUM MASK (after every attrs_end, and as the reply to lock)
MTSB1 error VERB CODE
```
@@ -87,5 +89,5 @@ Capability: the MTF1 cap mask advertises bit 64 (63 -> 127). Clients that don't
## Open items
- Attribute lock: AVA2 stores it at attribute index 7, which the current 7-slot layout cannot hold. It needs its own storage design (see the client migration plan, phase 6).
- Attribute lock: done separately in `FEATURE-ATTR-LOCK.md` (table `mt_item_attr_lock`, applied inside `CItem::ChangeAttribute`, so every bot roll keeps the locked slots). The bot's own item cannot be locked or unlocked while it rolls.
- In 203 `player.item_proto`, 71084, 71151, 76014, 76023 and 39028 are USE/USE_CHANGE_ATTRIBUTE (subtype 18); 71302 (the AVA2 talisman scroll) does not exist. The bot accepts any `USE_CHANGE_ATTRIBUTE` scroll and leaves item rules to the original path.