392 lines
29 KiB
C++
392 lines
29 KiB
C++
#include "loot_translation.hpp"
|
|
#include "serializer.h"
|
|
#include <algorithm>
|
|
#include <set>
|
|
using namespace gateway;
|
|
namespace {
|
|
Guid128 read_guid(Reader& r){Guid128 out{};auto v=unpack_guid(r,16);std::copy(v.begin(),v.end(),out.begin());return out;}
|
|
NativeLootTranslation::Binding owner(std::span<const uint8_t> body,const NativeEntityTranslation& entities){
|
|
Reader r(body);auto guid=read_guid(r);r.end();
|
|
for(const auto& [source,a]:entities.actors)if(a.kind==3 && a.sent && a.modern==guid)
|
|
return {source,a.generation,guid};
|
|
throw Error("loot request outside visible source creature binding");
|
|
}
|
|
bool current(const NativeLootTranslation::Binding& b,const NativeEntityTranslation& entities){
|
|
auto a=entities.actors.find(b.source);return a!=entities.actors.end() && a->second.sent &&
|
|
a->second.kind==b.kind && a->second.generation==b.generation && a->second.modern==b.modern &&
|
|
((b.kind==3 && (!b.realm || (a->second.eligible &&
|
|
a->second.presentation_realm==b.realm && a->second.presentation_map==b.map))) || (b.kind==5 && a->second.eligible &&
|
|
(a->second.gameobject_type==3 || a->second.gameobject_type==17 || a->second.gameobject_type==25) &&
|
|
entities.diagnostic_scope_known && entities.diagnostic_realm==b.realm && entities.diagnostic_map==b.map &&
|
|
a->second.presentation_realm==b.realm && a->second.presentation_map==b.map));
|
|
}
|
|
void soft_interaction(Reader& r){if(r.le(1)&0x7f)throw Error("unsupported loot interaction bits");r.end();}
|
|
Guid128 object_id(uint64_t serial,uint16_t realm,uint32_t map){
|
|
if(!serial || serial>0xffffffffffULL || realm>8191 || map>8191)throw Error("loot presentation identity bound");
|
|
Bytes b;le(b,serial,8);le(b,(uint64_t(15)<<58)|(uint64_t(realm)<<42)|(uint64_t(map)<<29),8);
|
|
Guid128 out{};std::copy(b.begin(),b.end(),out.begin());return out;
|
|
}
|
|
bool same_roll(const TurtleLootRoll& a,const TurtleLootRoll& b){
|
|
return a.owner==b.owner && a.slot==b.slot && a.item==b.item &&
|
|
a.suffix==b.suffix && a.random_property==b.random_property;
|
|
}
|
|
bool same_group(const TurtleGroupList& a,const TurtleGroupList& b){
|
|
if(a.explicit_solo!=b.explicit_solo || a.leader!=b.leader || a.type!=b.type ||
|
|
a.loot.has_value()!=b.loot.has_value() || a.members.size()!=b.members.size())return false;
|
|
if(a.loot && (a.loot->method!=b.loot->method || a.loot->master!=b.loot->master ||
|
|
a.loot->threshold!=b.loot->threshold))return false;
|
|
for(const auto& m:a.members)if(std::none_of(b.members.begin(),b.members.end(),
|
|
[&](const auto& n){return n.guid==m.guid;}))return false;
|
|
return true;
|
|
}
|
|
bool member(uint64_t id,uint64_t self,const TurtleGroupList& group){
|
|
return id==self || std::any_of(group.members.begin(),group.members.end(),[&](const auto& m){return m.guid==id;});
|
|
}
|
|
void queue_roll_chat(std::deque<std::string>& notices,char kind,const NativeLootTranslation::Roll& roll,
|
|
const TurtleLootRoll& result,const Character& self,const TurtleGroupList& group){
|
|
if(!roll.item || notices.size()>=128 || result.number<1 || result.number>100 || (result.type!=1 && result.type!=2))return;
|
|
std::string name;
|
|
if(result.player==self.guid)name=self.name;
|
|
else for(const auto& player:group.members)if(player.guid==result.player){name=player.name;break;}
|
|
if(name.empty() || name.size()>48 || std::any_of(name.begin(),name.end(),[](unsigned char c){return c<32 || c==127 || c=='|';}))return;
|
|
const auto key=std::to_string(roll.owner.source)+":"+std::to_string(roll.owner.generation)+":"+
|
|
std::to_string(roll.start.slot)+":"+std::to_string(result.player);
|
|
notices.push_back(std::string("GWLOOT1 ")+kind+" "+key+" "+std::to_string(roll.item->item)+" "+
|
|
std::to_string(result.number)+" "+std::to_string(result.type)+" "+(result.player==self.guid?"1 ":"0 ")+name);
|
|
}
|
|
void roll_item(Bytes& b,const TurtleLootItem& item){
|
|
le(b,item.slot,1);le(b,item.item,4);le(b,item.count,4);le(b,uint32_t(item.random_property),4);
|
|
}
|
|
Bytes roll_input(uint8_t kind,const NativeLootTranslation::Roll& roll,uint64_t player,uint16_t realm,
|
|
int32_t number,uint8_t type,uint8_t ui=1){
|
|
if(!roll.item)throw Error("roll has no genuine source item");
|
|
Bytes b{kind};append(b,roll.object);roll_item(b,*roll.item);
|
|
if(kind==6){le(b,roll.map,4);le(b,uint32_t(number),4);le(b,roll.method,1);}
|
|
else if(kind==7 || kind==8){append(b,forever_player_guid(player,realm));le(b,uint32_t(number),4);le(b,type,1);}
|
|
if(kind==7)le(b,ui,1);
|
|
return b;
|
|
}
|
|
}
|
|
LegacyPacket NativeLootTranslation::gameplay(uint32_t op,std::span<const uint8_t> body,const NativeEntityTranslation& entities)const{
|
|
if(op==0x3e00cc){
|
|
auto b=owner(body,entities);
|
|
if(opening && current(*opening,entities))throw Error("loot open while a source response is pending");
|
|
return {0x15d,turtle_loot_open(b.source)};
|
|
}
|
|
if(op==0x3e00d2 && !window && opening){
|
|
Reader r(body);auto guid=read_guid(r);r.end();
|
|
if(guid!=opening->modern)throw Error("pending loot release owner mismatch");
|
|
return {0x15f,turtle_loot_release(opening->source)};
|
|
}
|
|
if(op==0x3e00d3){
|
|
Reader r(body);auto object=read_guid(r);auto slot=r.le(1),vote=r.le(1);r.end();
|
|
auto found=std::find_if(rolls.begin(),rolls.end(),[&](const Roll& roll){
|
|
return roll.published && !roll.voted && roll.object==object && roll.start.slot==slot &&
|
|
std::chrono::steady_clock::now()<roll.deadline && current(roll.owner,entities);
|
|
});
|
|
if(group_transition || !group || found==rolls.end() || vote>2)throw Error("loot vote outside current eligible source roll");
|
|
return {0x2a0,turtle_loot_roll_vote(found->owner.source,uint32_t(slot),uint8_t(vote))};
|
|
}
|
|
if(!window)throw Error("loot operation without genuine acquired source window");
|
|
if(op==0x3e00d2){Reader r(body);auto guid=read_guid(r);r.end();
|
|
if(guid!=window->owner.modern)throw Error("loot release owner mismatch");
|
|
return {0x15f,turtle_loot_release(window->owner.source)};
|
|
}
|
|
if(op==0x3e00cd){Reader r(body);soft_interaction(r);
|
|
if(!current(window->owner,entities))throw Error("loot money outside current source generation");
|
|
return {0x15e,turtle_loot_money()};
|
|
}
|
|
throw Error("unsupported native loot request");
|
|
}
|
|
std::vector<LegacyPacket> NativeLootTranslation::items(std::span<const uint8_t> body,const NativeEntityTranslation& entities)const{
|
|
if(!window || !current(window->owner,entities))throw Error("loot items without current acquired source window");
|
|
Reader r(body);auto count=r.le(4);if(!count || count>16)throw Error("loot item batch bound");
|
|
std::set<uint8_t> seen;std::vector<LegacyPacket> out;
|
|
for(unsigned i=0;i<count;++i){auto object=read_guid(r);auto slot=uint8_t(r.le(1));
|
|
if(object!=window->object || !seen.insert(slot).second)throw Error("loot item object/duplicate mismatch");
|
|
auto row=std::find_if(window->source.items.begin(),window->source.items.end(),[&](const auto& v){return v.slot==slot;});
|
|
if(row==window->source.items.end() || (row->ui!=TurtleLootSlotUi::allow && row->ui!=TurtleLootSlotUi::owner))
|
|
throw Error("loot item outside server-allowed slot");
|
|
out.push_back({0x108,turtle_loot_item(slot)});
|
|
}
|
|
soft_interaction(r);return out; // Validate the entire batch before any cipher/send/state change.
|
|
}
|
|
std::vector<LegacyPacket> NativeLootTranslation::master_items(std::span<const uint8_t> body,const NativeEntityTranslation& entities,uint64_t self,uint16_t realm)const{
|
|
if(!window || !current(window->owner,entities) || group_transition || !group || !group->loot ||
|
|
group->loot->method!=TurtleGroupLootMethod::master || group->loot->master!=self)
|
|
throw Error("master assignment outside acquired window/source master role");
|
|
Reader r(body);auto count=r.le(4);if(!count || count>16)throw Error("master item batch bound");
|
|
auto target=read_guid(r);uint64_t source_target=0;
|
|
for(auto candidate:window->master_candidates)if(forever_player_guid(candidate,realm)==target)source_target=candidate;
|
|
if(!source_target)throw Error("master target outside authoritative candidates");
|
|
std::set<uint8_t> seen;std::vector<LegacyPacket> out;
|
|
for(unsigned i=0;i<count;++i){auto object=read_guid(r);auto slot=uint8_t(r.le(1));
|
|
if(object!=window->object || !seen.insert(slot).second)throw Error("master loot object/duplicate mismatch");
|
|
auto row=std::find_if(window->source.items.begin(),window->source.items.end(),[&](const auto& item){return item.slot==slot;});
|
|
if(row==window->source.items.end() || row->ui!=TurtleLootSlotUi::master)throw Error("master assignment outside source master slot");
|
|
Bytes request;le(request,window->owner.source,8);le(request,slot,1);le(request,source_target,8);out.push_back({0x2a3,std::move(request)});
|
|
}
|
|
r.end();return out; // Validate all source slots/recipient before cipher advance.
|
|
}
|
|
void NativeLootTranslation::sent(uint32_t op,std::span<const uint8_t> body,const NativeEntityTranslation& entities){
|
|
if(op==0x3e00cc){opening=owner(body,entities);opening_deadline.reset();pending_master.reset();} // Only after genuine request serialization succeeded.
|
|
if(op==0x3e00d3){
|
|
Reader r(body);auto object=read_guid(r);auto slot=r.le(1);r.le(1);r.end();
|
|
for(auto& roll:rolls)if(roll.published && roll.object==object && roll.start.slot==slot && current(roll.owner,entities))roll.voted=true;
|
|
}
|
|
}
|
|
// #99 (2026-11-26): a genuine GameObjUse of the player's own visible bobber (FISHINGNODE 17) is the only request
|
|
// that precedes Source fishing loot. Arm it like a confirmed chest cast; the later same-owner LOOT_RESPONSE (owner =
|
|
// the bobber, or the fishing hole (25) the bobber landed in) publishes the window.
|
|
void NativeLootTranslation::confirmed_fishing_use(uint64_t source,uint16_t realm,uint32_t map,const NativeEntityTranslation& entities){
|
|
if(opening && opening_deadline && std::chrono::steady_clock::now()>=*opening_deadline){opening.reset();opening_deadline.reset();}
|
|
if(opening || window)return;
|
|
auto a=entities.actors.find(source);
|
|
if(a==entities.actors.end() || a->second.kind!=5 || !a->second.sent || !a->second.eligible || !a->second.generation ||
|
|
a->second.gameobject_type!=17 || a->second.presentation_realm!=realm || a->second.presentation_map!=map ||
|
|
a->second.modern!=forever_game_object_guid(source,a->second.entry,realm,map))return;
|
|
Binding bound{source,a->second.generation,a->second.modern,5,realm,map};
|
|
if(current(bound,entities)){opening=bound;opening_deadline=std::chrono::steady_clock::now()+std::chrono::seconds(10);fishing=true;pending_master.reset();}
|
|
}
|
|
// Source successful effect-95 GO precedes its direct LOOT_RESPONSE. This
|
|
// opening is scoped like a cast interaction, not an unsolicited loot reply.
|
|
void NativeLootTranslation::confirmed_skinning_cast(uint64_t source,uint16_t realm,uint32_t map,const NativeEntityTranslation& entities){
|
|
if(opening && opening_deadline && std::chrono::steady_clock::now()>=*opening_deadline){opening.reset();opening_deadline.reset();}
|
|
if(opening || window || !realm || realm>8191 || map>8191 || (source>>48)!=0xf130)return;
|
|
auto a=entities.actors.find(source);
|
|
if(a==entities.actors.end() || a->second.kind!=3 || !a->second.sent || !a->second.eligible ||
|
|
!a->second.generation || a->second.presentation_realm!=realm || a->second.presentation_map!=map ||
|
|
a->second.modern!=forever_creature_guid(source,a->second.entry,realm,map))return;
|
|
// Nonzero scope distinguishes this confirmed creature cast from ordinary
|
|
// explicit creature loot bindings (which retain their existing policy).
|
|
opening=Binding{source,a->second.generation,a->second.modern,3,realm,map};
|
|
opening_deadline=std::chrono::steady_clock::now()+std::chrono::seconds(10);
|
|
fishing=false;pending_master.reset();
|
|
}
|
|
void NativeLootTranslation::confirmed_chest_cast(uint64_t source,uint16_t realm,uint32_t map,const NativeEntityTranslation& entities){
|
|
if(opening && opening_deadline && std::chrono::steady_clock::now()>=*opening_deadline){opening.reset();opening_deadline.reset();}
|
|
if(opening || window)return; // Never replace an already acquired interaction.
|
|
auto a=entities.actors.find(source);
|
|
if(a==entities.actors.end() || a->second.kind!=5 || !a->second.sent || !a->second.eligible ||
|
|
!a->second.generation || a->second.gameobject_type!=3 || a->second.presentation_realm!=realm ||
|
|
a->second.presentation_map!=map || (source>>48)!=0xf110 ||
|
|
a->second.modern!=forever_game_object_guid(source,a->second.entry,realm,map))return;
|
|
Binding bound{source,a->second.generation,a->second.modern,5,realm,map};
|
|
if(current(bound,entities)){opening=bound;opening_deadline=std::chrono::steady_clock::now()+std::chrono::seconds(10);pending_master.reset();}
|
|
}
|
|
std::vector<LegacyPacket> NativeLootTranslation::translate(const LegacyPacket& packet,const Character& self,
|
|
uint16_t realm,uint32_t map,const NativeEntityTranslation& entities,const NativeEntityTranslation::Codec& codec){
|
|
auto& body=packet.body;std::vector<LegacyPacket> output;
|
|
auto emit=[&](uint32_t opcode,Bytes input){output.push_back({opcode,codec(GW_CODEC_LOOT_EVENT,input)});};
|
|
const auto now=std::chrono::steady_clock::now();
|
|
if(opening && opening_deadline && now>=*opening_deadline){opening.reset();opening_deadline.reset();}
|
|
rolls.erase(std::remove_if(rolls.begin(),rolls.end(),[&](const Roll& roll){
|
|
return !current(roll.owner,entities) || now>=roll.deadline+(roll.published?std::chrono::minutes(2):std::chrono::minutes(0));
|
|
}),rolls.end());
|
|
if(packet.opcode==0x7d){auto next=turtle_group_list(body);if(!group || !same_group(*group,next))rolls.clear();group=std::move(next);group_transition=false;return output;}
|
|
if(packet.opcode==0x77 || packet.opcode==0x7c){Reader r(body);r.end();group_transition=true;rolls.clear();return output;}
|
|
if(packet.opcode==0x2a1){
|
|
auto event=turtle_loot_roll_start(body);
|
|
// An actual opening or acquired window supplies a generation-bound owner. Tortoise
|
|
// Group.cpp:751 also starts the roll for every eligible nearby member who never opened
|
|
// the corpse: bind those to the visible source creature (same generation rules).
|
|
auto bound=opening && opening->source==event.owner?opening:
|
|
window && window->owner.source==event.owner?std::optional(window->owner):std::nullopt;
|
|
const bool opener=bound.has_value();
|
|
if(!bound)if(auto a=entities.actors.find(event.owner);a!=entities.actors.end() && a->second.kind==3 && a->second.sent && a->second.generation)
|
|
bound=Binding{event.owner,a->second.generation,a->second.modern};
|
|
if(!bound || !current(*bound,entities) || group_transition || !group || !group->loot ||
|
|
(group->loot->method!=TurtleGroupLootMethod::group && group->loot->method!=TurtleGroupLootMethod::need_before_greed) ||
|
|
!event.countdown || event.countdown>60000 || event.suffix || event.random_property<0)return output;
|
|
auto existing=std::find_if(rolls.begin(),rolls.end(),[&](const Roll& x){return x.owner.source==event.owner && x.start.slot==event.slot;});
|
|
if(existing!=rolls.end())return output; // No deadline extension or overlapping slot generation.
|
|
if(rolls.size()>=16)return output;
|
|
Roll roll;roll.owner=*bound;roll.start=event;roll.map=map;roll.method=uint8_t(group->loot->method);
|
|
roll.deadline=std::chrono::steady_clock::now()+std::chrono::milliseconds(event.countdown);
|
|
rolls.push_back(std::move(roll));
|
|
if(!opener){
|
|
// The original 1.12 GetLootRollItemInfo returns a constant 1.0
|
|
// for its third result, independent of the template/stack limit.
|
|
// This is the single roll-slot presentation, NOT loot quantity.
|
|
auto& pending=rolls.back();
|
|
TurtleLootItem item;item.slot=uint8_t(event.slot);item.item=event.item;item.count=1;
|
|
item.random_property=event.random_property;item.ui=TurtleLootSlotUi::roll_ongoing;
|
|
auto sibling=std::find_if(rolls.begin(),rolls.end()-1,[&](const Roll& r){
|
|
return r.published && r.owner.source==bound->source && r.owner.generation==bound->generation && current(r.owner,entities);});
|
|
pending.object=sibling!=rolls.end()-1?sibling->object:object_id(++serial,realm,map);pending.item=item;
|
|
auto ms=std::chrono::duration_cast<std::chrono::milliseconds>(pending.deadline-std::chrono::steady_clock::now()).count();
|
|
if(ms>0){emit(0x4700c7,roll_input(6,pending,0,realm,int32_t(ms),0));pending.published=true;}
|
|
return output;
|
|
}
|
|
if(window && window->owner.source==event.owner && window->owner.generation==bound->generation){
|
|
auto& pending=rolls.back();
|
|
auto row=std::find_if(window->source.items.begin(),window->source.items.end(),[&](const auto& item){
|
|
return item.slot==event.slot && item.item==event.item && item.random_property==event.random_property &&
|
|
item.ui==TurtleLootSlotUi::roll_ongoing && item.count;
|
|
});
|
|
if(row!=window->source.items.end()){
|
|
pending.object=window->object;pending.item=*row;pending.quantity_known=true;
|
|
auto ms=std::chrono::duration_cast<std::chrono::milliseconds>(pending.deadline-std::chrono::steady_clock::now()).count();
|
|
if(ms>0){emit(0x4700c7,roll_input(6,pending,0,realm,int32_t(ms),0));pending.published=true;}
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
if(packet.opcode==0x2a2 || packet.opcode==0x29f || packet.opcode==0x29e){
|
|
auto event=packet.opcode==0x2a2?turtle_loot_roll_update(body):
|
|
packet.opcode==0x29f?turtle_loot_roll_won(body):turtle_loot_roll_all_passed(body);
|
|
auto it=std::find_if(rolls.begin(),rolls.end(),[&](const Roll& roll){return same_roll(roll.start,event) && current(roll.owner,entities);});
|
|
if(it==rolls.end())return output;
|
|
if(packet.opcode==0x2a2){
|
|
auto presentation=turtle_loot_roll_presentation(event.number,event.type);
|
|
if(!presentation || !group || group_transition || !member(event.player,self.guid,*group))return output;
|
|
if(event.player==self.guid && ((event.number==128 && (event.type==128 || event.type==2)) || (event.number==0 && event.type==0)))it->voted=true;
|
|
// Native LootRoll::SendLootRollWon sends numeric broadcasts with
|
|
// ALLOW_LOOT for the winning recipient, ROLL_ONGOING for others.
|
|
// Source supplies winner only in the following terminal packet.
|
|
if(event.number>=1 && event.number<=100){
|
|
if(it->results.size()<40 && std::none_of(it->results.begin(),it->results.end(),[&](const auto& prior){return prior.player==event.player;}))it->results.push_back(event);
|
|
return output;
|
|
}
|
|
if(!it->published){if(it->updates.size()<128)it->updates.push_back(event);return output;}
|
|
emit(0x4700c8,roll_input(7,*it,event.player,realm,presentation->roll,presentation->vote));return output;
|
|
}
|
|
if(it->published){
|
|
if(packet.opcode==0x29f){
|
|
if(!group || !member(event.player,self.guid,*group) || event.number<1 || event.number>100 || (event.type!=1 && event.type!=2)){rolls.erase(it);return output;}
|
|
if(event.player==self.guid && it->quantity_known && it->item && received_award_intents.size()<16)received_award_intents.push_back(*it->item);
|
|
for(const auto& result:it->results)if(member(result.player,self.guid,*group))
|
|
emit(0x4700c8,roll_input(7,*it,result.player,realm,result.number,result.type,event.player==self.guid?0:1));
|
|
auto input=roll_input(8,*it,event.player,realm,event.number,event.type);le(input,event.player==self.guid?1:0,1);emit(0x4700cc,std::move(input));
|
|
// Encounter0 supplies no native loot-history entry on70334.
|
|
// Keep the original packets; expose only genuine numeric values
|
|
// and the genuine terminal through the accepted local chat route.
|
|
bool winner_number=false;
|
|
for(const auto& result:it->results)if(member(result.player,self.guid,*group)){
|
|
if(result.player==event.player){winner_number=true;queue_roll_chat(roll_chat_notices,'N',*it,event,self,*group);}
|
|
else queue_roll_chat(roll_chat_notices,'N',*it,result,self,*group);
|
|
}
|
|
if(!winner_number)queue_roll_chat(roll_chat_notices,'N',*it,event,self,*group);
|
|
queue_roll_chat(roll_chat_notices,'W',*it,event,self,*group);
|
|
}else emit(0x4700cb,roll_input(9,*it,0,realm,0,0));
|
|
}
|
|
if(packet.opcode==0x29e && window && window->owner.source==it->owner.source &&
|
|
window->owner.generation==it->owner.generation && (!it->published || window->object==it->object)){
|
|
auto row=std::find_if(window->source.items.begin(),window->source.items.end(),[&](const auto& item){
|
|
return item.slot==event.slot && item.item==event.item && item.random_property==event.random_property && item.ui==TurtleLootSlotUi::roll_ongoing;
|
|
});
|
|
if(row!=window->source.items.end())row->ui=TurtleLootSlotUi::allow;
|
|
}
|
|
rolls.erase(it);return output;
|
|
}
|
|
if(packet.opcode==0x2a4){
|
|
Reader r(body);auto count=r.le(1);if(count>40)throw Error("source master candidate bound");
|
|
std::vector<uint64_t> candidates;std::set<uint64_t> seen;
|
|
for(unsigned i=0;i<count;++i){auto id=r.le(8);if(!id || id>UINT32_MAX || !seen.insert(id).second)throw Error("source master candidate identity");candidates.push_back(id);}r.end();
|
|
// Source Group::MasterLoot sends this inside Player::SendLoot before
|
|
// LOOT_RESPONSE. No owner is transmitted; only the outstanding actual
|
|
// LOOT request can supply its current generation-bound owner.
|
|
if(opening && current(*opening,entities))pending_master=PendingMaster{*opening,body,std::move(candidates)};
|
|
return output;
|
|
}
|
|
if(packet.opcode==0x163){(void)turtle_loot_money_notify(body);++deferred_money_notifications;return output;}
|
|
if(packet.opcode==0x160){
|
|
auto event=turtle_loot_response(body);
|
|
// A bobber that landed in a school is looted through the fishing hole: rebind the armed bobber opening to that
|
|
// visible, current hole (type 25) of the same realm/map; any other owner change stays unpublished.
|
|
if(opening && fishing && opening->source!=event.owner){
|
|
auto h=entities.actors.find(event.owner);
|
|
if(h!=entities.actors.end() && h->second.kind==5 && h->second.gameobject_type==25 && h->second.sent && h->second.eligible &&
|
|
h->second.generation && h->second.presentation_realm==opening->realm && h->second.presentation_map==opening->map)
|
|
opening=Binding{event.owner,h->second.generation,h->second.modern,5,opening->realm,opening->map};
|
|
}
|
|
if(!opening || opening->source!=event.owner || !current(*opening,entities))return output;
|
|
auto bound=*opening;
|
|
if(bound.kind==3 && bound.realm && (bound.realm!=realm || bound.map!=map || (!event.error && event.type!=2)))return output;
|
|
const bool fish=bound.kind==5 && fishing;Bytes input{uint8_t(bound.kind==5?(fish?11:10):0)};fishing=false;le(input,bound.source,8);append(input,bound.modern);
|
|
if(event.error){
|
|
if(bound.kind==5){opening.reset();opening_deadline.reset();pending_master.reset();return output;}
|
|
append(input,Guid128{});append(input,Bytes{0,2});append(input,body);emit(0x4700bf,std::move(input));opening.reset();opening_deadline.reset();pending_master.reset();return output;}
|
|
// Source CharacterHandler sends current GROUP_LIST before completing
|
|
// login and processing subsequent world requests. On this genuine loot
|
|
// response, no group announcement in a fresh selected session retains
|
|
// the protocol's ungrouped default. Never call that a received solo
|
|
// snapshot. A received group-of-one without policy remains unknown.
|
|
bool policy_known=!group_transition && (!group || group->explicit_solo || group->loot.has_value());
|
|
// Positive Source random properties are vanilla RandomProperties IDs; the
|
|
// codec maps them to the generated native ItemBonusList (or presents
|
|
// the base item when no verified list exists). Negative IDs are
|
|
// RandomSuffix factors with no Source producer reviewed: still held.
|
|
// #99: Player::SendLoot writes LOOT_FISHING (3) for bobber and fishing-hole loot; chests stay type 2.
|
|
bool content_known=(bound.kind==3?(bound.realm?event.type==2:event.type==1):((fish?event.type==3:event.type==2) && (!group || group->explicit_solo))) &&
|
|
std::none_of(event.items.begin(),event.items.end(),[](const auto& row){return row.random_property<0;});
|
|
if(!policy_known || !content_known){
|
|
// Gracefully release a source interaction we cannot faithfully
|
|
// present. No fake server error, hidden item grant or connection
|
|
// crash. Loot remains on the authoritative creature.
|
|
if(requests.size()>=16)throw Error("deferred loot release bound");
|
|
requests.push_back({0x15f,turtle_loot_release(bound.source)});++deferred_content;return output;
|
|
}
|
|
// Reopen must reuse an active roll's presentation identity for this
|
|
// exact Source generation; the roll can outlive the loot window.
|
|
auto active=std::find_if(rolls.begin(),rolls.end(),[&](const Roll& roll){
|
|
return roll.published && roll.owner.source==bound.source && roll.owner.generation==bound.generation && current(roll.owner,entities);
|
|
});
|
|
auto id=active==rolls.end()?object_id(serial+1,realm,map):active->object;append(input,id);
|
|
le(input,group && group->loot?uint8_t(group->loot->method):0,1);
|
|
le(input,group && group->loot?group->loot->threshold:2,1);append(input,body);
|
|
emit(0x4700bf,std::move(input));if(active==rolls.end())++serial;window=Window{bound,id,std::move(event),{}};
|
|
for(auto& roll:rolls){
|
|
if(roll.owner.source!=bound.source || roll.owner.generation!=bound.generation || !current(roll.owner,entities))continue;
|
|
auto now=std::chrono::steady_clock::now();if(now>=roll.deadline)continue;
|
|
auto row=std::find_if(window->source.items.begin(),window->source.items.end(),[&](const auto& item){
|
|
return item.slot==roll.start.slot && item.item==roll.start.item && item.random_property==roll.start.random_property &&
|
|
item.ui==TurtleLootSlotUi::roll_ongoing && item.count;
|
|
});
|
|
if(row==window->source.items.end())continue;
|
|
roll.object=id;roll.item=*row;roll.quantity_known=true;
|
|
if(roll.published)continue; // Upgrade quantity without repeating the roll popup.
|
|
auto remaining=std::chrono::duration_cast<std::chrono::milliseconds>(roll.deadline-now).count();
|
|
if(remaining<=0)continue;
|
|
emit(0x4700c7,roll_input(6,roll,0,realm,int32_t(remaining),0));roll.published=true;
|
|
for(const auto& update:roll.updates){
|
|
auto value=turtle_loot_roll_presentation(update.number,update.type);
|
|
if(value && group && member(update.player,self.guid,*group))emit(0x4700c8,roll_input(7,roll,update.player,realm,value->roll,value->vote));
|
|
}
|
|
roll.updates.clear();
|
|
}
|
|
if(pending_master && pending_master->owner.source==bound.source && pending_master->owner.generation==bound.generation && pending_master->owner.modern==bound.modern &&
|
|
group && group->loot && group->loot->method==TurtleGroupLootMethod::master){
|
|
Bytes master{5};le(master,realm,2);append(master,id);append(master,pending_master->body);emit(0x4700c9,std::move(master));
|
|
window->master_candidates=std::move(pending_master->candidates);
|
|
}
|
|
opening.reset();opening_deadline.reset();pending_master.reset();return output;
|
|
}
|
|
if(packet.opcode==0x161){
|
|
auto release=turtle_loot_release_response(body);
|
|
auto bound=window && window->owner.source==release.owner?std::optional(window->owner):
|
|
opening && opening->source==release.owner?opening:std::nullopt;
|
|
if(!bound)return output;
|
|
Bytes input{3};le(input,bound->source,8);append(input,bound->modern);append(input,forever_player_guid(self.guid,realm));append(input,body);
|
|
emit(0x4700c5,std::move(input));
|
|
pending_master.reset();
|
|
if(window && window->owner.source==release.owner)window.reset();
|
|
else if(opening && opening->source==release.owner){opening.reset();opening_deadline.reset();}
|
|
return output;
|
|
}
|
|
if(packet.opcode==0x162){
|
|
auto slot=turtle_loot_removed(body);if(!window)return output;
|
|
auto& rows=window->source.items;auto row=std::find_if(rows.begin(),rows.end(),[&](const auto& v){return v.slot==slot;});
|
|
if(row==rows.end())return output;
|
|
Bytes input{1};append(input,window->owner.modern);append(input,window->object);append(input,body);
|
|
emit(0x4700c0,std::move(input));
|
|
rolls.erase(std::remove_if(rolls.begin(),rolls.end(),[&](const Roll& roll){return roll.owner.source==window->owner.source && roll.owner.generation==window->owner.generation && roll.start.slot==slot;}),rolls.end());
|
|
rows.erase(row);return output;
|
|
}
|
|
if(packet.opcode==0x165){
|
|
turtle_loot_clear_money(body);if(!window)return output;
|
|
Bytes input{2};append(input,window->object);emit(0x4700c1,std::move(input));window->source.money=0;return output;
|
|
}
|
|
throw Error("unsupported source loot event");
|
|
}
|