diff --git a/src/server/common/item_length.h b/src/server/common/item_length.h index 0b9d07e4..c63c0584 100755 --- a/src/server/common/item_length.h +++ b/src/server/common/item_length.h @@ -99,6 +99,9 @@ enum EWeaponSubTypes WEAPON_FAN, WEAPON_ARROW, WEAPON_MOUNT_SPEAR, + // PORT: preserve AVA2 numeric positions; reserving claw does not enable Wolfman. + WEAPON_CLAW = 8, + WEAPON_QUIVER = 9, WEAPON_NUM_TYPES, }; diff --git a/src/server/db/src/ProtoReader.cpp b/src/server/db/src/ProtoReader.cpp index 59098ed8..e3f007b1 100755 --- a/src/server/db/src/ProtoReader.cpp +++ b/src/server/db/src/ProtoReader.cpp @@ -102,7 +102,7 @@ int get_Item_Type_Value(string inputString) int get_Item_SubType_Value(int type_value, string inputString) { static string arSub1[] = { "WEAPON_SWORD", "WEAPON_DAGGER", "WEAPON_BOW", "WEAPON_TWO_HANDED", - "WEAPON_BELL", "WEAPON_FAN", "WEAPON_ARROW", "WEAPON_MOUNT_SPEAR"}; + "WEAPON_BELL", "WEAPON_FAN", "WEAPON_ARROW", "WEAPON_MOUNT_SPEAR", "WEAPON_CLAW", "WEAPON_QUIVER"}; static string arSub2[] = { "ARMOR_BODY", "ARMOR_HEAD", "ARMOR_SHIELD", "ARMOR_WRIST", "ARMOR_FOOTS", "ARMOR_NECK", "ARMOR_EAR", "ARMOR_PENDANT", "ARMOR_NUM_TYPES"}; static string arSub3[] = { "USE_POTION", "USE_TALISMAN", "USE_TUNING", "USE_MOVE", "USE_TREASURE_BOX", "USE_MONEYBAG", "USE_BAIT", diff --git a/src/server/game/src/Makefile b/src/server/game/src/Makefile index 79ade9f3..58baae4d 100755 --- a/src/server/game/src/Makefile +++ b/src/server/game/src/Makefile @@ -146,3 +146,5 @@ $(OBJDIR)/char.o $(OBJDIR)/cmd_general.o $(OBJDIR)/input_p2p.o $(OBJDIR)/p2p.o $ $(OBJDIR)/cmd_general.o: port_features.h port_rank_runtime.h $(OBJDIR)/char_battle.o $(OBJDIR)/char_skill.o: port_magic_reduction.h +$(OBJDIR)/char_battle.o: port_quiver.h +$(OBJDIR)/char.o: port_quiver.h port_quiver_sync.inc diff --git a/src/server/game/src/char.cpp b/src/server/game/src/char.cpp index 090be6a4..1cd210ff 100755 --- a/src/server/game/src/char.cpp +++ b/src/server/game/src/char.cpp @@ -70,6 +70,9 @@ #include "PetSystem.h" #endif #include "DragonSoul.h" +#include "item.h" +#include "port_quiver.h" +#include "port_quiver_sync.inc" extern const BYTE g_aBuffOnAttrPoints; extern bool RaceToJob(unsigned race, unsigned *ret_job); @@ -137,6 +140,7 @@ CHARACTER::~CHARACTER() void CHARACTER::Initialize() { + m_portQuiverSync=false; CEntity::Initialize(ENTITY_CHARACTER); m_bNoOpenedShop = true; @@ -984,6 +988,7 @@ void CHARACTER::EncodeInsertPacket(LPENTITY entity) } d->Packet(&addPacket, sizeof(TPacketGCCharacterAdditionalInfo)); + PortSendQuiverState(ch); } if (iDur) @@ -1132,6 +1137,7 @@ void CHARACTER::UpdatePacket() { PacketAround(&pack, sizeof(pack)); } + PortBroadcastQuiverState(); } LPCHARACTER CHARACTER::FindCharacterInView(const char * c_pszName, bool bFindPCOnly) diff --git a/src/server/game/src/char.h b/src/server/game/src/char.h index b598d973..256cc44f 100755 --- a/src/server/game/src/char.h +++ b/src/server/game/src/char.h @@ -571,6 +571,10 @@ class CHARACTER : public CEntity, public CFSM, public CHorseRider public: DWORD m_portFeatureLastQuery = 0; // PORT: read-only query throttle, not persisted. + bool m_portQuiverSync = false; // PORT: only this character's negotiated client. + void PortEnableQuiverSync(); + void PortSendQuiverState(LPCHARACTER viewer); + void PortBroadcastQuiverState(); bool m_portQuietInfo = false; // PORT: batch actions hold back per-item INFO lines... std::string m_portLastInfo; // ...keeping the last one to report a rejected item. DWORD m_portPickupLastCommand = 0; // PORT: batch pickup throttle. diff --git a/src/server/game/src/char_battle.cpp b/src/server/game/src/char_battle.cpp index 405eec0d..054d00fd 100755 --- a/src/server/game/src/char_battle.cpp +++ b/src/server/game/src/char_battle.cpp @@ -46,6 +46,7 @@ #include "port_battlepass_hooks.h" #include "ava2_itemshop.h" #include +#include "port_quiver.h" DWORD AdjustExpByLevel(const LPCHARACTER ch, const DWORD exp) { if (PLAYER_EXP_TABLE_MAX < ch->GetLevel()) @@ -2876,12 +2877,19 @@ int CHARACTER::GetArrowAndBow(LPITEM * ppkBow, LPITEM * ppkArrow, int iArrowCoun LPITEM pkArrow; if (!(pkArrow = GetWear(WEAR_ARROW)) || pkArrow->GetType() != ITEM_WEAPON || - pkArrow->GetProto()->bSubType != WEAPON_ARROW) + (pkArrow->GetProto()->bSubType != WEAPON_ARROW && pkArrow->GetProto()->bSubType != WEAPON_QUIVER)) { return 0; } - iArrowCount = MIN(iArrowCount, pkArrow->GetCount()); + if (pkArrow->GetSubType() == WEAPON_QUIVER) + { + if (!PortQuiverUsable(pkArrow)) + return 0; + iArrowCount = MIN(iArrowCount, 200); + } + else + iArrowCount = MIN(iArrowCount, pkArrow->GetCount()); *ppkBow = pkBow; *ppkArrow = pkArrow; @@ -2891,6 +2899,9 @@ int CHARACTER::GetArrowAndBow(LPITEM * ppkBow, LPITEM * ppkArrow, int iArrowCoun void CHARACTER::UseArrow(LPITEM pkArrow, DWORD dwArrowCount) { + if (pkArrow->GetSubType() == WEAPON_QUIVER) + return; + int iCount = pkArrow->GetCount(); DWORD dwVnum = pkArrow->GetVnum(); iCount = iCount - MIN(iCount, dwArrowCount); diff --git a/src/server/game/src/constants.cpp b/src/server/game/src/constants.cpp index 14b54ea7..c17ac954 100755 --- a/src/server/game/src/constants.cpp +++ b/src/server/game/src/constants.cpp @@ -791,6 +791,8 @@ const int aiWeaponSocketQty[WEAPON_NUM_TYPES] = 3, // WEAPON_FAN, 0, // WEAPON_ARROW, 0, // WEAPON_MOUNT_SPEAR + 3, // WEAPON_CLAW (numeric reservation) + 0, // WEAPON_QUIVER }; const int aiArmorSocketQty[ARMOR_NUM_TYPES] = diff --git a/src/server/game/src/item_attribute.cpp b/src/server/game/src/item_attribute.cpp index 4fe1c166..cc2b6660 100755 --- a/src/server/game/src/item_attribute.cpp +++ b/src/server/game/src/item_attribute.cpp @@ -14,7 +14,7 @@ int CItem::GetAttributeSetIndex() { if (GetType() == ITEM_WEAPON) { - if (GetSubType() == WEAPON_ARROW) + if (GetSubType() == WEAPON_ARROW || GetSubType() == WEAPON_QUIVER) return -1; return ATTRIBUTE_SET_WEAPON; diff --git a/src/server/game/src/packet.h b/src/server/game/src/packet.h index a0285e22..0a01b7f0 100755 --- a/src/server/game/src/packet.h +++ b/src/server/game/src/packet.h @@ -326,6 +326,7 @@ enum HEADER_GC_CHESTDROP = 66, // PORT: AVA2, DWORD dynamic length HEADER_GC_SWITCHBOT = 171, // AVA2 switchbot (port_switchbot.cpp) HEADER_GC_EXTENDED_POINTS = 218, // PORT: AVA2 64-bit gold + HEADER_GC_PORT_QUIVER = 219, // PORT: negotiated, transient actor arrow subtype. #if defined(__BL_67_ATTR__) HEADER_GC_OPEN_67_ATTR = 242, #endif @@ -1002,6 +1003,14 @@ enum ECharacterEquipmentPart CHR_EQUIPPART_NUM, }; +struct TPacketGCQuiverState +{ + BYTE header; + DWORD vid; + BYTE type; // 0 none, 6 arrow, 9 quiver. No persistent character layout change. +}; +static_assert(sizeof(TPacketGCQuiverState)==6,"quiver state wire size"); + typedef struct packet_add_char { BYTE header; diff --git a/src/server/game/src/port_features.h b/src/server/game/src/port_features.h index 6ede47fa..1b5c3720 100644 --- a/src/server/game/src/port_features.h +++ b/src/server/game/src/port_features.h @@ -47,7 +47,12 @@ ACMD(do_port_features) { ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 error %u rate",request);return; } ch->m_portFeatureLastQuery=get_dword_time(); - if(kind=="cap" && args.size()==3) {ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 cap %u 1 1983 %lld",request,(long long)get_global_time());return;} + if(kind=="cap" && args.size()==3) {ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 cap %u 1 4031 %lld",request,(long long)get_global_time());return;} + if(kind=="quiver" && args.size()==3) { + ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 begin %u quiver 0 0 0 0 %lld",request,(long long)get_global_time()); + ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 end %u 0",request); + ch->PortEnableQuiverSync();return; + } if(kind=="dungeons" && args.size()==3) {PortDungeonQuery(ch,request);return;} if(kind=="bosses" && args.size()==3) {PortBossQuery(ch,request);return;} if(kind=="stats" && args.size()==3) { diff --git a/src/server/game/src/port_quiver.h b/src/server/game/src/port_quiver.h new file mode 100644 index 00000000..aaeb33a4 --- /dev/null +++ b/src/server/game/src/port_quiver.h @@ -0,0 +1,22 @@ +#ifndef METIN2_PORT_QUIVER_H +#define METIN2_PORT_QUIVER_H + +// PORT: do not grant a shot during the gap before the generic expiry event. +// Timer duration and first-use initialization still belong to the item system. +static bool PortQuiverUsable(LPITEM item) +{ + if (!item || item->GetCount() <= 0) + return false; + for (int i = 0; i < ITEM_LIMIT_MAX_NUM; ++i) + { + const int type = item->GetLimitType(i); + if ((type == LIMIT_REAL_TIME || + (type == LIMIT_REAL_TIME_START_FIRST_USE && item->GetSocket(1) != 0)) && + get_global_time() > item->GetSocket(0)) + return false; + if (type == LIMIT_TIMER_BASED_ON_WEAR && item->GetSocket(0) <= 0) + return false; + } + return true; +} +#endif diff --git a/src/server/game/src/port_quiver_sync.inc b/src/server/game/src/port_quiver_sync.inc new file mode 100644 index 00000000..0fe8113b --- /dev/null +++ b/src/server/game/src/port_quiver_sync.inc @@ -0,0 +1,37 @@ +// Included after CHARACTER and item dependencies, never as persistent state. +void CHARACTER::PortSendQuiverState(LPCHARACTER viewer) +{ + if (!IsPC() || !viewer || !viewer->m_portQuiverSync || !viewer->GetDesc()) + return; + if (IsObserverMode() && viewer != this) + return; + BYTE type=0; + LPITEM ammo=GetWear(WEAR_ARROW); + if (ammo && ammo->GetType()==ITEM_WEAPON && ammo->GetCount()>0) + { + if (ammo->GetSubType()==WEAPON_ARROW) + type=WEAPON_ARROW; + else if (ammo->GetSubType()==WEAPON_QUIVER && PortQuiverUsable(ammo)) + type=WEAPON_QUIVER; + } + TPacketGCQuiverState packet={HEADER_GC_PORT_QUIVER,GetVID(),type}; + viewer->GetDesc()->Packet(&packet,sizeof(packet)); +} + +void CHARACTER::PortBroadcastQuiverState() +{ + PortSendQuiverState(this); + if (IsObserverMode())return; + for (const auto& entry:m_map_view) + if (entry.first && entry.first->IsType(ENTITY_CHARACTER)) + PortSendQuiverState(static_cast(entry.first)); +} + +void CHARACTER::PortEnableQuiverSync() +{ + m_portQuiverSync=true; + PortSendQuiverState(this); + for (const auto& entry:m_map_view) + if (entry.first && entry.first!=this && entry.first->IsType(ENTITY_CHARACTER)) + static_cast(entry.first)->PortSendQuiverState(this); +} diff --git a/tests/features/test_bosses.py b/tests/features/test_bosses.py index c744e398..e8674a5a 100644 --- a/tests/features/test_bosses.py +++ b/tests/features/test_bosses.py @@ -8,7 +8,8 @@ SRC=ROOT/'src/server/game/src' def text(name):return (SRC/name).read_bytes().decode('latin1') features=text('port_features.h');glue=text('port_boss_track.cpp');p2p=text('input_p2p.cpp');main=text('main.cpp') assert 'if(kind=="bosses" && args.size()==3) {PortBossQuery(ch,request);return;}' in features -assert '"MTF1 cap %u 1 959 %lld"' in features +# Current shared capabilities include later rank and negotiated quiver support. +assert '"MTF1 cap %u 1 4031 %lld"' in features assert 'port_boss_track.cpp' in text('Makefile') assert 'PortBossTick();' in main.split('if (!g_bAuthServer)',1)[1].split('}',1)[0] # 1 s block, game cores only # Relay hook runs after the size checks and before the player lookup; old cores just miss the name. diff --git a/tests/features/test_queries.py b/tests/features/test_queries.py index 113acfaa..fe2e29b7 100644 --- a/tests/features/test_queries.py +++ b/tests/features/test_queries.py @@ -59,6 +59,7 @@ static DWORD clockms=1000;unsigned get_dword_time(){return clockms;}long long ge struct Character { unsigned m_portFeatureLastQuery=0;bool pc=true;std::vector messages;std::map points; bool IsPC(){return pc;}void* GetDesc(){return this;} + void PortEnableQuiverSync(){} int GetPoint(unsigned point){return points[point];} int GetHP(){return 123;}int GetMaxHP(){return 500;}int GetSP(){return 42;}int GetMaxSP(){return 100;}int GetLevel(){return 30;} void ChatPacket(int,const char* format,...) { @@ -118,7 +119,7 @@ int main(){ assert(rewards[0].total==100 && rewards[0].rule==WIKI_SINGLE); rewards=items.PortWikiChest(50012);assert(rewards[1].rule==WIKI_INDEPENDENT && rewards[1].weight==150 && rewards[1].total==100); assert(items.PortWikiChest(50013).empty() && items.PortWikiChest(19).empty()); - Character ch;query(ch,"1 99 travel 2493 0");assert(travelBoss==2493);query(ch,"1 1 cap");assert(ch.messages.back()=="MTF1 cap 1 1 1983 1700000000"); + Character ch;query(ch,"1 99 travel 2493 0");assert(travelBoss==2493);query(ch,"1 1 cap");assert(ch.messages.back()=="MTF1 cap 1 1 4031 1700000000"); query(ch,"1 90 rank 0 0");assert(rankQuery==0); query(ch,"1 91 rank 10 0");assert(ch.messages.back()=="MTF1 error 91 invalid" && rankQuery==0); query(ch,"1 92 rank 6 1");assert(ch.messages.back()=="MTF1 error 92 invalid" && rankQuery==0); diff --git a/tests/features/test_quiver_runtime.py b/tests/features/test_quiver_runtime.py new file mode 100644 index 00000000..5c0ec6ab --- /dev/null +++ b/tests/features/test_quiver_runtime.py @@ -0,0 +1,109 @@ +"""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 +#include +#include +#include +using std::string; +#define MIN(a,b) std::min((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()