110 lines
5.3 KiB
Python
110 lines
5.3 KiB
Python
"""Compile actual ammunition functions with explicit item/character substitutes."""
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SRC = ROOT / 'src/server/game/src'
|
|
|
|
|
|
class QuiverRuntimeTests(unittest.TestCase):
|
|
def test_actual_ammunition_and_expiry_paths(self):
|
|
text = (SRC / 'char_battle.cpp').read_bytes().decode('latin1')
|
|
functions = text[text.index('int CHARACTER::GetArrowAndBow('):text.index('class CFuncShoot')]
|
|
self.assertIn('m_me->UseArrow(pkArrow, iUseArrow)', text)
|
|
self.assertIn('m_me->UseArrow(pkArrow, 1)', text)
|
|
enum = (ROOT / 'src/server/common/item_length.h').read_text()
|
|
self.assertIn('WEAPON_CLAW = 8', enum)
|
|
self.assertIn('WEAPON_QUIVER = 9', enum)
|
|
reader = (ROOT / 'src/server/db/src/ProtoReader.cpp').read_bytes()
|
|
self.assertIn(b'"WEAPON_MOUNT_SPEAR", "WEAPON_CLAW", "WEAPON_QUIVER"', reader)
|
|
attrs = (SRC / 'item_attribute.cpp').read_text()
|
|
self.assertIn('GetSubType() == WEAPON_ARROW || GetSubType() == WEAPON_QUIVER', attrs)
|
|
proto = reader.decode('latin1')
|
|
parser = proto[proto.index('int get_Item_SubType_Value('):proto.index('int get_Item_AntiFlag_Value(')]
|
|
trim = proto[proto.index('inline string trim_left('):proto.index('static string* StringSplit(')]
|
|
code = r'''
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <string>
|
|
using std::string;
|
|
#define MIN(a,b) std::min<unsigned>((a),(b))
|
|
#define sys_log(...) ((void)0)
|
|
#define sys_err(...) ((void)0)
|
|
#define _countof(a) (sizeof(a)/sizeof((a)[0]))
|
|
using DWORD=uint32_t;
|
|
enum {WEAR_WEAPON,WEAR_ARROW,ITEM_WEAPON=1,ITEM_LIMIT_MAX_NUM=2,LIMIT_REAL_TIME=7,
|
|
LIMIT_REAL_TIME_START_FIRST_USE=8,LIMIT_TIMER_BASED_ON_WEAR=9};
|
|
''' + re.search(r'enum EWeaponSubTypes\s*\{.*?\};', enum, re.S).group(0) + trim + parser + r'''
|
|
long now=1000;long get_global_time(){return now;}
|
|
struct Item {
|
|
struct Proto{int bSubType=WEAPON_ARROW;} proto;
|
|
int type=ITEM_WEAPON,count=1,limits[2]={0,0};long sockets[3]={0,0,0};
|
|
unsigned vnum=80000;
|
|
Proto* GetProto(){return &proto;}int GetType(){return type;}
|
|
int GetSubType(){return proto.bSubType;}int GetCount(){return count;}
|
|
void SetCount(int n){count=n;}unsigned GetVnum(){return vnum;}
|
|
int GetLimitType(int i){return limits[i];}long GetSocket(int i){return sockets[i];}
|
|
};
|
|
using LPITEM=Item*;
|
|
#include "port_quiver.h"
|
|
struct CHARACTER {
|
|
Item *bow=nullptr,*ammo=nullptr,*replacement=nullptr,*equipped=nullptr;
|
|
LPITEM GetWear(int slot){return slot==WEAR_WEAPON?bow:ammo;}
|
|
LPITEM FindSpecifyItem(DWORD){return replacement;}
|
|
void EquipItem(LPITEM p){equipped=p;}
|
|
int GetArrowAndBow(LPITEM*,LPITEM*,int=1);void UseArrow(LPITEM,DWORD);
|
|
};
|
|
''' + functions + r'''
|
|
int main(){
|
|
static_assert(WEAPON_CLAW==8 && WEAPON_QUIVER==9 && WEAPON_NUM_TYPES==10);
|
|
assert(get_Item_SubType_Value(ITEM_WEAPON,"WEAPON_QUIVER")==WEAPON_QUIVER);
|
|
assert(get_Item_SubType_Value(ITEM_WEAPON,"WEAPON_CLAW")==WEAPON_CLAW);
|
|
assert(get_Item_SubType_Value(ITEM_WEAPON,"WEAPON_ARROW")==WEAPON_ARROW);
|
|
assert(get_Item_SubType_Value(ITEM_WEAPON,"WEAPON_UNKNOWN")==-1);
|
|
Item bow,arrow,quiver,next;bow.proto.bSubType=WEAPON_BOW;
|
|
quiver.proto.bSubType=WEAPON_QUIVER;arrow.count=3;
|
|
CHARACTER ch;LPITEM outBow=nullptr,outAmmo=nullptr;
|
|
assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);
|
|
ch.bow=&bow;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);
|
|
ch.ammo=&arrow;
|
|
assert(ch.GetArrowAndBow(&outBow,&outAmmo,5)==3);
|
|
assert(outBow==&bow && outAmmo==&arrow);
|
|
ch.UseArrow(&arrow,2);assert(arrow.count==1);
|
|
ch.replacement=&next;ch.UseArrow(&arrow,5);
|
|
assert(arrow.count==0 && ch.equipped==&next);
|
|
ch.ammo=&quiver;
|
|
assert(ch.GetArrowAndBow(&outBow,&outAmmo,300)==200);
|
|
for(int i=0;i<1000;++i){assert(ch.GetArrowAndBow(&outBow,&outAmmo)==1);ch.UseArrow(&quiver,200);}
|
|
assert(quiver.count==1 && outAmmo==&quiver);
|
|
quiver.type=3;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);quiver.type=ITEM_WEAPON;
|
|
quiver.count=0;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);quiver.count=1;
|
|
bow.proto.bSubType=0;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);bow.proto.bSubType=WEAPON_BOW;
|
|
quiver.limits[0]=LIMIT_REAL_TIME;quiver.sockets[0]=now-1;
|
|
assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);
|
|
quiver.sockets[0]=now;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==1);
|
|
++now;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);
|
|
quiver.limits[0]=LIMIT_REAL_TIME_START_FIRST_USE;quiver.sockets[1]=1;
|
|
assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);
|
|
quiver.sockets[0]=now+60;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==1);
|
|
quiver.limits[0]=LIMIT_TIMER_BASED_ON_WEAR;quiver.sockets[0]=0;
|
|
assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);
|
|
quiver.sockets[0]=1;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==1);
|
|
ch.ammo=nullptr;assert(ch.GetArrowAndBow(&outBow,&outAmmo)==0);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory(prefix='ch5-quiver-') as folder:
|
|
root = Path(folder)
|
|
(root / 'probe.cpp').write_text(code)
|
|
subprocess.run(['c++', '-std=c++17', '-fsanitize=address,undefined',
|
|
'-fno-sanitize-recover=all', '-I'+str(SRC),
|
|
str(root/'probe.cpp'), '-o', str(root/'probe')], check=True)
|
|
subprocess.run([str(root/'probe')], check=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|