147 lines
7.6 KiB
Python
147 lines
7.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Verify Dragon Soul / Alchemy mechanics for items 100001, 100002, 100500."""
|
|
from pathlib import Path
|
|
import re
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class DragonSoulAlchemyTest(unittest.TestCase):
|
|
def test_item_proto_definitions(self):
|
|
"""Verify item types, sub-types, and values from item_proto sources."""
|
|
proto_file = (
|
|
ROOT / '..' / 'AVA2' / '3. Source BIN' / 'Tools' / 'DumpProto' / 'Release' / 'item_proto.txt'
|
|
)
|
|
if not proto_file.exists():
|
|
proto_file = ROOT / '..' / 'metin2-client' / 'build' / 'wiki-source' / 'item_proto.txt'
|
|
self.assertTrue(proto_file.exists(), "item_proto.txt must exist")
|
|
|
|
lines = proto_file.read_text(encoding='latin1').splitlines()
|
|
protos = {}
|
|
for line in lines:
|
|
parts = line.split('\t')
|
|
if len(parts) >= 4 and parts[0] in ('100001', '100002', '100500'):
|
|
protos[parts[0]] = parts
|
|
|
|
# 100001: USE_TIME_CHARGE_PER, Value0 = 50 (%)
|
|
self.assertIn('100001', protos)
|
|
p100001 = protos['100001']
|
|
self.assertEqual(p100001[2], 'ITEM_USE')
|
|
self.assertEqual(p100001[3], 'USE_TIME_CHARGE_PER')
|
|
# Value0 index in TSV is typically 24
|
|
self.assertIn('50', p100001)
|
|
|
|
# 100002: USE_TIME_CHARGE_FIX, Value0 = 86400 (24h)
|
|
if '100002' in protos:
|
|
p100002 = protos['100002']
|
|
self.assertEqual(p100002[2], 'ITEM_USE')
|
|
self.assertEqual(p100002[3], 'USE_TIME_CHARGE_FIX')
|
|
self.assertIn('86400', p100002)
|
|
|
|
# 100500: MATERIAL_DS_REFINE_HOLLY
|
|
if '100500' in protos:
|
|
p100500 = protos['100500']
|
|
self.assertEqual(p100500[2], 'ITEM_MATERIAL')
|
|
self.assertEqual(p100500[3], 'MATERIAL_DS_REFINE_HOLLY')
|
|
|
|
def test_dragonsoul_time_charge_logic_char_item(self):
|
|
"""Verify char_item.cpp correctly handles USE_TIME_CHARGE_PER and USE_TIME_CHARGE_FIX."""
|
|
source = (ROOT / 'src/server/game/src/char_item.cpp').read_text(encoding='latin1')
|
|
|
|
# USE_TIME_CHARGE_PER block
|
|
self.assertIn('case USE_TIME_CHARGE_PER:', source)
|
|
per_block = source.split('case USE_TIME_CHARGE_PER:', 1)[1].split('break;', 1)[0]
|
|
self.assertIn('pDestItem->IsDragonSoul()', per_block)
|
|
self.assertIn('pDestItem->GiveMoreTime_Per', per_block)
|
|
self.assertIn('item->SetCount(item->GetCount() - 1);', per_block)
|
|
|
|
# USE_TIME_CHARGE_FIX block
|
|
self.assertIn('case USE_TIME_CHARGE_FIX:', source)
|
|
fix_block = source.split('case USE_TIME_CHARGE_FIX:', 1)[1].split('break;', 1)[0]
|
|
self.assertIn('pDestItem->IsDragonSoul()', fix_block)
|
|
self.assertIn('pDestItem->GiveMoreTime_Fix', fix_block)
|
|
self.assertIn('item->SetCount(item->GetCount() - 1);', fix_block)
|
|
|
|
def test_actual_time_charge_methods_reject_corruption_and_cap_before_conversion(self):
|
|
import subprocess, tempfile
|
|
source=(ROOT/'src/server/game/src/item.cpp').read_bytes().decode('latin1')
|
|
body=source[source.index('int CItem::GiveMoreTime_Per('):source.index('int\tCItem::GetDuration()')]
|
|
harness=r"""
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
using DWORD=uint32_t;
|
|
constexpr int ITEM_SOCKET_REMAIN_SEC=0;
|
|
struct CItem {bool dragon=true;int64_t remaining=0;unsigned writes=0;
|
|
bool IsDragonSoul(){return dragon;}int64_t GetSocket(int){return remaining;}
|
|
void SetSocket(int,int value){remaining=value;++writes;}
|
|
int GiveMoreTime_Per(float);int GiveMoreTime_Fix(DWORD);
|
|
};
|
|
struct DSManager {uint32_t duration=86400;static DSManager& instance(){static DSManager m;return m;}uint32_t GetDuration(CItem*){return duration;}};
|
|
"""
|
|
main=r"""
|
|
int main(){
|
|
CItem i;auto& d=DSManager::instance();
|
|
assert(i.GiveMoreTime_Per(50)==43200&&i.remaining==43200);i.remaining=60000;assert(i.GiveMoreTime_Per(50)==26400&&i.remaining==86400);
|
|
unsigned writes=i.writes;assert(i.GiveMoreTime_Per(50)==0&&i.GiveMoreTime_Fix(86400)==0&&i.writes==writes);
|
|
i.remaining=1000;assert(i.GiveMoreTime_Fix(86400)==85400&&i.remaining==86400);
|
|
i.remaining=0;writes=i.writes;
|
|
for(float f:{-1.f,0.f,std::numeric_limits<float>::infinity(),std::numeric_limits<float>::quiet_NaN()})assert(i.GiveMoreTime_Per(f)==0&&i.writes==writes);
|
|
assert(i.GiveMoreTime_Per(.00001f)==0&&i.writes==writes);
|
|
assert(i.GiveMoreTime_Per(std::numeric_limits<float>::max())==86400&&i.remaining==86400);
|
|
for(int64_t remaining:{int64_t(-1),int64_t(INT32_MIN),int64_t(86401),int64_t(INT32_MAX)}){i.remaining=remaining;writes=i.writes;assert(i.GiveMoreTime_Per(50)==0&&i.GiveMoreTime_Fix(UINT32_MAX)==0&&i.remaining==remaining&&i.writes==writes);}
|
|
d.duration=INT32_MAX;i.remaining=INT32_MAX-10;assert(i.GiveMoreTime_Fix(UINT32_MAX)==10&&i.remaining==INT32_MAX);
|
|
for(uint32_t duration:{0u,UINT32_MAX}){d.duration=duration;i.remaining=0;writes=i.writes;assert(i.GiveMoreTime_Per(50)==0&&i.GiveMoreTime_Fix(86400)==0&&i.writes==writes);}
|
|
d.duration=86400;i.dragon=false;assert(i.GiveMoreTime_Per(50)==0&&i.GiveMoreTime_Fix(86400)==0);
|
|
}
|
|
"""
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
p=Path(tmp);(p/'test.cpp').write_text(harness+body+main)
|
|
subprocess.run(['c++','-std=c++17','-include','initializer_list','-fsanitize=address,undefined','-fno-sanitize-recover=all',str(p/'test.cpp'),'-o',str(p/'probe')],check=True)
|
|
subprocess.run([str(p/'probe')],check=True)
|
|
|
|
def test_dragon_bean_100500_refine_strength_mechanics(self):
|
|
"""Verify 100500 MATERIAL_DS_REFINE_HOLLY in DSManager::DoRefineStrength."""
|
|
source = (ROOT / 'src/server/game/src/DragonSoul.cpp').read_text(encoding='latin1')
|
|
|
|
# Check IsDragonSoulRefineMaterial recognizes MATERIAL_DS_REFINE_HOLLY
|
|
mat_check = source.split('bool IsDragonSoulRefineMaterial(LPITEM pItem)', 1)[1].split('bool DSManager::DoRefineStrength', 1)[0]
|
|
self.assertIn('MATERIAL_DS_REFINE_HOLLY', mat_check)
|
|
|
|
# Check DoRefineStrength handling of refine stone and fee
|
|
refine_func = source.split('bool DSManager::DoRefineStrength', 1)[1].split('SendRefineResultPacket(ch, bSubHeader,', 1)[0]
|
|
|
|
# Fee deduction before roll
|
|
self.assertIn('if (ch->GetGold() < fee)', refine_func)
|
|
self.assertIn('ch->PointChange(POINT_GOLD, -fee);', refine_func)
|
|
|
|
# Material stack decrement in both branches
|
|
success_branch = refine_func.split('if (fnumber(0.f, 100.f) <= fProb)', 1)[1].split('else', 1)[0]
|
|
fail_branch = refine_func.split('else', 1)[1]
|
|
|
|
self.assertIn('pRefineStone->SetCount(pRefineStone->GetCount() - 1);', success_branch)
|
|
self.assertIn('pRefineStone->SetCount(pRefineStone->GetCount() - 1);', fail_branch)
|
|
|
|
def test_dragon_soul_table_holly_values(self):
|
|
"""Verify RefineStrengthTables configures MATERIAL_DS_REFINE_HOLLY fee and probs."""
|
|
table_path = ROOT / '..' / '40250' / 'Server Client TMP4' / 'Server' / 'metin2' / 'server' / 'share' / 'locale' / 'english' / 'dragon_soul_table.txt'
|
|
self.assertTrue(table_path.exists(), "dragon_soul_table.txt must exist")
|
|
|
|
content = table_path.read_text(encoding='latin1')
|
|
match = re.search(r'MATERIAL_DS_REFINE_HOLLY\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)', content)
|
|
self.assertIsNotNone(match, "MATERIAL_DS_REFINE_HOLLY table line must exist")
|
|
|
|
fee = int(match.group(1))
|
|
probs = [int(match.group(i)) for i in range(2, 8)]
|
|
|
|
# Holly bean fee = 30000 gold
|
|
self.assertEqual(fee, 30000)
|
|
# Probabilities for strength 0 to 5: 100%, 90%, 80%, 60%, 40%, 30%
|
|
self.assertEqual(probs, [100, 90, 80, 60, 40, 30])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|