Skip to content

Commit

Permalink
Load POLITIC.PAK
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Jul 25, 2015
1 parent ff5bcaf commit 00f0613
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
72 changes: 37 additions & 35 deletions src/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,39 +337,8 @@ void World::initialize(osgViewer::Viewer *viewer)
mRegions[regnum] = std::move(region);
}

{
stream = VFS::Manager::get().open("CLIMATE.PAK");
if(!stream) throw std::runtime_error("Failed to open CLIMATE.PAK");

size_t rownum = 0;
std::map<size_t,size_t> offsets_rows;
offsets_rows[VFS::read_le32(*stream)] = rownum++;
while(stream->tellg() < offsets_rows.begin()->first)
offsets_rows[VFS::read_le32(*stream)] = rownum++;

auto iter = offsets_rows.begin();
while(iter != offsets_rows.end())
{
auto next = std::next(iter);

PakArray pak;
while((next != offsets_rows.end() && stream->tellg() < next->first) ||
(next == offsets_rows.end() && stream->peek() != std::istream::traits_type::eof()))
{
uint16_t count = VFS::read_le16(*stream);
uint8_t val = stream->get();
pak.push_back(std::make_pair(count, val));
}

if(iter->second >= mClimates.size())
mClimates.resize(iter->second+1);
mClimates[iter->second] = std::move(pak);

iter = next;
}

stream = nullptr;
}
loadPakList("CLIMATE.PAK", mClimates);
loadPakList("POLITIC.PAK", mPolitics);

mViewer = viewer;

Expand All @@ -387,7 +356,40 @@ void World::deinitialize()
}


uint8_t World::getClimateValue(size_t x, size_t y) const
void World::loadPakList(std::string&& fname, std::vector<PakArray> &paklist)
{
VFS::IStreamPtr stream = VFS::Manager::get().open(fname.c_str());
if(!stream) throw std::runtime_error("Failed to open "+fname);

size_t rownum = 0;
std::map<size_t,size_t> offsets_rows;
offsets_rows[VFS::read_le32(*stream)] = rownum++;
while(stream->tellg() < offsets_rows.begin()->first)
offsets_rows[VFS::read_le32(*stream)] = rownum++;

auto iter = offsets_rows.begin();
while(iter != offsets_rows.end())
{
auto next = std::next(iter);

PakArray pak;
while((next != offsets_rows.end() && stream->tellg() < next->first) ||
(next == offsets_rows.end() && stream->peek() != std::istream::traits_type::eof()))
{
uint16_t count = VFS::read_le16(*stream);
uint8_t val = stream->get();
pak.push_back(std::make_pair(count, val));
}

if(iter->second >= paklist.size())
paklist.resize(iter->second+1);
paklist[iter->second] = std::move(pak);

iter = next;
}
}

uint8_t World::getPakListValue(const std::vector<PakArray> &paklist, size_t x, size_t y)
{
x = x/32768 + 2;

Expand All @@ -396,7 +398,7 @@ uint8_t World::getClimateValue(size_t x, size_t y) const
else y = 499 - y;

uint8_t value = 0;
const PakArray &pak = mClimates[std::min(y, mClimates.size()-1)];
const PakArray &pak = paklist[std::min(y, paklist.size()-1)];
for(const auto &entry : pak)
{
value = entry.second;
Expand Down
8 changes: 7 additions & 1 deletion src/world/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class World : public WorldIface {

std::vector<MapRegion> mRegions;
std::vector<PakArray> mClimates;
std::vector<PakArray> mPolitics;

const MapRegion *mCurrentRegion;
const ExteriorLocation *mCurrentExterior;
Expand All @@ -78,7 +79,12 @@ class World : public WorldIface {
World();
~World();

uint8_t getClimateValue(size_t x, size_t y) const;
static void loadPakList(std::string&& fname, std::vector<PakArray> &paklist);

static uint8_t getPakListValue(const std::vector<PakArray> &paklist, size_t x, size_t y);

uint8_t getClimateValue(size_t x, size_t y) const { return getPakListValue(mClimates, x, y); }
uint8_t getPoliticValue(size_t x, size_t y) const { return getPakListValue(mPolitics, x, y); }

public:
static World sWorld;
Expand Down

0 comments on commit 00f0613

Please sign in to comment.