simpli map load/save

This commit is contained in:
Eduardo Bart 2012-01-30 01:11:05 -02:00
parent 9db7bd2602
commit 7d8d214324
9 changed files with 66 additions and 11 deletions

View File

@ -123,7 +123,7 @@ bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, u
return true;
}
bool ResourceManager::saveFile(const std::string& fileName, std::istream& in)
bool ResourceManager::saveFile(const std::string& fileName, std::iostream& in)
{
std::streampos oldPos = in.tellg();
in.seekg(0, std::ios::end);

View File

@ -44,7 +44,7 @@ public:
bool saveFile(const std::string& fileName, const uchar* data, uint size);
bool saveFile(const std::string& fileName, const std::string& data);
bool saveFile(const std::string& fileName, std::istream& in);
bool saveFile(const std::string& fileName, std::iostream& in);
bool deleteFile(const std::string& fileName);

View File

@ -94,7 +94,7 @@ void Game::processLogout()
m_protocolGame = nullptr;
}
g_map.clean();
g_map.save();
}
void Game::processDeath()

View File

@ -30,6 +30,7 @@
#include <framework/core/eventdispatcher.h>
#include "mapview.h"
#include <framework/core/resourcemanager.h>
Map g_map;
@ -51,6 +52,51 @@ void Map::notificateTileUpdateToMapViews(const Position& pos)
mapView->onTileUpdate(pos);
}
void Map::load()
{
if(!g_resources.fileExists("/map.otcmap"))
return;
std::stringstream in;
g_resources.loadFile("/map.otcmap", in);
while(!in.eof()) {
Position pos;
in.read((char*)&pos, sizeof(pos));
uint16 id;
in.read((char*)&id, sizeof(id));
while(id != 0xFFFF) {
addThing(Item::create(id), pos);
in.read((char*)&id, sizeof(id));
}
}
}
void Map::save()
{
std::stringstream out;
for(auto& pair : m_tiles) {
Position pos = pair.first;
TilePtr tile = pair.second;
if(!tile)
continue;
out.write((char*)&pos, sizeof(pos));
uint16 id;
for(const ThingPtr& thing : tile->getThings()) {
if(ItemPtr item = thing->asItem()) {
id = item->getId();
out.write((char*)&id, sizeof(id));
}
}
id = 0xFFFF;
out.write((char*)&id, sizeof(id));
}
g_resources.saveFile("/map.otcmap", out);
}
void Map::clean()
{
m_tiles.clear();

View File

@ -34,6 +34,8 @@ public:
void removeMapView(const MapViewPtr& mapView);
void notificateTileUpdateToMapViews(const Position& pos);
void load();
void save();
void clean();
// thing related

View File

@ -53,6 +53,7 @@ void MapView::draw(const Rect& rect)
bool updated = updateVisibleTilesCache();
float scaleFactor = m_tileSize/(float)Otc::TILE_PIXELS;
Position cameraPosition = getCameraPosition();
if(updated || m_animated) {
m_framebuffer->bind();
@ -76,6 +77,10 @@ void MapView::draw(const Rect& rect)
g_painter.releaseCustomProgram();
// this could happen if the player position is not known yet
if(!cameraPosition.isValid())
return;
float horizontalStretchFactor = rect.width() / (float)(m_visibleDimension.width() * m_tileSize);
float verticalStretchFactor = rect.height() / (float)(m_visibleDimension.height() * m_tileSize);
Size tileStretchedSize = Size(m_tileSize * horizontalStretchFactor, m_tileSize * verticalStretchFactor);
@ -139,6 +144,11 @@ bool MapView::updateVisibleTilesCache()
// clear current visible tiles cache
m_cachedVisibleTiles.clear();
m_cachedFloorVisibleCreatures.clear();
// there is no tile to render on invalid positions
if(!cameraPosition.isValid())
return true;
// cache visible tiles in draw order
// draw from last floor (the lower) to first floor (the higher)
@ -238,8 +248,6 @@ void MapView::setVisibleDimension(const Size& visibleDimension)
m_virtualCenterOffset = (m_drawDimension/2 - Size(1,1)).toPoint();
recalculateTileSize();
dump << m_framebuffer->getSize();
dump << visibleDimension * m_tileSize;
requestVisibleTilesCacheUpdate();
}

View File

@ -45,10 +45,6 @@ public:
int getThingStackpos(const ThingPtr& thing);
ThingPtr getTopThing();
void addWalkingCreature(const CreaturePtr& creature);
void removeWalkingCreature(const CreaturePtr& creature);
ThingPtr getTopLookThing();
ThingPtr getTopUseThing();
CreaturePtr getTopCreature();
@ -58,6 +54,7 @@ public:
const Position& getPosition() { return m_position; }
int getDrawElevation() { return m_drawElevation; }
std::vector<CreaturePtr> getCreatures();
const std::vector<ThingPtr>& getThings() { return m_things; }
ItemPtr getGround();
int getGroundSpeed();
bool isWalkable();
@ -73,7 +70,6 @@ public:
TilePtr asTile() { return std::static_pointer_cast<Tile>(shared_from_this()); }
private:
std::vector<CreaturePtr> m_walkingCreatures;
std::vector<EffectPtr> m_effects; // leave this outside m_things because it has no stackpos.
std::vector<ThingPtr> m_things;
Position m_position;

View File

@ -24,6 +24,7 @@
#include <framework/core/modulemanager.h>
#include "core/game.h"
#include <framework/core/resourcemanager.h>
#include "core/map.h"
OTClient::OTClient(const std::string& appName) : Application(appName)
{
@ -40,6 +41,8 @@ void OTClient::init(const std::vector<std::string>& args)
g_modules.ensureModuleLoaded("client");
g_modules.autoLoadModules(1000);
g_map.load();
// load otclientrc.lua
if(g_resources.fileExists("/otclientrc.lua")) {
try {

View File

@ -98,7 +98,7 @@ public:
return Otc::InvalidDirection;
}
bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x <= 65535 && y <= 65535 && z <= 255; }
bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x <= 65535 && y <= 65535 && z <= 15; }
Position operator+(const Position& other) const { return Position(x + other.x, y + other.y, z + other.z); }
Position& operator+=(const Position& other) { x+=other.x; y+=other.y; z +=other.z; return *this; }