2014-12-18 07:02:48 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
#include <gk/resource/ResourceHandler.hpp>
|
|
|
|
|
2020-02-15 22:48:56 +09:00
|
|
|
#include "EngineConfig.hpp"
|
2014-12-18 07:02:48 +01:00
|
|
|
#include "World.hpp"
|
|
|
|
|
2018-07-06 11:58:34 +02:00
|
|
|
bool World::isReloadRequested = false;
|
2018-06-30 05:07:57 +02:00
|
|
|
|
2020-01-16 11:59:14 +09:00
|
|
|
Chunk *World::getChunkAtBlockPos(int x, int y, int z) const {
|
|
|
|
return getChunk(
|
2020-02-24 00:32:17 +01:00
|
|
|
(x & -CHUNK_WIDTH) / CHUNK_WIDTH,
|
|
|
|
(y & -CHUNK_DEPTH) / CHUNK_DEPTH,
|
|
|
|
(z & -CHUNK_HEIGHT) / CHUNK_HEIGHT
|
2020-01-16 11:04:52 +09:00
|
|
|
);
|
2020-01-16 11:59:14 +09:00
|
|
|
}
|
2020-01-16 11:04:52 +09:00
|
|
|
|
2020-01-16 11:59:14 +09:00
|
|
|
BlockData *World::getBlockData(int x, int y, int z) const {
|
|
|
|
Chunk *chunk = getChunkAtBlockPos(x, y, z);
|
2020-01-16 10:07:01 +09:00
|
|
|
if (chunk)
|
2020-02-20 11:38:30 +01:00
|
|
|
return chunk->getBlockData(x & (CHUNK_WIDTH - 1), y & (CHUNK_DEPTH - 1), z & (CHUNK_HEIGHT - 1));
|
2020-01-16 10:07:01 +09:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:15:44 +09:00
|
|
|
BlockData *World::addBlockData(int x, int y, int z, int inventoryWidth, int inventoryHeight) const {
|
|
|
|
Chunk *chunk = getChunkAtBlockPos(x, y, z);
|
|
|
|
if (chunk)
|
2020-02-20 11:38:30 +01:00
|
|
|
return chunk->addBlockData(x & (CHUNK_WIDTH - 1), y & (CHUNK_DEPTH - 1), z & (CHUNK_HEIGHT - 1), inventoryWidth, inventoryHeight);
|
2020-02-07 23:15:44 +09:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-16 10:07:01 +09:00
|
|
|
u16 World::getBlock(int x, int y, int z) const {
|
2020-01-16 11:59:14 +09:00
|
|
|
Chunk *chunk = getChunkAtBlockPos(x, y, z);
|
2020-01-16 10:07:01 +09:00
|
|
|
if (chunk)
|
2020-02-20 11:38:30 +01:00
|
|
|
return chunk->getBlock(x & (CHUNK_WIDTH - 1), y & (CHUNK_DEPTH - 1), z & (CHUNK_HEIGHT - 1));
|
2020-01-16 10:07:01 +09:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void World::setBlock(int x, int y, int z, u16 id) const {
|
2020-01-16 11:59:14 +09:00
|
|
|
Chunk *chunk = getChunkAtBlockPos(x, y, z);
|
2020-01-16 10:07:01 +09:00
|
|
|
if (chunk)
|
2020-02-20 11:38:30 +01:00
|
|
|
chunk->setBlock(x & (CHUNK_WIDTH - 1), y & (CHUNK_DEPTH - 1), z & (CHUNK_HEIGHT - 1), id);
|
2020-01-16 10:07:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
u16 World::getData(int x, int y, int z) const {
|
2020-01-16 11:59:14 +09:00
|
|
|
Chunk *chunk = getChunkAtBlockPos(x, y, z);
|
2020-01-16 10:07:01 +09:00
|
|
|
if (chunk)
|
2020-02-20 11:38:30 +01:00
|
|
|
return chunk->getData(x & (CHUNK_WIDTH - 1), y & (CHUNK_DEPTH - 1), z & (CHUNK_HEIGHT - 1));
|
2020-01-16 10:07:01 +09:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-20 14:11:34 +09:00
|
|
|
void World::setData(int x, int y, int z, u16 data) const {
|
2020-01-16 11:59:14 +09:00
|
|
|
Chunk *chunk = getChunkAtBlockPos(x, y, z);
|
2020-01-16 10:07:01 +09:00
|
|
|
if (chunk)
|
2020-02-20 11:38:30 +01:00
|
|
|
chunk->setData(x & (CHUNK_WIDTH - 1), y & (CHUNK_DEPTH - 1), z & (CHUNK_HEIGHT - 1), data);
|
2020-01-16 10:07:01 +09:00
|
|
|
}
|