diff --git a/common/map/region_pager.cpp b/common/map/region_pager.cpp index d0a9249..39558c0 100644 --- a/common/map/region_pager.cpp +++ b/common/map/region_pager.cpp @@ -38,25 +38,25 @@ RegionPagerBase::~RegionPagerBase() { } int RegionPagerBase::SetTile(int x, int y, int z, int v) { - Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y)); + Region* ptr = GetRegion(x, y); return ptr->SetTile(x - ptr->GetX(), y = ptr->GetY(), z, v); } int RegionPagerBase::GetTile(int x, int y, int z) { - Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y)); + Region* ptr = GetRegion(x, y); return ptr->GetTile(x - ptr->GetX(), y = ptr->GetY(), z); } Region* RegionPagerBase::GetRegion(int x, int y) { - //TODO: clean this up + //snap the coords + x = snapToBase(regionWidth, x); + y = snapToBase(regionHeight, y); + Region* ptr = nullptr; //find the loaded region auto iter = std::find_if(regionList.begin(), regionList.end(), [x,y](Region& it) { - if (it.GetX() == x && it.GetY() == y) - return true; - else - return false; + return it.GetX() == x && it.GetY() == y; }); if (iter != regionList.end()) { //ugly hack ptr = &(*iter); diff --git a/common/map/region_pager.hpp b/common/map/region_pager.hpp index 3130922..9321011 100644 --- a/common/map/region_pager.hpp +++ b/common/map/region_pager.hpp @@ -23,7 +23,9 @@ #define REGIONPAGER_HPP_ #include "region.hpp" +#include "utility.hpp" +#include #include class RegionPagerBase { @@ -59,26 +61,73 @@ protected: template class RegionPager : public RegionPagerBase { public: + RegionPager() = delete; + RegionPager(int w, int h, int d): + RegionPagerBase(w, h, d) + { + //EMPTY + } + ~RegionPager() = default; + Region* LoadRegion(int x, int y) { - //TODO - //if there's no region to load, return null + //snap the coords + x = snapToBase(regionWidth, x); + y = snapToBase(regionHeight, y); + + //load the region if possible + Region* region = nullptr; + format.Load(®ion, x, y); + if (region) { + regionList.push_back(std::move(*region)); + return ®ionList.back(); + } + return nullptr; } Region* SaveRegion(int x, int y) { - //TODO - //save the region using the functor + //snap the coords + x = snapToBase(regionWidth, x); + y = snapToBase(regionHeight, y); + + //find the specified region + auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){ + return it.GetX() == x && it.GetY() == y; + }); + + //save the region if it's loaded + if (iter != regionList.end()) { + format.Save(&(*iter, x, y)); + return &(*iter); + } + return nullptr; } Region* CreateRegion(int x, int y) { - //TODO - //create the region - //DON'T call this on a non-zero region, - //or if there's a region saved to the disk + //snap the coords + x = snapToBase(regionWidth, x); + y = snapToBase(regionHeight, y); + + //create and push the object + Region* region = nullptr; + generator.Create(®ion); + regionList.push_back(std::move(*region)); + return regionList.back(); } void UnloadRegion(int x, int y) { - //TODO - //free the region, possibly saving it + //snap the coords + x = snapToBase(regionWidth, x); + y = snapToBase(regionHeight, y); + + //find the specified region + auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){ + return it.GetX() == x && it.GetY() == y; + }); + + //pass it to the generator for unloading + if (iter != regionList.end()) { + generator.Unload(&(*iter)); + } } //accessors