20 Commits

Author SHA1 Message Date
3ebed84cdb FIX: crash while joining to large world server 2026-07-02 16:32:17 +03:00
b73f7f121d ee 2026-05-12 21:29:17 +02:00
Kolyah35
5e19ef778e FIX: memset ._. 2026-05-12 22:12:52 +03:00
5bc19c4ab2 FEAT: Ban command, save ops and banned ppl in files
TODO: fix many crashes...
2026-05-09 16:37:11 +02:00
dfb3bf1da7 FIX: render
ty shredder
2026-05-09 14:41:31 +02:00
263118f2e0 Fix 2026-05-09 13:09:45 +02:00
1d0264a9c5 FEAT: World size 2026-05-09 01:45:19 +02:00
43f0d1f75f server config 2026-05-08 23:42:41 +02:00
70308d4151 qqqqqq 2026-05-05 21:55:03 +02:00
830549791a 121 2026-05-05 21:51:35 +02:00
27b22d8108 vegetables 2026-05-05 21:41:26 +02:00
4047fd71e1 oi 2026-05-05 21:36:35 +02:00
30dd344208 123 2026-05-05 21:33:28 +02:00
7e9de7a461 fck android build!!! 2026-05-05 21:19:08 +02:00
2465292473 jopa 2026-05-05 21:14:17 +02:00
26b2c196fa :\ 2026-05-05 21:09:42 +02:00
18793a6e05 idk? 2026-05-05 20:40:41 +02:00
Kolyah35
3e48c7fabd FIX: compilation 2026-05-02 21:31:48 +03:00
de72a9a3b7 all was broken :\ 2026-04-27 23:43:36 +02:00
0645905fc6 eeee it doesnt work 😭 2026-04-27 22:18:18 +02:00
32 changed files with 529 additions and 221 deletions

View File

@@ -109,6 +109,7 @@ LOCAL_SRC_FILES := ../../../src/main.cpp \
../../../src/client/renderer/Textures.cpp \
../../../src/client/renderer/TileRenderer.cpp \
../../../src/client/renderer/gles.cpp \
../../../src/world/level/LevelConstants.cpp \
../../../src/client/renderer/culling/Frustum.cpp \
../../../src/client/renderer/entity/ArrowRenderer.cpp \
../../../src/client/renderer/entity/ChickenRenderer.cpp \
@@ -139,6 +140,12 @@ LOCAL_SRC_FILES := ../../../src/main.cpp \
../../../src/network/Packet.cpp \
../../../src/network/RakNetInstance.cpp \
../../../src/network/ServerSideNetworkHandler.cpp \
../../../src/commands/Command.cpp \
../../../src/commands/CommandHelp.cpp \
../../../src/commands/CommandKick.cpp \
../../../src/commands/CommandBan.cpp \
../../../src/commands/CommandManager.cpp \
../../../src/commands/CommandOp.cpp \
../../../src/server/ServerLevel.cpp \
../../../src/server/ServerPlayer.cpp \
../../../src/util/DataIO.cpp \

View File

@@ -228,6 +228,7 @@ LOCAL_SRC_FILES := ../../../src/main.cpp \
../../../src/world/level/tile/entity/TileEntity.cpp \
../../../src/world/level/tile/entity/FurnaceTileEntity.cpp \
../../../src/world/phys/HitResult.cpp
../../../src/world/level/LevelConstants.cpp
LOCAL_CFLAGS := -Wno-psabi $(LOCAL_CFLAGS)

View File

