127 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
83 lines
4.5 KiB
C++
83 lines
4.5 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#pragma once
|
|
#include "gateway/quest_packets.hpp"
|
|
#include "gateway/quest_poi_response.hpp"
|
|
#include <algorithm>
|
|
#include <bit>
|
|
#include <optional>
|
|
|
|
namespace gateway {
|
|
// QPOI v1: terrain-derived Source geometry. The 60141 native pilot accepted
|
|
// opcode 0x67001d and this response layout; sniffed Forever rows establish
|
|
// StorageIndex, ender -1, giver 32, and per-objective BlobIndex conventions.
|
|
class QuestPoiDataset {
|
|
std::map<uint32_t, QuestPoiRecord> records_;
|
|
public:
|
|
explicit QuestPoiDataset(std::span<const uint8_t> bytes) {
|
|
if(bytes.size()>16*1024*1024)throw Error("Quest POI dataset byte bound");
|
|
Reader h(bytes);
|
|
if(h.le(4)!=0x494f5051 || h.le(4)!=1)throw Error("Quest POI dataset signature/version");
|
|
const auto count=uint32_t(h.le(4));
|
|
if(!count || count>100000 || 12ull+8ull*count>bytes.size())throw Error("Quest POI dataset index bound");
|
|
std::vector<std::pair<uint32_t,uint32_t>> index;
|
|
for(uint32_t i=0;i<count;++i){
|
|
const auto id=uint32_t(h.le(4)),offset=uint32_t(h.le(4));
|
|
if(!id || id>0x1fffffff || (i && (id<=index.back().first || offset<=index.back().second)) || (!i && offset))
|
|
throw Error("Quest POI dataset index order");
|
|
index.emplace_back(id,offset);
|
|
}
|
|
const auto data=bytes.subspan(12+8*count);
|
|
for(size_t i=0;i<index.size();++i){
|
|
const auto [id,offset]=index[i];
|
|
const auto end=i+1<index.size()?size_t(index[i+1].second):data.size();
|
|
if(offset>=end || end>data.size())throw Error("Quest POI dataset record bounds");
|
|
Reader r(data.subspan(offset,end-offset));QuestPoiRecord record;record.quest=int32_t(id);
|
|
const auto blobs=r.le(2);if(!blobs || blobs>128)throw Error("Quest POI dataset blob bound");
|
|
for(size_t j=0;j<blobs;++j){
|
|
QuestPoiBlob b;
|
|
const auto objective=std::bit_cast<int8_t>(uint8_t(r.le(1)));
|
|
const auto blob=r.le(1),flags=r.le(1),points=r.le(1);
|
|
const auto object=std::bit_cast<int32_t>(uint32_t(r.le(4)));
|
|
const auto map=r.le(2),uimap=r.le(2);
|
|
if((objective < -2 || (objective>7 && objective!=32)) || !points || points>12 || !uimap ||
|
|
(flags!=0 && flags!=1 && flags!=7) || object<0 ||
|
|
((objective==-1 || objective==32) && object!=0))throw Error("Quest POI dataset blob fields");
|
|
b.fields={int32_t(blob),objective,0,object,int32_t(map),int32_t(uimap),0,int32_t(flags),0,0,0,0};
|
|
for(size_t k=0;k<points;++k){
|
|
auto x=std::bit_cast<int16_t>(uint16_t(r.le(2)));
|
|
auto y=std::bit_cast<int16_t>(uint16_t(r.le(2)));
|
|
auto z=std::bit_cast<int16_t>(uint16_t(r.le(2)));
|
|
b.points.push_back({x,y,z});
|
|
}
|
|
record.blobs.push_back(std::move(b));
|
|
}
|
|
r.end();records_.emplace(id,std::move(record));
|
|
}
|
|
}
|
|
size_t size() const noexcept{return records_.size();}
|
|
bool contains(uint32_t id) const{return records_.contains(id);}
|
|
// Definitions are genuine Source replies already projected by QuestInfo.
|
|
// A missing/changed slot or object must not acquire stale dataset geometry.
|
|
std::optional<QuestPoiRecord> project(uint32_t id,const QuestDefinition& definition) const{
|
|
auto found=records_.find(id);if(found==records_.end() || definition.id!=id)return std::nullopt;
|
|
QuestPoiRecord out;out.quest=int32_t(id);
|
|
for(auto blob:found->second.blobs){
|
|
const auto storage=blob.fields[1];
|
|
if(storage>=0 && storage<=7){
|
|
auto objective=std::find_if(definition.objectives.begin(),definition.objectives.end(),[&](const auto& o){
|
|
return o.storage==storage && o.object==uint32_t(blob.fields[3]) && o.count &&
|
|
(storage>=4?o.type==1:(o.type==0 || o.type==2));
|
|
});
|
|
if(objective==definition.objectives.end())continue;
|
|
blob.fields[2]=int32_t(quest_objective_presentation_id(id,size_t(objective-definition.objectives.begin())));
|
|
}else if(storage==-2){
|
|
if(std::any_of(definition.objectives.begin(),definition.objectives.end(),[](const auto& o){return o.storage==0;}))continue;
|
|
blob.fields[1]=0; // Sniffed exploration quest870, no manufactured objective identity.
|
|
}
|
|
out.blobs.push_back(std::move(blob));
|
|
}
|
|
return out;
|
|
}
|
|
};
|
|
}
|