voxel long

This commit is contained in:
alexei-zebra 2024-01-09 18:58:58 +03:00
parent 3542a00445
commit 0175a8b76c
54 changed files with 615 additions and 593 deletions

View file

@ -302,7 +302,7 @@ void JObject::num(std::string key, float& dst) const {
dst = getNum(key, dst); dst = getNum(key, dst);
} }
void JObject::num(std::string key, u_char8& dst) const { void JObject::num(std::string key, u_char& dst) const {
dst = getInteger(key, dst); dst = getInteger(key, dst);
} }

View file

@ -86,7 +86,7 @@ namespace json {
void num(std::string key, uint& dst) const; void num(std::string key, uint& dst) const;
void num(std::string key, int64_t& dst) const; void num(std::string key, int64_t& dst) const;
void num(std::string key, uint64_t& dst) const; void num(std::string key, uint64_t& dst) const;
void num(std::string key, u_char8& dst) const; void num(std::string key, u_char& dst) const;
void num(std::string key, double& dst) const; void num(std::string key, double& dst) const;
JObject* obj(std::string key) const; JObject* obj(std::string key) const;
JArray* arr(std::string key) const; JArray* arr(std::string key) const;

View file

@ -16,7 +16,7 @@
#include <png.h> #include <png.h>
// returns 0 if all-right, 1 otherwise // returns 0 if all-right, 1 otherwise
int _png_write(const char* filename, u_int width, u_int height, const u_char8* data, bool alpha) { int _png_write(const char* filename, u_int width, u_int height, const u_char* data, bool alpha) {
u_int pixsize = alpha ? 4 : 3; u_int pixsize = alpha ? 4 : 3;
// Open file for writing (binary mode) // Open file for writing (binary mode)
@ -355,5 +355,5 @@ Texture* png::load_texture(std::string filename) {
} }
void png::write_image(std::string filename, const ImageData* image) { void png::write_image(std::string filename, const ImageData* image) {
_png_write(filename.c_str(), image->getWidth(), image->getHeight(), (const u_char8*)image->getData(), image->getFormat() == ImageFormat::rgba8888); _png_write(filename.c_str(), image->getWidth(), image->getHeight(), (const u_char*)image->getData(), image->getFormat() == ImageFormat::rgba8888);
} }

View file

@ -11,8 +11,8 @@ const int CHUNK_W = 16;
const int CHUNK_H = 256; const int CHUNK_H = 256;
const int CHUNK_D = 16; const int CHUNK_D = 16;
const uint VOXEL_USER_BITS = 8; const u_int VOXEL_USER_BITS = 8;
constexpr uint VOXEL_USER_BITS_OFFSET = sizeof(blockstate_t)*8-VOXEL_USER_BITS; constexpr u_int VOXEL_USER_BITS_OFFSET = sizeof(blockstate_t)*8-VOXEL_USER_BITS;
/* Chunk volume (count of voxels per Chunk) */ /* Chunk volume (count of voxels per Chunk) */
constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D); constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D);
@ -21,7 +21,7 @@ constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D);
const blockid_t BLOCK_VOID = std::numeric_limits<blockid_t>::max(); const blockid_t BLOCK_VOID = std::numeric_limits<blockid_t>::max();
const blockid_t MAX_BLOCKS = BLOCK_VOID; const blockid_t MAX_BLOCKS = BLOCK_VOID;
inline uint vox_index(int x, int y, int z, int w=CHUNK_W, int d=CHUNK_D) { inline u_int vox_index(int x, int y, int z, int w=CHUNK_W, int d=CHUNK_D) {
return (y * d + z) * w + x; return (y * d + z) * w + x;
} }

View file

@ -65,7 +65,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
} }
} }
Audio::initialize(); // Audio::initialize();
gui = std::make_unique<gui::GUI>(); gui = std::make_unique<gui::GUI>();
if (settings.ui.language == "auto") { if (settings.ui.language == "auto") {
settings.ui.language = langs::locale_by_envlocale(platform::detect_locale(), paths->getResources()); settings.ui.language = langs::locale_by_envlocale(platform::detect_locale(), paths->getResources());
@ -126,7 +126,7 @@ Engine::~Engine() {
scripting::close(); scripting::close();
screen = nullptr; screen = nullptr;
Audio::finalize(); // Audio::finalize();
std::cout << "-- shutting down" << std::endl; std::cout << "-- shutting down" << std::endl;
assets.reset(); assets.reset();

View file

@ -36,7 +36,7 @@ class Engine {
EnginePaths* paths; EnginePaths* paths;
std::unique_ptr<ResPaths> resPaths = nullptr; std::unique_ptr<ResPaths> resPaths = nullptr;
uint64_t frame = 0; u_long frame = 0;
double lastTime = 0.0; double lastTime = 0.0;
double delta = 0.0; double delta = 0.0;

View file

@ -53,7 +53,7 @@ void WorldConverter::convertNext() {
for (uint cx = 0; cx < REGION_SIZE; cx++) { for (uint cx = 0; cx < REGION_SIZE; cx++) {
int gx = cx + x * REGION_SIZE; int gx = cx + x * REGION_SIZE;
int gz = cz + z * REGION_SIZE; int gz = cz + z * REGION_SIZE;
std::unique_ptr<u_char8[]> data (wfile->getChunk(gx, gz)); std::unique_ptr<u_char[]> data (wfile->getChunk(gx, gz));
if (data == nullptr) if (data == nullptr)
continue; continue;
if (wfile->getVoxelRegionVersion(x, z) != REGION_FORMAT_VERSION) { if (wfile->getVoxelRegionVersion(x, z) != REGION_FORMAT_VERSION) {

View file

@ -34,7 +34,7 @@ regfile::regfile(fs::path filename) : file(filename) {
file.read(header, REGION_HEADER_SIZE); file.read(header, REGION_HEADER_SIZE);
// avoid of use strcmp_s // avoid of use strcmp_s
if (std::string(header, strlen(REGION_FORMAT_MAGIC)) != REGION_FORMAT_MAGIC) { if (std::string(header, std::strlen(REGION_FORMAT_MAGIC)) != REGION_FORMAT_MAGIC) {
throw std::runtime_error("invalid region file magic number"); throw std::runtime_error("invalid region file magic number");
} }
version = header[8]; version = header[8];
@ -45,7 +45,7 @@ regfile::regfile(fs::path filename) : file(filename) {
} }
WorldRegion::WorldRegion() { WorldRegion::WorldRegion() {
chunksData = new u_char8*[REGION_CHUNKS_COUNT]{}; chunksData = new u_char*[REGION_CHUNKS_COUNT]{};
sizes = new uint32_t[REGION_CHUNKS_COUNT]{}; sizes = new uint32_t[REGION_CHUNKS_COUNT]{};
} }
@ -64,7 +64,7 @@ bool WorldRegion::isUnsaved() const {
return unsaved; return unsaved;
} }
u_char8** WorldRegion::getChunks() const { u_char** WorldRegion::getChunks() const {
return chunksData; return chunksData;
} }
@ -72,14 +72,14 @@ uint32_t* WorldRegion::getSizes() const {
return sizes; return sizes;
} }
void WorldRegion::put(uint x, uint z, u_char8* data, uint32_t size) { void WorldRegion::put(uint x, uint z, u_char* data, uint32_t size) {
size_t chunk_index = z * REGION_SIZE + x; size_t chunk_index = z * REGION_SIZE + x;
delete[] chunksData[chunk_index]; delete[] chunksData[chunk_index];
chunksData[chunk_index] = data; chunksData[chunk_index] = data;
sizes[chunk_index] = size; sizes[chunk_index] = size;
} }
u_char8* WorldRegion::getChunkData(uint x, uint z) { u_char* WorldRegion::getChunkData(uint x, uint z) {
return chunksData[z * REGION_SIZE + x]; return chunksData[z * REGION_SIZE + x];
} }
@ -91,7 +91,7 @@ WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
: directory(directory), : directory(directory),
generatorTestMode(settings.generatorTestMode), generatorTestMode(settings.generatorTestMode),
doWriteLights(settings.doWriteLights) { doWriteLights(settings.doWriteLights) {
compressionBuffer.reset(new u_char8[CHUNK_DATA_LEN * 2]); compressionBuffer.reset(new u_char[CHUNK_DATA_LEN * 2]);
} }
WorldFiles::~WorldFiles(){ WorldFiles::~WorldFiles(){
@ -113,20 +113,20 @@ WorldRegion* WorldFiles::getOrCreateRegion(regionsmap& regions, int x, int z) {
return region; return region;
} }
u_char8* WorldFiles::compress(const u_char8* src, size_t srclen, size_t& len) { u_char* WorldFiles::compress(const u_char* src, size_t srclen, size_t& len) {
u_char8* buffer = this->compressionBuffer.get(); u_char* buffer = this->compressionBuffer.get();
len = extrle::encode(src, srclen, buffer); len = rle::encode((voxel_t *)src, srclen, (voxel_t *)buffer);
u_char8* data = new u_char8[len]; u_char* data = new u_char[len];
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
data[i] = buffer[i]; data[i] = buffer[i];
} }
return data; return data;
} }
u_char8* WorldFiles::decompress(const u_char8* src, size_t srclen, size_t dstlen) { u_char* WorldFiles::decompress(const u_char* src, size_t srclen, size_t dstlen) {
u_char8* decompressed = new u_char8[dstlen]; u_char* decompressed = new u_char[dstlen];
extrle::decode(src, srclen, decompressed); rle::decode((voxel_t *)src, srclen, (voxel_t *)decompressed);
return decompressed; return decompressed;
} }
@ -160,7 +160,7 @@ int WorldFiles::getVoxelRegionsVersion() {
* @param x chunk.x * @param x chunk.x
* @param z chunk.z * @param z chunk.z
*/ */
void WorldFiles::put(int x, int z, const u_char8* voxelData) { void WorldFiles::put(int x, int z, const u_char* voxelData) {
int regionX = floordiv(x, REGION_SIZE); int regionX = floordiv(x, REGION_SIZE);
int regionZ = floordiv(z, REGION_SIZE); int regionZ = floordiv(z, REGION_SIZE);
int localX = x - (regionX * REGION_SIZE); int localX = x - (regionX * REGION_SIZE);
@ -170,7 +170,7 @@ void WorldFiles::put(int x, int z, const u_char8* voxelData) {
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ); WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
region->setUnsaved(true); region->setUnsaved(true);
size_t compressedSize; size_t compressedSize;
u_char8* data = compress(voxelData, CHUNK_DATA_LEN, compressedSize); u_char* data = compress(voxelData, CHUNK_DATA_LEN, compressedSize);
region->put(localX, localZ, data, compressedSize); region->put(localX, localZ, data, compressedSize);
} }
} }
@ -188,8 +188,8 @@ void WorldFiles::put(Chunk* chunk){
/* Writing Voxels */ { /* Writing Voxels */ {
size_t compressedSize; size_t compressedSize;
std::unique_ptr<u_char8[]> chunk_data (chunk->encode()); std::unique_ptr<u_char[]> chunk_data (chunk->encode());
u_char8* data = compress(chunk_data.get(), CHUNK_DATA_LEN, compressedSize); u_char* data = compress(chunk_data.get(), CHUNK_DATA_LEN, compressedSize);
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ); WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
region->setUnsaved(true); region->setUnsaved(true);
@ -197,8 +197,8 @@ void WorldFiles::put(Chunk* chunk){
} }
if (doWriteLights && chunk->isLighted()) { if (doWriteLights && chunk->isLighted()) {
size_t compressedSize; size_t compressedSize;
std::unique_ptr<u_char8[]> light_data (chunk->lightmap->encode()); std::unique_ptr<u_char[]> light_data (chunk->lightmap->encode());
u_char8* data = compress(light_data.get(), LIGHTMAP_DATA_LEN, compressedSize); u_char* data = compress(light_data.get(), LIGHTMAP_DATA_LEN, compressedSize);
WorldRegion* region = getOrCreateRegion(lights, regionX, regionZ); WorldRegion* region = getOrCreateRegion(lights, regionX, regionZ);
region->setUnsaved(true); region->setUnsaved(true);
@ -256,20 +256,20 @@ fs::path WorldFiles::getPacksFile() const {
return directory/fs::path("packs.list"); return directory/fs::path("packs.list");
} }
u_char8* WorldFiles::getChunk(int x, int z){ u_char* WorldFiles::getChunk(int x, int z){
return getData(regions, getRegionsFolder(), x, z, REGION_LAYER_VOXELS); return getData(regions, getRegionsFolder(), x, z, REGION_LAYER_VOXELS);
} }
/* Get cached lights for chunk at x,z /* Get cached lights for chunk at x,z
* @return lights data or nullptr */ * @return lights data or nullptr */
light_t* WorldFiles::getLights(int x, int z) { light_t* WorldFiles::getLights(int x, int z) {
std::unique_ptr<u_char8> data (getData(lights, getLightsFolder(), x, z, REGION_LAYER_LIGHTS)); std::unique_ptr<u_char> data (getData(lights, getLightsFolder(), x, z, REGION_LAYER_LIGHTS));
if (data == nullptr) if (data == nullptr)
return nullptr; return nullptr;
return Lightmap::decode(data.get()); return Lightmap::decode(data.get());
} }
u_char8* WorldFiles::getData(regionsmap& regions, const fs::path& folder, u_char* WorldFiles::getData(regionsmap& regions, const fs::path& folder,
int x, int z, int layer) { int x, int z, int layer) {
int regionX = floordiv(x, REGION_SIZE); int regionX = floordiv(x, REGION_SIZE);
int regionZ = floordiv(z, REGION_SIZE); int regionZ = floordiv(z, REGION_SIZE);
@ -278,7 +278,7 @@ u_char8* WorldFiles::getData(regionsmap& regions, const fs::path& folder,
int localZ = z - (regionZ * REGION_SIZE); int localZ = z - (regionZ * REGION_SIZE);
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ); WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
u_char8* data = region->getChunkData(localX, localZ); u_char* data = region->getChunkData(localX, localZ);
if (data == nullptr) { if (data == nullptr) {
uint32_t size; uint32_t size;
data = readChunkData(x, z, size, folder, layer); data = readChunkData(x, z, size, folder, layer);
@ -312,7 +312,7 @@ regfile* WorldFiles::getRegFile(glm::ivec3 coord, const fs::path& folder) {
return openRegFiles[coord].get(); return openRegFiles[coord].get();
} }
u_char8* WorldFiles::readChunkData(int x, u_char* WorldFiles::readChunkData(int x,
int z, int z,
uint32_t& length, uint32_t& length,
fs::path folder, fs::path folder,
@ -339,14 +339,14 @@ u_char8* WorldFiles::readChunkData(int x,
uint32_t offset; uint32_t offset;
file.seekg(table_offset + chunkIndex * 4); file.seekg(table_offset + chunkIndex * 4);
file.read((char*)(&offset), 4); file.read((char*)(&offset), 4);
offset = dataio::read_int32_big((const u_char8*)(&offset), 0); offset = dataio::read_int32_big((const u_char*)(&offset), 0);
if (offset == 0){ if (offset == 0){
return nullptr; return nullptr;
} }
file.seekg(offset); file.seekg(offset);
file.read((char*)(&offset), 4); file.read((char*)(&offset), 4);
length = dataio::read_int32_big((const u_char8*)(&offset), 0); length = dataio::read_int32_big((const u_char*)(&offset), 0);
u_char8* data = new u_char8[length]; u_char* data = new u_char[length];
file.read((char*)data, length); file.read((char*)data, length);
if (data == nullptr) { if (data == nullptr) {
std::cerr << "ERROR: failed to read data of chunk x("<< x <<"), z("<< z <<")" << std::endl; std::cerr << "ERROR: failed to read data of chunk x("<< x <<"), z("<< z <<")" << std::endl;
@ -359,7 +359,7 @@ u_char8* WorldFiles::readChunkData(int x,
* (see REGION_LAYER_* constants) * (see REGION_LAYER_* constants)
*/ */
void WorldFiles::fetchChunks(WorldRegion* region, int x, int z, fs::path folder, int layer) { void WorldFiles::fetchChunks(WorldRegion* region, int x, int z, fs::path folder, int layer) {
u_char8** chunks = region->getChunks(); u_char** chunks = region->getChunks();
uint32_t* sizes = region->getSizes(); uint32_t* sizes = region->getSizes();
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
@ -396,18 +396,18 @@ void WorldFiles::writeRegion(int x, int z, WorldRegion* entry, fs::path folder,
char intbuf[4]{}; char intbuf[4]{};
uint offsets[REGION_CHUNKS_COUNT]{}; uint offsets[REGION_CHUNKS_COUNT]{};
u_char8** region = entry->getChunks(); u_char** region = entry->getChunks();
uint32_t* sizes = entry->getSizes(); uint32_t* sizes = entry->getSizes();
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
u_char8* chunk = region[i]; u_char* chunk = region[i];
if (chunk == nullptr){ if (chunk == nullptr){
offsets[i] = 0; offsets[i] = 0;
} else { } else {
offsets[i] = offset; offsets[i] = offset;
size_t compressedSize = sizes[i]; size_t compressedSize = sizes[i];
dataio::write_int32_big(compressedSize, (u_char8*)intbuf, 0); dataio::write_int32_big(compressedSize, (u_char*)intbuf, 0);
offset += 4 + compressedSize; offset += 4 + compressedSize;
file.write(intbuf, 4); file.write(intbuf, 4);
@ -415,7 +415,7 @@ void WorldFiles::writeRegion(int x, int z, WorldRegion* entry, fs::path folder,
} }
} }
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
dataio::write_int32_big(offsets[i], (u_char8*)intbuf, 0); dataio::write_int32_big(offsets[i], (u_char*)intbuf, 0);
file.write(intbuf, 4); file.write(intbuf, 4);
} }
} }

View file

@ -41,21 +41,21 @@ public:
}; };
class WorldRegion { class WorldRegion {
u_char8** chunksData; u_char** chunksData;
uint32_t* sizes; uint32_t* sizes;
bool unsaved = false; bool unsaved = false;
public: public:
WorldRegion(); WorldRegion();
~WorldRegion(); ~WorldRegion();
void put(uint x, uint z, u_char8* data, uint32_t size); void put(uint x, uint z, u_char* data, uint32_t size);
u_char8* getChunkData(uint x, uint z); u_char* getChunkData(uint x, uint z);
uint getChunkDataSize(uint x, uint z); uint getChunkDataSize(uint x, uint z);
void setUnsaved(bool unsaved); void setUnsaved(bool unsaved);
bool isUnsaved() const; bool isUnsaved() const;
u_char8** getChunks() const; u_char** getChunks() const;
uint32_t* getSizes() const; uint32_t* getSizes() const;
}; };
@ -89,16 +89,16 @@ class WorldFiles {
@param src source buffer @param src source buffer
@param srclen length of source buffer @param srclen length of source buffer
@param len (out argument) length of result buffer */ @param len (out argument) length of result buffer */
u_char8* compress(const u_char8* src, size_t srclen, size_t& len); u_char* compress(const u_char* src, size_t srclen, size_t& len);
/* Decompress buffer with extrle /* Decompress buffer with extrle
@param src compressed buffer @param src compressed buffer
@param srclen length of compressed buffer @param srclen length of compressed buffer
@param dstlen max expected length of source buffer @param dstlen max expected length of source buffer
*/ */
u_char8* decompress(const u_char8* src, size_t srclen, size_t dstlen); u_char* decompress(const u_char* src, size_t srclen, size_t dstlen);
u_char8* readChunkData(int x, int y, u_char* readChunkData(int x, int y,
uint32_t& length, uint32_t& length,
std::filesystem::path folder, std::filesystem::path folder,
int layer); int layer);
@ -108,7 +108,7 @@ class WorldFiles {
void writeRegions(regionsmap& regions, void writeRegions(regionsmap& regions,
const std::filesystem::path& folder, int layer); const std::filesystem::path& folder, int layer);
u_char8* getData(regionsmap& regions, u_char* getData(regionsmap& regions,
const std::filesystem::path& folder, const std::filesystem::path& folder,
int x, int z, int layer); int x, int z, int layer);
@ -121,7 +121,7 @@ public:
regionsmap regions; regionsmap regions;
regionsmap lights; regionsmap lights;
std::filesystem::path directory; std::filesystem::path directory;
std::unique_ptr<u_char8[]> compressionBuffer; std::unique_ptr<u_char[]> compressionBuffer;
bool generatorTestMode; bool generatorTestMode;
bool doWriteLights; bool doWriteLights;
@ -129,12 +129,12 @@ public:
~WorldFiles(); ~WorldFiles();
void put(Chunk* chunk); void put(Chunk* chunk);
void put(int x, int z, const u_char8* voxelData); void put(int x, int z, const u_char* voxelData);
int getVoxelRegionVersion(int x, int z); int getVoxelRegionVersion(int x, int z);
int getVoxelRegionsVersion(); int getVoxelRegionsVersion();
u_char8* getChunk(int x, int z); u_char* getChunk(int x, int z);
light_t* getLights(int x, int z); light_t* getLights(int x, int z);
bool readWorldInfo(World* world); bool readWorldInfo(World* world);

View file

@ -4,7 +4,7 @@
#include <limits> #include <limits>
#include <stdexcept> #include <stdexcept>
void BinaryWriter::put(u_char8 b) { void BinaryWriter::put(u_char b) {
buffer.push_back(b); buffer.push_back(b);
} }
@ -22,7 +22,7 @@ void BinaryWriter::put(const std::string& s) {
throw std::domain_error("length > INT16_MAX"); throw std::domain_error("length > INT16_MAX");
} }
putInt16(len); putInt16(len);
put((const u_char8*)s.data(), len); put((const u_char*)s.data(), len);
} }
void BinaryWriter::putShortStr(const std::string& s) { void BinaryWriter::putShortStr(const std::string& s) {
@ -31,10 +31,10 @@ void BinaryWriter::putShortStr(const std::string& s) {
throw std::domain_error("length > 255"); throw std::domain_error("length > 255");
} }
put(len); put(len);
put((const u_char8*)s.data(), len); put((const u_char*)s.data(), len);
} }
void BinaryWriter::put(const u_char8* arr, size_t size) { void BinaryWriter::put(const u_char* arr, size_t size) {
buffer.reserve(buffer.size() + size); buffer.reserve(buffer.size() + size);
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
buffer.push_back(arr[i]); buffer.push_back(arr[i]);
@ -75,7 +75,7 @@ void BinaryWriter::putFloat32(float val) {
putInt32(value.vali32); putInt32(value.vali32);
} }
BinaryReader::BinaryReader(const u_char8* data, size_t size) BinaryReader::BinaryReader(const u_char* data, size_t size)
: data(data), size(size), pos(0) { : data(data), size(size), pos(0) {
} }
@ -84,14 +84,14 @@ void BinaryReader::checkMagic(const char* data, size_t size) {
throw std::runtime_error("invalid magic number"); throw std::runtime_error("invalid magic number");
} }
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
if (this->data[pos + i] != (u_char8)data[i]){ if (this->data[pos + i] != (u_char)data[i]){
throw std::runtime_error("invalid magic number"); throw std::runtime_error("invalid magic number");
} }
} }
pos += size; pos += size;
} }
u_char8 BinaryReader::get() { u_char BinaryReader::get() {
if (pos == size) { if (pos == size) {
throw std::underflow_error("buffer underflow"); throw std::underflow_error("buffer underflow");
} }
@ -152,7 +152,7 @@ std::string BinaryReader::getString() {
} }
std::string BinaryReader::getShortString() { std::string BinaryReader::getShortString() {
u_char8 length = get(); u_char length = get();
if (pos+length > size) { if (pos+length > size) {
throw std::underflow_error("unexpected end"); throw std::underflow_error("unexpected end");
} }

View file

@ -6,35 +6,35 @@
#include "../typedefs.h" #include "../typedefs.h"
class BinaryWriter { class BinaryWriter {
std::vector<u_char8> buffer; std::vector<u_char> buffer;
public: public:
void put(u_char8 b); void put(u_char b);
void putCStr(const char* str); void putCStr(const char* str);
void putInt16(int16_t val); void putInt16(int16_t val);
void putInt32(int32_t val); void putInt32(int32_t val);
void putInt64(int64_t val); void putInt64(int64_t val);
void putFloat32(float val); void putFloat32(float val);
void put(const std::string& s); void put(const std::string& s);
void put(const u_char8* arr, size_t size); void put(const u_char* arr, size_t size);
void putShortStr(const std::string& s); void putShortStr(const std::string& s);
inline size_t size() const { inline size_t size() const {
return buffer.size(); return buffer.size();
} }
inline const u_char8* data() const { inline const u_char* data() const {
return buffer.data(); return buffer.data();
} }
}; };
class BinaryReader { class BinaryReader {
const u_char8* data; const u_char* data;
size_t size; size_t size;
size_t pos; size_t pos;
public: public:
BinaryReader(const u_char8* data, size_t size); BinaryReader(const u_char* data, size_t size);
void checkMagic(const char* data, size_t size); void checkMagic(const char* data, size_t size);
u_char8 get(); u_char get();
int16_t getInt16(); int16_t getInt16();
int32_t getInt32(); int32_t getInt32();
int64_t getInt64(); int64_t getInt64();

View file

@ -1,90 +1,93 @@
#include "rle.h" #include "rle.h"
// template <class T>
// size_t rle::decode(const T* src, size_t srclen, T* dst) {
// size_t offset = 0;
// for (size_t i = 0; i < srclen;) {
// T len = src[i++];
// T c = src[i++];
// for (size_t j = 0; j <= len; j++) {
// dst[offset++] = c;
// }
// }
// return offset;
// }
size_t rle::decode(const u_char8* src, size_t srclen, u_char8* dst) { // template <class T>
size_t offset = 0; // size_t rle::encode(const T* src, size_t srclen, T* dst) {
for (size_t i = 0; i < srclen;) { // if (srclen == 0) {
u_char8 len = src[i++]; // return 0;
u_char8 c = src[i++]; // }
for (size_t j = 0; j <= len; j++) { // size_t offset = 0;
dst[offset++] = c; // u_int counter = 0;
} // T c = src[0];
} // for (size_t i = 1; i < srclen; i++) {
return offset; // T cnext = src[i];
} // if (cnext != c || counter == 255) {
// dst[offset++] = counter;
size_t rle::encode(const u_char8* src, size_t srclen, u_char8* dst) { // dst[offset++] = c;
if (srclen == 0) { // c = cnext;
return 0; // counter = 0;
} // }
size_t offset = 0; // else {
u_char8 counter = 0; // counter++;
u_char8 c = src[0]; // }
for (size_t i = 1; i < srclen; i++) { // }
u_char8 cnext = src[i]; // dst[offset++] = counter;
if (cnext != c || counter == 255) { // dst[offset++] = c;
dst[offset++] = counter; // return offset;
dst[offset++] = c; // }
c = cnext;
counter = 0;
}
else {
counter++;
}
}
dst[offset++] = counter;
dst[offset++] = c;
return offset;
}
size_t extrle::decode(const u_char8* src, size_t srclen, u_char8* dst) { // template <class T>
size_t offset = 0; // size_t extrle::decode(const T* src, size_t srclen, T* dst) {
for (size_t i = 0; i < srclen;) { // size_t offset = 0;
uint len = src[i++]; // for (size_t i = 0; i < srclen;) {
if (len & 0x80) { // T len = src[i++];
len &= 0x7F; // if (len & 0x80) {
len |= ((uint)src[i++]) << 7; // len &= 0x7F;
} // len |= ((T)src[i++]) << 7;
u_char8 c = src[i++]; // }
for (size_t j = 0; j <= len; j++) { // T c = src[i++];
dst[offset++] = c; // for (size_t j = 0; j <= len; j++) {
} // dst[offset++] = c;
} // }
return offset; // }
} // return offset;
// }
size_t extrle::encode(const u_char8* src, size_t srclen, u_char8* dst) { // template <class T>
if (srclen == 0) { // size_t extrle::encode(const T* src, size_t srclen, T* dst) {
return 0; // if (srclen == 0) {
} // return 0;
size_t offset = 0; // }
uint counter = 0; // size_t offset = 0;
u_char8 c = src[0]; // u_int counter = 0;
for (size_t i = 1; i < srclen; i++) { // T c = src[0];
u_char8 cnext = src[i]; // for (size_t i = 1; i < srclen; i++) {
if (cnext != c || counter == max_sequence) { // T cnext = src[i];
if (counter >= 0x80) { // if (cnext != c || counter == max_sequence) {
dst[offset++] = 0x80 | (counter & 0x7F); // if (counter >= 0x80) {
dst[offset++] = counter >> 7; // dst[offset++] = 0x80 | (counter & 0x7F);
} // dst[offset++] = counter >> 7;
else { // }
dst[offset++] = counter; // else {
} // dst[offset++] = counter;
dst[offset++] = c; // }
c = cnext; // dst[offset++] = c;
counter = 0; // c = cnext;
} // counter = 0;
else { // }
counter++; // else {
} // counter++;
} // }
if (counter >= 0x80) { // }
dst[offset++] = 0x80 | (counter & 0x7F); // if (counter >= 0x80) {
dst[offset++] = counter >> 7; // dst[offset++] = 0x80 | (counter & 0x7F);
} // dst[offset++] = counter >> 7;
else { // }
dst[offset++] = counter; // else {
} // dst[offset++] = counter;
dst[offset++] = c; // }
return offset; // dst[offset++] = c;
} // return offset;
// }

View file

@ -4,14 +4,44 @@
#include "../typedefs.h" #include "../typedefs.h"
namespace rle { namespace rle {
size_t encode(const u_char8* src, size_t length, u_char8* dst);
size_t decode(const u_char8* src, size_t length, u_char8* dst);
}
namespace extrle { template <class T>
constexpr uint max_sequence = 0x7FFF; T decode(const T* src, T srclen, T* dst) {
size_t encode(const u_char8* src, size_t length, u_char8* dst); T offset = 0;
size_t decode(const u_char8* src, size_t length, u_char8* dst); for (T i = 0; i < srclen / sizeof(T);) {
T len = src[i++];
T c = src[i++];
for (T j = 0; j <= len; j++) {
dst[offset++] = c;
}
}
return offset*sizeof(T);
}
template <class T>
T encode(const T* src, T srclen, T* dst) {
if (srclen == 0) {
return 0;
}
T offset = 0;
u_int counter = 0;
T c = src[0];
for (T i = 1; i < srclen / sizeof(T); i++) {
T cnext = src[i];
if (cnext != c || counter == 255) {
dst[offset++] = counter;
dst[offset++] = c;
c = cnext;
counter = 0;
}
else {
counter++;
}
}
dst[offset++] = counter;
dst[offset++] = c;
return offset*sizeof(T);
}
} }
#endif // FILES_RLE_H_ #endif // FILES_RLE_H_

View file

@ -125,7 +125,7 @@ HudRenderer::HudRenderer(Engine* engine,
panel->add(shared_ptr<Label>(create_label([this](){ panel->add(shared_ptr<Label>(create_label([this](){
auto player = this->level->player; auto player = this->level->player;
auto indices = this->level->content->indices; auto indices = this->level->content->indices;
auto str_ = L"dir: "+std::to_wstring(player->selectedVoxel.getDir()) + L" signal: "+std::to_wstring(player->selectedVoxel.getSig()); auto str_ = L"dir: "+std::to_wstring(player->selectedVoxel.dir) + L" signal: "+std::to_wstring(player->selectedVoxel.signal);
return str_; return str_;
}))); })));
panel->add(shared_ptr<Label>(create_label([this](){ panel->add(shared_ptr<Label>(create_label([this](){

View file

@ -23,7 +23,7 @@ Batch3D::Batch3D(size_t capacity)
mesh = new Mesh(buffer, 0, attrs); mesh = new Mesh(buffer, 0, attrs);
index = 0; index = 0;
u_char8 pixels[] = { u_char pixels[] = {
255, 255, 255, 255, 255, 255, 255, 255,
}; };
blank = new Texture(pixels, 1, 1, GL_RGBA); blank = new Texture(pixels, 1, 1, GL_RGBA);

View file

@ -187,7 +187,7 @@ void BlocksRenderer::blockXSprite(int x, int y, int z,
/* AABB blocks render method */ /* AABB blocks render method */
void BlocksRenderer::blockAABB(const ivec3& icoord, void BlocksRenderer::blockAABB(const ivec3& icoord,
const UVRegion(&texfaces)[6], const UVRegion(&texfaces)[6],
const Block* block, u_char8 rotation, const Block* block, u_char rotation,
bool lights) { bool lights) {
AABB hitbox = block->hitbox; AABB hitbox = block->hitbox;
vec3 size = hitbox.size(); vec3 size = hitbox.size();
@ -221,9 +221,9 @@ void BlocksRenderer::blockAABB(const ivec3& icoord,
void BlocksRenderer::blockCube(int x, int y, int z, void BlocksRenderer::blockCube(int x, int y, int z,
const UVRegion(&texfaces)[6], const UVRegion(&texfaces)[6],
const Block* block, const Block* block,
u_char8 states, voxel_t dir,
bool lights) { bool lights) {
u_char8 group = block->drawGroup; u_char group = block->drawGroup;
vec3 X(1, 0, 0); vec3 X(1, 0, 0);
vec3 Y(0, 1, 0); vec3 Y(0, 1, 0);
@ -231,7 +231,7 @@ void BlocksRenderer::blockCube(int x, int y, int z,
vec3 coord(x, y, z); vec3 coord(x, y, z);
if (block->rotatable) { if (block->rotatable) {
auto& rotations = block->rotations; auto& rotations = block->rotations;
auto& orient = rotations.variants[states & BLOCK_DIR_MASK]; auto& orient = rotations.variants[dir];
X = orient.axisX; X = orient.axisX;
Y = orient.axisY; Y = orient.axisY;
Z = orient.axisZ; Z = orient.axisZ;
@ -258,7 +258,7 @@ void BlocksRenderer::blockCube(int x, int y, int z,
} }
// Does block allow to see other blocks sides (is it transparent) // Does block allow to see other blocks sides (is it transparent)
bool BlocksRenderer::isOpen(int x, int y, int z, u_char8 group) const { bool BlocksRenderer::isOpen(int x, int y, int z, u_char group) const {
blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x, blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x,
y, y,
chunk->z * CHUNK_D + z); chunk->z * CHUNK_D + z);
@ -340,7 +340,7 @@ void BlocksRenderer::render(const voxel* voxels) {
int z = (i / CHUNK_D) % CHUNK_W; int z = (i / CHUNK_D) % CHUNK_W;
switch (def.model) { switch (def.model) {
case BlockModel::block: case BlockModel::block:
blockCube(x, y, z, texfaces, &def, vox.states, !def.rt.emissive); blockCube(x, y, z, texfaces, &def, vox.dir, !def.rt.emissive);
break; break;
case BlockModel::xsprite: { case BlockModel::xsprite: {
blockXSprite(x, y, z, vec3(1.0f), blockXSprite(x, y, z, vec3(1.0f),
@ -348,7 +348,7 @@ void BlocksRenderer::render(const voxel* voxels) {
break; break;
} }
case BlockModel::aabb: { case BlockModel::aabb: {
blockAABB(ivec3(x,y,z), texfaces, &def, vox.getDir(), !def.rt.emissive); blockAABB(ivec3(x,y,z), texfaces, &def, vox.dir, !def.rt.emissive);
break; break;
} }
default: default:

View file

@ -60,16 +60,16 @@ class BlocksRenderer {
const UVRegion& region, const UVRegion& region,
bool lights); bool lights);
void blockCube(int x, int y, int z, const UVRegion(&faces)[6], const Block* block, u_char8 states, bool lights); void blockCube(int x, int y, int z, const UVRegion(&faces)[6], const Block* block, voxel_t dir, bool lights);
void blockAABB(const glm::ivec3& coord, void blockAABB(const glm::ivec3& coord,
const UVRegion(&faces)[6], const UVRegion(&faces)[6],
const Block* block, const Block* block,
u_char8 rotation, u_char rotation,
bool lights); bool lights);
void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread); void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread);
bool isOpenForLight(int x, int y, int z) const; bool isOpenForLight(int x, int y, int z) const;
bool isOpen(int x, int y, int z, u_char8 group) const; bool isOpen(int x, int y, int z, u_char group) const;
glm::vec4 pickLight(int x, int y, int z) const; glm::vec4 pickLight(int x, int y, int z) const;
glm::vec4 pickLight(const glm::ivec3& coord) const; glm::vec4 pickLight(const glm::ivec3& coord) const;

View file

@ -14,8 +14,8 @@ inline int max(int a, int b) {
ImageData::ImageData(ImageFormat format, uint width, uint height) ImageData::ImageData(ImageFormat format, uint width, uint height)
: format(format), width(width), height(height) { : format(format), width(width), height(height) {
switch (format) { switch (format) {
case ImageFormat::rgb888: data = new u_char8[width*height*3]{}; break; case ImageFormat::rgb888: data = new u_char[width*height*3]{}; break;
case ImageFormat::rgba8888: data = new u_char8[width*height*4]{}; break; case ImageFormat::rgba8888: data = new u_char[width*height*4]{}; break;
default: default:
throw std::runtime_error("format is not supported"); throw std::runtime_error("format is not supported");
} }
@ -29,7 +29,7 @@ ImageData::~ImageData() {
switch (format) { switch (format) {
case ImageFormat::rgb888: case ImageFormat::rgb888:
case ImageFormat::rgba8888: case ImageFormat::rgba8888:
delete[] (u_char8*)data; delete[] (u_char*)data;
break; break;
} }
} }
@ -40,11 +40,11 @@ void ImageData::flipX() {
case ImageFormat::rgb888: case ImageFormat::rgb888:
case ImageFormat::rgba8888: { case ImageFormat::rgba8888: {
size = (format == ImageFormat::rgba8888) ? 4 : 3; size = (format == ImageFormat::rgba8888) ? 4 : 3;
u_char8* pixels = (u_char8*)data; u_char* pixels = (u_char*)data;
for (uint y = 0; y < height; y++) { for (uint y = 0; y < height; y++) {
for (uint x = 0; x < width/2; x++) { for (uint x = 0; x < width/2; x++) {
for (uint c = 0; c < size; c++) { for (uint c = 0; c < size; c++) {
u_char8 temp = pixels[(y * width + x) * size + c]; u_char temp = pixels[(y * width + x) * size + c];
pixels[(y * width + x) * size + c] = pixels[(y * width + (width - x - 1)) * size + c]; pixels[(y * width + x) * size + c] = pixels[(y * width + (width - x - 1)) * size + c];
pixels[(y * width + (width - x - 1)) * size + c] = temp; pixels[(y * width + (width - x - 1)) * size + c] = temp;
} }
@ -63,11 +63,11 @@ void ImageData::flipY() {
case ImageFormat::rgb888: case ImageFormat::rgb888:
case ImageFormat::rgba8888: { case ImageFormat::rgba8888: {
size = (format == ImageFormat::rgba8888) ? 4 : 3; size = (format == ImageFormat::rgba8888) ? 4 : 3;
u_char8* pixels = (u_char8*)data; u_char* pixels = (u_char*)data;
for (uint y = 0; y < height/2; y++) { for (uint y = 0; y < height/2; y++) {
for (uint x = 0; x < width; x++) { for (uint x = 0; x < width; x++) {
for (uint c = 0; c < size; c++) { for (uint c = 0; c < size; c++) {
u_char8 temp = pixels[(y * width + x) * size + c]; u_char temp = pixels[(y * width + x) * size + c];
pixels[(y * width + x) * size + c] = pixels[(y * width + x) * size + c] =
pixels[((height-y-1) * width + x) * size + c]; pixels[((height-y-1) * width + x) * size + c];
pixels[((height-y-1) * width + x) * size + c] = temp; pixels[((height-y-1) * width + x) * size + c] = temp;
@ -95,8 +95,8 @@ void ImageData::blit(const ImageData* image, int x, int y) {
} }
void ImageData::blitRGB_on_RGBA(const ImageData* image, int x, int y) { void ImageData::blitRGB_on_RGBA(const ImageData* image, int x, int y) {
ubyte* pixels = static_cast<ubyte*>(data); u_char* pixels = static_cast<u_char*>(data);
ubyte* source = static_cast<ubyte*>(image->getData()); u_char* source = static_cast<u_char*>(image->getData());
uint srcwidth = image->getWidth(); uint srcwidth = image->getWidth();
uint srcheight = image->getHeight(); uint srcheight = image->getHeight();
@ -122,8 +122,8 @@ void ImageData::blitMatchingFormat(const ImageData* image, int x, int y) {
default: default:
throw std::runtime_error("only unsigned byte formats supported"); throw std::runtime_error("only unsigned byte formats supported");
} }
u_char8* pixels = static_cast<u_char8*>(data); u_char* pixels = static_cast<u_char*>(data);
u_char8* source = static_cast<u_char8*>(image->getData()); u_char* source = static_cast<u_char*>(image->getData());
uint srcwidth = image->getWidth(); uint srcwidth = image->getWidth();
uint srcheight = image->getHeight(); uint srcheight = image->getHeight();
@ -150,7 +150,7 @@ void ImageData::extrude(int x, int y, int w, int h) {
default: default:
throw std::runtime_error("only unsigned byte formats supported"); throw std::runtime_error("only unsigned byte formats supported");
} }
u_char8* pixels = static_cast<u_char8*>(data); u_char* pixels = static_cast<u_char*>(data);
int rx = x + w - 1; int rx = x + w - 1;
int ry = y + h - 1; int ry = y + h - 1;
@ -237,7 +237,7 @@ void ImageData::extrude(int x, int y, int w, int h) {
} }
void ImageData::fixAlphaColor() { void ImageData::fixAlphaColor() {
u_char8* pixels = static_cast<u_char8*>(data); u_char* pixels = static_cast<u_char*>(data);
// Fixing black transparent pixels for Mip-Mapping // Fixing black transparent pixels for Mip-Mapping
for (uint ly = 0; ly < height-1; ly++) { for (uint ly = 0; ly < height-1; ly++) {
@ -265,8 +265,8 @@ ImageData* add_atlas_margins(ImageData* image, int grid_size) {
int dstwidth = srcwidth + grid_size * 2; int dstwidth = srcwidth + grid_size * 2;
int dstheight = srcheight + grid_size * 2; int dstheight = srcheight + grid_size * 2;
const u_char8* srcdata = (const u_char8*)image->getData(); const u_char* srcdata = (const u_char*)image->getData();
u_char8* dstdata = new u_char8[dstwidth*dstheight * 4]; u_char* dstdata = new u_char[dstwidth*dstheight * 4];
int imgres = image->getWidth() / grid_size; int imgres = image->getWidth() / grid_size;
for (int row = 0; row < grid_size; row++) { for (int row = 0; row < grid_size; row++) {

View file

@ -5,7 +5,7 @@
#include "../typedefs.h" #include "../typedefs.h"
struct vattr { struct vattr {
u_char8 size; u_char size;
}; };
class Mesh { class Mesh {

View file

@ -8,7 +8,7 @@ Texture::Texture(uint id, int width, int height)
: id(id), width(width), height(height) { : id(id), width(width), height(height) {
} }
Texture::Texture(u_char8* data, int width, int height, uint format) Texture::Texture(u_char* data, int width, int height, uint format)
: width(width), height(height) { : width(width), height(height) {
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
@ -30,7 +30,7 @@ void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
} }
void Texture::reload(u_char8* data){ void Texture::reload(u_char* data){
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data); GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
@ -48,5 +48,5 @@ Texture* Texture::from(const ImageData* image) {
default: default:
throw std::runtime_error("unsupported image data format"); throw std::runtime_error("unsupported image data format");
} }
return new Texture((u_char8*)data, width, height, format); return new Texture((u_char*)data, width, height, format);
} }

View file

@ -12,11 +12,11 @@ public:
int width; int width;
int height; int height;
Texture(uint id, int width, int height); Texture(uint id, int width, int height);
Texture(u_char8* data, int width, int height, uint format); Texture(u_char* data, int width, int height, uint format);
~Texture(); ~Texture();
void bind(); void bind();
void reload(u_char8* data); void reload(u_char* data);
static Texture* from(const ImageData* image); static Texture* from(const ImageData* image);
}; };

View file

@ -27,18 +27,18 @@ void Lightmap::set(light_t* map) {
static_assert(sizeof(light_t) == 2, "replace dataio calls to new light_t"); static_assert(sizeof(light_t) == 2, "replace dataio calls to new light_t");
u_char8* Lightmap::encode() const { u_char* Lightmap::encode() const {
u_char8* buffer = new u_char8[LIGHTMAP_DATA_LEN]; u_char* buffer = new u_char[LIGHTMAP_DATA_LEN];
for (uint i = 0; i < CHUNK_VOL; i+=2) { for (uint i = 0; i < CHUNK_VOL; i+=2) {
buffer[i/2] = ((map[i] >> 12) & 0xF) | ((map[i+1] >> 8) & 0xF0); buffer[i/2] = ((map[i] >> 12) & 0xF) | ((map[i+1] >> 8) & 0xF0);
} }
return buffer; return buffer;
} }
light_t* Lightmap::decode(u_char8* buffer) { light_t* Lightmap::decode(u_char* buffer) {
light_t* lights = new light_t[CHUNK_VOL]; light_t* lights = new light_t[CHUNK_VOL];
for (uint i = 0; i < CHUNK_VOL; i+=2) { for (uint i = 0; i < CHUNK_VOL; i+=2) {
u_char8 b = buffer[i/2]; u_char b = buffer[i/2];
lights[i] = ((b & 0xF) << 12); lights[i] = ((b & 0xF) << 12);
lights[i+1] = ((b & 0xF0) << 8); lights[i+1] = ((b & 0xF0) << 8);
} }

View file

@ -80,12 +80,12 @@ public:
return r | (g << 4) | (b << 8) | (s << 12); return r | (g << 4) | (b << 8) | (s << 12);
} }
static inline light_t extract(light_t light, u_char8 channel) { static inline light_t extract(light_t light, u_char channel) {
return (light >> (channel << 2)) & 0xF; return (light >> (channel << 2)) & 0xF;
} }
u_char8* encode() const; u_char* encode() const;
static light_t* decode(u_char8* buffer); static light_t* decode(u_char* buffer);
}; };
#endif /* LIGHTING_LIGHTMAP_H_ */ #endif /* LIGHTING_LIGHTMAP_H_ */

View file

@ -57,7 +57,7 @@ void BlocksController::updateSides(int x, int y, int z) {
} }
void BlocksController::breakBlock(Player* player, const Block* def, int x, int y, int z) { void BlocksController::breakBlock(Player* player, const Block* def, int x, int y, int z) {
chunks->set(x,y,z, voxel{0,0}); chunks->set(x,y,z, voxel());
lighting->onBlockSet(x,y,z, 0); lighting->onBlockSet(x,y,z, 0);
if (def->rt.funcsset.onbroken) { if (def->rt.funcsset.onbroken) {
scripting::on_block_broken(player, def, x, y, z); scripting::on_block_broken(player, def, x, y, z);
@ -75,10 +75,9 @@ void BlocksController::updateBlock(int x, int y, int z) {
return; return;
} }
if (def->rt.funcsset.update) { if (def->rt.funcsset.update) {
int states = vox->states; voxel tmp = *vox;
int id = vox->id;
scripting::update_block(def, x, y, z); scripting::update_block(def, x, y, z);
if (states != vox->states || id!= vox->id) if (tmp != *vox)
updateSides(x, y, z); updateSides(x, y, z);
} }
} }

View file

@ -229,7 +229,6 @@ void PlayerController::updateInteraction(){
if (vox != nullptr){ if (vox != nullptr){
player->selectedVoxel = *vox; player->selectedVoxel = *vox;
selectedBlockId = vox->id; selectedBlockId = vox->id;
selectedBlockStates = vox->states;
selectedBlockPosition = iend; selectedBlockPosition = iend;
selectedPointPosition = end; selectedPointPosition = end;
selectedBlockNormal = norm; selectedBlockNormal = norm;
@ -242,21 +241,21 @@ void PlayerController::updateInteraction(){
if (def->rotatable){ if (def->rotatable){
const std::string& name = def->rotations.name; const std::string& name = def->rotations.name;
if (name == "pipe") { if (name == "pipe") {
if (norm.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST); if (norm.x < 0.0f) voxel.dir = (voxel_t)VOX_DIR::WEST;
else if (norm.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST); else if (norm.x > 0.0f) voxel.dir = (voxel_t)VOX_DIR::EAST;
else if (norm.y > 0.0f) voxel.setDir(BLOCK_DIR_UP); else if (norm.y > 0.0f) voxel.dir = (voxel_t)VOX_DIR::UP;
else if (norm.y < 0.0f) voxel.setDir(BLOCK_DIR_DOWN); else if (norm.y < 0.0f) voxel.dir = (voxel_t)VOX_DIR::DOWN;
else if (norm.z > 0.0f) voxel.setDir(BLOCK_DIR_NORTH); else if (norm.z > 0.0f) voxel.dir = (voxel_t)VOX_DIR::NORTH;
else if (norm.z < 0.0f) voxel.setDir(BLOCK_DIR_SOUTH); else if (norm.z < 0.0f) voxel.dir = (voxel_t)VOX_DIR::SOUTH;
} else if (name == "pane") { } else if (name == "pane") {
glm::vec3 vec = camera->dir; glm::vec3 vec = camera->dir;
if (abs(vec.x) > abs(vec.z)){ if (abs(vec.x) > abs(vec.z)){
if (vec.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST); if (vec.x > 0.0f) voxel.dir = (voxel_t)VOX_DIR::EAST;
if (vec.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST); if (vec.x < 0.0f) voxel.dir = (voxel_t)VOX_DIR::WEST;
} }
if (abs(vec.x) < abs(vec.z)){ if (abs(vec.x) < abs(vec.z)){
if (vec.z > 0.0f) voxel.setDir(BLOCK_DIR_SOUTH); if (vec.z > 0.0f) voxel.dir = (voxel_t)VOX_DIR::SOUTH;
if (vec.z < 0.0f) voxel.setDir(BLOCK_DIR_NORTH); if (vec.z < 0.0f) voxel.dir = (voxel_t)VOX_DIR::NORTH;
} }
} }
} }
@ -300,7 +299,7 @@ void PlayerController::updateInteraction(){
player->chosenBlock = chunks->get(x,y,z)->id; player->chosenBlock = chunks->get(x,y,z)->id;
} }
} else { } else {
player->selectedVoxel = voxel{0, 0}; player->selectedVoxel = voxel();
selectedBlockId = -1; selectedBlockId = -1;
selectedBlockStates = 0; selectedBlockStates = 0;
} }

View file

@ -2,22 +2,115 @@
#include "scripting.h" #include "scripting.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <bit>
#include "../../physics/Hitbox.h"
#include "../../objects/Player.h"
#include "../../world/Level.h" #include "../../world/Level.h"
#include "../../objects/Player.h"
#include "../../physics/Hitbox.h"
#include "../../window/Camera.h"
#include "../../content/Content.h" #include "../../content/Content.h"
#include "../../voxels/Block.h" #include "../../voxels/Block.h"
#include "../../voxels/Chunks.h" #include "../../voxels/Chunks.h"
#include "../../voxels/voxel.h" #include "../../voxels/voxel.h"
#include "../../lighting/Lighting.h" #include "../../lighting/Lighting.h"
int l_block_name(lua_State* L) {
/**
* ,--------,
*: | PLAYER |
* |--------'--------------------------------------,
* | get_player_pos(): |
* | return x:float, y:float, z:float |
* | |
* | set_player_pos(x:float, y:float, z:float) |
* | |
* | get_player_rot(): |
* | return x:float, y:float |
* | |
* | set_player_rot(x:float, y:float) |
* | |
* | get_player_front(): |
* | return x:float, y:float, z:float |
* | |
*TODO | set_player_front(x:float, y:float, z:float) |
* '-----------------------------------------------'
*/
int l_get_player_pos(lua_State* L) {
glm::vec3 pos = scripting::level->player->hitbox->position;
lua_pushnumber(L, pos.x);
lua_pushnumber(L, pos.y);
lua_pushnumber(L, pos.z);
return 3;
}
int l_set_player_pos(lua_State* L) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
double z = lua_tonumber(L, 3);
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
return 0;
}
int l_get_player_rot(lua_State* L) {
glm::vec2 rot = scripting::level->player->cam;
lua_pushnumber(L, rot.x);
lua_pushnumber(L, rot.y);
return 2;
}
int l_set_player_rot(lua_State* L) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
scripting::level->player->cam = glm::vec2(x, y);
return 0;
}
int l_get_player_front(lua_State* L) {
glm::vec3 front = scripting::level->player->camera->front;
lua_pushnumber(L, front.x);
lua_pushnumber(L, front.y);
lua_pushnumber(L, front.z);
return 3;
}
// TODO: set_player_front(x:float, y:float, z:float);
/**
* ,--------,
*: | VOXELS |
* |--------'---------------------------------------,
* | pars_vox(vox:long): |
* | return id:short, variant:char, |
* | dir:char, signal:char, states:int |
* | |
* | creat_vox(id:short, variant:char, dir:char, |
* | signal:char, states:int): |
* | return vox:long |
* | |
* | idv_2_name(id:short, variant:char): |
* | return name:str |
* | |
* | name_2_idv(name:str): |
* | return id:short, variant:char |
* '------------------------------------------------'
*/
int l_block_id_2_name(lua_State* L) {
int id = lua_tointeger(L, 1); int id = lua_tointeger(L, 1);
lua_pushstring(L, scripting::content->indices->getBlockDef(id)->name.c_str()); lua_pushstring(L, scripting::content->indices->getBlockDef(id)->name.c_str());
return 1; return 1;
} }
int l_block_name_2_id(lua_State* L) {
auto name = lua_tostring(L, 1);
lua_pushinteger(L, scripting::content->requireBlock(name)->rt.id);
return 1;
}
int l_is_solid_at(lua_State* L) { int l_is_solid_at(lua_State* L) {
int x = lua_tointeger(L, 1); int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2); int y = lua_tointeger(L, 2);
@ -32,21 +125,14 @@ int l_blocks_count(lua_State* L) {
return 1; return 1;
} }
int l_block_index(lua_State* L) {
auto name = lua_tostring(L, 1);
lua_pushinteger(L, scripting::content->requireBlock(name)->rt.id);
return 1;
}
int l_set_block(lua_State* L) { int l_set_block(lua_State* L) {
voxel voxel;
int x = lua_tointeger(L, 1); int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2); int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3); int z = lua_tointeger(L, 3);
voxel.id = lua_tointeger(L, 4); voxel vox = std::bit_cast<voxel>(lua_tointeger(L, 4));
voxel.states = lua_tointeger(L, 5); scripting::level->chunks->set(x, y, z, vox);
scripting::level->chunks->set(x, y, z, voxel); scripting::level->lighting->onBlockSet(x,y,z, vox.id);
scripting::level->lighting->onBlockSet(x,y,z, voxel.id);
return 0; return 0;
} }
@ -55,68 +141,8 @@ int l_get_block(lua_State* L) {
int y = lua_tointeger(L, 2); int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3); int z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z); voxel* vox = scripting::level->chunks->get(x, y, z);
int id = vox == nullptr ? -1 : vox->id; voxel_t vox_ = std::bit_cast<voxel_t>(vox);
lua_pushinteger(L, id); lua_pushinteger(L, vox_);
return 1;
}
inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
lua_pushinteger(L, x);
lua_pushinteger(L, y);
lua_pushinteger(L, z);
return 3;
}
int l_get_player_pos(lua_State* L) {
glm::vec3 pos = scripting::level->player->hitbox->position;
lua_pushnumber(L, pos.x);
lua_pushnumber(L, pos.y);
lua_pushnumber(L, pos.z);
return 3;
}
int l_get_player_rot(lua_State* L) {
glm::vec2 rot = scripting::level->player->cam;
lua_pushnumber(L, rot.x);
lua_pushnumber(L, rot.y);
return 2;
}
int l_set_player_rot(lua_State* L) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
glm::vec2& cam = scripting::level->player->cam;
cam.x = x;
cam.y = y;
return 0;
}
int l_set_player_pos(lua_State* L) {
double x = lua_tonumber(L, 1);
double y = lua_tonumber(L, 2);
double z = lua_tonumber(L, 3);
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
return 0;
}
// old functions, not recommended to use
int l_get_block_states(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
int states = vox == nullptr ? 0 : vox->states;
lua_pushinteger(L, states);
return 1;
}
int l_is_replaceable_at(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
return 1; return 1;
} }
@ -126,8 +152,7 @@ int l_get_block_dir(lua_State* L) {
y = lua_tointeger(L, 2), y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3); z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z); voxel* vox = scripting::level->chunks->get(x, y, z);
int states = vox->getDir(); lua_pushinteger(L, vox->dir);
lua_pushinteger(L, states);
return 1; return 1;
} }
int l_set_block_dir(lua_State* L) { int l_set_block_dir(lua_State* L) {
@ -136,7 +161,7 @@ int l_set_block_dir(lua_State* L) {
z = lua_tointeger(L, 3), z = lua_tointeger(L, 3),
s = lua_tointeger(L, 4); s = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z); voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setDir(s); vox->dir = s;
return 0; return 0;
} }
@ -145,61 +170,19 @@ int l_get_block_sig(lua_State* L) {
y = lua_tointeger(L, 2), y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3); z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z); voxel* vox = scripting::level->chunks->get(x, y, z);
bool sig = vox->getSig(); lua_pushinteger(L, vox->signal);
lua_pushboolean(L, sig);
return 1; return 1;
} }
int l_set_block_sig(lua_State* L) { int l_set_block_sig(lua_State* L) {
int x = lua_tointeger(L, 1), int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2), y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3); z = lua_tointeger(L, 3);
bool s = lua_toboolean(L, 4); bool sig = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z); voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setSig(s); vox->signal = sig;
return 0; return 0;
} }
int l_get_block_custom_state(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3),
i = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
bool state = vox->getCustomState(i);
lua_pushboolean(L, state);
return 1;
}
int l_set_block_custom_state(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3),
i = lua_tointeger(L, 4);
bool s = lua_toboolean(L, 5);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setCustomState(i, s);
return 0;
}
int l_get_block_custom_value(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
int state = vox->getCustomValue();
lua_pushinteger(L, state);
return 1;
}
int l_set_block_custom_value(lua_State* L) {
int x = lua_tointeger(L, 1),
y = lua_tointeger(L, 2),
z = lua_tointeger(L, 3),
v = lua_tointeger(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->setCustomValue(v);
return 0;
}
// for specific modes
int l_get_block_custom_bits(lua_State* L) { int l_get_block_custom_bits(lua_State* L) {
int x = lua_tointeger(L, 1); int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2); int y = lua_tointeger(L, 2);
@ -228,6 +211,26 @@ int l_set_block_custom_bits(lua_State* L) {
return 0; return 0;
} }
// old functions, not recommended to use
// int l_get_block_states(lua_State* L) {
// int x = lua_tointeger(L, 1);
// int y = lua_tointeger(L, 2);
// int z = lua_tointeger(L, 3);
// voxel* vox = scripting::level->chunks->get(x, y, z);
// int states = vox == nullptr ? 0 : vox->states;
// lua_pushinteger(L, states);
// return 1;
// }
int l_is_replaceable_at(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
return 1;
}
inline int lua_pushivec3(lua_State* L, int x, int y, int z) { inline int lua_pushivec3(lua_State* L, int x, int y, int z) {
lua_pushinteger(L, x); lua_pushinteger(L, x);
@ -248,7 +251,7 @@ int l_get_block_x(lua_State* L) {
if (!def->rotatable) { if (!def->rotatable) {
return lua_pushivec3(L, 1, 0, 0); return lua_pushivec3(L, 1, 0, 0);
} else { } else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()]; const CoordSystem& rot = def->rotations.variants[vox->dir];
return lua_pushivec3(L, rot.axisX.x, rot.axisX.y, rot.axisX.z); return lua_pushivec3(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
} }
} }
@ -264,7 +267,7 @@ int l_get_block_y(lua_State* L) {
if (!def->rotatable) { if (!def->rotatable) {
return lua_pushivec3(L, 0, 1, 0); return lua_pushivec3(L, 0, 1, 0);
} else { } else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()]; const CoordSystem& rot = def->rotations.variants[vox->dir];
return lua_pushivec3(L, rot.axisY.x, rot.axisY.y, rot.axisY.z); return lua_pushivec3(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
} }
} }
@ -280,7 +283,7 @@ int l_get_block_z(lua_State* L) {
if (!def->rotatable) { if (!def->rotatable) {
return lua_pushivec3(L, 0, 0, 1); return lua_pushivec3(L, 0, 0, 1);
} else { } else {
const CoordSystem& rot = def->rotations.variants[vox->getDir()]; const CoordSystem& rot = def->rotations.variants[vox->dir];
return lua_pushivec3(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z); return lua_pushivec3(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
} }
} }
@ -290,8 +293,12 @@ int l_get_block_z(lua_State* L) {
lua_setglobal(L, NAME)) lua_setglobal(L, NAME))
void apilua::create_funcs(lua_State* L) { void apilua::create_funcs(lua_State* L) {
lua_addfunc(L, l_block_index, "block_index"); // old functions
lua_addfunc(L, l_block_name, "block_name"); lua_addfunc(L, l_block_id_2_name, "block_index");
lua_addfunc(L, l_block_name_2_id, "block_name");
lua_addfunc(L, l_block_id_2_name, "block_id_2_name");
lua_addfunc(L, l_block_name_2_id, "block_name_2_id");
lua_addfunc(L, l_blocks_count, "blocks_count"); lua_addfunc(L, l_blocks_count, "blocks_count");
lua_addfunc(L, l_is_solid_at, "is_solid_at"); lua_addfunc(L, l_is_solid_at, "is_solid_at");
lua_addfunc(L, l_is_replaceable_at, "is_replaceable_at"); lua_addfunc(L, l_is_replaceable_at, "is_replaceable_at");
@ -305,14 +312,10 @@ void apilua::create_funcs(lua_State* L) {
lua_addfunc(L, l_get_player_rot, "get_player_rot"); lua_addfunc(L, l_get_player_rot, "get_player_rot");
lua_addfunc(L, l_set_player_rot, "set_player_rot"); lua_addfunc(L, l_set_player_rot, "set_player_rot");
lua_addfunc(L, l_get_block_states, "get_block_states"); // old function // lua_addfunc(L, l_get_block_states, "get_block_states"); // old function
lua_addfunc(L, l_get_block_dir, "get_block_dir"); lua_addfunc(L, l_get_block_dir, "get_block_dir");
lua_addfunc(L, l_set_block_dir, "set_block_dir"); lua_addfunc(L, l_set_block_dir, "set_block_dir");
lua_addfunc(L, l_get_block_custom_state, "get_block_custom_state");
lua_addfunc(L, l_set_block_custom_state, "set_block_custom_state");
lua_addfunc(L, l_get_block_custom_value, "get_block_custom_value");
lua_addfunc(L, l_set_block_custom_value, "set_block_custom_value");
lua_addfunc(L, l_get_block_sig, "get_block_sig"); lua_addfunc(L, l_get_block_sig, "get_block_sig");
lua_addfunc(L, l_set_block_sig, "set_block_sig"); lua_addfunc(L, l_set_block_sig, "set_block_sig");
lua_addfunc(L, l_get_block_x, "get_block_X"); lua_addfunc(L, l_get_block_x, "get_block_X");

View file

@ -25,15 +25,15 @@ inline int min(int a, int b) {
return (a < b) ? a : b; return (a < b) ? a : b;
} }
inline int64_t max(int64_t a, int64_t b) { inline long max(long a, long b) {
return (a > b) ? a : b; return (a > b) ? a : b;
} }
inline int64_t min(int64_t a, int64_t b) { inline long min(long a, long b) {
return (a < b) ? a : b; return (a < b) ? a : b;
} }
static unsigned int g_seed; static u_int g_seed;
inline void fast_srand(int seed) { inline void fast_srand(int seed) {
g_seed = seed; g_seed = seed;
@ -44,7 +44,7 @@ inline int fast_rand(void) {
return (g_seed >> 16) & 0x7FFF; return (g_seed >> 16) & 0x7FFF;
} }
inline light_t light_pack(u_char8 r, u_char8 g, u_char8 b, u_char8 s) { inline light_t light_pack(u_char r, u_char g, u_char b, u_char s) {
return r | (g << 4) | (b << 8) | (s << 12); return r | (g << 4) | (b << 8) | (s << 12);
} }

View file

@ -36,7 +36,7 @@ public:
bool noclip = false; bool noclip = false;
bool debug = false; bool debug = false;
int chosenBlock; int chosenBlock;
voxel selectedVoxel {0, 0}; voxel selectedVoxel {0};
glm::vec2 cam = {}; glm::vec2 cam = {};

View file

@ -16,7 +16,7 @@ void PhysicsSolver::step(
Chunks* chunks, Chunks* chunks,
Hitbox* hitbox, Hitbox* hitbox,
float delta, float delta,
uint substeps, u_int substeps,
bool shifting, bool shifting,
float gravityScale, float gravityScale,
bool collisions) bool collisions)
@ -26,7 +26,7 @@ void PhysicsSolver::step(
float s = 2.0f/BLOCK_AABB_GRID; float s = 2.0f/BLOCK_AABB_GRID;
hitbox->grounded = false; hitbox->grounded = false;
for (uint i = 0; i < substeps; i++) { for (u_int i = 0; i < substeps; i++) {
vec3& pos = hitbox->position; vec3& pos = hitbox->position;
vec3& half = hitbox->halfsize; vec3& half = hitbox->halfsize;
vec3& vel = hitbox->velocity; vec3& vel = hitbox->velocity;

View file

@ -14,7 +14,7 @@ public:
void step(Chunks* chunks, void step(Chunks* chunks,
Hitbox* hitbox, Hitbox* hitbox,
float delta, float delta,
uint substeps, u_int substeps,
bool shifting, bool shifting,
float gravityScale, float gravityScale,
bool collisions); bool collisions);

View file

@ -25,11 +25,11 @@ struct DisplaySettings {
struct ChunksSettings { struct ChunksSettings {
/* Max milliseconds that engine uses for chunks loading only */ /* Max milliseconds that engine uses for chunks loading only */
uint loadSpeed = 10; u_int loadSpeed = 10;
/* Radius of chunks loading zone (chunk is unit) */ /* Radius of chunks loading zone (chunk is unit) */
uint loadDistance = 22; u_int loadDistance = 22;
/* Buffer zone where chunks are not unloading (chunk is unit)*/ /* Buffer zone where chunks are not unloading (chunk is unit)*/
uint padding = 2; u_int padding = 2;
}; };
struct CameraSettings { struct CameraSettings {

View file

@ -4,12 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
typedef uint64_t voxel_t;
typedef uint8_t u_char8;
typedef uint16_t u_short16;
typedef uint32_t u_int32;
typedef uint64_t u_long64;
typedef uint16_t blockid_t; typedef uint16_t blockid_t;
typedef uint16_t blockstate_t; typedef uint16_t blockstate_t;
typedef uint16_t light_t; typedef uint16_t light_t;

View file

@ -5,19 +5,19 @@
namespace dataio { namespace dataio {
/* Read big-endian 16 bit signed integer from bytes */ /* Read big-endian 16 bit signed integer from bytes */
inline int16_t read_int16_big(const u_char8* src, size_t offset) { inline int16_t read_int16_big(const u_char* src, size_t offset) {
return (src[offset] << 8) | return (src[offset] << 8) |
(src[offset+1]); (src[offset+1]);
} }
/* Read big-endian 32 bit signed integer from bytes */ /* Read big-endian 32 bit signed integer from bytes */
inline int32_t read_int32_big(const u_char8* src, size_t offset) { inline int32_t read_int32_big(const u_char* src, size_t offset) {
return (src[offset] << 24) | return (src[offset] << 24) |
(src[offset+1] << 16) | (src[offset+1] << 16) |
(src[offset+2] << 8) | (src[offset+2] << 8) |
(src[offset+3]); (src[offset+3]);
} }
/* Read big-endian 64 bit signed integer from bytes */ /* Read big-endian 64 bit signed integer from bytes */
inline int64_t read_int64_big(const u_char8* src, size_t offset) { inline int64_t read_int64_big(const u_char* src, size_t offset) {
return (int64_t(src[offset]) << 56) | return (int64_t(src[offset]) << 56) |
(int64_t(src[offset+1]) << 48) | (int64_t(src[offset+1]) << 48) |
(int64_t(src[offset+2]) << 40) | (int64_t(src[offset+2]) << 40) |
@ -28,19 +28,19 @@ namespace dataio {
(int64_t(src[offset+7])); (int64_t(src[offset+7]));
} }
/* Write big-endian 16 bit signed integer to bytes */ /* Write big-endian 16 bit signed integer to bytes */
inline void write_int16_big(int16_t value, u_char8* dest, size_t offset) { inline void write_int16_big(int16_t value, u_char* dest, size_t offset) {
dest[offset] = (char) (value >> 8 & 255); dest[offset] = (char) (value >> 8 & 255);
dest[offset+1] = (char) (value >> 0 & 255); dest[offset+1] = (char) (value >> 0 & 255);
} }
/* Write big-endian 32 bit signed integer to bytes */ /* Write big-endian 32 bit signed integer to bytes */
inline void write_int32_big(int32_t value, u_char8* dest, size_t offset) { inline void write_int32_big(int32_t value, u_char* dest, size_t offset) {
dest[offset] = (char) (value >> 24 & 255); dest[offset] = (char) (value >> 24 & 255);
dest[offset+1] = (char) (value >> 16 & 255); dest[offset+1] = (char) (value >> 16 & 255);
dest[offset+2] = (char) (value >> 8 & 255); dest[offset+2] = (char) (value >> 8 & 255);
dest[offset+3] = (char) (value >> 0 & 255); dest[offset+3] = (char) (value >> 0 & 255);
} }
/* Write big-endian 64 bit signed integer to bytes */ /* Write big-endian 64 bit signed integer to bytes */
inline void write_int64_big(int64_t value, u_char8* dest, size_t offset) { inline void write_int64_big(int64_t value, u_char* dest, size_t offset) {
dest[offset] = (char) (value >> 56 & 255); dest[offset] = (char) (value >> 56 & 255);
dest[offset+1] = (char) (value >> 48 & 255); dest[offset+1] = (char) (value >> 48 & 255);
dest[offset+2] = (char) (value >> 40 & 255); dest[offset+2] = (char) (value >> 40 & 255);

View file

@ -6,31 +6,31 @@
#include <stdexcept> #include <stdexcept>
#include <algorithm> #include <algorithm>
std::wstring util::lfill(std::wstring s, uint length, wchar_t c) { std::wstring util::lfill(std::wstring s, u_int length, wchar_t c) {
if (s.length() >= length) { if (s.length() >= length) {
return s; return s;
} }
std::wstringstream ss; std::wstringstream ss;
for (uint i = 0; i < length-s.length(); i++) { for (u_int i = 0; i < length-s.length(); i++) {
ss << c; ss << c;
} }
ss << s; ss << s;
return ss.str(); return ss.str();
} }
std::wstring util::rfill(std::wstring s, uint length, wchar_t c) { std::wstring util::rfill(std::wstring s, u_int length, wchar_t c) {
if (s.length() >= length) { if (s.length() >= length) {
return s; return s;
} }
std::wstringstream ss; std::wstringstream ss;
ss << s; ss << s;
for (uint i = 0; i < length-s.length(); i++) { for (u_int i = 0; i < length-s.length(); i++) {
ss << c; ss << c;
} }
return ss.str(); return ss.str();
} }
uint util::encode_utf8(uint32_t c, u_char8* bytes) { u_int util::encode_utf8(u_int c, u_char* bytes) {
if (c < 0x80) { if (c < 0x80) {
bytes[0] = ((c >> 0) & 0x7F) | 0x00; bytes[0] = ((c >> 0) & 0x7F) | 0x00;
return 1; return 1;
@ -55,8 +55,8 @@ uint util::encode_utf8(uint32_t c, u_char8* bytes) {
struct utf_t { struct utf_t {
char mask; char mask;
char lead; char lead;
uint32_t beg; u_int beg;
uint32_t end; u_int end;
int bits_stored; int bits_stored;
}; };
@ -71,8 +71,8 @@ const utf_t utf[] = {
}; };
inline uint utf8_len(u_char8 cp) { inline u_int utf8_len(u_char cp) {
uint len = 0; u_int len = 0;
for (const utf_t* u = utf; u->mask; ++u) { for (const utf_t* u = utf; u->mask; ++u) {
if((cp >= u->beg) && (cp <= u->end)) { if((cp >= u->beg) && (cp <= u->end)) {
break; break;
@ -85,12 +85,12 @@ inline uint utf8_len(u_char8 cp) {
return len; return len;
} }
extern uint32_t util::decode_utf8(uint& size, const char* chr) { extern u_int util::decode_utf8(u_int& size, const char* chr) {
size = utf8_len(*chr); size = utf8_len(*chr);
int shift = utf[0].bits_stored * (size - 1); int shift = utf[0].bits_stored * (size - 1);
uint32_t code = (*chr++ & utf[size].mask) << shift; u_int code = (*chr++ & utf[size].mask) << shift;
for(uint i = 1; i < size; ++i, ++chr) { for(u_int i = 1; i < size; ++i, ++chr) {
shift -= utf[0].bits_stored; shift -= utf[0].bits_stored;
code |= ((char)*chr & utf[0].mask) << shift; code |= ((char)*chr & utf[0].mask) << shift;
} }
@ -101,8 +101,8 @@ std::string util::wstr2str_utf8(const std::wstring ws) {
std::vector<char> chars; std::vector<char> chars;
char buffer[4]; char buffer[4];
for (wchar_t wc : ws) { for (wchar_t wc : ws) {
uint size = encode_utf8((uint)wc, (u_char8*)buffer); u_int size = encode_utf8((u_int)wc, (u_char*)buffer);
for (uint i = 0; i < size; i++) { for (u_int i = 0; i < size; i++) {
chars.push_back(buffer[i]); chars.push_back(buffer[i]);
} }
} }
@ -112,7 +112,7 @@ std::string util::wstr2str_utf8(const std::wstring ws) {
std::wstring util::str2wstr_utf8(const std::string s) { std::wstring util::str2wstr_utf8(const std::string s) {
std::vector<wchar_t> chars; std::vector<wchar_t> chars;
size_t pos = 0; size_t pos = 0;
uint size = 0; u_int size = 0;
while (pos < s.length()) { while (pos < s.length()) {
chars.push_back(decode_utf8(size, &s.at(pos))); chars.push_back(decode_utf8(size, &s.at(pos)));
pos += size; pos += size;

View file

@ -5,11 +5,11 @@
#include "../typedefs.h" #include "../typedefs.h"
namespace util { namespace util {
extern std::wstring lfill(std::wstring s, uint length, wchar_t c); extern std::wstring lfill(std::wstring s, u_int length, wchar_t c);
extern std::wstring rfill(std::wstring s, uint length, wchar_t c); extern std::wstring rfill(std::wstring s, u_int length, wchar_t c);
extern uint encode_utf8(uint32_t c, u_char8* bytes); extern u_int encode_utf8(u_int c, u_char* bytes);
extern uint32_t decode_utf8(uint& size, const char* bytes); extern u_int decode_utf8(u_int& size, const char* bytes);
extern std::string wstr2str_utf8(const std::wstring ws); extern std::string wstr2str_utf8(const std::wstring ws);
extern std::wstring str2wstr_utf8(const std::string s); extern std::wstring str2wstr_utf8(const std::string s);
extern bool is_integer(std::string text); extern bool is_integer(std::string text);

View file

@ -9,7 +9,7 @@ using std::chrono::microseconds;
timeutil::Timer::Timer() { timeutil::Timer::Timer() {
start = high_resolution_clock::now(); start = high_resolution_clock::now();
} }
int64_t timeutil::Timer::stop() { long timeutil::Timer::stop() {
return duration_cast<microseconds>(high_resolution_clock::now()-start).count(); return duration_cast<microseconds>(high_resolution_clock::now()-start).count();
} }

View file

@ -9,7 +9,7 @@ namespace timeutil {
std::chrono::high_resolution_clock::time_point start; std::chrono::high_resolution_clock::time_point start;
public: public:
Timer(); Timer();
int64_t stop(); long stop();
}; };
/* Timer that stops and prints time when destructor called /* Timer that stops and prints time when destructor called

View file

@ -55,5 +55,8 @@ int main(int argc, char** argv) {
std::cerr << "could not to initialize engine" << std::endl; std::cerr << "could not to initialize engine" << std::endl;
std::cerr << err.what() << std::endl; std::cerr << err.what() << std::endl;
} }
catch (...) {
std::cerr << "break" << std::endl;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View file

@ -7,14 +7,14 @@
#include "../maths/aabb.h" #include "../maths/aabb.h"
#include "../typedefs.h" #include "../typedefs.h"
const uint FACE_MX = 0; const u_int FACE_MX = 0;
const uint FACE_PX = 1; const u_int FACE_PX = 1;
const uint FACE_MY = 2; const u_int FACE_MY = 2;
const uint FACE_PY = 3; const u_int FACE_PY = 3;
const uint FACE_MZ = 4; const u_int FACE_MZ = 4;
const uint FACE_PZ = 5; const u_int FACE_PZ = 5;
const uint BLOCK_AABB_GRID = 16; const u_int BLOCK_AABB_GRID = 16;
struct block_funcs_set { struct block_funcs_set {
bool init: 1; bool init: 1;

View file

@ -12,7 +12,6 @@ Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos){
voxels = new voxel[CHUNK_VOL]; voxels = new voxel[CHUNK_VOL];
for (size_t i = 0; i < CHUNK_VOL; i++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
voxels[i].id = 2; voxels[i].id = 2;
voxels[i].states = 0;
} }
lightmap = new Lightmap(); lightmap = new Lightmap();
renderData.vertices = nullptr; renderData.vertices = nullptr;
@ -74,13 +73,18 @@ Chunk* Chunk::clone() const {
Total size: (CHUNK_VOL * 4) bytes Total size: (CHUNK_VOL * 4) bytes
*/ */
u_char8* Chunk::encode() const { u_char* Chunk::encode() const {
u_char8* buffer = new u_char8[CHUNK_DATA_LEN]; u_char* buffer = new u_char[CHUNK_DATA_LEN];
voxel_t* tmp_voxs = (voxel_t *)this->voxels;
for (size_t i = 0; i < CHUNK_VOL; i++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
buffer[i] = voxels[i].id >> 8; ((voxel_t*)buffer)[i] = tmp_voxs[i];
buffer[CHUNK_VOL+i] = voxels[i].id & 0xFF; // buffer[CHUNK_VOL+i] = tmp_voxs[i] >> 8;
buffer[CHUNK_VOL*2 + i] = voxels[i].states >> 8; // buffer[CHUNK_VOL*2 + i] = tmp_voxs[i] >> 16;
buffer[CHUNK_VOL*3 + i] = voxels[i].states & 0xFF; // buffer[CHUNK_VOL*3 + i] = tmp_voxs[i] >> 24;
// buffer[CHUNK_VOL*4 + i] = tmp_voxs[i] >> 32;
// buffer[CHUNK_VOL*5 + i] = tmp_voxs[i] >> 40;
// buffer[CHUNK_VOL*6 + i] = tmp_voxs[i] >> 48;
// buffer[CHUNK_VOL*7 + i] = tmp_voxs[i] >> 56;
} }
return buffer; return buffer;
} }
@ -89,18 +93,22 @@ u_char8* Chunk::encode() const {
/** /**
* @return true if all is fine * @return true if all is fine
**/ **/
bool Chunk::decode(u_char8* data) { bool Chunk::decode(u_char* data) {
for (size_t i = 0; i < CHUNK_VOL; i++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
voxel& vox = voxels[i]; voxel& vox = voxels[i];
u_char8 bid1 = data[i]; vox = ((voxel_t*)data)[i];
u_char8 bid2 = data[CHUNK_VOL + i]; // u_char b1 = data[i];
// u_char b2 = data[CHUNK_VOL + i];
// u_char b3 = data[CHUNK_VOL*2 + i];
// u_char b4 = data[CHUNK_VOL*3 + i];
// u_char b3 = data[CHUNK_VOL*4 + i];
// u_char b4 = data[CHUNK_VOL*5 + i];
// u_char b3 = data[CHUNK_VOL*6 + i];
// u_char b4 = data[CHUNK_VOL*7 + i];
u_char8 bst1 = data[CHUNK_VOL*2 + i]; // vox.id = (blockid_t(bid1) << 8) | (blockid_t(bid2));
u_char8 bst2 = data[CHUNK_VOL*3 + i]; // vox.states = (blockstate_t(bst1) << 8) | (blockstate_t(bst2));
vox.id = (blockid_t(bid1) << 8) | (blockid_t(bid2));
vox.states = (blockstate_t(bst1) << 8) | (blockstate_t(bst2));
} }
return true; return true;
} }
@ -108,7 +116,7 @@ bool Chunk::decode(u_char8* data) {
/* /*
* Convert chunk voxels data from 16 bit to 32 bit * Convert chunk voxels data from 16 bit to 32 bit
*/ */
void Chunk::fromOld(u_char8* data) { void Chunk::fromOld(u_char* data) {
for (size_t i = 0; i < CHUNK_VOL; i++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
data[i + CHUNK_VOL*3] = data[i + CHUNK_VOL]; data[i + CHUNK_VOL*3] = data[i + CHUNK_VOL];
data[i + CHUNK_VOL] = data[i]; data[i + CHUNK_VOL] = data[i];
@ -117,7 +125,7 @@ void Chunk::fromOld(u_char8* data) {
} }
} }
void Chunk::convert(u_char8* data, const ContentLUT* lut) { void Chunk::convert(u_char* data, const ContentLUT* lut) {
for (size_t i = 0; i < CHUNK_VOL; i++) { for (size_t i = 0; i < CHUNK_VOL; i++) {
// see encode method to understand what the hell is going on here // see encode method to understand what the hell is going on here
blockid_t id = ((blockid_t(data[i]) << 8) | blockid_t id = ((blockid_t(data[i]) << 8) |

View file

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "../constants.h" #include "../constants.h"
#include "voxel.h"
struct ChunkFlag{ struct ChunkFlag{
static const int MODIFIED = 0x1; static const int MODIFIED = 0x1;
@ -12,9 +13,8 @@ struct ChunkFlag{
static const int UNSAVED = 0x10; static const int UNSAVED = 0x10;
static const int LOADED_LIGHTS = 0x20; static const int LOADED_LIGHTS = 0x20;
}; };
#define CHUNK_DATA_LEN (CHUNK_VOL*4) #define CHUNK_DATA_LEN (CHUNK_VOL*sizeof(voxel))
struct voxel;
class Lightmap; class Lightmap;
class ContentLUT; class ContentLUT;
@ -75,11 +75,11 @@ public:
inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);} inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);}
u_char8* encode() const; u_char* encode() const;
bool decode(u_char8* data); bool decode(u_char* data);
static void fromOld(u_char8* data); static void fromOld(u_char* data);
static void convert(u_char8* data, const ContentLUT* lut); static void convert(u_char* data, const ContentLUT* lut);
}; };
#endif /* VOXELS_CHUNK_H_ */ #endif /* VOXELS_CHUNK_H_ */

View file

@ -75,7 +75,7 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z){
const Block* def = contentIds->getBlockDef(v->id); const Block* def = contentIds->getBlockDef(v->id);
if (def->obstacle) { if (def->obstacle) {
const AABB& hitbox = def->rotatable const AABB& hitbox = def->rotatable
? def->rt.hitboxes[v->getDir()] ? def->rt.hitboxes[v->dir]
: def->hitbox; : def->hitbox;
if (def->rt.solid) { if (def->rt.solid) {
return &hitbox; return &hitbox;
@ -109,7 +109,7 @@ bool Chunks::isObstacleBlock(int x, int y, int z) {
return contentIds->getBlockDef(v->id)->obstacle; return contentIds->getBlockDef(v->id)->obstacle;
} }
u_char8 Chunks::getLight(int x, int y, int z, int channel){ u_char Chunks::getLight(int x, int y, int z, int channel){
x -= ox * CHUNK_W; x -= ox * CHUNK_W;
z -= oz * CHUNK_D; z -= oz * CHUNK_D;
int cx = floordiv(x, CHUNK_W); int cx = floordiv(x, CHUNK_W);
@ -177,8 +177,7 @@ void Chunks::set(int x, int y, int z, voxel voxel) {
return; return;
int lx = x - cx * CHUNK_W; int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D; int lz = z - cz * CHUNK_D;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].id = voxel.id; chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx] = voxel;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].states = voxel.states;
chunk->setUnsaved(true); chunk->setUnsaved(true);
chunk->setModified(true); chunk->setModified(true);
@ -251,7 +250,7 @@ voxel* Chunks::rayCast(vec3 start,
if (!def->rt.solid) { if (!def->rt.solid) {
const AABB& box = def->rotatable const AABB& box = def->rotatable
? def->rt.hitboxes[voxel->getDir()] ? def->rt.hitboxes[voxel->dir]
: def->hitbox; : def->hitbox;
scalar_t distance; scalar_t distance;
Ray ray(start, dir); Ray ray(start, dir);
@ -349,7 +348,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
if (def->obstacle) { if (def->obstacle) {
if (!def->rt.solid) { if (!def->rt.solid) {
const AABB& box = def->rotatable const AABB& box = def->rotatable
? def->rt.hitboxes[voxel->getDir()] ? def->rt.hitboxes[voxel->dir]
: def->hitbox; : def->hitbox;
scalar_t distance; scalar_t distance;
ivec3 norm; ivec3 norm;
@ -403,7 +402,7 @@ void Chunks::setCenter(int x, int z) {
} }
void Chunks::translate(int dx, int dz){ void Chunks::translate(int dx, int dz){
for (uint i = 0; i < volume; i++){ for (u_int i = 0; i < volume; i++){
chunksSecond[i] = nullptr; chunksSecond[i] = nullptr;
} }
for (int z = 0; z < d; z++){ for (int z = 0; z < d; z++){

View file

@ -41,7 +41,7 @@ public:
Chunk* getChunkByVoxel(int x, int y, int z); Chunk* getChunkByVoxel(int x, int y, int z);
voxel* get(int x, int y, int z); voxel* get(int x, int y, int z);
light_t getLight(int x, int y, int z); light_t getLight(int x, int y, int z);
u_char8 getLight(int x, int y, int z, int channel); u_char getLight(int x, int y, int z, int channel);
void set(int x, int y, int z, voxel voxel); void set(int x, int y, int z, voxel voxel);
voxel* rayCast(glm::vec3 start, voxel* rayCast(glm::vec3 start,

View file

@ -60,7 +60,7 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
auto chunk = std::make_shared<Chunk>(x, z); auto chunk = std::make_shared<Chunk>(x, z);
store(chunk); store(chunk);
std::unique_ptr<u_char8[]> data(world->wfile->getChunk(chunk->x, chunk->z)); std::unique_ptr<u_char[]> data(world->wfile->getChunk(chunk->x, chunk->z));
if (data) { if (data) {
chunk->decode(data.get()); chunk->decode(data.get());
chunk->setLoaded(true); chunk->setLoaded(true);
@ -111,7 +111,7 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
for (int lx = max(x, cx * CHUNK_W); for (int lx = max(x, cx * CHUNK_W);
lx < min(x + w, (cx + 1) * CHUNK_W); lx < min(x + w, (cx + 1) * CHUNK_W);
lx++) { lx++) {
uint idx = vox_index(lx - x, ly - y, lz - z, w, d); u_int idx = vox_index(lx - x, ly - y, lz - z, w, d);
voxels[idx].id = BLOCK_VOID; voxels[idx].id = BLOCK_VOID;
lights[idx] = 0; lights[idx] = 0;
} }
@ -128,8 +128,8 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
for (int lx = max(x, cx * CHUNK_W); for (int lx = max(x, cx * CHUNK_W);
lx < min(x + w, (cx + 1) * CHUNK_W); lx < min(x + w, (cx + 1) * CHUNK_W);
lx++) { lx++) {
uint vidx = vox_index(lx - x, ly - y, lz - z, w, d); u_int vidx = vox_index(lx - x, ly - y, lz - z, w, d);
uint cidx = vox_index(lx - cx * CHUNK_W, ly, u_int cidx = vox_index(lx - cx * CHUNK_W, ly,
lz - cz * CHUNK_D, CHUNK_W, CHUNK_D); lz - cz * CHUNK_D, CHUNK_W, CHUNK_D);
voxels[vidx] = cvoxels[cidx]; voxels[vidx] = cvoxels[cidx];
light_t light = clights[cidx]; light_t light = clights[cidx];

View file

@ -26,7 +26,7 @@ public:
void getVoxels(VoxelsVolume* volume, bool backlight=false) const; void getVoxels(VoxelsVolume* volume, bool backlight=false) const;
std::shared_ptr<Chunk> create(int x, int z); std::shared_ptr<Chunk> create(int x, int z);
light_t getLight(int x, int y, int z, u_char8 channel) const; light_t getLight(int x, int y, int z, u_char channel) const;
}; };

View file

@ -63,10 +63,10 @@ public:
}; };
class PseudoRandom { class PseudoRandom {
unsigned short seed; u_short seed;
public: public:
PseudoRandom(){ PseudoRandom(){
seed = (unsigned short)time(0); seed = (u_short)time(0);
} }
int rand(){ int rand(){
@ -81,11 +81,11 @@ public:
} }
void setSeed(int number){ void setSeed(int number){
seed = ((unsigned short)(number*23729) ^ (unsigned short)(number+16786)); seed = ((u_short)(number*23729) ^ (u_short)(number+16786));
rand(); rand();
} }
void setSeed(int number1,int number2){ void setSeed(int number1,int number2){
seed = (((unsigned short)(number1*23729) | (unsigned short)(number2%16786)) ^ (unsigned short)(number2*number1)); seed = (((u_short)(number1*23729) | (u_short)(number2%16786)) ^ (u_short)(number2*number1));
rand(); rand();
} }
}; };
@ -150,7 +150,7 @@ int generate_tree(fnl_state *noise,
if (height < SEA_LEVEL+1) if (height < SEA_LEVEL+1)
return 0; return 0;
int lx = cur_x - centerX; int lx = cur_x - centerX;
int radius = random->rand() % 4 + 2; int radius = random->rand() % 4 + 5;
int ly = cur_y - height - 3 * radius; int ly = cur_y - height - 3 * radius;
int lz = cur_z - centerZ; int lz = cur_z - centerZ;
if (lx == 0 && lz == 0 && cur_y - height < (3*radius + radius/2)) if (lx == 0 && lz == 0 && cur_y - height < (3*radius + radius/2))
@ -161,7 +161,7 @@ int generate_tree(fnl_state *noise,
} }
void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
const int treesTile = 12; const int treesTile = 16;
fnl_state noise = fnlCreateState(); fnl_state noise = fnlCreateState();
noise.noise_type = FNL_NOISE_OPENSIMPLEX2; noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
noise.seed = seed * 60617077 % 25896307; noise.seed = seed * 60617077 % 25896307;
@ -183,8 +183,8 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
float send = fnlGetNoise2D(&noise, cur_x * 0.1 - 633, cur_z * 0.1 + 1000); float send = fnlGetNoise2D(&noise, cur_x * 0.1 - 633, cur_z * 0.1 + 1000);
float cliff = pow((send + abs(send)) / 2, 2); float cliff = pow((send + abs(send)) / 2, 2);
float w = pow(fmax(-abs(height-SEA_LEVEL)+4,0)/6,2) * cliff; float w = pow(fmax(-abs(height-SEA_LEVEL)+4,0)/6,2) * cliff;
float h1 = -abs(height-SEA_LEVEL - 0.03); float h1 = -abs(height-SEA_LEVEL - 0.06);
float h2 = abs(height-SEA_LEVEL + 0.04); float h2 = abs(height-SEA_LEVEL + 0.08);
float h = (h1 + h2)*100; float h = (h1 + h2)*100;
height += (h * w); height += (h * w);
heights.set(MAPS::HEIGHT, cur_x, cur_z, height); heights.set(MAPS::HEIGHT, cur_x, cur_z, height);
@ -203,19 +203,19 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
for (int cur_y = 0; cur_y < CHUNK_H; cur_y++){ for (int cur_y = 0; cur_y < CHUNK_H; cur_y++){
// int cur_y = y; // int cur_y = y;
int id = cur_y < SEA_LEVEL ? idWater : BLOCK_AIR; voxel vox;
int states = 0; vox.id = cur_y < SEA_LEVEL ? idWater : BLOCK_AIR;
if ((cur_y == (int)height) && (SEA_LEVEL-2 < cur_y)) { if ((cur_y == (int)height) && (SEA_LEVEL-2 < cur_y)) {
id = idGrassBlock; vox.id = idGrassBlock;
} else if (cur_y < (height - 6)){ } else if (cur_y < (height - 6)){
id = idStone; vox.id = idStone;
} else if (cur_y < height){ } else if (cur_y < height){
id = idDirt; vox.id = idDirt;
} else { } else {
int tree = generate_tree(&noise, &randomtree, heights, cur_x, cur_y, cur_z, treesTile, idWood, idLeaves); int tree = generate_tree(&noise, &randomtree, heights, cur_x, cur_y, cur_z, treesTile, idWood, idLeaves);
if (tree) { if (tree) {
id = tree; vox.id = tree;
states = BLOCK_DIR_UP; vox.dir = (voxel_t)VOX_DIR::UP;
} }
} }
float send = fmax(heights.get(MAPS::SEND, cur_x, cur_z), heights.get(MAPS::CLIFF, cur_x, cur_z)); float send = fmax(heights.get(MAPS::SEND, cur_x, cur_z), heights.get(MAPS::CLIFF, cur_x, cur_z));
@ -224,24 +224,23 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
+ +
(5*send)) < cur_y + (height - 0.01- (int)height)) (5*send)) < cur_y + (height - 0.01- (int)height))
&& (cur_y < height)){ && (cur_y < height)){
id = idSand; vox.id = idSand;
} }
if (cur_y <= 2) if (cur_y <= 2)
id = idBazalt; vox.id = idBazalt;
randomgrass.setSeed(cur_x,cur_z); randomgrass.setSeed(cur_x,cur_z);
if ((id == 0) && ((height > SEA_LEVEL+0.4) || (send > 0.1)) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 56000)){ if ((vox.id == 0) && ((height > SEA_LEVEL+0.4) || (send > 0.1)) && ((int)(height + 1) == cur_y) && ((u_short)randomgrass.rand() > 56000)){
id = idGrass; vox.id = idGrass;
} }
if ((id == 0) && (height > SEA_LEVEL+0.4) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 65000)){ if ((vox.id == 0) && (height > SEA_LEVEL+0.4) && ((int)(height + 1) == cur_y) && ((u_short)randomgrass.rand() > 65000)){
id = idFlower; vox.id = idFlower;
} }
if ((height > SEA_LEVEL+1) && ((int)(height + 1) == cur_y) && ((unsigned short)randomgrass.rand() > 65533)){ if ((height > SEA_LEVEL+1) && ((int)(height + 1) == cur_y) && ((u_short)randomgrass.rand() > 65533)){
id = idWood; vox.id = idWood;
states = BLOCK_DIR_UP; vox.dir = (voxel_t)VOX_DIR::UP;
} }
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].id = id; voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x] = vox;
voxels[(cur_y * CHUNK_D + z) * CHUNK_W + x].states = states;
} }
} }
} }

View file

@ -2,129 +2,115 @@
#define VOXELS_VOXEL_H_ #define VOXELS_VOXEL_H_
#include "../typedefs.h" #include "../typedefs.h"
#include <bit>
#include <cstring>
const u_int32 BLOCK_DIR_NORTH = 0x0; namespace std {
const u_int32 BLOCK_DIR_WEST = 0x1; #ifndef __cpp_lib_bit_cast
const u_int32 BLOCK_DIR_SOUTH = 0x2;
const u_int32 BLOCK_DIR_EAST = 0x3; // #include <type_traits>
const u_int32 BLOCK_DIR_UP = 0x4;
const u_int32 BLOCK_DIR_DOWN = 0x5; template<class To, class From>
enable_if_t<
sizeof(To) == sizeof(From) &&
is_trivially_copyable_v<From> &&
is_trivially_copyable_v<To>,
To>
// constexpr support needs compiler magic
bit_cast(const From& src) noexcept
{
// static_assert(is_trivially_constructible_v<To>,
// "This implementation additionally requires "
// "destination type to be trivially constructible");
To dst;
memcpy(&dst, &src, sizeof(To));
return dst;
}
#endif
}
const u_int32 BLOCK_DIR_MASK = 0b111; /**
const u_int32 BLOCK_DIR_OFFSET = 0x0; * ,------------------------------------------------------------------------------------------------------------------------------------,
* | |
* | ,---------------------, |
* | | OLD VOXEL BIT MAP | |
* | '---------------------' |
* | 0 16 19 20 24 28 32 |
* | | | | / | | | |
* | |# # # # # # # # # # # # # # # #|# # #|#|# # # #|# # # #|#'#'#'#| |
* | id dir / \ variant value 4states |
* | |sig| |
* | |
* | |
* | ,---------------------, |
*: | | NEW VOXEL BIT MAP | |
* | '---------------------' |
* | 0 16 24 28 32 40 48 56 64 |
* | | | | | | : : : | |
* | |# # # # # # # # # # # # # # # #'# # # # # # # #|# # # #|# # # #|# # # # # # # #'# # # # # # # #'# # # # # # # #'# # # # # # # #| |
* | id variant dir signal custom bits states |
* | |
* '------------------------------------------------------------------------------------------------------------------------------------'
*/
const u_int32 BLOCK_SIG_MASK = 0b1; enum class VOX_DIR:voxel_t {
const u_int32 BLOCK_SIG_OFFSET = 0x3; NORTH,
WEST,
SOUTH,
EAST,
UP,
DOWN
};
const u_int32 BLOCK_VARIANT_MASK = 0b1111; enum class VOX_ROT:voxel_t {
const u_int32 BLOCK_VARIANT_OFFSET = 0x4; NN,
WW,
SS,
EE,
NW,
WS,
SE,
EN
};
const u_int32 BLOCK_BITS_MASK = 0b11111111; struct voxel
const u_int32 BLOCK_BITS_OFFSET = 0x8; {
const u_int32 BLOCK_VALUE_MASK = 0b1111; voxel_t id:16 = 0;
const u_int32 BLOCK_VALUE_OFFSET = 0x8; voxel_t variant:8 = 0;
const u_int32 BLOCK_STATE_MASK = 0b1111; voxel_t dir:4 = 0;
const u_int32 BLOCK_STATE_OFFSET = 0xC; voxel_t signal:4 = 0;
voxel_t bits:32 = 0;
voxel() = default;
//* VOXEL BIT MAP | voxel(voxel_t vox) : voxel(std::bit_cast<voxel>(vox)) {
//*-----------------------------------------------------------------------|
//* 0 16 19 20 24 28 32 |
//* | | | / | | | |
//* |* * * * * * * * * * * * * * * *|* * *|*|* * * *|* * * *|*'*'*'*| |
//* id dir / \ variant value 4states |
//* |sig| |
//*-----------------------------------------------------------------------|
//TODO NEW VOXEL BIT MAP |
//*-------------------------------------------------------------------------------------------------------|
//* 0 16 24 28 29 32 40 48 |
//* | | | | / | 8 bool states | value | |
//* |# # # # # # # # # # # # # # # #'# # # # # # # #|# # # #|#|# # #|#`#`#`#`#`#`#`#'# # # # # # # #| |
//* id sub id variant/ \ dir dynamic bits states |
//* |sig| |
//*-------------------------------------------------------------------------------------------------------|
struct voxel {
blockid_t id;
u_short16 states;
inline u_int32 getId() const {
return id;
} }
inline u_char8 getDir() const { explicit operator voxel_t() const {
return (states >> BLOCK_DIR_OFFSET) & BLOCK_DIR_MASK; return std::bit_cast<voxel_t>(*this);
};
inline u_char getCustomBits(u_char offset, u_char len) const {
return (bits >> (offset)) & ((1 << len) - 1);
} }
inline void setDir(u_char8 dir) { inline void setCustomBits(u_char offset, u_char len, u_char value) {
states &= ~BLOCK_DIR_MASK << BLOCK_DIR_OFFSET; int mask = ((1 << len) - 1);
states |= dir << BLOCK_DIR_OFFSET; value &= mask;
bits &= ~(mask << offset);
bits |= value << offset;
} }
inline u_char8 getVariant() const { bool operator==(const voxel& other) const {
return (states >> BLOCK_VALUE_OFFSET) & BLOCK_VARIANT_MASK; return id == other.id && variant == other.variant && dir == other.dir && signal == other.signal && bits == other.bits;
} }
inline void setVariant(u_char8 variant) { bool operator!=(const voxel& other) const {
states &= ~BLOCK_VARIANT_MASK << BLOCK_VARIANT_OFFSET; return!(*this == other);
states |= variant << BLOCK_VARIANT_OFFSET; }
}
inline u_char8 getCustomBits(u_char8 offset, u_char8 len) const {
return (states >> (BLOCK_BITS_OFFSET + offset)) & ((1 << len) - 1);
}
inline void setCustomBits(u_char8 offset, u_char8 len, u_char8 value) {
int len_musk = ((1 << len) - 1);
offset += BLOCK_BITS_OFFSET;
value &= len_musk;
states &= ~(len_musk << offset);
states |= value << offset;
}
// id of the state in range 0 to 3
inline bool getCustomState(u_char8 id) const {
return states & (1 << id+24);
}
// id of the state in range 0 to 3
inline void setCustomState(u_char8 id, bool state) {
if (state) {
states |= (1 << id+24);
} else {
states &= ~(1 << id+24);
}
}
// can use value in range 0 to 15
inline u_char8 getCustomValue() const {
return states >> 20;
}
/* if the block has a custom state you
can use value in range 0 to 15 else
you can use value in range 0 to 255 */
inline void setCustomValue(u_char8 value) {
states &= ~(0xF << 20);
states |= (value << 20);
}
inline bool getSig() const {
return (states >> BLOCK_SIG_OFFSET) & BLOCK_SIG_MASK;
}
inline void setSig(bool sig) {
states &= ~(BLOCK_SIG_MASK << BLOCK_SIG_OFFSET);
states |= sig << BLOCK_SIG_OFFSET;
}
}; };
#endif /* VOXELS_VOXEL_H_ */ #endif /* VOXELS_VOXEL_H_ */

View file

@ -5,14 +5,14 @@
#include <string.h> #include <string.h>
bool Events::_keys[KEYS_BUFFER_SIZE] = {}; bool Events::_keys[KEYS_BUFFER_SIZE] = {};
uint Events::_frames[KEYS_BUFFER_SIZE] = {}; u_int Events::_frames[KEYS_BUFFER_SIZE] = {};
uint Events::_current = 0; u_int Events::_current = 0;
int Events::scroll = 0; int Events::scroll = 0;
glm::vec2 Events::delta = {}; glm::vec2 Events::delta = {};
glm::vec2 Events::cursor = {}; glm::vec2 Events::cursor = {};
bool Events::cursor_drag = false; bool Events::cursor_drag = false;
bool Events::_cursor_locked = false; bool Events::_cursor_locked = false;
std::vector<uint> Events::codepoints; std::vector<u_int> Events::codepoints;
std::vector<int> Events::pressedKeys; std::vector<int> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings; std::unordered_map<std::string, Binding> Events::bindings;

View file

@ -8,22 +8,20 @@
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
typedef unsigned int uint;
const short KEYS_BUFFER_SIZE = 1036; const short KEYS_BUFFER_SIZE = 1036;
const short _MOUSE_KEYS_OFFSET = 1024; const short _MOUSE_KEYS_OFFSET = 1024;
class Events { class Events {
public: public:
static bool _keys[KEYS_BUFFER_SIZE]; static bool _keys[KEYS_BUFFER_SIZE];
static uint _frames[KEYS_BUFFER_SIZE]; static u_int _frames[KEYS_BUFFER_SIZE];
static uint _current; static u_int _current;
static int scroll; static int scroll;
static glm::vec2 delta; static glm::vec2 delta;
static glm::vec2 cursor; static glm::vec2 cursor;
static bool cursor_drag; static bool cursor_drag;
static bool _cursor_locked; static bool _cursor_locked;
static std::vector<uint> codepoints; static std::vector<u_int> codepoints;
static std::vector<int> pressedKeys; static std::vector<int> pressedKeys;
static std::unordered_map<std::string, Binding> bindings; static std::unordered_map<std::string, Binding> bindings;

View file

@ -15,8 +15,8 @@ GLFWwindow* Window::window = nullptr;
DisplaySettings* Window::settings = nullptr; DisplaySettings* Window::settings = nullptr;
std::stack<vec4> Window::scissorStack; std::stack<vec4> Window::scissorStack;
vec4 Window::scissorArea; vec4 Window::scissorArea;
uint Window::width = 0; u_int Window::width = 0;
uint Window::height = 0; u_int Window::height = 0;
int Window::posX = 0; int Window::posX = 0;
int Window::posY = 0; int Window::posY = 0;
@ -292,7 +292,7 @@ DisplaySettings* Window::getSettings() {
} }
ImageData* Window::takeScreenshot() { ImageData* Window::takeScreenshot() {
u_char8* data = new u_char8[width * height * 3]; u_char* data = new u_char[width * height * 3];
glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
return new ImageData(ImageFormat::rgb888, width, height, data); return new ImageData(ImageFormat::rgb888, width, height, data);
@ -307,10 +307,10 @@ bool Window::tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor) {
glm::ivec4 workArea(0); glm::ivec4 workArea(0);
glfwGetWindowFrameSize(window, &windowFrame.x, &windowFrame.y, &windowFrame.z, &windowFrame.w); glfwGetWindowFrameSize(window, &windowFrame.x, &windowFrame.y, &windowFrame.z, &windowFrame.w);
glfwGetMonitorWorkarea(monitor, &workArea.x, &workArea.y, &workArea.z, &workArea.w); glfwGetMonitorWorkarea(monitor, &workArea.x, &workArea.y, &workArea.z, &workArea.w);
if (Window::width > (uint)workArea.z) Window::width = (uint)workArea.z; if (Window::width > (u_int)workArea.z) Window::width = (u_int)workArea.z;
if (Window::height > (uint)workArea.w) Window::height = (uint)workArea.w; if (Window::height > (u_int)workArea.w) Window::height = (u_int)workArea.w;
if (Window::width >= (uint)(workArea.z - (windowFrame.x + windowFrame.z)) && if (Window::width >= (u_int)(workArea.z - (windowFrame.x + windowFrame.z)) &&
Window::height >= (uint)(workArea.w - (windowFrame.y + windowFrame.w))) { Window::height >= (u_int)(workArea.w - (windowFrame.y + windowFrame.w))) {
glfwMaximizeWindow(window); glfwMaximizeWindow(window);
return true; return true;
} }

View file

@ -24,8 +24,8 @@ class Window {
public: public:
static int posX; static int posX;
static int posY; static int posY;
static uint width; static u_int width;
static uint height; static u_int height;
static int initialize(DisplaySettings& settings); static int initialize(DisplaySettings& settings);
static void terminate(); static void terminate();

View file

@ -19,7 +19,7 @@ Level::Level(World* world, const Content* content, Player* player, EngineSetting
settings(settings) { settings(settings) {
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0)); physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
uint matrixSize = (settings.chunks.loadDistance+ u_int matrixSize = (settings.chunks.loadDistance+
settings.chunks.padding) * 2; settings.chunks.padding) * 2;
chunks = new Chunks(matrixSize, matrixSize, 0, 0, chunks = new Chunks(matrixSize, matrixSize, 0, 0,
world->wfile, events, content); world->wfile, events, content);

View file

@ -25,7 +25,7 @@ world_load_error::world_load_error(string message) : std::runtime_error(message)
World::World(string name, World::World(string name,
path directory, path directory,
uint64_t seed, u_long seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack> packs) const std::vector<ContentPack> packs)
@ -71,7 +71,7 @@ const float DEF_PLAYER_SPEED = 4.0f;
Level* World::create(string name, Level* World::create(string name,
path directory, path directory,
uint64_t seed, u_long seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs) { const std::vector<ContentPack>& packs) {

View file

@ -30,7 +30,7 @@ class World {
public: public:
std::string name; std::string name;
WorldFiles* wfile; WorldFiles* wfile;
uint64_t seed; u_long seed;
/* Day/night loop timer in range 0..1 /* Day/night loop timer in range 0..1
0.0 - is midnight 0.0 - is midnight
@ -41,7 +41,7 @@ public:
World(std::string name, World(std::string name,
std::filesystem::path directory, std::filesystem::path directory,
uint64_t seed, u_long seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
std::vector<ContentPack> packs); std::vector<ContentPack> packs);
@ -55,7 +55,7 @@ public:
static Level* create(std::string name, static Level* create(std::string name,
std::filesystem::path directory, std::filesystem::path directory,
uint64_t seed, u_long seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs); const std::vector<ContentPack>& packs);