@@ -278,7 +278,10 @@ void Minecraft::setLevel(Level* level, const std::string& message /* ="" */, Loc
}
this->level = level;
// So uhhh
level->ops.emplace(options.getStringValue(OPTIONS_USERNAME));
std::string op = options.getStringValue(OPTIONS_USERNAME);
std::transform(op.begin(), op.end(), op.begin(), ::tolower);
level->ops.emplace(op);
_hasSignaledGeneratingLevelFinished = false;
#ifdef STANDALONE_SERVER
const bool threadedLevelCreation = false;
@@ -362,10 +365,10 @@ void Minecraft::prepareLevel(const std::string& title) {
if (!level->isNew())
level->setUpdateLights(false);
int Max = CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH;
int Max = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH;
int pp = 0;
for (int x = 8; x < (CHUNK_CACHE_WIDTH * CHUNK_WIDTH); x += CHUNK_WIDTH) {
for (int z = 8; z < (CHUNK_CACHE_WIDTH * CHUNK_WIDTH); z += CHUNK_WIDTH) {
for (int x = 8; x < (LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_WIDTH); x += LevelConstants::CHUNK_WIDTH) {
for (int z = 8; z < (LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_WIDTH); z += LevelConstants::CHUNK_WIDTH) {
progressStagePercentage = 100 * pp++ / Max;
//printf("level generation progress %d\n", progressStagePercentage);
B.start();
@@ -382,9 +385,9 @@ void Minecraft::prepareLevel(const std::string& title) {
level->setUpdateLights(true);
C.start();
for (int x = 0; x < CHUNK_CACHE_WIDTH; x++)
for (int x = 0; x < LevelConstants::CHUNK_CACHE_WIDTH; x++)
{
for (int z = 0; z < CHUNK_CACHE_WIDTH; z++)
for (int z = 0; z < LevelConstants::CHUNK_CACHE_WIDTH; z++)
{
LevelChunk* chunk = level->getChunk(x, z);
if (chunk && !chunk->createdFromSave)
@@ -732,19 +735,19 @@ void Minecraft::tickInput() {
}
// TODO: replace it with client /give command :face_vomiting:
if (key == Keyboard::KEY_F4) {
player->inventory->add(new ItemInstance(Tile::redBrick));
player->inventory->add(new ItemInstance(Item::ironIngot, 64));
player->inventory->add(new ItemInstance(Item::ironIngot, 34));
player->inventory->add(new ItemInstance(Tile::stonecutterBench));
player->inventory->add(new ItemInstance(Tile::workBench));
player->inventory->add(new ItemInstance(Tile::furnace));
player->inventory->add(new ItemInstance(Tile::wood, 54));
player->inventory->add(new ItemInstance(Item::stick, 14));
player->inventory->add(new ItemInstance(Item::coal, 31));
player->inventory->add(new ItemInstance(Tile::sand, 6));
// if (key == Keyboard::KEY_F4) {
// player->inventory->add(new ItemInstance(Tile::redBrick));
// player->inventory->add(new ItemInstance(Item::ironIngot, 64));
// player->inventory->add(new ItemInstance(Item::ironIngot, 34));
// player->inventory->add(new ItemInstance(Tile::stonecutterBench));
// player->inventory->add(new ItemInstance(Tile::workBench));
// player->inventory->add(new ItemInstance(Tile::furnace));
// player->inventory->add(new ItemInstance(Tile::wood, 54));
// player->inventory->add(new ItemInstance(Item::stick, 14));
// player->inventory->add(new ItemInstance(Item::coal, 31));
// player->inventory->add(new ItemInstance(Tile::sand, 6));
}
// }
if (key == Keyboard::KEY_F5) {
options.toggle(OPTIONS_THIRD_PERSON_VIEW);
@@ -1134,6 +1137,7 @@ bool Minecraft::supportNonTouchScreen() {
}
void Minecraft::init()
{
LevelChunk::ChunkBlockCount = LevelConstants::CHUNK_BLOCK_COUNT;
#ifndef STANDALONE_SERVER
checkGlError("Init enter");
@@ -1322,12 +1326,9 @@ bool Minecraft::joinMultiplayerFromString( const std::string& server )
} else {
ip = server;
}
printf("%s \n", port.c_str());
if (isLookingForMultiplayer && netCallback) {
isLookingForMultiplayer = false;
printf("test");
int portNum = atoi(port.c_str());
return raknetInstance->connect(ip.c_str(), portNum);
}
@@ -1606,6 +1607,6 @@ void Minecraft::addMessage(const std::string& msg) {
#ifndef STANDALONE_SERVER
gui.addMessage(msg);
#else
LOGI("%s", msg.c_str());
LOGI("%s \n", msg.c_str());
#endif
}

View File

@@ -65,7 +65,7 @@ LevelRenderer::LevelRenderer( Minecraft* mc)
destroyProgress(0)
{
#ifdef OPENGL_ES
int maxChunksWidth = 2 * LEVEL_WIDTH / CHUNK_SIZE + 1;
int maxChunksWidth = 2 * LevelConstants::LEVEL_WIDTH / CHUNK_SIZE + 1;
numListsOrBuffers = maxChunksWidth * maxChunksWidth * (128/CHUNK_SIZE) * 3;
chunkBuffers = new GLuint[numListsOrBuffers];
glGenBuffers2(numListsOrBuffers, chunkBuffers);

View File

@@ -3,5 +3,8 @@
#include <client/Minecraft.h>
bool Command::isPlayerOp(Minecraft& mc, Player& player) {
return mc.level->ops.find(player.name) != mc.level->ops.end();
std::string nicknameLower = player.name;
std::transform(nicknameLower.begin(), nicknameLower.end(), nicknameLower.begin(), ::tolower);
return mc.level->ops.find(nicknameLower) != mc.level->ops.end();
}

View File

@@ -0,0 +1,52 @@
#include "CommandBan.hpp"
#include "commands/Command.hpp"
#include "network/RakNetInstance.h"
#include "raknet/RakPeerInterface.h"
#include "world/level/Level.h"
#include <algorithm>
#include <client/Minecraft.h>
CommandBan::CommandBan() : Command("ban") {}
std::string CommandBan::execute(Minecraft& mc, Player& player, const std::vector<std::string>& args) {
if (!isPlayerOp(mc, player)) {
return "You aren't enough priveleged to run this command";
}
if (args.empty()) {
return help(mc);
}
std::string nicknameLower = args[0];
std::transform(nicknameLower.begin(), nicknameLower.end(), nicknameLower.begin(), ::tolower);
auto it = std::find_if(mc.level->players.begin(), mc.level->players.end(), [args, nicknameLower] (auto& it) -> bool {
std::string lower = it->name;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
return lower == nicknameLower;
});
if (*it == (Player*)mc.player) {
return "ban: you can't ban urself lol";
}
if (it != mc.level->players.end()) {
mc.level->removePlayer(*it);
(*it)->remove();
mc.raknetInstance->getPeer()->CloseConnection((*it)->owner, true);
} else {
for (auto& banned : mc.level->bannedPpl) {
if (nicknameLower == banned) {
return args[0] + "already banned!";
}
}
}
mc.level->bannedPpl.insert(nicknameLower);
return "ban: successfully banned player " + args[0];
}
std::string CommandBan::help(Minecraft& mc) {
return "Usage: /ban <player>";
}

View File

@@ -0,0 +1,9 @@
#include "Command.hpp"
class CommandBan : public Command {
public:
CommandBan();
std::string execute(Minecraft& mc, Player& player, const std::vector<std::string>& args);
std::string help(Minecraft& mc);
};

View File

@@ -1,12 +1,11 @@
#include "CommandKick.hpp"
#include "commands/Command.hpp"
#include "network/RakNetInstance.h"
#include "raknet/RakPeer.h"
#include "raknet/RakPeerInterface.h"
#include "world/level/Level.h"
#include <algorithm>
#include <client/Minecraft.h>
CommandKick::CommandKick() : Command("kick") {}
std::string CommandKick::execute(Minecraft& mc, Player& player, const std::vector<std::string>& args) {
@@ -18,8 +17,14 @@ std::string CommandKick::execute(Minecraft& mc, Player& player, const std::vecto
return help(mc);
}
auto it = std::find_if(mc.level->players.begin(), mc.level->players.end(), [args] (auto& it) -> bool {
return it->name == args[0];
std::string nicknameLower = args[0];
std::transform(nicknameLower.begin(), nicknameLower.end(), nicknameLower.begin(), ::tolower);
auto it = std::find_if(mc.level->players.begin(), mc.level->players.end(), [args, nicknameLower] (auto& it) -> bool {
std::string lower = it->name;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
return lower == nicknameLower;
});
if (it == mc.level->players.end()) {
@@ -33,7 +38,7 @@ std::string CommandKick::execute(Minecraft& mc, Player& player, const std::vecto
mc.level->removePlayer(*it);
(*it)->remove();
mc.raknetInstance->getPeer()->CloseConnection((*it)->owner, true);
mc.addMessage("kick: successfully kicked player " + args[0]);
return "kick: successfully kicked player " + args[0];
}
std::string CommandKick::help(Minecraft& mc) {

View File

@@ -7,12 +7,11 @@
#include "commands/CommandHelp.hpp"
#include "commands/CommandKick.hpp"
#include "commands/CommandOp.hpp"
#include "commands/CommandBan.hpp"
#include "network/packet/ChatPacket.h"
#include "network/RakNetInstance.h"
#include "world/level/Level.h"
CommandManager::CommandManager() {
registerAllCommands();
}
@@ -21,6 +20,7 @@ void CommandManager::registerAllCommands() {
m_commands.push_back(new CommandHelp());
m_commands.push_back(new CommandKick());
m_commands.push_back(new CommandOp());
m_commands.push_back(new CommandBan());
}
std::vector<std::string> CommandManager::getListAllCommands() {

View File

@@ -1,12 +1,9 @@
#include "CommandOp.hpp"
#include "commands/Command.hpp"
#include "network/RakNetInstance.h"
#include "raknet/RakPeer.h"
#include "world/level/Level.h"
#include <algorithm>
#include <client/Minecraft.h>
CommandOp::CommandOp() : Command("op") {}
std::string CommandOp::execute(Minecraft& mc, Player& player, const std::vector<std::string>& args) {
@@ -18,15 +15,14 @@ std::string CommandOp::execute(Minecraft& mc, Player& player, const std::vector<
return help(mc);
}
auto it = std::find_if(mc.level->players.begin(), mc.level->players.end(), [args] (auto& it) -> bool {
return it->name == args[0];
});
if (mc.level->ops.find(args[0]) != mc.level->ops.end()) {
std::string nicknameLower = args[0];
std::transform(nicknameLower.begin(), nicknameLower.end(), nicknameLower.begin(), ::tolower);
if (mc.level->ops.find(nicknameLower) != mc.level->ops.end()) {
return "op: player " + args[0] + " already opped";
}
mc.level->ops.emplace((*it)->name);
mc.level->ops.emplace(nicknameLower);
return "op: successfully opped player " + args[0];
}

View File

@@ -1,3 +1,4 @@
#include <fstream>
#include <iostream>
#include "NinecraftApp.h"
#include "AppPlatform.h"
@@ -11,6 +12,7 @@
#include "server/ArgumentsSettings.h"
#include "platform/time.h"
#include "SharedConstants.h"
#include "world/level/LevelConstants.h"
#define MAIN_CLASS NinecraftApp
static App* g_app = 0;
@@ -27,45 +29,175 @@ void signal_callback_handler(int signum) {
}
}
int main(int numArguments, char* pszArgs[]) {
ArgumentsSettings aSettings(numArguments, pszArgs);
if(aSettings.getShowHelp()) {
// TODO: Map with args and print it with std::cout and for loop
// TODO: World size setting
std::string findStringInConfig(std::string line, std::string config) {
auto valuePos = config.find(line);
ArgumentsSettings defaultSettings(0, NULL);
if (line.empty()) {
throw std::runtime_error("Key cannot be empty");
}
printf("Minecraft Pockect Edition Server %s\n", Common::getGameVersionString("").c_str());
printf("-------------------------------------------------------\n");
if (valuePos == std::string::npos) {
throw std::runtime_error("Cannot find value");
}
std::string valueStr = "";
printf("--cachepath - Path to where the server can store temp stuff (not sure if this is used) [default: \"%s\"]\n", defaultSettings.getCachePath().c_str());
printf("--externalpath - The path to the place where the server should store the levels. [default: \"%s\"]\n", defaultSettings.getExternalPath().c_str());
printf("--levelname - The name of the server [default: \"%s\"]\n", defaultSettings.getLevelName().c_str());
printf("--gamemode - The name of the gamemode [default: \"%s\"]\n", defaultSettings.getGamemode().c_str());
printf("--leveldir - The name of the server [default: \"%s\"]\n", defaultSettings.getLevelDir().c_str());
printf("--help - Shows this message.\n");
printf("--port - The port to run the server on. [default: %d]\n", defaultSettings.getPort());
printf("--serverkey - The key that the server should use for API calls. [default: \"%s\"]\n", defaultSettings.getServerKey().c_str());
valuePos += 1 + line.size();
printf("-------------------------------------------------------\n");
while (true) {
auto sym = config.at(valuePos);
return 0;
if (sym == '\n') {
break;
}
if (valuePos == config.size()) {
throw std::runtime_error("Cannot find end of the line");
}
valueStr += sym;
valuePos++;
}
return valueStr;
}
int main(int numArguments, char* pszArgs[]) {
std::ifstream serverProperties("server.properties");
int port = 0;
int gamemode = 0;
int worldSize = 0;
std::string levelDir = "";
std::string cachePath = "";
std::string externalPath = "";
std::string levelName = "";
if (serverProperties.is_open()) {
std::string propString;
std::string line;
while (std::getline(serverProperties, line)) {
propString += line;
propString += "\n";
}
if (propString.empty()) {
throw std::runtime_error("Config cannot be empty");
}
port = std::stoi(findStringInConfig("port", propString));
worldSize = std::stoi(findStringInConfig("world-size", propString));
if (worldSize < 16 || worldSize > 32) {
throw std::runtime_error("Incorrect world size.");
}
LevelConstants::CHUNK_CACHE_WIDTH = worldSize;
LevelConstants::LEVEL_WIDTH = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_WIDTH;
LevelConstants::LEVEL_DEPTH = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_DEPTH;
cachePath = findStringInConfig("level-cachepath", propString);
externalPath = findStringInConfig("level-externalpath", propString);
levelName = findStringInConfig("level-name", propString);
levelDir = findStringInConfig("level-dir", propString);
std::string gamemodeStr = findStringInConfig("gamemode", propString);
if (gamemodeStr == "survival") {
gamemode = GameType::Survival;
} else if (gamemodeStr == "creative") {
gamemode = GameType::Creative;
} else {
gamemode = GameType::Survival;
}
printf("%d %s %s %d %d\n", port, cachePath.c_str(), externalPath.c_str(), gamemode, LevelConstants::CHUNK_CACHE_WIDTH);
} else {
std::ofstream defaultProperties("server.properties");
if (defaultProperties.is_open()) {
ArgumentsSettings defaultSettings(0, NULL);
std::stringstream buffer;
buffer << "// MCPE Server properties" << std::endl;
buffer << "port=" << defaultSettings.getPort() << std::endl;
buffer << "level-dir=" << defaultSettings.getLevelDir() << std::endl;
buffer << "level-cachepath=" << defaultSettings.getCachePath() << std::endl;
buffer << "level-externalpath=" << defaultSettings.getExternalPath() << std::endl;
buffer << "gamemode=survival" << std::endl;
buffer << "level-name=" << defaultSettings.getLevelName() << std::endl;
buffer << "world-size=16" << std::endl;
defaultProperties << buffer.str();
printf("%s \n", buffer.str().c_str());
printf("Edit a config file first!\n");
defaultProperties.close();
exit(0);
} else {
throw std::runtime_error("Cannot create default config.");
}
}
printf("Level Name: %s\n", aSettings.getLevelName().c_str());
printf("Level Name: %s\n", levelName.c_str());
AppContext appContext;
appContext.platform = new AppPlatform();
App* app = new MAIN_CLASS();
signal(SIGINT, signal_callback_handler);
g_app = app;
((MAIN_CLASS*)g_app)->externalStoragePath = aSettings.getExternalPath();
((MAIN_CLASS*)g_app)->externalCacheStoragePath = aSettings.getCachePath();
((MAIN_CLASS*)g_app)->externalStoragePath = externalPath;
((MAIN_CLASS*)g_app)->externalCacheStoragePath = cachePath;
g_app->init(appContext);
LevelSettings settings(getEpochTimeS(), GameType::Creative);
LevelSettings settings(getEpochTimeS(), gamemode);
float startTime = getTimeS();
((MAIN_CLASS*)g_app)->selectLevel(aSettings.getLevelDir(), aSettings.getLevelName(), settings);
((MAIN_CLASS*)g_app)->hostMultiplayer(aSettings.getPort());
((MAIN_CLASS*)g_app)->selectLevel(levelDir, levelName, settings);
((MAIN_CLASS*)g_app)->hostMultiplayer(port);
// Reading ops
std::ifstream ops("ops.txt");
if (ops.is_open()) {
std::string line;
while (std::getline(ops, line)) {
if (!line.empty()) {
((MAIN_CLASS*)g_app)->level->ops.insert(line);
}
}
} else {
std::ofstream opsDefault("ops.txt");
if (!opsDefault.is_open()) {
throw std::runtime_error("Cannot create ops list.");
}
}
// Reading banned ppl
std::ifstream banned("banned-players.txt");
if (banned.is_open()) {
std::string line;
while (std::getline(banned, line)) {
if (!line.empty()) {
((MAIN_CLASS*)g_app)->level->bannedPpl.insert(line);
}
}
} else {
std::ofstream bannedPpl("banned-players.txt");
if (!bannedPpl.is_open()) {
throw std::runtime_error("Cannot create banned players list.");
}
}
std::cout << "Level has been generated in " << getTimeS() - startTime << std::endl;
((MAIN_CLASS*)g_app)->level->saveLevelData();
@@ -76,7 +208,31 @@ int main(int numArguments, char* pszArgs[]) {
//pthread_yield();
sleepMs(20);
}
std::ofstream opsWrite("ops.txt");
if (opsWrite.is_open()) {
for (auto& op : ((MAIN_CLASS*)g_app)->level->ops) {
opsWrite << op << std::endl;
}
opsWrite.close();
} else {
throw std::runtime_error("Cannot open ops list.");
}
std::ofstream bannedWrite("banned-players.txt");
if (bannedWrite.is_open()) {
for (auto& banned : ((MAIN_CLASS*)g_app)->level->bannedPpl) {
bannedWrite << banned << std::endl;
}
bannedWrite.close();
} else {
throw std::runtime_error("Cannot open banned players list.");
}
((MAIN_CLASS*)g_app)->level->saveLevelData();
delete app;
appContext.platform->finish();
delete appContext.platform;

View File

@@ -12,6 +12,8 @@
#include "../client/Minecraft.h"
#include "../client/gamemode/GameMode.h"
#include "world/item/ItemInstance.h"
#include "world/level/LevelConstants.h"
#include <cstddef>
#ifndef STANDALONE_SERVER
#include "../client/gui/screens/DisconnectionScreen.h"
#endif
@@ -44,6 +46,9 @@ ClientSideNetworkHandler::ClientSideNetworkHandler(Minecraft* minecraft, IRakNet
requestNextChunkPosition(0),
requestNextChunkIndex(0)
{
NumRequestChunks = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH;
requestNextChunkIndexList.resize(NumRequestChunks);
chunksLoaded.resize(NumRequestChunks);
rakPeer = raknetInstance->getPeer();
}
@@ -61,7 +66,7 @@ void ClientSideNetworkHandler::requestNextChunk()
//LOGI("requesting chunks @ (%d, %d)\n", chunk.x, chunk.y);
//raknetInstance->send(new RequestChunkPacket(requestNextChunkPosition % CHUNK_CACHE_WIDTH, requestNextChunkPosition / CHUNK_CACHE_WIDTH));
//raknetInstance->send(new RequestChunkPacket(requestNextChunkPosition % LevelConstants::CHUNK_CACHE_WIDTH, requestNextChunkPosition / LevelConstants::CHUNK_CACHE_WIDTH));
requestNextChunkIndex++;
requestNextChunkPosition++;
}
@@ -69,16 +74,16 @@ void ClientSideNetworkHandler::requestNextChunk()
bool ClientSideNetworkHandler::areAllChunksLoaded()
{
return (requestNextChunkPosition >= (CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH));
return (requestNextChunkPosition >= (LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH));
}
bool ClientSideNetworkHandler::isChunkLoaded(int x, int z)
{
if (x < 0 || x >= CHUNK_CACHE_WIDTH || z < 0 || z >= CHUNK_CACHE_WIDTH) {
if (x < 0 || x >= LevelConstants::CHUNK_CACHE_WIDTH || z < 0 || z >= LevelConstants::CHUNK_CACHE_WIDTH) {
LOGE("Error: Tried to request chunk (%d, %d)\n", x, z);
return true;
}
return chunksLoaded[x * CHUNK_CACHE_WIDTH + z];
return chunksLoaded[x * LevelConstants::CHUNK_CACHE_WIDTH + z];
//return areAllChunksLoaded();
}
@@ -99,6 +104,10 @@ void ClientSideNetworkHandler::onUnableToConnect()
void ClientSideNetworkHandler::onDisconnect(const RakNet::RakNetGUID& guid)
{
LevelConstants::CHUNK_CACHE_WIDTH = 16;
LevelConstants::LEVEL_WIDTH = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_WIDTH;
LevelConstants::LEVEL_DEPTH = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_DEPTH;
// TODO: Good disconnecting
LOGI("onDisconnect\n");
if (level)
@@ -112,6 +121,7 @@ void ClientSideNetworkHandler::onDisconnect(const RakNet::RakNetGUID& guid)
}
}
}
minecraft->setLevel(NULL);
#ifndef STANDALONE_SERVER
minecraft->gui.addMessage("Disconnected from server");
#endif
@@ -139,6 +149,12 @@ void ClientSideNetworkHandler::handle(const RakNet::RakNetGUID& source, LoginSta
LOGI("Disconnect! Nickname is taken!\n");
#ifndef STANDALONE_SERVER
minecraft->setScreen(new DisconnectionScreen("Could not connect: Nickname is taken!"));
#endif
}
if (packet->status == LoginStatus::Failed_Banned) {
LOGI("Disconnect! You're banned from this server!\n");
#ifndef STANDALONE_SERVER
minecraft->setScreen(new DisconnectionScreen("Could not connect: You're banned from this server!"));
#endif
}
}
@@ -155,6 +171,19 @@ void ClientSideNetworkHandler::handle(const RakNet::RakNetGUID& source, StartGam
}
#endif
// @todo @note i think its trash
// we can put it directly to selectLevel?
if (packet->chunkCacheWidth != 0) {
printf("lol \n");
LevelConstants::CHUNK_CACHE_WIDTH = packet->chunkCacheWidth;
LevelConstants::LEVEL_WIDTH = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_WIDTH;
LevelConstants::LEVEL_DEPTH = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_DEPTH;
LevelChunk::ChunkBlockCount = LevelConstants::CHUNK_BLOCK_COUNT;
}
NumRequestChunks = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH;
requestNextChunkIndexList.resize(NumRequestChunks);
chunksLoaded.resize(NumRequestChunks);
const std::string& levelId = LevelStorageSource::TempLevelId;
LevelStorageSource* storageSource = minecraft->getLevelSource();
storageSource->deleteLevel(levelId);
@@ -333,7 +362,7 @@ void ClientSideNetworkHandler::handle(const RakNet::RakNetGUID& source, RemoveEn
if (!level) return;
Entity* entity = level->getEntity(packet->entityId);
LOGI("RemoveEntityPacket %p %p, %d\n", entity, minecraft->player, entity?(int)(entity->isPlayer()): -1);
// LOGI("RemoveEntityPacket %p %p, %d\n", entity, minecraft->player, entity?(int)(entity->isPlayer()): -1);
if (!entity) return;
level->removeEntity(entity);
@@ -533,19 +562,19 @@ void ClientSideNetworkHandler::handle(const RakNet::RakNetGUID& source, ChunkDat
//unsigned char* blockIds = chunk->getBlockData();
DataLayer& blockData = chunk->data;
const int setSize = LEVEL_HEIGHT / 8;
const int setShift = 4; // power of LEVEL_HEIGHT / 8
const int setSize = LevelConstants::LEVEL_HEIGHT / 8;
const int setShift = 4; // power of LevelConstants::LEVEL_HEIGHT / 8
bool recalcHeight = false;
int x0 = 16, x1 = 0, z0 = 16, z1 = 0, y0 = LEVEL_HEIGHT, y1 = 0;
int x0 = 16, x1 = 0, z0 = 16, z1 = 0, y0 = LevelConstants::LEVEL_HEIGHT, y1 = 0;
int rx = packet->x << 4;
int rz = packet->z << 4;
unsigned char readBlockBuffer[setSize];
unsigned char readDataBuffer[setSize / 2];
unsigned char* readBlockBuffer = new unsigned char[setSize];
unsigned char* readDataBuffer = new unsigned char[setSize / 2];
for (int i = 0; i < CHUNK_COLUMNS; i++)
for (int i = 0; i < LevelConstants::CHUNK_COLUMNS; i++)
{
unsigned char updateBits = 0;
packet->chunkData.Read(updateBits);
@@ -554,8 +583,8 @@ void ClientSideNetworkHandler::handle(const RakNet::RakNetGUID& source, ChunkDat
{
recalcHeight = true;
int colX = (i % CHUNK_WIDTH);
int colZ = (i / CHUNK_WIDTH);
int colX = (i % LevelConstants::CHUNK_WIDTH);
int colZ = (i / LevelConstants::CHUNK_WIDTH);
int colDataPosition = colX << 11 | colZ << 7;
for (int set = 0; set < 8; set++)
@@ -589,30 +618,33 @@ void ClientSideNetworkHandler::handle(const RakNet::RakNetGUID& source, ChunkDat
{
y0 = ((1 << set) << setShift);
}
if (((1 << set) << setShift) + (LEVEL_HEIGHT / 8) - 1 > y1)
if (((1 << set) << setShift) + (LevelConstants::LEVEL_HEIGHT / 8) - 1 > y1)
{
y1 = ((1 << set) << setShift) + (LEVEL_HEIGHT / 8) - 1;
y1 = ((1 << set) << setShift) + (LevelConstants::LEVEL_HEIGHT / 8) - 1;
}
}
if ((i % CHUNK_WIDTH) < x0)
if ((i % LevelConstants::CHUNK_WIDTH) < x0)
{
x0 = (i % CHUNK_WIDTH);
x0 = (i % LevelConstants::CHUNK_WIDTH);
}
if ((i % CHUNK_WIDTH) > x1)
if ((i % LevelConstants::CHUNK_WIDTH) > x1)
{
x1 = (i % CHUNK_WIDTH);
x1 = (i % LevelConstants::CHUNK_WIDTH);
}
if ((i / CHUNK_WIDTH) < z0)
if ((i / LevelConstants::CHUNK_WIDTH) < z0)
{
z0 = (i / CHUNK_WIDTH);
z0 = (i / LevelConstants::CHUNK_WIDTH);
}
if ((i / CHUNK_WIDTH) > z1)
if ((i / LevelConstants::CHUNK_WIDTH) > z1)
{
z1 = (i / CHUNK_WIDTH);
z1 = (i / LevelConstants::CHUNK_WIDTH);
}
}
}
delete[] readBlockBuffer;
delete[] readDataBuffer;
if (recalcHeight)
{
// chunk->recalcHeightmap();
@@ -637,7 +669,7 @@ void ClientSideNetworkHandler::handle(const RakNet::RakNetGUID& source, ChunkDat
//chunk->terrainPopulated = true;
chunk->unsaved = false;
chunksLoaded[packet->x * CHUNK_CACHE_WIDTH + packet->z] = true;
chunksLoaded[packet->x * LevelConstants::CHUNK_CACHE_WIDTH + packet->z] = true;
if (areAllChunksLoaded())
{
@@ -685,18 +717,18 @@ void ClientSideNetworkHandler::arrangeRequestChunkOrder() {
clearChunksLoaded();
// Default sort is around center of the world
int cx = CHUNK_CACHE_WIDTH / 2;
int cz = CHUNK_CACHE_WIDTH / 2;
int cx = LevelConstants::CHUNK_CACHE_WIDTH / 2;
int cz = LevelConstants::CHUNK_CACHE_WIDTH / 2;
// If player exists, let's sort around him
Player* p = minecraft? minecraft->player : NULL;
if (p) {
cx = Mth::floor(p->x / (float)CHUNK_WIDTH);
cz = Mth::floor(p->z / (float)CHUNK_DEPTH);
cx = Mth::floor(p->x / (float)LevelConstants::CHUNK_WIDTH);
cz = Mth::floor(p->z / (float)LevelConstants::CHUNK_DEPTH);
}
_ChunkSorter sorter(cx, cz);
std::sort(requestNextChunkIndexList, requestNextChunkIndexList + NumRequestChunks, sorter);
std::sort(requestNextChunkIndexList.begin(), requestNextChunkIndexList.end(), sorter);
}
void ClientSideNetworkHandler::levelGenerated(Level* level)
@@ -986,8 +1018,8 @@ void ClientSideNetworkHandler::clearChunksLoaded()
{
// Init the chunk positions
for (int i = 0; i < NumRequestChunks; ++i) {
requestNextChunkIndexList[i].x = i/CHUNK_WIDTH;
requestNextChunkIndexList[i].y = i%CHUNK_WIDTH;
requestNextChunkIndexList[i].x = i / LevelConstants::CHUNK_WIDTH;
requestNextChunkIndexList[i].y = i % LevelConstants::CHUNK_WIDTH;
chunksLoaded[i] = false;
}
}

View File

@@ -102,11 +102,11 @@ private:
BlockUpdateList bufferedBlockUpdates;
int requestNextChunkPosition;
static const int NumRequestChunks = CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH;
int NumRequestChunks;
int requestNextChunkIndex;
IntPair requestNextChunkIndexList[NumRequestChunks];
bool chunksLoaded[NumRequestChunks];
std::vector<IntPair> requestNextChunkIndexList;
std::vector<bool> chunksLoaded;
};
#endif

View File

@@ -27,6 +27,7 @@
#include "util/Mth.h"
#include "util/StringUtils.h"
#include "world/item/ItemInstance.h"
#include "world/level/LevelConstants.h"
#include "world/level/storage/LevelStorage.h"
#include "world/phys/Vec3.h"
#include "world/item/crafting/Recipe.h"
@@ -73,11 +74,11 @@ void ServerSideNetworkHandler::tileChanged(int x, int y, int z)
Packet* ServerSideNetworkHandler::getAddPacketFromEntity( Entity* entity ) {
if (entity->isMob() && !entity->isPlayer()) { //@fix: This code is duplicated. See if it can be unified.
if (minecraft->player) {
// if (minecraft->player) {
// I guess this should always be true, but it crashed somewhere in this
// function once and I only see this one as a potential problem
return new AddMobPacket((Mob*)entity);
}
// }
}
else if (entity->isPlayer()) {
@@ -158,7 +159,7 @@ void ServerSideNetworkHandler::handle(const RakNet::RakNetGUID& source, ChatPack
{
auto player = getPlayer(source);
if (player == nullptr) return; // TODO maybe kick?
if (player == nullptr) return; // @todo maybe kick?
if (packet->message[0] == '/') {
// This is a command
@@ -219,14 +220,30 @@ void ServerSideNetworkHandler::handle(const RakNet::RakNetGUID& source, LoginPac
//
bool oldClient = packet->clientNetworkVersion < SharedConstants::NetworkProtocolLowestSupportedVersion;
bool oldServer = packet->clientNetworkLowestSupportedVersion > SharedConstants::NetworkProtocolVersion;
if (oldClient || oldServer)
loginStatus = oldClient? LoginStatus::Failed_ClientOld : LoginStatus::Failed_ServerOld;
if (oldClient || oldServer || !packet->newProto)
loginStatus = oldClient || !packet->newProto? LoginStatus::Failed_ClientOld : LoginStatus::Failed_ServerOld;
std::string nicknameLower = packet->clientName.C_String();
std::transform(nicknameLower.begin(), nicknameLower.end(), nicknameLower.begin(), ::tolower);
printf("%s lower \n", nicknameLower.c_str());
for (int i = 0; i < level->players.size(); i++) {
ServerPlayer* player = (ServerPlayer*) level->players.at(i);
if (player->name == packet->clientName.C_String()) {
std::string clientLower = player->name;
std::transform(clientLower.begin(), clientLower.end(), clientLower.begin(), ::tolower);
// @todo to lower case
if (nicknameLower == clientLower) {
loginStatus = packet->newProto ? LoginStatus::Failed_TakenNickname : LoginStatus::Failed_ClientOld;
break;
}
}
for (auto& banned : level->bannedPpl) {
if (nicknameLower == banned) {
loginStatus = packet->newProto ? LoginStatus::Failed_Banned : LoginStatus::Failed_ClientOld;
break;
}
}
@@ -272,15 +289,11 @@ void ServerSideNetworkHandler::handle(const RakNet::RakNetGUID& source, LoginPac
level->getLevelData()->getGeneratorVersion(),
gameType,
newPlayer->entityId,
newPlayer->x, newPlayer->y - newPlayer->heightOffset, newPlayer->z
newPlayer->x, newPlayer->y - newPlayer->heightOffset, newPlayer->z,
LevelConstants::CHUNK_CACHE_WIDTH
).write(&bitStream);
rakPeer->Send(&bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, source, false);
if (!packet->newProto) {
MessagePacket packet("You're using outdated client. Some features disabled.");
raknetInstance->send(packet);
}
}
}
@@ -955,19 +968,17 @@ void ServerSideNetworkHandler::handle(const RakNet::RakNetGUID& source, Containe
void ServerSideNetworkHandler::handle( const RakNet::RakNetGUID& source, SetHealthPacket* packet )
{
LOGI("net idi nahui\n");
// for (unsigned int i = 0; i < level->players.size(); ++i) {
// Player* p = level->players[i];
// if (p->owner == source) {
// if (packet->health <= -32) {
// int diff = packet->health - SetHealthPacket::HEALTH_MODIFY_OFFSET;
// if (diff > 0) p->hurt(NULL, diff);
// else if (diff < 0) p->heal(-diff);
// }
// break;
// }
// }
for (unsigned int i = 0; i < level->players.size(); ++i) {
Player* p = level->players[i];
if (p->owner == source) {
if (packet->health <= -32) {
int diff = packet->health - SetHealthPacket::HEALTH_MODIFY_OFFSET;
if (diff > 0) p->hurt(NULL, diff);
else if (diff < 0) p->heal(-diff);
}
break;
}
}
}
void ServerSideNetworkHandler::handle( const RakNet::RakNetGUID& source, SignUpdatePacket* packet ) {

View File

@@ -236,9 +236,9 @@ std::string CommandServer::parse(ConnectedClient& client, const std::string& s)
if (x0 < 0) x0 = 0;
if (y0 < 0) y0 = 0;
if (z0 < 0) z0 = 0;
if (x1 >= LEVEL_WIDTH ) x1 = LEVEL_WIDTH - 1;
if (y1 >= LEVEL_HEIGHT) y1 = LEVEL_HEIGHT - 1;
if (z1 >= LEVEL_DEPTH ) z1 = LEVEL_DEPTH - 1;
if (x1 >= LevelConstants::LEVEL_WIDTH ) x1 = LevelConstants::LEVEL_WIDTH - 1;
if (y1 >= LevelConstants::LEVEL_HEIGHT) y1 = LevelConstants::LEVEL_HEIGHT - 1;
if (z1 >= LevelConstants::LEVEL_DEPTH ) z1 = LevelConstants::LEVEL_DEPTH - 1;
for (int y = y0; y <= y1; ++y)
for (int z = z0; z <= z1; ++z)
@@ -456,7 +456,7 @@ std::string CommandServer::parse(ConnectedClient& client, const std::string& s)
sw.start();
// Save a cuboid around the player
const int CSize = CHUNK_CACHE_WIDTH;
const int CSize = LevelConstants::CHUNK_CACHE_WIDTH;
int cx = (int)e->x / CSize;
int cz = (int)e->z / CSize;
@@ -504,7 +504,7 @@ bool CommandServer::handleCheckpoint(bool doRestore ) {
const int cz = restorePos.z;
const int y0 = restorePos.y;
const int y1 = y0 + RestoreHeight;
const int CSize = CHUNK_CACHE_WIDTH;
const int CSize = LevelConstants::CHUNK_CACHE_WIDTH;
const int numChunkBytes = RestoreHeight * CSize * CSize * 20 / 8;
if (!restoreBuffer) {

View File

@@ -33,18 +33,18 @@ public:
unsigned char* blockIds = chunk->getBlockData();
DataLayer& blockData = chunk->data;
const int setSize = LEVEL_HEIGHT / 8;
const int setShift = 4; // power of LEVEL_HEIGHT / 8
const int setSize = LevelConstants::LEVEL_HEIGHT / 8;
const int setShift = 4; // power of LevelConstants::LEVEL_HEIGHT / 8
chunkData.Reset();
for (int i = 0; i < CHUNK_COLUMNS; i++)
for (int i = 0; i < LevelConstants::CHUNK_COLUMNS; i++)
{
unsigned char updateBits = chunk->updateMap[i];
chunkData.Write(updateBits);
if (updateBits > 0)
{
int colDataPosition = (i % CHUNK_WIDTH) << 11 | (i / CHUNK_WIDTH) << 7;
int colDataPosition = (i % LevelConstants::CHUNK_WIDTH) << 11 | (i / LevelConstants::CHUNK_WIDTH) << 7;
for (int set = 0; set < 8; set++)
{

View File

@@ -9,6 +9,7 @@ namespace LoginStatus {
const int Failed_ClientOld = 1;
const int Failed_ServerOld = 2;
const int Failed_TakenNickname = 3;
const int Failed_Banned = 4;
}
class LoginStatusPacket : public Packet {

View File

@@ -2,6 +2,7 @@
#define NET_MINECRAFT_NETWORK_PACKET__StartGamePacket_H__
#include "../Packet.h"
#include "world/level/LevelConstants.h"
#include <cstdint>
class StartGamePacket : public Packet
@@ -14,18 +15,20 @@ public:
int entityId;
float x, y, z;
int chunkCacheWidth = 0;
StartGamePacket()
{
}
StartGamePacket(long seed, int levelGeneratorVersion, int gameType, int entityId, float x, float y, float z)
StartGamePacket(long seed, int levelGeneratorVersion, int gameType, int entityId, float x, float y, float z, int chunkCacheWidth)
: levelSeed((int32_t)seed),
levelGeneratorVersion(levelGeneratorVersion),
gameType(gameType),
entityId(entityId),
x(x),
y(y),
z(z)
z(z),
chunkCacheWidth(chunkCacheWidth)
{
}
@@ -40,6 +43,7 @@ public:
bitStream->Write(x);
bitStream->Write(y);
bitStream->Write(z);
bitStream->Write(LevelConstants::CHUNK_CACHE_WIDTH);
}
void read(RakNet::BitStream* bitStream)
@@ -51,6 +55,10 @@ public:
bitStream->Read(x);
bitStream->Read(y);
bitStream->Read(z);
if (bitStream->GetNumberOfUnreadBits() > 0) {
bitStream->Read(chunkCacheWidth);
}
}
void handle(const RakNet::RakNetGUID& source, NetEventCallback* callback)

View File

@@ -871,8 +871,8 @@ bool Entity::load( CompoundTag* tag )
// Add a small padding if standing next to the world edges
const float padding = bbWidth * 0.5f + 0.001f;
xx = Mth::clamp(xx, padding, (float)LEVEL_WIDTH - padding);
zz = Mth::clamp(zz, padding, (float)LEVEL_DEPTH - padding);
xx = Mth::clamp(xx, padding, (float)LevelConstants::LEVEL_WIDTH - padding);
zz = Mth::clamp(zz, padding, (float)LevelConstants::LEVEL_DEPTH - padding);
xo = xOld = x = xx;
yo = yOld = y = yy;

View File

@@ -29,6 +29,9 @@
#include "../Difficulty.h"
#include "../../network/packet/ExplodePacket.h"
const int Level::DEPTH = LevelConstants::LEVEL_HEIGHT;
const int Level::SEA_LEVEL = Level::DEPTH / 2 - 1;
Level::Level(LevelStorage* levelStorage, const std::string& levelName, const LevelSettings& settings, int generatorVersion, Dimension* fixedDimension /* = NULL */)
: levelStorage(levelStorage),
isClientSide(false),
@@ -278,8 +281,8 @@ void Level::tickTiles() {
for (int i = 0; i < pollChunkOffsetsSize; i += 2) {
const int xp = xx + pollChunkOffsets[i];
const int zp = zz + pollChunkOffsets[i+1];
if (xp >= 0 && xp < CHUNK_CACHE_WIDTH &&
zp >= 0 && zp < CHUNK_CACHE_WIDTH)
if (xp >= 0 && xp < LevelConstants::CHUNK_CACHE_WIDTH &&
zp >= 0 && zp < LevelConstants::CHUNK_CACHE_WIDTH)
_chunksToPoll.insert(ChunkPos(xp, zp));
}
}
@@ -428,17 +431,17 @@ bool Level::findPath(Path* path, Entity* from, int xBest, int yBest, int zBest,
/*protected*/
void Level::setInitialSpawn() {
isFindingSpawn = true;
int xSpawn = CHUNK_CACHE_WIDTH * CHUNK_WIDTH / 2; // (Level.MAX_LEVEL_SIZE - 100) * 0;
int xSpawn = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_WIDTH / 2; // (Level.MAX_LEVEL_SIZE - 100) * 0;
int ySpawn = 64;
int zSpawn = CHUNK_CACHE_WIDTH * CHUNK_DEPTH / 2; // (Level.MAX_LEVEL_SIZE - 100) * 0;
int zSpawn = LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_DEPTH / 2; // (Level.MAX_LEVEL_SIZE - 100) * 0;
while (!dimension->isValidSpawn(xSpawn, zSpawn)) {
xSpawn += random.nextInt(32) - random.nextInt(32);
zSpawn += random.nextInt(32) - random.nextInt(32);
if (xSpawn < 4) xSpawn += 32;
if (xSpawn >= LEVEL_WIDTH-4) xSpawn -= 32;
if (xSpawn >= LevelConstants::LEVEL_WIDTH-4) xSpawn -= 32;
if (zSpawn < 4) zSpawn += 32;
if (zSpawn >= LEVEL_DEPTH-4) zSpawn -= 32;
if (zSpawn >= LevelConstants::LEVEL_DEPTH-4) zSpawn -= 32;
}
levelData.setSpawn(xSpawn, ySpawn, zSpawn);
isFindingSpawn = false;
@@ -456,9 +459,9 @@ void Level::validateSpawn() {
zSpawn += random.nextInt(8) - random.nextInt(8);
if (xSpawn < 4) xSpawn += 8;
if (xSpawn >= LEVEL_WIDTH-4) xSpawn -= 8;
if (xSpawn >= LevelConstants::LEVEL_WIDTH-4) xSpawn -= 8;
if (zSpawn < 4) zSpawn += 8;
if (zSpawn >= LEVEL_DEPTH-4) zSpawn -= 8;
if (zSpawn >= LevelConstants::LEVEL_DEPTH-4) zSpawn -= 8;
}
levelData.setXSpawn(xSpawn);
levelData.setZSpawn(zSpawn);
@@ -944,7 +947,7 @@ HitResult Level::clip(const Vec3& A, const Vec3& b, bool liquid /*= false*/, boo
if (solidOnly && tile != NULL && tile->getAABB(this, xTile0, yTile0, zTile0) == NULL) {
// No collision
} else if (t > 0 && tile->mayPick(data, liquid)) {
if(xTile0 >= 0 && zTile0 >= 0 && xTile0 < LEVEL_WIDTH && zTile0 < LEVEL_WIDTH) {
if(xTile0 >= 0 && zTile0 >= 0 && xTile0 < LevelConstants::LEVEL_WIDTH && zTile0 < LevelConstants::LEVEL_WIDTH) {
HitResult r = tile->clip(this, xTile0, yTile0, zTile0, a, b);
if (r.isHit()) return r;
}
@@ -2222,9 +2225,9 @@ void Level::setNightMode( bool isNightMode ) {
}
bool Level::inRange( int x, int y, int z ) {
return x >= 0 && x < LEVEL_WIDTH
&& y >= 0 && y < LEVEL_HEIGHT
&& z >= 0 && z < LEVEL_DEPTH;
return x >= 0 && x < LevelConstants::LEVEL_WIDTH
&& y >= 0 && y < LevelConstants::LEVEL_HEIGHT
&& z >= 0 && z < LevelConstants::LEVEL_DEPTH;
}
//

View File

@@ -67,8 +67,8 @@ class Level: public LevelSource
public:
static const int MAX_LEVEL_SIZE = 32000000;
static const short DEPTH = LEVEL_HEIGHT;
static const short SEA_LEVEL = DEPTH / 2 - 1;
static const int DEPTH;
static const int SEA_LEVEL;
static const int MAX_BRIGHTNESS = 15;
static const int TICKS_PER_DAY = SharedConstants::TicksPerSecond * 60 * 16;// SharedConstants::TicksPerSecond * 60 * 12; // ORG:20*60*20
@@ -309,7 +309,7 @@ public:
Random random;
std::set<std::string> ops;
std::set<std::string> bannedPpl;
protected:
bool isFindingSpawn;

View File

@@ -0,0 +1,10 @@
#include "LevelConstants.h"
int LevelConstants::LEVEL_HEIGHT = 128;
int LevelConstants::CHUNK_CACHE_WIDTH = 16; // in chunks
int LevelConstants::CHUNK_WIDTH = 16; // in blocks
int LevelConstants::CHUNK_DEPTH = 16;
int LevelConstants::LEVEL_WIDTH = LevelConstants::CHUNK_CACHE_WIDTH * CHUNK_WIDTH;
int LevelConstants::LEVEL_DEPTH = LevelConstants::CHUNK_CACHE_WIDTH * CHUNK_DEPTH;
int LevelConstants::CHUNK_COLUMNS = LevelConstants::CHUNK_WIDTH * LevelConstants::CHUNK_DEPTH;
int LevelConstants::CHUNK_BLOCK_COUNT = LevelConstants::CHUNK_COLUMNS * LevelConstants::LEVEL_HEIGHT;

View File

@@ -1,14 +1,14 @@
#ifndef _MINECRAFT_WORLD_LEVELCONSTANTS_H_
#define _MINECRAFT_WORLD_LEVELCONSTANTS_H_
#pragma once
const int LEVEL_HEIGHT = 128;
const int CHUNK_CACHE_WIDTH = 16; // in chunks
const int CHUNK_WIDTH = 16; // in blocks
const int CHUNK_DEPTH = 16;
const int LEVEL_WIDTH = CHUNK_CACHE_WIDTH * CHUNK_WIDTH;
const int LEVEL_DEPTH = CHUNK_CACHE_WIDTH * CHUNK_DEPTH;
const int CHUNK_COLUMNS = CHUNK_WIDTH * CHUNK_DEPTH;
const int CHUNK_BLOCK_COUNT = CHUNK_COLUMNS * LEVEL_HEIGHT;
#endif
class LevelConstants {
public:
static int CHUNK_CACHE_WIDTH; // in chunks
static int LEVEL_WIDTH;
static int LEVEL_DEPTH;
static int LEVEL_HEIGHT;
static int CHUNK_WIDTH; // in blocks
static int CHUNK_DEPTH;
static int CHUNK_COLUMNS;
static int CHUNK_BLOCK_COUNT;
};

View File

@@ -10,7 +10,7 @@
#include "../LevelConstants.h"
class ChunkCache: public ChunkSource {
//static const int CHUNK_CACHE_WIDTH = CHUNK_CACHE_WIDTH; // WAS 32;
//static const int LevelConstants::CHUNK_CACHE_WIDTH = LevelConstants::CHUNK_CACHE_WIDTH; // WAS 32;
static const int MAX_SAVES = 2;
public:
ChunkCache(Level* level_, ChunkStorage* storage_, ChunkSource* source_)
@@ -24,14 +24,17 @@ public:
isChunkCache = true;
//emptyChunk = new EmptyLevelChunk(level_, emptyChunkBlocks, 0, 0);
emptyChunk = new EmptyLevelChunk(level_, NULL, 0, 0);
memset(chunks, 0, sizeof(LevelChunk*) * CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH);
chunks = new LevelChunk *[LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH];
memset(chunks, 0, sizeof(LevelChunk*) * LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH);
LOGI("Chunks allocated.");
}
~ChunkCache() {
delete source;
delete emptyChunk;
for (int i = 0; i < CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH; i++)
for (int i = 0; i < LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH; i++)
{
if (chunks[i])
{
@@ -42,7 +45,7 @@ public:
}
bool fits(int x, int z) {
return (x >= 0 && z >= 0 && x < CHUNK_CACHE_WIDTH && z < CHUNK_CACHE_WIDTH);
return (x >= 0 && z >= 0 && x < LevelConstants::CHUNK_CACHE_WIDTH && z < LevelConstants::CHUNK_CACHE_WIDTH);
}
bool hasChunk(int x, int z) {
@@ -52,9 +55,9 @@ public:
if (x == xLast && z == zLast && last != NULL) {
return true;
}
int xs = x & (CHUNK_CACHE_WIDTH - 1);
int zs = z & (CHUNK_CACHE_WIDTH - 1);
int slot = xs + zs * CHUNK_CACHE_WIDTH;
int xs = x & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
int zs = z & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
int slot = xs + zs * LevelConstants::CHUNK_CACHE_WIDTH;
return chunks[slot] != NULL && (chunks[slot] == emptyChunk || chunks[slot]->isAt(x, z));
}
@@ -71,9 +74,9 @@ public:
}
if (!fits(x, z)) return emptyChunk;
//if (!level->isFindingSpawn && !fits(x, z)) return emptyChunk;
int xs = x & (CHUNK_CACHE_WIDTH - 1);
int zs = z & (CHUNK_CACHE_WIDTH - 1);
int slot = xs + zs * CHUNK_CACHE_WIDTH;
int xs = x & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
int zs = z & (LevelConstants::CHUNK_CACHE_WIDTH - 1);
int slot = xs + zs * LevelConstants::CHUNK_CACHE_WIDTH;
if (!hasChunk(x, z)) {
if (chunks[slot] != NULL) {
chunks[slot]->unload();
@@ -199,8 +202,8 @@ public:
void saveAll(bool onlyUnsaved) {
if (storage != NULL) {
std::vector<LevelChunk*> chunks;
for (int z = 0; z < CHUNK_CACHE_WIDTH; ++z)
for (int x = 0; x < CHUNK_CACHE_WIDTH; ++x) {
for (int z = 0; z < LevelConstants::CHUNK_CACHE_WIDTH; ++z)
for (int x = 0; x < LevelConstants::CHUNK_CACHE_WIDTH; ++x) {
LevelChunk* chunk = level->getChunk(x, z);
if (!onlyUnsaved || chunk->shouldSave(false))
chunks.push_back( chunk );
@@ -211,7 +214,7 @@ public:
private:
LevelChunk* load(int x, int z) {
if (storage == NULL) return emptyChunk;
if (x < 0 || x >= CHUNK_CACHE_WIDTH || z < 0 || z >= CHUNK_CACHE_WIDTH)
if (x < 0 || x >= LevelConstants::CHUNK_CACHE_WIDTH || z < 0 || z >= LevelConstants::CHUNK_CACHE_WIDTH)
{
return emptyChunk;
}
@@ -254,7 +257,7 @@ private:
LevelChunk* emptyChunk;
ChunkSource* source;
ChunkStorage* storage;
LevelChunk* chunks[CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH];
LevelChunk** chunks;
Level* level;
LevelChunk* last;

View File

@@ -12,13 +12,14 @@
/*static*/
bool LevelChunk::touchedSky = false;
int LevelChunk::ChunkBlockCount;
LevelChunk::LevelChunk( Level* level, int x, int z )
: level(level),
x(x),
z(z),
xt(x * CHUNK_WIDTH),
zt(z * CHUNK_DEPTH)
xt(x * LevelConstants::CHUNK_WIDTH),
zt(z * LevelConstants::CHUNK_DEPTH)
{
init();
}
@@ -27,8 +28,8 @@ LevelChunk::LevelChunk( Level* level, unsigned char* blocks, int x, int z )
: level(level),
x(x),
z(z),
xt(x * CHUNK_WIDTH),
zt(z * CHUNK_DEPTH),
xt(x * LevelConstants::CHUNK_WIDTH),
zt(z * LevelConstants::CHUNK_DEPTH),
blocks(blocks),
data(ChunkBlockCount),
skyLight(ChunkBlockCount),
@@ -45,6 +46,9 @@ LevelChunk::~LevelChunk()
void LevelChunk::init()
{
heightmap = new char[LevelConstants::CHUNK_COLUMNS];
updateMap = new unsigned char[LevelConstants::CHUNK_COLUMNS];
terrainPopulated = false;
dontSave = false;
unsaved = false;

View File

@@ -106,9 +106,9 @@ private:
void recalcHeight(int x, int yStart, int z);
public:
static bool touchedSky;
static const int ChunkBlockCount = CHUNK_BLOCK_COUNT;
static const int ChunkSize = ChunkBlockCount;
static const int UpdateMapBitShift = 4; // power of (LEVEL_HEIGHT / 8) == 16
static int ChunkBlockCount;
const int ChunkSize = ChunkBlockCount;
static const int UpdateMapBitShift = 4; // power of (LevelConstants::LEVEL_HEIGHT / 8) == 16
int blocksLength; // ? needed or not? (i.e. are all chunks the same size?)
@@ -118,12 +118,12 @@ public:
DataLayer skyLight;
DataLayer blockLight;
char heightmap[CHUNK_COLUMNS];
unsigned char updateMap[CHUNK_COLUMNS]; // marks regions within block columns that have been modified
char* heightmap; // [LevelConstants::CHUNK_COLUMNS]
unsigned char* updateMap; // marks regions within block columns that have been modified [LevelConstants::CHUNK_COLUMNS]
int minHeight;
const int x, z;
const int xt, zt;
int x, z;
int xt, zt;
bool terrainPopulated;
bool unsaved;

View File

@@ -64,7 +64,7 @@ RandomLevelSource::~RandomLevelSource() {
/*public*/
void RandomLevelSource::prepareHeights(int xOffs, int zOffs, unsigned char* blocks, /*Biome*/void* biomes, float* temperatures) {
int xChunks = 16 / CHUNK_WIDTH;
int xChunks = 16 / LevelConstants::CHUNK_WIDTH;
int waterHeight = Level::DEPTH - 64;
int xSize = xChunks + 1;
@@ -87,23 +87,23 @@ void RandomLevelSource::prepareHeights(int xOffs, int zOffs, unsigned char* bloc
float s3a = (buffer[((xc + 1) * zSize + (zc + 1)) * ySize + (yc + 1)] - s3) * yStep;
for (int y = 0; y < CHUNK_HEIGHT; y++) {
float xStep = 1 / (float) CHUNK_WIDTH;
float xStep = 1 / (float) LevelConstants::CHUNK_WIDTH;
float _s0 = s0;
float _s1 = s1;
float _s0a = (s2 - s0) * xStep;
float _s1a = (s3 - s1) * xStep;
for (int x = 0; x < CHUNK_WIDTH; x++) {
int offs = (x + xc * CHUNK_WIDTH) << 11 | (0 + zc * CHUNK_WIDTH) << 7 | (yc * CHUNK_HEIGHT + y);
for (int x = 0; x < LevelConstants::CHUNK_WIDTH; x++) {
int offs = (x + xc * LevelConstants::CHUNK_WIDTH) << 11 | (0 + zc * LevelConstants::CHUNK_WIDTH) << 7 | (yc * CHUNK_HEIGHT + y);
int step = 1 << 7;
float zStep = 1 / (float) CHUNK_WIDTH;
float zStep = 1 / (float) LevelConstants::CHUNK_WIDTH;
float val = _s0;
float vala = (_s1 - _s0) * zStep;
for (int z = 0; z < CHUNK_WIDTH; z++) {
// + (zc * CHUNK_WIDTH + z)];
float temp = temperatures[(xc * CHUNK_WIDTH + x) * 16 + (zc * CHUNK_WIDTH + z)];
for (int z = 0; z < LevelConstants::CHUNK_WIDTH; z++) {
// + (zc * LevelConstants::CHUNK_WIDTH + z)];
float temp = temperatures[(xc * LevelConstants::CHUNK_WIDTH + x) * 16 + (zc * LevelConstants::CHUNK_WIDTH + z)];
int tileId = 0;
if (yc * CHUNK_HEIGHT + y < waterHeight) {
if (temp < SNOW_CUTOFF && yc * CHUNK_HEIGHT + y >= waterHeight - 1) {

View File

@@ -343,8 +343,8 @@ bool ExternalFileLevelStorage::readPlayerData(const std::string& filename, Level
Vec3& pos = dest.playerData.pos;
if (pos.x < 0.5f) pos.x = 0.5f;
if (pos.z < 0.5f) pos.z = 0.5f;
if (pos.x > (LEVEL_WIDTH - 0.5f)) pos.x = LEVEL_WIDTH - 0.5f;
if (pos.z > (LEVEL_DEPTH - 0.5f)) pos.z = LEVEL_DEPTH - 0.5f;
if (pos.x > (LevelConstants::LEVEL_WIDTH - 0.5f)) pos.x = LevelConstants::LEVEL_WIDTH - 0.5f;
if (pos.z > (LevelConstants::LEVEL_DEPTH - 0.5f)) pos.z = LevelConstants::LEVEL_DEPTH - 0.5f;
if (pos.y < 0) pos.y = 64;
dest.playerDataVersion = version;
@@ -362,14 +362,14 @@ void ExternalFileLevelStorage::tick()
LOGI("Saving level...\n");
// look for chunks that needs to be saved
for (int z = 0; z < CHUNK_CACHE_WIDTH; z++)
for (int z = 0; z < LevelConstants::CHUNK_CACHE_WIDTH; z++)
{
for (int x = 0; x < CHUNK_CACHE_WIDTH; x++)
for (int x = 0; x < LevelConstants::CHUNK_CACHE_WIDTH; x++)
{
LevelChunk* chunk = level->getChunk(x, z);
if (chunk && chunk->unsaved)
{
int pos = x + z * CHUNK_CACHE_WIDTH;
int pos = x + z * LevelConstants::CHUNK_CACHE_WIDTH;
UnsavedChunkList::iterator prev = unsavedChunkList.begin();
for ( ; prev != unsavedChunkList.end(); ++prev)
{
@@ -415,13 +415,13 @@ void ExternalFileLevelStorage::save(Level* level, LevelChunk* levelChunk)
// Write chunk
RakNet::BitStream chunkData;
chunkData.Write((const char*)levelChunk->getBlockData(), CHUNK_BLOCK_COUNT);
chunkData.Write((const char*)levelChunk->data.data, CHUNK_BLOCK_COUNT / 2);
chunkData.Write((const char*)levelChunk->getBlockData(), LevelConstants::CHUNK_BLOCK_COUNT);
chunkData.Write((const char*)levelChunk->data.data, LevelConstants::CHUNK_BLOCK_COUNT / 2);
chunkData.Write((const char*)levelChunk->skyLight.data, CHUNK_BLOCK_COUNT / 2);
chunkData.Write((const char*)levelChunk->blockLight.data, CHUNK_BLOCK_COUNT / 2);
chunkData.Write((const char*)levelChunk->skyLight.data, LevelConstants::CHUNK_BLOCK_COUNT / 2);
chunkData.Write((const char*)levelChunk->blockLight.data, LevelConstants::CHUNK_BLOCK_COUNT / 2);
chunkData.Write((const char*)levelChunk->updateMap, CHUNK_COLUMNS);
chunkData.Write((const char*)levelChunk->updateMap, LevelConstants::CHUNK_COLUMNS);
regionFile->writeChunk(levelChunk->x, levelChunk->z, chunkData);
@@ -453,16 +453,16 @@ LevelChunk* ExternalFileLevelStorage::load(Level* level, int x, int z)
chunkData->ResetReadPointer();
unsigned char* blockIds = new unsigned char[CHUNK_BLOCK_COUNT];
chunkData->Read((char*)blockIds, CHUNK_BLOCK_COUNT);
unsigned char* blockIds = new unsigned char[LevelConstants::CHUNK_BLOCK_COUNT];
chunkData->Read((char*)blockIds, LevelConstants::CHUNK_BLOCK_COUNT);
LevelChunk* levelChunk = new LevelChunk(level, blockIds, x, z);
chunkData->Read((char*)levelChunk->data.data, CHUNK_BLOCK_COUNT / 2);
chunkData->Read((char*)levelChunk->data.data, LevelConstants::CHUNK_BLOCK_COUNT / 2);
if (loadedStorageVersion >= ChunkVersion_Light) {
chunkData->Read((char*)levelChunk->skyLight.data, CHUNK_BLOCK_COUNT / 2);
chunkData->Read((char*)levelChunk->blockLight.data, CHUNK_BLOCK_COUNT / 2);
chunkData->Read((char*)levelChunk->skyLight.data, LevelConstants::CHUNK_BLOCK_COUNT / 2);
chunkData->Read((char*)levelChunk->blockLight.data, LevelConstants::CHUNK_BLOCK_COUNT / 2);
}
chunkData->Read((char*)levelChunk->updateMap, CHUNK_COLUMNS);
chunkData->Read((char*)levelChunk->updateMap, LevelConstants::CHUNK_COLUMNS);
// This will be difficult to maintain.. Storage version could be per chunk
// too (but probably better to just read all -> write all, so that all
// chunks got same version anyway)
@@ -485,7 +485,7 @@ LevelChunk* ExternalFileLevelStorage::load(Level* level, int x, int z)
//bool dbg = (x == 7 && z == 9);
//int t = 0;
//for (int i = 0; i < CHUNK_COLUMNS; ++i) {
//for (int i = 0; i < LevelConstants::CHUNK_COLUMNS; ++i) {
// char bits = levelChunk->updateMap[i];
// t += (bits != 0);
// int xx = x * 16 + i%16;

View File

@@ -47,7 +47,7 @@ bool Mushroom::mayPlaceOn( int tile ) {
}
bool Mushroom::canSurvive( Level* level, int x, int y, int z ) {
if (y < 0 || y >= LEVEL_HEIGHT/*Level::maxBuildHeight*/)
if (y < 0 || y >= LevelConstants::LEVEL_HEIGHT/*Level::maxBuildHeight*/)
return false;
int below = level->getTile(x, y - 1, z);

View File

@@ -53,7 +53,7 @@ bool NetherReactor::canSpawnStartNetherReactor( Level* level, int x, int y, int
if(!allPlayersCloseToReactor(level, x, y, z)) {
player->displayClientMessage("All players need to be close to the reactor.");
return false;
} else if(y > LEVEL_HEIGHT - 28) {
} else if(y > LevelConstants::LEVEL_HEIGHT - 28) {
player->displayClientMessage("The nether reactor needs to be built lower down.");
return false;
} else if(y < 2) {

View File

@@ -22,6 +22,7 @@
#include "../../item/StoneSlabTileItem.h"
#include "../../item/SaplingTileItem.h"
#include "../../item/ItemCategory.h"
#include "world/level/LevelConstants.h"
const int Tile::RENDERLAYER_OPAQUE = 0;
const int Tile::RENDERLAYER_ALPHATEST = 1;
@@ -512,10 +513,15 @@ bool Tile::shouldRenderFace( LevelSource* level, int x, int y, int z, int face )
{
if (face == 0 && y == -1) return false;
// For fixed size worlds //@todo: external constants rather than magic numbers
// @fixme @ahtung temp fix
auto xz = LevelConstants::CHUNK_CACHE_WIDTH == 16 ? 256 :
LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH / (LevelConstants::CHUNK_CACHE_WIDTH * LevelConstants::CHUNK_CACHE_WIDTH / 256 / 2);
if (face == 2 && z == -1) return false;
if (face == 3 && z == 256) return false;
if (face == 3 && z == xz) return false;
if (face == 4 && x == -1) return false;
if (face == 5 && x == 256) return false;
if (face == 5 && x == xz) return false;
// Common
if (face == 0 && yy0 > 0) return true;
if (face == 1 && yy1 < 1) return true;

View File

@@ -130,7 +130,7 @@ Vec3 NetherReactorTileEntity::getSpawnPosition( float minDistance, float varible
void NetherReactorTileEntity::spawnEnemy() {
Mob* mob = MobFactory::CreateMob(MobTypes::PigZombie, level);
Vec3 enemyPosition = getSpawnPosition(3, 4, -1);
while(enemyPosition.x < 0 || enemyPosition.z < 0 || enemyPosition.x >= LEVEL_WIDTH || enemyPosition.z >= LEVEL_DEPTH) {
while(enemyPosition.x < 0 || enemyPosition.z < 0 || enemyPosition.x >= LevelConstants::LEVEL_WIDTH || enemyPosition.z >= LevelConstants::LEVEL_DEPTH) {
enemyPosition = getSpawnPosition(3, 4, -1);
}
MobSpawner::addMob(level, mob, enemyPosition.x, enemyPosition.y, enemyPosition.z, 0, 0, true);
@@ -140,7 +140,7 @@ void NetherReactorTileEntity::spawnEnemy() {
void NetherReactorTileEntity::spawnItem() {
Vec3 itemPosition= getSpawnPosition(3, 4, -1);
while(itemPosition.x < 0 || itemPosition.z < 0 || itemPosition.x >= LEVEL_WIDTH || itemPosition.z >= LEVEL_DEPTH) {
while(itemPosition.x < 0 || itemPosition.z < 0 || itemPosition.x >= LevelConstants::LEVEL_WIDTH || itemPosition.z >= LevelConstants::LEVEL_DEPTH) {
itemPosition = getSpawnPosition(3, 4, -1);
}
ItemEntity* item = new ItemEntity(level, itemPosition.x, itemPosition.y, itemPosition.z, getSpawnItem());