forked from mirrors/kolyah35-minecraft-pe-0.6.1
Infinite world, LuaJIT mod API, WorldEdit port, skins and music
- Infinite world: streamed chunk cache, multi-region storage (old chunks.dat still loads), per-chunk entity/tile entity persistence, client chunk streaming, protocol version 10, view-distance server property - LuaJIT mod host (mods/, lua/prelude.lua) with events, commands, world and player API; WorldEdit command set ported to it (mods/worldedit) - /skin command, SetSkinPacket and bundled skins - OGG music player (data/music is ignored, tools/convert-music.ps1) - package-windows.ps1 builds client and dedicated server for Windows Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
db9f6c9f32
commit
110cb2a896
68 changed files with 5064 additions and 844 deletions
|
|
@ -878,10 +878,10 @@ bool Entity::load( CompoundTag* tag )
|
|||
float yy = pos->getFloat(1);
|
||||
float zz = pos->getFloat(2);
|
||||
|
||||
// Add a small padding if standing next to the world edges
|
||||
// The world is infinite, only keep the position inside the coordinate system
|
||||
const float padding = bbWidth * 0.5f + 0.001f;
|
||||
xx = Mth::clamp(xx, padding, (float)LevelConstants::LEVEL_WIDTH - padding);
|
||||
zz = Mth::clamp(zz, padding, (float)LevelConstants::LEVEL_DEPTH - padding);
|
||||
xx = Mth::clamp(xx, -(float)LevelConstants::LEVEL_WIDTH + padding, (float)LevelConstants::LEVEL_WIDTH - padding);
|
||||
zz = Mth::clamp(zz, -(float)LevelConstants::LEVEL_DEPTH + padding, (float)LevelConstants::LEVEL_DEPTH - padding);
|
||||
|
||||
xo = xOld = x = xx;
|
||||
yo = yOld = y = yy;
|
||||
|
|
|
|||
|
|
@ -164,6 +164,7 @@ public:
|
|||
float oBob, bob;
|
||||
|
||||
std::string name;
|
||||
std::string skinName; // file name (without .png) in data/images/skins, empty = default
|
||||
int dimension;
|
||||
|
||||
Abilities abilities;
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ public:
|
|||
}
|
||||
|
||||
bool operator<(const ChunkPos& rhs) const {
|
||||
return hashCode() < rhs.hashCode();
|
||||
return x < rhs.x || (x == rhs.x && z < rhs.z);
|
||||
}
|
||||
|
||||
float distanceToSqr(const Entity* e) const {
|
||||
|
|
|
|||
|
|
@ -281,8 +281,7 @@ void Level::tickTiles() {
|
|||
for (int i = 0; i < pollChunkOffsetsSize; i += 2) {
|
||||
const int xp = xx + pollChunkOffsets[i];
|
||||
const int zp = zz + pollChunkOffsets[i+1];
|
||||
if (xp >= 0 && xp < LevelConstants::CHUNK_CACHE_WIDTH &&
|
||||
zp >= 0 && zp < LevelConstants::CHUNK_CACHE_WIDTH)
|
||||
if (hasChunk(xp, zp))
|
||||
_chunksToPoll.insert(ChunkPos(xp, zp));
|
||||
}
|
||||
}
|
||||
|
|
@ -431,17 +430,13 @@ bool Level::findPath(Path* path, Entity* from, int xBest, int yBest, int zBest,
|
|||
/*protected*/
|
||||
void Level::setInitialSpawn() {
|
||||
isFindingSpawn = true;
|
||||
int xSpawn = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_WIDTH / 2; // (Level.MAX_LEVEL_SIZE - 100) * 0;
|
||||
// The world is infinite: start looking for dry land around the origin.
|
||||
int xSpawn = 0;
|
||||
int ySpawn = 64;
|
||||
int zSpawn = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_DEPTH / 2; // (Level.MAX_LEVEL_SIZE - 100) * 0;
|
||||
int zSpawn = 0;
|
||||
while (!dimension->isValidSpawn(xSpawn, zSpawn)) {
|
||||
xSpawn += random.nextInt(32) - random.nextInt(32);
|
||||
zSpawn += random.nextInt(32) - random.nextInt(32);
|
||||
|
||||
if (xSpawn < 4) xSpawn += 32;
|
||||
if (xSpawn >= LevelConstants::LEVEL_WIDTH-4) xSpawn -= 32;
|
||||
if (zSpawn < 4) zSpawn += 32;
|
||||
if (zSpawn >= LevelConstants::LEVEL_DEPTH-4) zSpawn -= 32;
|
||||
}
|
||||
levelData.setSpawn(xSpawn, ySpawn, zSpawn);
|
||||
isFindingSpawn = false;
|
||||
|
|
@ -457,11 +452,6 @@ void Level::validateSpawn() {
|
|||
while (getTopTile(xSpawn, zSpawn) == 0 || getTopTile(xSpawn, zSpawn) == Tile::invisible_bedrock->id) {
|
||||
xSpawn += random.nextInt(8) - random.nextInt(8);
|
||||
zSpawn += random.nextInt(8) - random.nextInt(8);
|
||||
|
||||
if (xSpawn < 4) xSpawn += 8;
|
||||
if (xSpawn >= LevelConstants::LEVEL_WIDTH-4) xSpawn -= 8;
|
||||
if (zSpawn < 4) zSpawn += 8;
|
||||
if (zSpawn >= LevelConstants::LEVEL_DEPTH-4) zSpawn -= 8;
|
||||
}
|
||||
levelData.setXSpawn(xSpawn);
|
||||
levelData.setZSpawn(zSpawn);
|
||||
|
|
@ -947,10 +937,8 @@ HitResult Level::clip(const Vec3& A, const Vec3& b, bool liquid /*= false*/, boo
|
|||
if (solidOnly && tile != NULL && tile->getAABB(this, xTile0, yTile0, zTile0) == NULL) {
|
||||
// No collision
|
||||
} else if (t > 0 && tile->mayPick(data, liquid)) {
|
||||
if(xTile0 >= 0 && zTile0 >= 0 && xTile0 < LevelConstants::LEVEL_WIDTH && zTile0 < LevelConstants::LEVEL_WIDTH) {
|
||||
HitResult r = tile->clip(this, xTile0, yTile0, zTile0, a, b);
|
||||
if (r.isHit()) return r;
|
||||
}
|
||||
HitResult r = tile->clip(this, xTile0, yTile0, zTile0, a, b);
|
||||
if (r.isHit()) return r;
|
||||
}
|
||||
}
|
||||
return HitResult();
|
||||
|
|
@ -1022,7 +1010,12 @@ bool Level::addEntity(Entity* e) {
|
|||
if (e->isPlayer() && std::find(players.begin(), players.end(), e) == players.end()) {
|
||||
players.push_back( (Player*) e );
|
||||
}
|
||||
getChunk(xc, zc)->addEntity(e);
|
||||
if (!isClientSide || hasChunk(xc, zc)) {
|
||||
getChunk(xc, zc)->addEntity(e);
|
||||
} else {
|
||||
// chunk not streamed in yet: the entity is attached by Level::tick(Entity*) later
|
||||
e->inChunk = false;
|
||||
}
|
||||
entities.push_back(e);
|
||||
entityIdLookup[e->entityId] = e;
|
||||
entityAdded(e);
|
||||
|
|
@ -2250,9 +2243,20 @@ void Level::setNightMode( bool isNightMode ) {
|
|||
}
|
||||
|
||||
bool Level::inRange( int x, int y, int z ) {
|
||||
return x >= 0 && x < LevelConstants::LEVEL_WIDTH
|
||||
return x > -LevelConstants::LEVEL_WIDTH && x < LevelConstants::LEVEL_WIDTH
|
||||
&& y >= 0 && y < LevelConstants::LEVEL_HEIGHT
|
||||
&& z >= 0 && z < LevelConstants::LEVEL_DEPTH;
|
||||
&& z > -LevelConstants::LEVEL_DEPTH && z < LevelConstants::LEVEL_DEPTH;
|
||||
}
|
||||
|
||||
// Makes sure the chunks around a block position exist (generating/loading
|
||||
// them right now). Used before something is placed in an area that no
|
||||
// player has streamed in yet.
|
||||
void Level::ensureChunksAround(int x, int z, int radiusInChunks) {
|
||||
const int cx = x >> 4;
|
||||
const int cz = z >> 4;
|
||||
for (int dx = -radiusInChunks; dx <= radiusInChunks; ++dx)
|
||||
for (int dz = -radiusInChunks; dz <= radiusInChunks; ++dz)
|
||||
getChunk(cx + dx, cz + dz);
|
||||
}
|
||||
|
||||
//
|
||||
|
|
|
|||
|
|
@ -274,6 +274,7 @@ public:
|
|||
*/
|
||||
/*virtual*/ void setInitialSpawn();
|
||||
bool inRange(int x, int y, int z);
|
||||
void ensureChunksAround(int x, int z, int radiusInChunks);
|
||||
protected:
|
||||
void setZombieAi(std::vector<Zombie*>& zombies);
|
||||
/*virtual*/ void entityAdded(Entity* e);
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
#include "LevelConstants.h"
|
||||
|
||||
int LevelConstants::LEVEL_HEIGHT = 128;
|
||||
int LevelConstants::CHUNK_CACHE_WIDTH = 16; // in chunks
|
||||
int LevelConstants::CHUNK_WIDTH = 16; // in blocks
|
||||
int LevelConstants::CHUNK_DEPTH = 16;
|
||||
int LevelConstants::LEVEL_WIDTH = LevelConstants::CHUNK_CACHE_WIDTH * CHUNK_WIDTH;
|
||||
int LevelConstants::LEVEL_DEPTH = LevelConstants::CHUNK_CACHE_WIDTH * CHUNK_DEPTH;
|
||||
// The world is infinite: these are only the outer bounds of the coordinate
|
||||
// system (blocks), kept far away so nothing ever reaches them in practice.
|
||||
int LevelConstants::LEVEL_WIDTH = 32000000;
|
||||
int LevelConstants::LEVEL_DEPTH = 32000000;
|
||||
// Legacy value, only sent over the network for compatibility.
|
||||
int LevelConstants::CHUNK_CACHE_WIDTH = 16; // in chunks
|
||||
int LevelConstants::VIEW_DISTANCE_CHUNKS = 10; // chunks kept loaded around every player
|
||||
int LevelConstants::CHUNK_COLUMNS = LevelConstants::CHUNK_WIDTH * LevelConstants::CHUNK_DEPTH;
|
||||
int LevelConstants::CHUNK_BLOCK_COUNT = LevelConstants::CHUNK_COLUMNS * LevelConstants::LEVEL_HEIGHT;
|
||||
int LevelConstants::CHUNK_BLOCK_COUNT = LevelConstants::CHUNK_COLUMNS * LevelConstants::LEVEL_HEIGHT;
|
||||
|
|
|
|||
|
|
@ -1,14 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
class LevelConstants {
|
||||
public:
|
||||
static int CHUNK_CACHE_WIDTH; // in chunks
|
||||
static int LEVEL_WIDTH;
|
||||
static int LEVEL_DEPTH;
|
||||
static int LEVEL_HEIGHT;
|
||||
|
||||
static int CHUNK_WIDTH; // in blocks
|
||||
static int CHUNK_DEPTH;
|
||||
static int CHUNK_COLUMNS;
|
||||
static int CHUNK_BLOCK_COUNT;
|
||||
};
|
||||
#pragma once
|
||||
|
||||
class LevelConstants {
|
||||
public:
|
||||
static int CHUNK_CACHE_WIDTH; // legacy: only used by the network protocol
|
||||
static int LEVEL_WIDTH; // blocks, outer bound of the (infinite) world
|
||||
static int LEVEL_DEPTH;
|
||||
static int LEVEL_HEIGHT;
|
||||
|
||||
// Number of chunks around each player that the server keeps loaded.
|
||||
static int VIEW_DISTANCE_CHUNKS;
|
||||
|
||||
static int CHUNK_WIDTH; // in blocks
|
||||
static int CHUNK_DEPTH;
|
||||
static int CHUNK_COLUMNS;
|
||||
static int CHUNK_BLOCK_COUNT;
|
||||
|
||||
// Chunk coordinates are packed into one 64 bit key.
|
||||
static long long chunkKey(int x, int z) {
|
||||
return ((long long)x << 32) | (unsigned int)z;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -38,12 +38,20 @@ int MobSpawner::tick(Level* level, bool spawnEnemies, bool spawnFriendlies) {
|
|||
// (The code above is the same as in the java version)
|
||||
// This code goes over the whole map one "row" at a time
|
||||
|
||||
// Spawn friendlies == loop over whole map, and disable Monster spawning this tick
|
||||
// Spawn friendlies == loop over the chunks around every player, and disable Monster spawning this tick
|
||||
if (spawnFriendlies) {
|
||||
spawnEnemies = false;
|
||||
for (int i = 0; i < 256; ++i)
|
||||
chunksToPoll.insert( std::make_pair( ChunkPos(i>>4, i&15), false) );
|
||||
|
||||
const int r = 8;
|
||||
for (unsigned int pi = 0; pi < level->players.size(); ++pi) {
|
||||
Player* p = level->players[pi];
|
||||
int xx = Mth::floor(p->x / 16);
|
||||
int zz = Mth::floor(p->z / 16);
|
||||
for (int x = -r; x <= r; x++)
|
||||
for (int z = -r; z <= r; z++) {
|
||||
if (level->hasChunk(xx + x, zz + z))
|
||||
chunksToPoll.insert(std::make_pair(ChunkPos(xx + x, zz + z), false));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Only spawn mobs, check around one player per tick (@todo: optimize the "count instances of"?)
|
||||
static unsigned int _pid = 0;
|
||||
|
|
@ -57,7 +65,7 @@ int MobSpawner::tick(Level* level, bool spawnEnemies, bool spawnFriendlies) {
|
|||
for (int z = -r; z <= r; z++) {
|
||||
const int cx = xx + x;
|
||||
const int cz = zz + z;
|
||||
if (cx >= 0 && cx < 16 && cz >= 0 && cz < 16)
|
||||
if (level->hasChunk(cx, cz))
|
||||
chunksToPoll.insert(std::make_pair(ChunkPos(cx, cz), false ));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
380
src/world/level/chunk/ChunkCache.cpp
Normal file
380
src/world/level/chunk/ChunkCache.cpp
Normal file
|
|
@ -0,0 +1,380 @@
|
|||
#include "ChunkCache.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
|
||||
#include "../tile/entity/TileEntity.h"
|
||||
#include "../../entity/EntityFactory.h"
|
||||
#include "../../entity/player/Player.h"
|
||||
#include "../../../nbt/CompoundTag.h"
|
||||
#include "../../../nbt/ListTag.h"
|
||||
#include "../../../platform/log.h"
|
||||
#include "../../../platform/time.h"
|
||||
#include "../../../util/Mth.h"
|
||||
|
||||
ChunkCache::ChunkCache(Level* level_, ChunkStorage* storage_, ChunkSource* source_)
|
||||
: loadBudgetMs(10),
|
||||
manualStreaming(false),
|
||||
xLast(-999999999),
|
||||
zLast(-999999999),
|
||||
source(source_),
|
||||
storage(storage_),
|
||||
level(level_),
|
||||
last(NULL),
|
||||
tickCount(0),
|
||||
spiralRadius(-1),
|
||||
playerRoundRobin(0)
|
||||
{
|
||||
isChunkCache = true;
|
||||
emptyChunk = new EmptyLevelChunk(level_, NULL, 0, 0);
|
||||
LOGI("Chunk cache created (infinite world).\n");
|
||||
}
|
||||
|
||||
ChunkCache::~ChunkCache()
|
||||
{
|
||||
delete source;
|
||||
delete emptyChunk;
|
||||
|
||||
for (ChunkMap::iterator it = chunks.begin(); it != chunks.end(); ++it) {
|
||||
it->second->deleteBlockData();
|
||||
delete it->second;
|
||||
}
|
||||
chunks.clear();
|
||||
}
|
||||
|
||||
bool ChunkCache::hasChunk(int x, int z)
|
||||
{
|
||||
if (x == xLast && z == zLast && last != NULL)
|
||||
return true;
|
||||
return chunks.find(LevelConstants::chunkKey(x, z)) != chunks.end();
|
||||
}
|
||||
|
||||
LevelChunk* ChunkCache::create(int x, int z)
|
||||
{
|
||||
return getChunk(x, z);
|
||||
}
|
||||
|
||||
LevelChunk* ChunkCache::getChunk(int x, int z)
|
||||
{
|
||||
if (x == xLast && z == zLast && last != NULL) {
|
||||
return last;
|
||||
}
|
||||
|
||||
const long long key = LevelConstants::chunkKey(x, z);
|
||||
ChunkMap::iterator found = chunks.find(key);
|
||||
if (found != chunks.end()) {
|
||||
xLast = x;
|
||||
zLast = z;
|
||||
last = found->second;
|
||||
return last;
|
||||
}
|
||||
|
||||
bool fromSave = false;
|
||||
LevelChunk* newChunk = load(x, z);
|
||||
if (newChunk != NULL) {
|
||||
fromSave = true;
|
||||
} else if (source == NULL) {
|
||||
// nothing can create this chunk
|
||||
return emptyChunk;
|
||||
} else {
|
||||
newChunk = source->getChunk(x, z);
|
||||
}
|
||||
|
||||
chunks[key] = newChunk;
|
||||
xLast = x;
|
||||
zLast = z;
|
||||
last = newChunk;
|
||||
|
||||
newChunk->lightLava();
|
||||
|
||||
if (fromSave) {
|
||||
for (int cx = 0; cx < 16; cx++)
|
||||
{
|
||||
for (int cz = 0; cz < 16; cz++)
|
||||
{
|
||||
int height = level->getHeightmap(cx + x * 16, cz + z * 16);
|
||||
for (int cy = height; cy >= 0; cy--)
|
||||
{
|
||||
level->updateLight(LightLayer::Sky, cx + x * 16, cy, cz + z * 16, cx + x * 16, cy, cz + z * 16);
|
||||
level->updateLight(LightLayer::Block, cx + x * 16 - 1, cy, cz + z * 16 - 1, cx + x * 16 + 1, cy, cz + z * 16 + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newChunk->load();
|
||||
restoreEntities(newChunk);
|
||||
|
||||
if (!newChunk->terrainPopulated && hasChunk(x + 1, z + 1) && hasChunk(x, z + 1) && hasChunk(x + 1, z)) postProcess(this, x, z);
|
||||
if (hasChunk(x - 1, z) && !getChunk(x - 1, z)->terrainPopulated && hasChunk(x - 1, z + 1) && hasChunk(x, z + 1) && hasChunk(x - 1, z)) postProcess(this, x - 1, z);
|
||||
if (hasChunk(x, z - 1) && !getChunk(x, z - 1)->terrainPopulated && hasChunk(x + 1, z - 1) && hasChunk(x, z - 1) && hasChunk(x + 1, z)) postProcess(this, x, z - 1);
|
||||
if (hasChunk(x - 1, z - 1) && !getChunk(x - 1, z - 1)->terrainPopulated && hasChunk(x - 1, z - 1) && hasChunk(x, z - 1) && hasChunk(x - 1, z)) postProcess(this, x - 1, z - 1);
|
||||
|
||||
xLast = x;
|
||||
zLast = z;
|
||||
last = newChunk;
|
||||
|
||||
// let the renderers know that there is something to draw here now
|
||||
level->setTilesDirty(x * 16 - 1, 0, z * 16 - 1, x * 16 + 16, LevelConstants::LEVEL_HEIGHT - 1, z * 16 + 16);
|
||||
return newChunk;
|
||||
}
|
||||
|
||||
void ChunkCache::postProcess(ChunkSource* parent, int x, int z)
|
||||
{
|
||||
LevelChunk* chunk = getChunk(x, z);
|
||||
if (!chunk->terrainPopulated) {
|
||||
chunk->terrainPopulated = true;
|
||||
if (source != NULL) {
|
||||
source->postProcess(parent, x, z);
|
||||
chunk->clearUpdateMap();
|
||||
// the decorations belong to the generated state, make sure they are written out
|
||||
chunk->unsaved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LevelChunk* ChunkCache::load(int x, int z)
|
||||
{
|
||||
if (storage == NULL || level->isClientSide) return NULL;
|
||||
|
||||
LevelChunk* levelChunk = storage->load(level, x, z);
|
||||
if (levelChunk != NULL) {
|
||||
levelChunk->lastSaveTime = level->getTime();
|
||||
}
|
||||
return levelChunk;
|
||||
}
|
||||
|
||||
void ChunkCache::save(LevelChunk* levelChunk)
|
||||
{
|
||||
if (storage == NULL || levelChunk->dontSave) return;
|
||||
|
||||
levelChunk->lastSaveTime = level->getTime();
|
||||
storage->save(level, levelChunk);
|
||||
levelChunk->unsaved = false;
|
||||
}
|
||||
|
||||
bool ChunkCache::hasLiveData(LevelChunk* levelChunk)
|
||||
{
|
||||
return levelChunk->countEntities() > 0 || !levelChunk->getTileEntityMap().empty();
|
||||
}
|
||||
|
||||
// Spawns the entities and tile entities that were stored together with a chunk
|
||||
void ChunkCache::restoreEntities(LevelChunk* levelChunk)
|
||||
{
|
||||
CompoundTag* tag = levelChunk->pendingData;
|
||||
if (!tag) return;
|
||||
levelChunk->pendingData = NULL;
|
||||
|
||||
if (!level->isClientSide) {
|
||||
if (tag->contains("TileEntities", Tag::TAG_List)) {
|
||||
ListTag* tileEntityTags = tag->getList("TileEntities");
|
||||
for (int i = 0; i < tileEntityTags->size(); ++i) {
|
||||
Tag* _et = tileEntityTags->get(i);
|
||||
if (!_et || _et->getId() != Tag::TAG_Compound) continue;
|
||||
|
||||
if (TileEntity* te = TileEntity::loadStatic((CompoundTag*)_et)) {
|
||||
if (!levelChunk->hasTileEntityAt(te)) {
|
||||
levelChunk->addTileEntity(te);
|
||||
} else {
|
||||
delete te;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tag->contains("Entities", Tag::TAG_List)) {
|
||||
ListTag* entityTags = tag->getList("Entities");
|
||||
for (int i = 0; i < entityTags->size(); ++i) {
|
||||
Tag* _et = entityTags->get(i);
|
||||
if (!_et || _et->getId() != Tag::TAG_Compound) continue;
|
||||
|
||||
if (Entity* e = EntityFactory::loadEntity((CompoundTag*)_et, level)) {
|
||||
level->addEntity(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tag->deleteChildren();
|
||||
delete tag;
|
||||
}
|
||||
|
||||
void ChunkCache::unloadChunk(int x, int z)
|
||||
{
|
||||
ChunkMap::iterator it = chunks.find(LevelConstants::chunkKey(x, z));
|
||||
if (it == chunks.end()) return;
|
||||
|
||||
LevelChunk* chunk = it->second;
|
||||
|
||||
if (!level->isClientSide) {
|
||||
// Write the chunk (and everything living in it) out first
|
||||
save(chunk);
|
||||
|
||||
// The entities leave the world together with their chunk; they come back when it is loaded again
|
||||
std::vector<Entity*> es;
|
||||
chunk->collectEntities(es);
|
||||
for (unsigned int i = 0; i < es.size(); ++i) {
|
||||
Entity* e = es[i];
|
||||
if (e->isPlayer()) continue;
|
||||
e->inChunk = false;
|
||||
level->removeEntity(e);
|
||||
}
|
||||
}
|
||||
chunk->detachEntities();
|
||||
|
||||
chunks.erase(it);
|
||||
if (last == chunk) {
|
||||
last = NULL;
|
||||
xLast = zLast = -999999999;
|
||||
}
|
||||
|
||||
// Tile entities are owned by the level
|
||||
LevelChunk::TEMap& teMap = chunk->getTileEntityMapMutable();
|
||||
for (LevelChunk::TEMap::iterator te = teMap.begin(); te != teMap.end(); ++te) {
|
||||
TileEntity* t = te->second;
|
||||
if (!t) continue;
|
||||
Util::remove(level->pendingTileEntities, t);
|
||||
Util::remove(level->tileEntities, t);
|
||||
delete t;
|
||||
}
|
||||
teMap.clear();
|
||||
|
||||
chunk->unload();
|
||||
if (source) source->releaseChunk(x, z);
|
||||
chunk->deleteBlockData();
|
||||
delete chunk;
|
||||
}
|
||||
|
||||
void ChunkCache::getLoadedChunks(std::vector<LevelChunk*>& out)
|
||||
{
|
||||
out.reserve(out.size() + chunks.size());
|
||||
for (ChunkMap::iterator it = chunks.begin(); it != chunks.end(); ++it)
|
||||
out.push_back(it->second);
|
||||
}
|
||||
|
||||
bool ChunkCache::tick()
|
||||
{
|
||||
tickCount++;
|
||||
if (storage != NULL) storage->tick();
|
||||
|
||||
if (!level->isClientSide && !manualStreaming) {
|
||||
streamAroundPlayers();
|
||||
if ((tickCount % 10) == 0) unloadFarChunks();
|
||||
autosave();
|
||||
}
|
||||
return source != NULL ? source->tick() : false;
|
||||
}
|
||||
|
||||
void ChunkCache::streamAroundPlayers()
|
||||
{
|
||||
const PlayerList& players = level->players;
|
||||
if (players.empty()) return;
|
||||
|
||||
const int radius = LevelConstants::VIEW_DISTANCE_CHUNKS;
|
||||
if (radius != spiralRadius) {
|
||||
spiralRadius = radius;
|
||||
spiral.clear();
|
||||
for (int dx = -radius; dx <= radius; ++dx)
|
||||
for (int dz = -radius; dz <= radius; ++dz)
|
||||
if (dx * dx + dz * dz <= radius * radius + radius)
|
||||
spiral.push_back(std::make_pair(dx, dz));
|
||||
struct ByDistance {
|
||||
bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b) const {
|
||||
return a.first * a.first + a.second * a.second < b.first * b.first + b.second * b.second;
|
||||
}
|
||||
};
|
||||
std::stable_sort(spiral.begin(), spiral.end(), ByDistance());
|
||||
}
|
||||
|
||||
std::vector<std::pair<int, int> > centers;
|
||||
for (unsigned int i = 0; i < players.size(); ++i) {
|
||||
Player* p = players[i];
|
||||
if (p->removed) continue;
|
||||
centers.push_back(std::make_pair(Mth::floor(p->x / 16.0f), Mth::floor(p->z / 16.0f)));
|
||||
}
|
||||
if (centers.empty()) return;
|
||||
|
||||
const int start = getTimeMs();
|
||||
for (unsigned int i = 0; i < spiral.size(); ++i) {
|
||||
for (unsigned int c = 0; c < centers.size(); ++c) {
|
||||
const int cx = centers[c].first + spiral[i].first;
|
||||
const int cz = centers[c].second + spiral[i].second;
|
||||
if (!hasChunk(cx, cz)) {
|
||||
getChunk(cx, cz);
|
||||
if (getTimeMs() - start >= loadBudgetMs) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ChunkCache::unloadFarChunks()
|
||||
{
|
||||
const PlayerList& players = level->players;
|
||||
if (players.empty()) return;
|
||||
|
||||
std::vector<std::pair<int, int> > centers;
|
||||
for (unsigned int i = 0; i < players.size(); ++i) {
|
||||
Player* p = players[i];
|
||||
centers.push_back(std::make_pair(Mth::floor(p->x / 16.0f), Mth::floor(p->z / 16.0f)));
|
||||
}
|
||||
|
||||
const int unloadRadius = LevelConstants::VIEW_DISTANCE_CHUNKS + 3;
|
||||
std::vector<std::pair<int, int> > victims;
|
||||
for (ChunkMap::iterator it = chunks.begin(); it != chunks.end() && victims.size() < 6; ++it) {
|
||||
LevelChunk* c = it->second;
|
||||
bool isNear = false;
|
||||
for (unsigned int i = 0; i < centers.size(); ++i) {
|
||||
if (std::abs(c->x - centers[i].first) <= unloadRadius && std::abs(c->z - centers[i].second) <= unloadRadius) {
|
||||
isNear = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isNear) victims.push_back(std::make_pair(c->x, c->z));
|
||||
}
|
||||
for (unsigned int i = 0; i < victims.size(); ++i)
|
||||
unloadChunk(victims[i].first, victims[i].second);
|
||||
}
|
||||
|
||||
void ChunkCache::autosave()
|
||||
{
|
||||
if (storage == NULL) return;
|
||||
|
||||
if ((tickCount % 1200) == 0) {
|
||||
autosaveQueue.clear();
|
||||
for (ChunkMap::iterator it = chunks.begin(); it != chunks.end(); ++it) {
|
||||
LevelChunk* c = it->second;
|
||||
if (c->shouldSave(false) || hasLiveData(c))
|
||||
autosaveQueue.push_back(it->first);
|
||||
}
|
||||
}
|
||||
|
||||
for (int n = 0; n < 3 && !autosaveQueue.empty(); ++n) {
|
||||
long long key = autosaveQueue.front();
|
||||
autosaveQueue.pop_front();
|
||||
ChunkMap::iterator it = chunks.find(key);
|
||||
if (it != chunks.end()) save(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
void ChunkCache::saveAll(bool onlyUnsaved)
|
||||
{
|
||||
if (storage == NULL) return;
|
||||
|
||||
std::vector<LevelChunk*> toSave;
|
||||
for (ChunkMap::iterator it = chunks.begin(); it != chunks.end(); ++it) {
|
||||
LevelChunk* c = it->second;
|
||||
if (c->dontSave) continue;
|
||||
if (!onlyUnsaved || c->shouldSave(false) || hasLiveData(c))
|
||||
toSave.push_back(c);
|
||||
}
|
||||
storage->saveAll(level, toSave);
|
||||
for (unsigned int i = 0; i < toSave.size(); ++i)
|
||||
toSave[i]->unsaved = false;
|
||||
}
|
||||
|
||||
std::string ChunkCache::gatherStats()
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "ChunkCache: " << chunks.size();
|
||||
return ss.str();
|
||||
}
|
||||
|
|
@ -1,270 +1,87 @@
|
|||
#ifndef NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkCache_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkCache_H__
|
||||
|
||||
//package net.minecraft.world.level.chunk;
|
||||
|
||||
#include "ChunkSource.h"
|
||||
#include "storage/ChunkStorage.h"
|
||||
#include "EmptyLevelChunk.h"
|
||||
#include "../Level.h"
|
||||
#include "../LevelConstants.h"
|
||||
|
||||
class ChunkCache: public ChunkSource {
|
||||
//static const int LevelConstants::CHUNK_CACHE_WIDTH = LevelConstants::CHUNK_CACHE_WIDTH; // WAS 32;
|
||||
static const int MAX_SAVES = 2;
|
||||
public:
|
||||
ChunkCache(Level* level_, ChunkStorage* storage_, ChunkSource* source_)
|
||||
: xLast(-999999999),
|
||||
zLast(-999999999),
|
||||
last(NULL),
|
||||
level(level_),
|
||||
storage(storage_),
|
||||
source(source_)
|
||||
{
|
||||
isChunkCache = true;
|
||||
//emptyChunk = new EmptyLevelChunk(level_, emptyChunkBlocks, 0, 0);
|
||||
emptyChunk = new EmptyLevelChunk(level_, NULL, 0, 0);
|
||||
|
||||
chunks = new LevelChunk *[LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH];
|
||||
memset(chunks, 0, sizeof(LevelChunk*) * LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH);
|
||||
LOGI("Chunks allocated.");
|
||||
}
|
||||
|
||||
~ChunkCache() {
|
||||
delete source;
|
||||
delete emptyChunk;
|
||||
|
||||
for (int i = 0; i < LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH; i++)
|
||||
{
|
||||
if (chunks[i])
|
||||
{
|
||||
chunks[i]->deleteBlockData();
|
||||
delete chunks[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool fits(int x, int z) {
|
||||
return (x >= 0 && z >= 0 && x < LevelConstants::CHUNK_CACHE_WIDTH && z < LevelConstants::CHUNK_CACHE_WIDTH);
|
||||
}
|
||||
|
||||
bool hasChunk(int x, int z) {
|
||||
// with a fixed world size, chunks outside the fitting area are always available (emptyChunks)
|
||||
if (!fits(x, z)) return true;
|
||||
|
||||
if (x == xLast && z == zLast && last != NULL) {
|
||||
return true;
|
||||
}
|
||||
int xs = x & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
|
||||
int zs = z & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
|
||||
int slot = xs + zs * LevelConstants::CHUNK_CACHE_WIDTH;
|
||||
return chunks[slot] != NULL && (chunks[slot] == emptyChunk || chunks[slot]->isAt(x, z));
|
||||
}
|
||||
|
||||
LevelChunk* create(int x, int z) {
|
||||
return getChunk(x, z);
|
||||
}
|
||||
|
||||
LevelChunk* getChunk(int x, int z) {
|
||||
//static Stopwatch sw;
|
||||
//sw.start();
|
||||
|
||||
if (x == xLast && z == zLast && last != NULL) {
|
||||
return last;
|
||||
}
|
||||
if (!fits(x, z)) return emptyChunk;
|
||||
//if (!level->isFindingSpawn && !fits(x, z)) return emptyChunk;
|
||||
int xs = x & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
|
||||
int zs = z & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
|
||||
int slot = xs + zs * LevelConstants::CHUNK_CACHE_WIDTH;
|
||||
if (!hasChunk(x, z)) {
|
||||
if (chunks[slot] != NULL) {
|
||||
chunks[slot]->unload();
|
||||
save(chunks[slot]);
|
||||
saveEntities(chunks[slot]);
|
||||
}
|
||||
|
||||
LevelChunk* newChunk = load(x, z);
|
||||
bool updateLights = false;
|
||||
if (newChunk == NULL) {
|
||||
if (source == NULL) {
|
||||
newChunk = emptyChunk;
|
||||
} else {
|
||||
newChunk = source->getChunk(x, z);
|
||||
}
|
||||
} else {
|
||||
//return emptyChunk;
|
||||
updateLights = true;
|
||||
}
|
||||
chunks[slot] = newChunk;
|
||||
newChunk->lightLava();
|
||||
|
||||
if (updateLights)
|
||||
{
|
||||
for (int cx = 0; cx < 16; cx++)
|
||||
{
|
||||
for (int cz = 0; cz < 16; cz++)
|
||||
{
|
||||
int height = level->getHeightmap(cx + x * 16, cz + z * 16);
|
||||
for (int cy = height; cy >= 0; cy--)
|
||||
{
|
||||
level->updateLight(LightLayer::Sky, cx + x * 16, cy, cz + z * 16, cx + x * 16, cy, cz + z * 16);
|
||||
level->updateLight(LightLayer::Block, cx + x * 16 - 1, cy, cz + z * 16 - 1, cx + x * 16 + 1, cy, cz + z * 16 + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
//level->updateLight(LightLayer::Sky, x * 16, 0, z * 16, x * 16 + 15, 128, z * 16 + 15);
|
||||
//level->updateLight(LightLayer::Block, x * 16, 0, z * 16, x * 16 + 15, 128, z * 16 + 15);
|
||||
}
|
||||
|
||||
if (chunks[slot] != NULL) {
|
||||
chunks[slot]->load();
|
||||
}
|
||||
|
||||
if (!chunks[slot]->terrainPopulated && hasChunk(x + 1, z + 1) && hasChunk(x, z + 1) && hasChunk(x + 1, z)) postProcess(this, x, z);
|
||||
if (hasChunk(x - 1, z) && !getChunk(x - 1, z)->terrainPopulated && hasChunk(x - 1, z + 1) && hasChunk(x, z + 1) && hasChunk(x - 1, z)) postProcess(this, x - 1, z);
|
||||
if (hasChunk(x, z - 1) && !getChunk(x, z - 1)->terrainPopulated && hasChunk(x + 1, z - 1) && hasChunk(x, z - 1) && hasChunk(x + 1, z)) postProcess(this, x, z - 1);
|
||||
if (hasChunk(x - 1, z - 1) && !getChunk(x - 1, z - 1)->terrainPopulated && hasChunk(x - 1, z - 1) && hasChunk(x, z - 1) && hasChunk(x - 1, z)) postProcess(this, x - 1, z - 1);
|
||||
}
|
||||
xLast = x;
|
||||
zLast = z;
|
||||
last = chunks[slot];
|
||||
|
||||
//sw.stop();
|
||||
//sw.printEvery(500000, "ChunkCache::load: ");
|
||||
|
||||
return chunks[slot];
|
||||
}
|
||||
|
||||
Biome::MobList getMobsAt(const MobCategory& mobCategory, int x, int y, int z) {
|
||||
return source->getMobsAt(mobCategory, x, y, z);
|
||||
}
|
||||
|
||||
void postProcess(ChunkSource* parent, int x, int z) {
|
||||
if (!fits(x, z)) return;
|
||||
LevelChunk* chunk = getChunk(x, z);
|
||||
if (!chunk->terrainPopulated) {
|
||||
chunk->terrainPopulated = true;
|
||||
if (source != NULL) {
|
||||
source->postProcess(parent, x, z);
|
||||
chunk->clearUpdateMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//bool save(bool force, ProgressListener progressListener) {
|
||||
// int saves = 0;
|
||||
// int count = 0;
|
||||
// if (progressListener != NULL) {
|
||||
// for (int i = 0; i < chunks.length; i++) {
|
||||
// if (chunks[i] != NULL && chunks[i].shouldSave(force)) {
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// int cc = 0;
|
||||
// for (int i = 0; i < chunks.length; i++) {
|
||||
// if (chunks[i] != NULL) {
|
||||
// if (force && !chunks[i].dontSave) saveEntities(chunks[i]);
|
||||
// if (chunks[i].shouldSave(force)) {
|
||||
// save(chunks[i]);
|
||||
// chunks[i].unsaved = false;
|
||||
// if (++saves == MAX_SAVES && !force) return false;
|
||||
// if (progressListener != NULL) {
|
||||
// if (++cc % 10 == 0) {
|
||||
// progressListener.progressStagePercentage(cc * 100 / count);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (force) {
|
||||
// if (storage == NULL) return true;
|
||||
// storage.flush();
|
||||
// }
|
||||
// return true;
|
||||
//}
|
||||
|
||||
bool tick() {
|
||||
if (storage != NULL) storage->tick();
|
||||
return source->tick();
|
||||
}
|
||||
|
||||
bool shouldSave() {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string gatherStats() {
|
||||
// return "ChunkCache: 1024";
|
||||
std::stringstream ss;
|
||||
ss << "ChunkCache: " << LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void saveAll(bool onlyUnsaved) {
|
||||
if (storage != NULL) {
|
||||
std::vector<LevelChunk*> chunks;
|
||||
for (int z = 0; z < LevelConstants::CHUNK_CACHE_WIDTH; ++z)
|
||||
for (int x = 0; x < LevelConstants::CHUNK_CACHE_WIDTH; ++x) {
|
||||
LevelChunk* chunk = level->getChunk(x, z);
|
||||
if (!onlyUnsaved || chunk->shouldSave(false))
|
||||
chunks.push_back( chunk );
|
||||
}
|
||||
storage->saveAll(level, chunks);
|
||||
}
|
||||
}
|
||||
private:
|
||||
LevelChunk* load(int x, int z) {
|
||||
if (storage == NULL) return emptyChunk;
|
||||
if (x < 0 || x >= LevelConstants::CHUNK_CACHE_WIDTH || z < 0 || z >= LevelConstants::CHUNK_CACHE_WIDTH)
|
||||
{
|
||||
return emptyChunk;
|
||||
}
|
||||
//try {
|
||||
LevelChunk* levelChunk = storage->load(level, x, z);
|
||||
if (levelChunk != NULL) {
|
||||
levelChunk->lastSaveTime = level->getTime();
|
||||
}
|
||||
return levelChunk;
|
||||
//} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// return emptyChunk;
|
||||
//}
|
||||
}
|
||||
|
||||
void saveEntities(LevelChunk* levelChunk) {
|
||||
if (storage == NULL) return;
|
||||
//try {
|
||||
storage->saveEntities(level, levelChunk);
|
||||
//} catch (Error e) {
|
||||
// e.printStackTrace();
|
||||
//}
|
||||
}
|
||||
|
||||
void save(LevelChunk* levelChunk) {
|
||||
if (storage == NULL) return;
|
||||
//try {
|
||||
levelChunk->lastSaveTime = level->getTime();
|
||||
storage->save(level, levelChunk);
|
||||
//} catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
//}
|
||||
}
|
||||
|
||||
public:
|
||||
int xLast;
|
||||
int zLast;
|
||||
private:
|
||||
//unsigned char emptyChunkBlocks[LevelChunk::ChunkBlockCount];
|
||||
LevelChunk* emptyChunk;
|
||||
ChunkSource* source;
|
||||
ChunkStorage* storage;
|
||||
LevelChunk** chunks;
|
||||
Level* level;
|
||||
|
||||
LevelChunk* last;
|
||||
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkCache_H__*/
|
||||
#ifndef NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkCache_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkCache_H__
|
||||
|
||||
//package net.minecraft.world.level.chunk;
|
||||
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "ChunkSource.h"
|
||||
#include "storage/ChunkStorage.h"
|
||||
#include "EmptyLevelChunk.h"
|
||||
#include "../Level.h"
|
||||
#include "../LevelConstants.h"
|
||||
|
||||
/**
|
||||
* Holds every chunk of the (infinite) world that is currently in memory.
|
||||
*
|
||||
* On a server / in single player the cache streams chunks in around each
|
||||
* player, saves and unloads the ones that are left behind, and persists the
|
||||
* entities and tile entities that live in them. On a multiplayer client the
|
||||
* chunks are driven by the network handler instead (see unloadChunk).
|
||||
*/
|
||||
class ChunkCache: public ChunkSource {
|
||||
public:
|
||||
ChunkCache(Level* level_, ChunkStorage* storage_, ChunkSource* source_);
|
||||
~ChunkCache();
|
||||
|
||||
bool hasChunk(int x, int z);
|
||||
LevelChunk* create(int x, int z);
|
||||
LevelChunk* getChunk(int x, int z);
|
||||
|
||||
Biome::MobList getMobsAt(const MobCategory& mobCategory, int x, int y, int z) {
|
||||
return source->getMobsAt(mobCategory, x, y, z);
|
||||
}
|
||||
|
||||
void postProcess(ChunkSource* parent, int x, int z);
|
||||
|
||||
bool tick();
|
||||
bool shouldSave() { return true; }
|
||||
std::string gatherStats();
|
||||
|
||||
void saveAll(bool onlyUnsaved);
|
||||
void unloadChunk(int x, int z);
|
||||
void getLoadedChunks(std::vector<LevelChunk*>& out);
|
||||
int getLoadedChunkCount() { return (int)chunks.size(); }
|
||||
|
||||
// Chunks around the players that were not generated/loaded yet are
|
||||
// created nearest first, at most for this many milliseconds per tick.
|
||||
int loadBudgetMs;
|
||||
|
||||
// Disables the automatic loading/unloading around the players.
|
||||
bool manualStreaming;
|
||||
|
||||
public:
|
||||
int xLast;
|
||||
int zLast;
|
||||
|
||||
private:
|
||||
LevelChunk* load(int x, int z);
|
||||
void save(LevelChunk* levelChunk);
|
||||
void restoreEntities(LevelChunk* levelChunk);
|
||||
bool hasLiveData(LevelChunk* levelChunk);
|
||||
|
||||
void streamAroundPlayers();
|
||||
void unloadFarChunks();
|
||||
void autosave();
|
||||
|
||||
typedef std::unordered_map<long long, LevelChunk*> ChunkMap;
|
||||
ChunkMap chunks;
|
||||
|
||||
LevelChunk* emptyChunk;
|
||||
ChunkSource* source;
|
||||
ChunkStorage* storage;
|
||||
Level* level;
|
||||
|
||||
LevelChunk* last;
|
||||
|
||||
int tickCount;
|
||||
int unloadRadiusCache;
|
||||
std::vector<std::pair<int, int> > spiral; // chunk offsets sorted by distance
|
||||
int spiralRadius;
|
||||
unsigned int playerRoundRobin;
|
||||
std::deque<long long> autosaveQueue;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkCache_H__*/
|
||||
|
|
|
|||
|
|
@ -1,46 +1,56 @@
|
|||
#ifndef NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkSource_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkSource_H__
|
||||
|
||||
//package net.minecraft.world.level.chunk;
|
||||
|
||||
#include <string>
|
||||
#include "../biome/Biome.h"
|
||||
|
||||
class ProgressListener;
|
||||
class LevelChunk;
|
||||
|
||||
class ChunkSource
|
||||
{
|
||||
public:
|
||||
bool isChunkCache;
|
||||
|
||||
ChunkSource()
|
||||
: isChunkCache(false)
|
||||
{}
|
||||
|
||||
virtual ~ChunkSource(){}
|
||||
|
||||
virtual bool hasChunk(int x, int y) = 0;
|
||||
|
||||
virtual LevelChunk* getChunk(int x, int z) = 0;
|
||||
|
||||
virtual LevelChunk* create(int x, int z) = 0;
|
||||
|
||||
virtual void postProcess(ChunkSource* parent, int x, int z) = 0;
|
||||
|
||||
//virtual bool save(bool force, ProgressListener* progressListener) = 0;
|
||||
|
||||
virtual bool tick() = 0;
|
||||
|
||||
virtual bool shouldSave() = 0;
|
||||
virtual void saveAll(bool onlyUnsaved) {}
|
||||
|
||||
virtual Biome::MobList getMobsAt(const MobCategory& mobCategory, int x, int y, int z) = 0;
|
||||
|
||||
/**
|
||||
* Returns some stats that are rendered when the user holds F3.
|
||||
*/
|
||||
virtual std::string gatherStats() = 0;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkSource_H__*/
|
||||
#ifndef NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkSource_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkSource_H__
|
||||
|
||||
//package net.minecraft.world.level.chunk;
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "../biome/Biome.h"
|
||||
|
||||
class ProgressListener;
|
||||
class LevelChunk;
|
||||
|
||||
class ChunkSource
|
||||
{
|
||||
public:
|
||||
bool isChunkCache;
|
||||
|
||||
ChunkSource()
|
||||
: isChunkCache(false)
|
||||
{}
|
||||
|
||||
virtual ~ChunkSource(){}
|
||||
|
||||
virtual bool hasChunk(int x, int y) = 0;
|
||||
|
||||
virtual LevelChunk* getChunk(int x, int z) = 0;
|
||||
|
||||
// Called when the owner of the chunk has unloaded (and freed) it, so a
|
||||
// generator that keeps its own lookup table must forget about it.
|
||||
virtual void releaseChunk(int x, int z) {}
|
||||
|
||||
// Saves (server) and frees a chunk. Only chunk caches keep chunks.
|
||||
virtual void unloadChunk(int x, int z) {}
|
||||
virtual void getLoadedChunks(std::vector<LevelChunk*>& out) {}
|
||||
virtual int getLoadedChunkCount() { return 0; }
|
||||
|
||||
virtual LevelChunk* create(int x, int z) = 0;
|
||||
|
||||
virtual void postProcess(ChunkSource* parent, int x, int z) = 0;
|
||||
|
||||
//virtual bool save(bool force, ProgressListener* progressListener) = 0;
|
||||
|
||||
virtual bool tick() = 0;
|
||||
|
||||
virtual bool shouldSave() = 0;
|
||||
virtual void saveAll(bool onlyUnsaved) {}
|
||||
|
||||
virtual Biome::MobList getMobsAt(const MobCategory& mobCategory, int x, int y, int z) = 0;
|
||||
|
||||
/**
|
||||
* Returns some stats that are rendered when the user holds F3.
|
||||
*/
|
||||
virtual std::string gatherStats() = 0;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_CHUNK__ChunkSource_H__*/
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include "../tile/entity/TileEntity.h"
|
||||
#include "../tile/EntityTile.h"
|
||||
#include "../LevelConstants.h"
|
||||
#include "../../../nbt/CompoundTag.h"
|
||||
|
||||
/*static*/
|
||||
bool LevelChunk::touchedSky = false;
|
||||
|
|
@ -41,7 +42,13 @@ LevelChunk::LevelChunk( Level* level, unsigned char* blocks, int x, int z )
|
|||
|
||||
LevelChunk::~LevelChunk()
|
||||
{
|
||||
//delete blocks;
|
||||
//delete blocks; (see deleteBlockData)
|
||||
delete[] heightmap;
|
||||
delete[] updateMap;
|
||||
if (pendingData) {
|
||||
pendingData->deleteChildren();
|
||||
delete pendingData;
|
||||
}
|
||||
}
|
||||
|
||||
void LevelChunk::init()
|
||||
|
|
@ -49,6 +56,8 @@ void LevelChunk::init()
|
|||
heightmap = new char[LevelConstants::CHUNK_COLUMNS];
|
||||
updateMap = new unsigned char[LevelConstants::CHUNK_COLUMNS];
|
||||
|
||||
pendingData = NULL;
|
||||
loaded = false;
|
||||
terrainPopulated = false;
|
||||
dontSave = false;
|
||||
unsaved = false;
|
||||
|
|
@ -537,6 +546,23 @@ void LevelChunk::getEntitiesOfClass( int type, const AABB& bb, EntityList& list
|
|||
}
|
||||
}
|
||||
|
||||
void LevelChunk::collectEntities(std::vector<Entity*>& out) const
|
||||
{
|
||||
for (int yc = 0; yc < EntityBlocksArraySize; yc++) {
|
||||
out.insert(out.end(), entityBlocks[yc].begin(), entityBlocks[yc].end());
|
||||
}
|
||||
}
|
||||
|
||||
void LevelChunk::detachEntities()
|
||||
{
|
||||
for (int yc = 0; yc < EntityBlocksArraySize; yc++) {
|
||||
for (unsigned int i = 0; i < entityBlocks[yc].size(); i++) {
|
||||
entityBlocks[yc][i]->inChunk = false;
|
||||
}
|
||||
entityBlocks[yc].clear();
|
||||
}
|
||||
}
|
||||
|
||||
int LevelChunk::countEntities()
|
||||
{
|
||||
int entityCount = 0;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ class Entity;
|
|||
class LightLayer;
|
||||
class AABB;
|
||||
class TileEntity;
|
||||
class CompoundTag;
|
||||
typedef std::vector<Entity*> EntityList;
|
||||
|
||||
class LevelChunk
|
||||
|
|
@ -99,6 +100,12 @@ public:
|
|||
|
||||
virtual bool isEmpty();
|
||||
const TEMap& getTileEntityMap() const;
|
||||
TEMap& getTileEntityMapMutable() { return tileEntities; }
|
||||
|
||||
// Appends every entity registered in this chunk (players included)
|
||||
void collectEntities(std::vector<Entity*>& out) const;
|
||||
// Forgets that the entities of this chunk are listed here (the chunk is going away)
|
||||
void detachEntities();
|
||||
private:
|
||||
void lightGap(int x, int z, int source);
|
||||
void lightGaps(int x, int z);
|
||||
|
|
@ -125,6 +132,10 @@ public:
|
|||
int x, z;
|
||||
int xt, zt;
|
||||
|
||||
// Entities and tile entities read from the save file, waiting to be spawned
|
||||
// by the chunk cache once the chunk is registered in the level (owned).
|
||||
CompoundTag* pendingData;
|
||||
|
||||
bool terrainPopulated;
|
||||
bool unsaved;
|
||||
bool dontSave;
|
||||
|
|
|
|||
|
|
@ -537,9 +537,13 @@ LevelChunk* RandomLevelSource::create(int x, int z) {
|
|||
return getChunk(x, z);
|
||||
}
|
||||
|
||||
void RandomLevelSource::releaseChunk(int x, int z) {
|
||||
chunkMap.erase(LevelConstants::chunkKey(x, z));
|
||||
}
|
||||
|
||||
LevelChunk* RandomLevelSource::getChunk(int xOffs, int zOffs) {
|
||||
//static int chunkx = 0;
|
||||
int hashedPos = ChunkPos::hashCode(xOffs, zOffs);
|
||||
const long long hashedPos = LevelConstants::chunkKey(xOffs, zOffs);
|
||||
|
||||
ChunkMap::iterator it = chunkMap.find(hashedPos);
|
||||
if (it != chunkMap.end())
|
||||
|
|
|
|||
|
|
@ -7,20 +7,9 @@ class Biome;
|
|||
class Level;
|
||||
class LevelChunk;
|
||||
|
||||
#if 1 || USE_MAP
|
||||
#include <map>
|
||||
typedef std::map<int, LevelChunk*> ChunkMap;
|
||||
#else
|
||||
#if defined(__APPLE__)
|
||||
#include <ext/hash_map>
|
||||
namespace std {
|
||||
using namespace __gnu_cxx;
|
||||
}
|
||||
#else
|
||||
#include <hash_map>
|
||||
#endif
|
||||
typedef std::hash_map<int, LevelChunk*> ChunkMap;
|
||||
#endif
|
||||
#include <map>
|
||||
// Keyed by LevelConstants::chunkKey(x, z)
|
||||
typedef std::map<long long, LevelChunk*> ChunkMap;
|
||||
|
||||
|
||||
#include "../chunk/ChunkSource.h"
|
||||
|
|
@ -44,6 +33,7 @@ public:
|
|||
bool hasChunk(int x, int y);
|
||||
LevelChunk* create(int x, int z);
|
||||
LevelChunk* getChunk(int xOffs, int zOffs);
|
||||
void releaseChunk(int x, int z);
|
||||
|
||||
void prepareHeights(int xOffs, int zOffs, unsigned char* blocks, /*Biome*/void* biomes, float* temperatures);
|
||||
void buildSurfaces(int xOffs, int zOffs, unsigned char* blocks, Biome** biomes);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#include "ExternalFileLevelStorage.h"
|
||||
#include "FolderMethods.h"
|
||||
#include "../chunk/LevelChunk.h"
|
||||
#include "../chunk/ChunkSource.h"
|
||||
#include "../Level.h"
|
||||
#include "../LevelConstants.h"
|
||||
#include "platform/log.h"
|
||||
|
|
@ -82,10 +83,7 @@ ExternalFileLevelStorage::ExternalFileLevelStorage(const std::string& levelId, c
|
|||
: levelId(levelId),
|
||||
levelPath(fullPath),
|
||||
loadedLevelData(NULL),
|
||||
regionFile(NULL),
|
||||
entitiesFile(NULL),
|
||||
tickCount(0),
|
||||
lastSavedEntitiesTick(-999999),
|
||||
level(NULL),
|
||||
loadedStorageVersion(SharedConstants::StorageVersion)
|
||||
{
|
||||
|
|
@ -109,10 +107,53 @@ ExternalFileLevelStorage::ExternalFileLevelStorage(const std::string& levelId, c
|
|||
|
||||
ExternalFileLevelStorage::~ExternalFileLevelStorage()
|
||||
{
|
||||
delete regionFile;
|
||||
closeRegions();
|
||||
delete loadedLevelData;
|
||||
}
|
||||
|
||||
void ExternalFileLevelStorage::closeRegions()
|
||||
{
|
||||
for (std::map<long long, RegionFile*>::iterator it = regions.begin(); it != regions.end(); ++it)
|
||||
delete it->second;
|
||||
regions.clear();
|
||||
regionOrder.clear();
|
||||
}
|
||||
|
||||
// Region files are opened on demand; only a handful stay open at once.
|
||||
RegionFile* ExternalFileLevelStorage::getRegion(int chunkX, int chunkZ)
|
||||
{
|
||||
const int rx = chunkX >> 5;
|
||||
const int rz = chunkZ >> 5;
|
||||
const long long key = LevelConstants::chunkKey(rx, rz);
|
||||
|
||||
std::map<long long, RegionFile*>::iterator it = regions.find(key);
|
||||
if (it != regions.end()) {
|
||||
regionOrder.remove(key);
|
||||
regionOrder.push_back(key);
|
||||
return it->second;
|
||||
}
|
||||
|
||||
static const size_t MaxOpenRegions = 12;
|
||||
while (regions.size() >= MaxOpenRegions && !regionOrder.empty()) {
|
||||
long long oldest = regionOrder.front();
|
||||
regionOrder.pop_front();
|
||||
std::map<long long, RegionFile*>::iterator ot = regions.find(oldest);
|
||||
if (ot != regions.end()) {
|
||||
delete ot->second;
|
||||
regions.erase(ot);
|
||||
}
|
||||
}
|
||||
|
||||
RegionFile* region = new RegionFile(levelPath, rx, rz);
|
||||
if (!region->open()) {
|
||||
delete region;
|
||||
return NULL;
|
||||
}
|
||||
regions[key] = region;
|
||||
regionOrder.push_back(key);
|
||||
return region;
|
||||
}
|
||||
|
||||
void ExternalFileLevelStorage::saveLevelData(LevelData& levelData, std::vector<Player*>* players) {
|
||||
ExternalFileLevelStorage::saveLevelData(levelPath, levelData, players);
|
||||
}
|
||||
|
|
@ -341,10 +382,6 @@ bool ExternalFileLevelStorage::readPlayerData(const std::string& filename, Level
|
|||
|
||||
// Fix coordinates
|
||||
Vec3& pos = dest.playerData.pos;
|
||||
if (pos.x < 0.5f) pos.x = 0.5f;
|
||||
if (pos.z < 0.5f) pos.z = 0.5f;
|
||||
if (pos.x > (LevelConstants::LEVEL_WIDTH - 0.5f)) pos.x = LevelConstants::LEVEL_WIDTH - 0.5f;
|
||||
if (pos.z > (LevelConstants::LEVEL_DEPTH - 0.5f)) pos.z = LevelConstants::LEVEL_DEPTH - 0.5f;
|
||||
if (pos.y < 0) pos.y = 64;
|
||||
|
||||
dest.playerDataVersion = version;
|
||||
|
|
@ -357,61 +394,15 @@ bool ExternalFileLevelStorage::readPlayerData(const std::string& filename, Level
|
|||
|
||||
void ExternalFileLevelStorage::tick()
|
||||
{
|
||||
// Chunks (and the entities inside them) are saved by the chunk cache.
|
||||
tickCount++;
|
||||
if ((tickCount % 1000) == 0 && level) {
|
||||
LOGI("Saving level...\n");
|
||||
|
||||
// look for chunks that needs to be saved
|
||||
for (int z = 0; z < LevelConstants::CHUNK_CACHE_WIDTH; z++)
|
||||
{
|
||||
for (int x = 0; x < LevelConstants::CHUNK_CACHE_WIDTH; x++)
|
||||
{
|
||||
LevelChunk* chunk = level->getChunk(x, z);
|
||||
if (chunk && chunk->unsaved)
|
||||
{
|
||||
int pos = x + z * LevelConstants::CHUNK_CACHE_WIDTH;
|
||||
UnsavedChunkList::iterator prev = unsavedChunkList.begin();
|
||||
for ( ; prev != unsavedChunkList.end(); ++prev)
|
||||
{
|
||||
if ((*prev).pos == pos)
|
||||
{
|
||||
// the chunk has been modified again, so update its time
|
||||
(*prev).addedToList = RakNet::GetTimeMS();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (prev == unsavedChunkList.end())
|
||||
{
|
||||
UnsavedLevelChunk unsaved;
|
||||
unsaved.pos = pos;
|
||||
unsaved.addedToList = RakNet::GetTimeMS();
|
||||
unsaved.chunk = chunk;
|
||||
unsavedChunkList.push_back(unsaved);
|
||||
}
|
||||
chunk->unsaved = false; // not actually saved, but in our working list at least
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
savePendingUnsavedChunks(2);
|
||||
}
|
||||
if (tickCount - lastSavedEntitiesTick > (60 * SharedConstants::TicksPerSecond)) {
|
||||
saveEntities(level, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void ExternalFileLevelStorage::save(Level* level, LevelChunk* levelChunk)
|
||||
{
|
||||
if (!regionFile)
|
||||
{
|
||||
regionFile = new RegionFile(levelPath);
|
||||
if (!regionFile->open())
|
||||
{
|
||||
delete regionFile;
|
||||
regionFile = NULL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
RegionFile* region = getRegion(levelChunk->x, levelChunk->z);
|
||||
if (!region)
|
||||
return;
|
||||
|
||||
// Write chunk
|
||||
RakNet::BitStream chunkData;
|
||||
|
|
@ -423,29 +414,68 @@ void ExternalFileLevelStorage::save(Level* level, LevelChunk* levelChunk)
|
|||
|
||||
chunkData.Write((const char*)levelChunk->updateMap, LevelConstants::CHUNK_COLUMNS);
|
||||
|
||||
regionFile->writeChunk(levelChunk->x, levelChunk->z, chunkData);
|
||||
// Flags: bit 0 = the terrain decorations (trees, ores, ...) were placed
|
||||
unsigned char flags = levelChunk->terrainPopulated ? 1 : 0;
|
||||
chunkData.Write((const char*)&flags, 1);
|
||||
|
||||
// Write entities
|
||||
// Entities and tile entities that live in this chunk
|
||||
if (!level->isClientSide) {
|
||||
CompoundTag base;
|
||||
ListTag* entityTags = new ListTag();
|
||||
ListTag* tileEntityTags = new ListTag();
|
||||
|
||||
std::vector<Entity*> chunkEntities;
|
||||
levelChunk->collectEntities(chunkEntities);
|
||||
for (unsigned int i = 0; i < chunkEntities.size(); ++i) {
|
||||
Entity* e = chunkEntities[i];
|
||||
if (e->removed || e->isPlayer()) continue;
|
||||
|
||||
CompoundTag* tag = new CompoundTag();
|
||||
if (e->save(tag))
|
||||
entityTags->add(tag);
|
||||
else
|
||||
delete tag;
|
||||
}
|
||||
|
||||
const LevelChunk::TEMap& teMap = levelChunk->getTileEntityMap();
|
||||
for (LevelChunk::TEMapCIterator it = teMap.begin(); it != teMap.end(); ++it) {
|
||||
TileEntity* te = it->second;
|
||||
if (!te || te->isRemoved() || !te->shouldSave()) continue;
|
||||
|
||||
CompoundTag* tag = new CompoundTag();
|
||||
if (te->save(tag))
|
||||
tileEntityTags->add(tag);
|
||||
else
|
||||
delete tag;
|
||||
}
|
||||
|
||||
const bool hasData = entityTags->size() > 0 || tileEntityTags->size() > 0;
|
||||
base.put("Entities", entityTags);
|
||||
base.put("TileEntities", tileEntityTags);
|
||||
|
||||
if (hasData) {
|
||||
RakNet::BitStream stream;
|
||||
RakDataOutput dos(stream);
|
||||
NbtIo::write(&base, &dos);
|
||||
int numBytes = (int)stream.GetNumberOfBytesUsed();
|
||||
chunkData.Write(numBytes);
|
||||
chunkData.Write((const char*)stream.GetData(), numBytes);
|
||||
}
|
||||
base.deleteChildren();
|
||||
}
|
||||
|
||||
region->writeChunk(levelChunk->x & 31, levelChunk->z & 31, chunkData);
|
||||
//LOGI("Saved chunk (%d, %d)\n", levelChunk->x, levelChunk->z);
|
||||
|
||||
}
|
||||
|
||||
LevelChunk* ExternalFileLevelStorage::load(Level* level, int x, int z)
|
||||
{
|
||||
if (!regionFile)
|
||||
{
|
||||
regionFile = new RegionFile(levelPath);
|
||||
if (!regionFile->open())
|
||||
{
|
||||
delete regionFile;
|
||||
regionFile = NULL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
RegionFile* region = getRegion(x, z);
|
||||
if (!region)
|
||||
return NULL;
|
||||
|
||||
RakNet::BitStream* chunkData = NULL;
|
||||
if (!regionFile->readChunk(x, z, &chunkData))
|
||||
if (!region->readChunk(x & 31, z & 31, &chunkData))
|
||||
{
|
||||
//LOGI("Failed to read data for %d, %d\n", x, z);
|
||||
return NULL;
|
||||
|
|
@ -463,47 +493,37 @@ LevelChunk* ExternalFileLevelStorage::load(Level* level, int x, int z)
|
|||
chunkData->Read((char*)levelChunk->blockLight.data, LevelConstants::CHUNK_BLOCK_COUNT / 2);
|
||||
}
|
||||
chunkData->Read((char*)levelChunk->updateMap, LevelConstants::CHUNK_COLUMNS);
|
||||
// This will be difficult to maintain.. Storage version could be per chunk
|
||||
// too (but probably better to just read all -> write all, so that all
|
||||
// chunks got same version anyway)
|
||||
//if (loadedStorageVersion >= ChunkVersion_Entity) {
|
||||
// int dictSize;
|
||||
// chunkData->Read(dictSize);
|
||||
|
||||
// RakDataInput dis(*chunkData);
|
||||
// Tag* tmp = Tag::readNamedTag(&dis);
|
||||
// if (tmp && tmp->getId() == Tag::TAG_Compound) {
|
||||
// CompoundTag* tag = (CompoundTag*) tmp;
|
||||
// Chunks written by older versions end here and were always fully decorated
|
||||
bool populated = true;
|
||||
if (chunkData->GetNumberOfUnreadBits() >= 8) {
|
||||
unsigned char flags = 0;
|
||||
chunkData->Read((char*)&flags, 1);
|
||||
populated = (flags & 1) != 0;
|
||||
}
|
||||
|
||||
// delete tmp;
|
||||
// }
|
||||
//}
|
||||
// Optional trailer: the entities and tile entities of the chunk
|
||||
if (!level->isClientSide && chunkData->GetNumberOfUnreadBits() >= 32) {
|
||||
int numBytes = 0;
|
||||
chunkData->Read(numBytes);
|
||||
if (numBytes > 0 && numBytes <= (int)(chunkData->GetNumberOfUnreadBits() >> 3)) {
|
||||
unsigned char* buf = new unsigned char[numBytes];
|
||||
chunkData->Read((char*)buf, numBytes);
|
||||
|
||||
RakNet::BitStream stream(buf, numBytes, false);
|
||||
RakDataInput dis(stream);
|
||||
levelChunk->pendingData = NbtIo::read(&dis);
|
||||
|
||||
delete[] buf;
|
||||
}
|
||||
}
|
||||
|
||||
delete [] chunkData->GetData();
|
||||
delete chunkData;
|
||||
|
||||
//bool dbg = (x == 7 && z == 9);
|
||||
|
||||
//int t = 0;
|
||||
//for (int i = 0; i < LevelConstants::CHUNK_COLUMNS; ++i) {
|
||||
// char bits = levelChunk->updateMap[i];
|
||||
// t += (bits != 0);
|
||||
// int xx = x * 16 + i%16;
|
||||
// int zz = z * 16 + i/16;
|
||||
// if (dbg && xx == 125 && zz == 152) {
|
||||
// LOGI("xz: %d, %d: %d\n", xx, zz, bits);
|
||||
// for (int j = 0; j < 8; ++j) {
|
||||
// if (bits & (1 << j)) {
|
||||
// LOGI("%d - %d\n", j << 4, ((j+1) << 4) - 1);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//
|
||||
// Convert LevelChunks here if necessary
|
||||
//
|
||||
//LOGI("level version: %d: upd: %d - (%d, %d)\n", loadedStorageVersion, t, x, z);
|
||||
bool changed = false;
|
||||
|
||||
// Loaded level has old Cloth types (one Tile* per color)
|
||||
|
|
@ -511,104 +531,39 @@ LevelChunk* ExternalFileLevelStorage::load(Level* level, int x, int z)
|
|||
changed |= LevelConverters::v1_ClothIdToClothData(levelChunk);
|
||||
|
||||
// Loaded level is newer than our level - replace all unavailable block types
|
||||
//if (loadedStorageVersion > SharedConstants::StorageVersion)
|
||||
changed |= LevelConverters::ReplaceUnavailableBlocks(levelChunk);
|
||||
changed |= LevelConverters::ReplaceUnavailableBlocks(levelChunk);
|
||||
|
||||
levelChunk->recalcHeightmap();
|
||||
levelChunk->unsaved = changed;
|
||||
levelChunk->terrainPopulated = true;
|
||||
levelChunk->terrainPopulated = populated;
|
||||
levelChunk->createdFromSave = true;
|
||||
|
||||
return levelChunk;
|
||||
}
|
||||
|
||||
void ExternalFileLevelStorage::saveEntities( Level* level, LevelChunk* levelChunk )
|
||||
{
|
||||
lastSavedEntitiesTick = tickCount;
|
||||
int count = 0;
|
||||
float st = getTimeS();
|
||||
|
||||
// Version 1: Save ALL Entities for all chunks in one structure
|
||||
EntityList& entities = level->entities;
|
||||
|
||||
ListTag* entityTags = new ListTag();
|
||||
for (unsigned int i = 0; i < entities.size(); ++i) {
|
||||
Entity* e = entities[i];
|
||||
|
||||
CompoundTag* tag = new CompoundTag();
|
||||
if (e->save(tag)) {
|
||||
count++;
|
||||
entityTags->add(tag);
|
||||
} else
|
||||
delete tag;
|
||||
}
|
||||
|
||||
// Version 1: Save ALL TileEntities for all chunks in one structure
|
||||
TileEntityList& tileEntities = level->tileEntities;
|
||||
//TileEntityList keep, dontKeep;
|
||||
//partitionTileEntities
|
||||
|
||||
ListTag* tileEntityTags = new ListTag();
|
||||
for (unsigned int i = 0; i < tileEntities.size(); ++i) {
|
||||
TileEntity* e = tileEntities[i];
|
||||
if (!e->shouldSave()) continue;
|
||||
|
||||
CompoundTag* tag = new CompoundTag();
|
||||
if (e->save(tag)) {
|
||||
count++;
|
||||
tileEntityTags->add(tag);
|
||||
} else
|
||||
delete tag;
|
||||
}
|
||||
|
||||
CompoundTag base;
|
||||
base.put("Entities", entityTags);
|
||||
base.put("TileEntities", tileEntityTags);
|
||||
|
||||
RakNet::BitStream stream;
|
||||
RakDataOutput dos(stream);
|
||||
NbtIo::write(&base, &dos);
|
||||
int numBytes = stream.GetNumberOfBytesUsed();
|
||||
|
||||
FILE* fp = fopen((levelPath + "/entities.dat").c_str(), "wb");
|
||||
if (fp) {
|
||||
int version = 1;
|
||||
fwrite("ENT\0", 1, 4, fp);
|
||||
fwrite(&version, sizeof(int), 1, fp);
|
||||
fwrite(&numBytes, sizeof(int), 1, fp);
|
||||
fwrite(stream.GetData(), 1, numBytes, fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
base.deleteChildren();
|
||||
|
||||
float tt = getTimeS() - st;
|
||||
LOGI("Time to save %d entities: %f s. Size: %d bytes\n", count, tt, numBytes);
|
||||
}
|
||||
|
||||
// Imports the global entities.dat that older versions of the game wrote.
|
||||
void ExternalFileLevelStorage::loadEntities(Level* level, LevelChunk* chunk) {
|
||||
lastSavedEntitiesTick = tickCount;
|
||||
FILE* fp = fopen((levelPath + "/entities.dat").c_str(), "rb");
|
||||
if (fp) {
|
||||
char header[5];
|
||||
int version, numBytes;
|
||||
fread(header, 1, 4, fp);
|
||||
fread(&version, sizeof(int), 1, fp);
|
||||
fread(&numBytes, sizeof(int), 1, fp);
|
||||
const std::string path = levelPath + "/entities.dat";
|
||||
FILE* fp = fopen(path.c_str(), "rb");
|
||||
if (!fp) return;
|
||||
|
||||
int left = getRemainingFileSize(fp);
|
||||
if (numBytes <= left && numBytes > 0) {
|
||||
unsigned char* buf = new unsigned char[numBytes];
|
||||
char header[5] = {0};
|
||||
int version = 0, numBytes = 0;
|
||||
fread(header, 1, 4, fp);
|
||||
fread(&version, sizeof(int), 1, fp);
|
||||
fread(&numBytes, sizeof(int), 1, fp);
|
||||
|
||||
fread(buf, 1, numBytes, fp);
|
||||
int left = getRemainingFileSize(fp);
|
||||
if (numBytes <= left && numBytes > 0) {
|
||||
unsigned char* buf = new unsigned char[numBytes];
|
||||
|
||||
RakNet::BitStream stream(buf, numBytes, false);
|
||||
RakDataInput dis(stream);
|
||||
fread(buf, 1, numBytes, fp);
|
||||
|
||||
CompoundTag* tag = NbtIo::read(&dis);
|
||||
//
|
||||
// Read Entity:es
|
||||
//
|
||||
RakNet::BitStream stream(buf, numBytes, false);
|
||||
RakDataInput dis(stream);
|
||||
|
||||
CompoundTag* tag = NbtIo::read(&dis);
|
||||
if (tag) {
|
||||
if (tag->contains("Entities", Tag::TAG_List)) {
|
||||
ListTag* entityTags = tag->getList("Entities");
|
||||
for (int i = 0; i < entityTags->size(); ++i) {
|
||||
|
|
@ -623,9 +578,6 @@ void ExternalFileLevelStorage::loadEntities(Level* level, LevelChunk* chunk) {
|
|||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// Read TileEntity:s
|
||||
//
|
||||
if (tag->contains("TileEntities", Tag::TAG_List)) {
|
||||
ListTag* tileEntityTags = tag->getList("TileEntities");
|
||||
for (int i = 0; i < tileEntityTags->size(); ++i) {
|
||||
|
|
@ -636,67 +588,35 @@ void ExternalFileLevelStorage::loadEntities(Level* level, LevelChunk* chunk) {
|
|||
}
|
||||
CompoundTag* et = (CompoundTag*)_et;
|
||||
if (TileEntity* e = TileEntity::loadStatic(et)) {
|
||||
|
||||
LevelChunk* chunk = level->getChunkAt(e->x, e->z);
|
||||
if (chunk && !chunk->hasTileEntityAt(e)) {
|
||||
LOGI("Adding TileEntity %d to %d, %d, %d\n", e->type, e->x, e->y, e->z);
|
||||
chunk->addTileEntity(e);
|
||||
LevelChunk* lc = level->getChunkAt(e->x, e->z);
|
||||
if (lc && !lc->hasTileEntityAt(e)) {
|
||||
lc->addTileEntity(e);
|
||||
} else {
|
||||
if (!chunk)
|
||||
LOGE("Couldn't find chunk at %d, %d to add %d\n", e->x, e->z, e->type);
|
||||
else
|
||||
LOGE("Already have TileEntity at %d, %d to add %d\n", e->x, e->z, e->type);
|
||||
delete e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tag->deleteChildren();
|
||||
delete tag;
|
||||
|
||||
delete[] buf;
|
||||
}
|
||||
LOGI("header: %s, version: %d, bytes: %d (remaining: %d)\n", header, version, numBytes, left);
|
||||
|
||||
//fread(stream.GetData(), 1, numBytes, fp);
|
||||
fclose(fp);
|
||||
delete[] buf;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
// Entities are stored per chunk from now on.
|
||||
const std::string migrated = path + ".migrated";
|
||||
remove(migrated.c_str());
|
||||
rename(path.c_str(), migrated.c_str());
|
||||
}
|
||||
|
||||
void ExternalFileLevelStorage::saveGame(Level* level) {
|
||||
saveEntities(level, NULL);
|
||||
}
|
||||
|
||||
int ExternalFileLevelStorage::savePendingUnsavedChunks( int maxCount ) {
|
||||
if (maxCount < 0)
|
||||
maxCount = unsavedChunkList.size();
|
||||
|
||||
int count = 0;
|
||||
while (++count <= maxCount && !unsavedChunkList.empty()) {
|
||||
|
||||
UnsavedChunkList::iterator it = unsavedChunkList.begin();
|
||||
UnsavedChunkList::iterator remove = unsavedChunkList.begin();
|
||||
UnsavedLevelChunk* oldest = &(*it);
|
||||
|
||||
for ( ; it != unsavedChunkList.end(); ++it) {
|
||||
if ((*it).addedToList < oldest->addedToList) {
|
||||
oldest = &(*it);
|
||||
remove = it;
|
||||
}
|
||||
}
|
||||
LevelChunk* chunk = oldest->chunk;
|
||||
unsavedChunkList.erase(remove);
|
||||
|
||||
save(level, chunk);
|
||||
}
|
||||
return count;
|
||||
if (level && level->getChunkSource())
|
||||
level->getChunkSource()->saveAll(true);
|
||||
}
|
||||
|
||||
void ExternalFileLevelStorage::saveAll( Level* level, std::vector<LevelChunk*>& levelChunks ) {
|
||||
ChunkStorage::saveAll(level, levelChunks);
|
||||
int numChunks = savePendingUnsavedChunks(-1);
|
||||
LOGI("Saving %d additional chunks.\n", numChunks);
|
||||
}
|
||||
|
||||
#endif /*DEMO_MODE*/
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
//#include "com/mojang/nbt/CompoundTag.h"
|
||||
#include "LevelStorage.h"
|
||||
|
|
@ -16,16 +17,6 @@ class Player;
|
|||
class Dimension;
|
||||
class RegionFile;
|
||||
|
||||
|
||||
typedef struct UnsavedLevelChunk
|
||||
{
|
||||
int pos;
|
||||
RakNet::TimeMS addedToList;
|
||||
LevelChunk* chunk;
|
||||
} UnsavedLevelChunk;
|
||||
|
||||
typedef std::list<UnsavedLevelChunk> UnsavedChunkList;
|
||||
|
||||
/*public*/
|
||||
class ExternalFileLevelStorage:
|
||||
public LevelStorage,
|
||||
|
|
@ -54,16 +45,15 @@ public:
|
|||
static bool writeLevelData(const std::string& datFileName, LevelData& dest, const std::vector<Player*>* players);
|
||||
static void saveLevelData(const std::string& directory, LevelData& levelData, std::vector<Player*>* players);
|
||||
|
||||
int savePendingUnsavedChunks(int maxCount);
|
||||
|
||||
//
|
||||
// ChunkStorage methods
|
||||
//
|
||||
virtual LevelChunk* load(Level* level, int x, int z);
|
||||
void save(Level* level, LevelChunk* levelChunk);
|
||||
// @note, loadEntities and saveEntities dont use second parameter
|
||||
// Entities and tile entities are saved together with their chunk. This
|
||||
// only imports the old global "entities.dat" of worlds saved by older
|
||||
// versions (once).
|
||||
void loadEntities(Level* level, LevelChunk* levelChunk);
|
||||
void saveEntities(Level* level, LevelChunk* levelChunk);
|
||||
void saveGame(Level* level);
|
||||
void saveAll(Level* level, std::vector<LevelChunk*>& levelChunks);
|
||||
|
||||
|
|
@ -83,17 +73,20 @@ public:
|
|||
virtual void tick();
|
||||
virtual void flush() {}
|
||||
private:
|
||||
RegionFile* getRegion(int chunkX, int chunkZ);
|
||||
void closeRegions();
|
||||
|
||||
std::string levelId;
|
||||
std::string levelPath;
|
||||
LevelData* loadedLevelData;
|
||||
RegionFile* regionFile;
|
||||
RegionFile* entitiesFile;
|
||||
|
||||
// Open region files, least recently used first
|
||||
std::list<long long> regionOrder;
|
||||
std::map<long long, RegionFile*> regions;
|
||||
|
||||
Level* level;
|
||||
int tickCount;
|
||||
int loadedStorageVersion;
|
||||
UnsavedChunkList unsavedChunkList;
|
||||
int lastSavedEntitiesTick;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_STORAGE__ExternalFileLevelStorage_H__*/
|
||||
|
|
|
|||
|
|
@ -13,12 +13,27 @@ static void logAssert(int actual, int expected) {
|
|||
}
|
||||
}
|
||||
|
||||
RegionFile::RegionFile(const std::string& basePath)
|
||||
/*static*/
|
||||
std::string RegionFile::fileNameFor(const std::string& basePath, int regionX, int regionZ)
|
||||
{
|
||||
std::string name = basePath;
|
||||
name += "/";
|
||||
if (regionX == 0 && regionZ == 0) {
|
||||
name += REGION_DAT_NAME;
|
||||
} else {
|
||||
name += "r.";
|
||||
name += std::to_string(regionX);
|
||||
name += ".";
|
||||
name += std::to_string(regionZ);
|
||||
name += ".dat";
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
RegionFile::RegionFile(const std::string& basePath, int regionX, int regionZ)
|
||||
: file(NULL)
|
||||
{
|
||||
filename = basePath;
|
||||
filename += "/";
|
||||
filename += REGION_DAT_NAME;
|
||||
filename = fileNameFor(basePath, regionX, regionZ);
|
||||
|
||||
offsets = new int[SECTOR_INTS];
|
||||
|
||||
|
|
@ -96,6 +111,7 @@ void RegionFile::close()
|
|||
|
||||
bool RegionFile::readChunk(int x, int z, RakNet::BitStream** destChunkData)
|
||||
{
|
||||
if (!file) return false;
|
||||
int offset = offsets[x + z * SECTOR_COLS];
|
||||
|
||||
if (offset == 0)
|
||||
|
|
@ -127,6 +143,7 @@ bool RegionFile::readChunk(int x, int z, RakNet::BitStream** destChunkData)
|
|||
|
||||
bool RegionFile::writeChunk(int x, int z, RakNet::BitStream& chunkData)
|
||||
{
|
||||
if (!file) return false;
|
||||
int size = chunkData.GetNumberOfBytesUsed() + sizeof(int);
|
||||
|
||||
int offset = offsets[x + z * SECTOR_COLS];
|
||||
|
|
|
|||
|
|
@ -1,31 +1,38 @@
|
|||
#ifndef NET_MINECRAFT_WORLD_LEVEL_STORAGE__RegionFile_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_STORAGE__RegionFile_H__
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "../../../raknet/BitStream.h"
|
||||
|
||||
typedef std::map<int, bool> FreeSectorMap;
|
||||
|
||||
class RegionFile
|
||||
{
|
||||
public:
|
||||
RegionFile(const std::string& basePath);
|
||||
virtual ~RegionFile();
|
||||
|
||||
bool open();
|
||||
bool readChunk(int x, int z, RakNet::BitStream** destChunkData);
|
||||
bool writeChunk(int x, int z, RakNet::BitStream& chunkData);
|
||||
private:
|
||||
bool write(int sector, RakNet::BitStream& chunkData);
|
||||
void close();
|
||||
|
||||
FILE* file;
|
||||
std::string filename;
|
||||
int* offsets;
|
||||
int* emptyChunk;
|
||||
FreeSectorMap sectorFree;
|
||||
};
|
||||
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_STORAGE__RegionFile_H__*/
|
||||
#ifndef NET_MINECRAFT_WORLD_LEVEL_STORAGE__RegionFile_H__
|
||||
#define NET_MINECRAFT_WORLD_LEVEL_STORAGE__RegionFile_H__
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "../../../raknet/BitStream.h"
|
||||
|
||||
typedef std::map<int, bool> FreeSectorMap;
|
||||
|
||||
// One region holds the 32x32 chunks with (x >> 5, z >> 5) == (regionX, regionZ).
|
||||
// Region (0, 0) lives in the legacy "chunks.dat" so worlds created by the old
|
||||
// fixed-size versions keep loading.
|
||||
class RegionFile
|
||||
{
|
||||
public:
|
||||
RegionFile(const std::string& basePath, int regionX, int regionZ);
|
||||
virtual ~RegionFile();
|
||||
|
||||
bool open();
|
||||
void close();
|
||||
|
||||
// x and z are chunk coordinates local to the region (0..31)
|
||||
bool readChunk(int x, int z, RakNet::BitStream** destChunkData);
|
||||
bool writeChunk(int x, int z, RakNet::BitStream& chunkData);
|
||||
|
||||
static std::string fileNameFor(const std::string& basePath, int regionX, int regionZ);
|
||||
private:
|
||||
bool write(int sector, RakNet::BitStream& chunkData);
|
||||
|
||||
FILE* file;
|
||||
std::string filename;
|
||||
int* offsets;
|
||||
int* emptyChunk;
|
||||
FreeSectorMap sectorFree;
|
||||
};
|
||||
|
||||
|
||||
#endif /*NET_MINECRAFT_WORLD_LEVEL_STORAGE__RegionFile_H__*/
|
||||
|
|
|
|||
|
|
@ -512,16 +512,7 @@ float Tile::getBrightness( LevelSource* level, int x, int y, int z )
|
|||
bool Tile::shouldRenderFace( LevelSource* level, int x, int y, int z, int face )
|
||||
{
|
||||
if (face == 0 && y == -1) return false;
|
||||
// For fixed size worlds //@todo: external constants rather than magic numbers
|
||||
// @fixme @ahtung temp fix
|
||||
|
||||
auto xz = LevelConstants::CHUNK_CACHE_WIDTH == 16 ? 256 :
|
||||
LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH / (LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH / 256 / 2);
|
||||
|
||||
if (face == 2 && z == -1) return false;
|
||||
if (face == 3 && z == xz) return false;
|
||||
if (face == 4 && x == -1) return false;
|
||||
if (face == 5 && x == xz) return false;
|
||||
// The world is infinite, there is no edge whose outer faces could be skipped
|
||||
// Common
|
||||
if (face == 0 && yy0 > 0) return true;
|
||||
if (face == 1 && yy1 < 1) return true;
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ Vec3 NetherReactorTileEntity::getSpawnPosition( float minDistance, float varible
|
|||
void NetherReactorTileEntity::spawnEnemy() {
|
||||
Mob* mob = MobFactory::CreateMob(MobTypes::PigZombie, level);
|
||||
Vec3 enemyPosition = getSpawnPosition(3, 4, -1);
|
||||
while(enemyPosition.x < 0 || enemyPosition.z < 0 || enemyPosition.x >= LevelConstants::LEVEL_WIDTH || enemyPosition.z >= LevelConstants::LEVEL_DEPTH) {
|
||||
while(enemyPosition.x <= -LevelConstants::LEVEL_WIDTH || enemyPosition.z <= -LevelConstants::LEVEL_DEPTH || enemyPosition.x >= LevelConstants::LEVEL_WIDTH || enemyPosition.z >= LevelConstants::LEVEL_DEPTH) {
|
||||
enemyPosition = getSpawnPosition(3, 4, -1);
|
||||
}
|
||||
MobSpawner::addMob(level, mob, enemyPosition.x, enemyPosition.y, enemyPosition.z, 0, 0, true);
|
||||
|
|
@ -140,7 +140,7 @@ void NetherReactorTileEntity::spawnEnemy() {
|
|||
|
||||
void NetherReactorTileEntity::spawnItem() {
|
||||
Vec3 itemPosition= getSpawnPosition(3, 4, -1);
|
||||
while(itemPosition.x < 0 || itemPosition.z < 0 || itemPosition.x >= LevelConstants::LEVEL_WIDTH || itemPosition.z >= LevelConstants::LEVEL_DEPTH) {
|
||||
while(itemPosition.x <= -LevelConstants::LEVEL_WIDTH || itemPosition.z <= -LevelConstants::LEVEL_DEPTH || itemPosition.x >= LevelConstants::LEVEL_WIDTH || itemPosition.z >= LevelConstants::LEVEL_DEPTH) {
|
||||
itemPosition = getSpawnPosition(3, 4, -1);
|
||||
}
|
||||
ItemEntity* item = new ItemEntity(level, itemPosition.x, itemPosition.y, itemPosition.z, getSpawnItem());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue