Skip to content

Commit

Permalink
[BlockType] Removed everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Feb 7, 2020
1 parent e198cd2 commit a10c037
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 74 deletions.
4 changes: 2 additions & 2 deletions client/include/world/ClientChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class TextureAtlas;

class ClientChunk : public Chunk {
public:
ClientChunk(s32 x, s32 y, s32 z, TextureAtlas &textureAtlas)
: Chunk(x, y, z), m_textureAtlas(textureAtlas), m_builder{textureAtlas} {}
ClientChunk(s32 x, s32 y, s32 z, World &world, TextureAtlas &textureAtlas)
: Chunk(x, y, z, world), m_textureAtlas(textureAtlas), m_builder{textureAtlas} {}

void update();

Expand Down
9 changes: 7 additions & 2 deletions client/source/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ void ClientCommandHandler::setupCallbacks() {
packet >> pos.x >> pos.y >> pos.z;

BlockData *data = m_world.getBlockData(pos.x, pos.y, pos.z);
if (data) {
if (!data)
data = m_world.addBlockData(pos.x, pos.y, pos.z);

if (data)
packet >> data->inventory;
}
});

m_client.setCommandCallback(Network::Command::BlockDataUpdate, [this](sf::Packet &packet) {
Expand All @@ -157,6 +159,9 @@ void ClientCommandHandler::setupCallbacks() {
Chunk *chunk = m_world.getChunkAtBlockPos(pos.x, pos.y, pos.z);
if (chunk) {
BlockData *data = chunk->getBlockData(pos.x & (CHUNK_WIDTH - 1), pos.y & (CHUNK_HEIGHT - 1), pos.z & (CHUNK_DEPTH - 1));
if (!data)
data = m_world.addBlockData(pos.x, pos.y, pos.z);

if (data) {
bool useAltTiles;
packet >> data->data >> useAltTiles;
Expand Down
6 changes: 3 additions & 3 deletions client/source/world/ClientWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void ClientWorld::init(float playerX, float playerY, float playerZ) {
int pcy = std::floor(playerY / CHUNK_HEIGHT);
int pcz = std::floor(playerZ / CHUNK_DEPTH);

m_chunks.emplace(gk::Vector3i{pcx, pcy, pcz}, new ClientChunk(pcx, pcy, pcz, m_textureAtlas));
m_chunks.emplace(gk::Vector3i{pcx, pcy, pcz}, new ClientChunk(pcx, pcy, pcz, *this, m_textureAtlas));
}

void ClientWorld::update() {
Expand Down Expand Up @@ -78,7 +78,7 @@ void ClientWorld::receiveChunkData(sf::Packet &packet) {
// Get the chunk from the map or create it if it doesn't exist
ClientChunk *chunk = (ClientChunk *)getChunk(cx, cy, cz);
if (!chunk) {
auto it = m_chunks.emplace(gk::Vector3i{cx, cy, cz}, new ClientChunk(cx, cy, cz, m_textureAtlas));
auto it = m_chunks.emplace(gk::Vector3i{cx, cy, cz}, new ClientChunk(cx, cy, cz, *this, m_textureAtlas));
chunk = it.first->second.get();
}

Expand Down Expand Up @@ -154,7 +154,7 @@ void ClientWorld::createChunkNeighbours(ClientChunk *chunk) {

ClientChunk *neighbour = (ClientChunk *)getChunk(scx, scy, scz);
if (!neighbour) {
auto it = m_chunks.emplace(gk::Vector3i{scx, scy, scz}, new ClientChunk(scx, scy, scz, m_textureAtlas));
auto it = m_chunks.emplace(gk::Vector3i{scx, scy, scz}, new ClientChunk(scx, scy, scz, *this, m_textureAtlas));
neighbour = it.first->second.get();
}

Expand Down
39 changes: 0 additions & 39 deletions common/include/inventory/BlockType.hpp

This file was deleted.

1 change: 1 addition & 0 deletions common/include/inventory/Inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Inventory : public ISerializable {

u16 width() const { return m_width; }
u16 height() const { return m_height; }
void resize(u16 width, u16 height) { m_width = width; m_height = height; m_items.resize(width * height); }

const std::vector<ItemStack> &items() const { return m_items; }

Expand Down
9 changes: 8 additions & 1 deletion common/include/world/Chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "BlockData.hpp"
#include "ChunkLightmap.hpp"

class World;

class Chunk : public gk::NonCopyable {
public:
enum {
Expand All @@ -39,7 +41,7 @@ class Chunk : public gk::NonCopyable {
};

public:
Chunk(s32 x, s32 y, s32 z);
Chunk(s32 x, s32 y, s32 z, World &world);
virtual ~Chunk() = default;

u16 getBlock(int x, int y, int z) const;
Expand All @@ -49,7 +51,10 @@ class Chunk : public gk::NonCopyable {

void setBlockRaw(int x, int y, int z, u16 block);

virtual void onBlockPlaced(int, int, int, const Block &) const {}

BlockData *getBlockData(int x, int y, int z) const;
BlockData *addBlockData(int x, int y, int z, int inventoryWidth = 0, int inventoryHeight = 0);

s32 x() const { return m_x; }
s32 y() const { return m_y; }
Expand Down Expand Up @@ -85,6 +90,8 @@ class Chunk : public gk::NonCopyable {
s32 m_y;
s32 m_z;

World &m_world;

DataArray m_data;

ChunkLightmap m_lightmap{this};
Expand Down
1 change: 1 addition & 0 deletions common/include/world/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class World {
Chunk *getChunkAtBlockPos(int x, int y, int z) const;

BlockData *getBlockData(int x, int y, int z) const;
BlockData *addBlockData(int x, int y, int z, int inventoryWidth = 0, int inventoryHeight = 0) const;

u16 getBlock(int x, int y, int z) const;
void setBlock(int x, int y, int z, u16 id) const;
Expand Down
7 changes: 5 additions & 2 deletions common/source/inventory/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void Inventory::addStack(const std::string &name, u16 amount) {
}

void Inventory::serialize(sf::Packet &packet) const {
packet << u8(m_inBlock)
packet << m_width << m_height << u8(m_inBlock)
<< s32(m_blockPos.x) << s32(m_blockPos.y) << s32(m_blockPos.z);

int i = 0;
Expand All @@ -49,10 +49,13 @@ void Inventory::serialize(sf::Packet &packet) const {
void Inventory::deserialize(sf::Packet &packet) {
u8 inBlock;
s32 bx, by, bz;
packet >> inBlock >> bx >> by >> bz;
packet >> m_width >> m_height >> inBlock >> bx >> by >> bz;
m_inBlock = inBlock;
m_blockPos = gk::Vector3i{bx, by, bz};

if (m_items.size() != m_width * m_height)
m_items.resize(m_width * m_height);

std::string name;
u16 amount;
u8 x, y;
Expand Down
43 changes: 29 additions & 14 deletions common/source/world/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@

#include <gk/core/GameClock.hpp>

#include "BlockType.hpp"
#include "Chunk.hpp"
#include "Config.hpp"
#include "Registry.hpp"

Chunk::Chunk(s32 x, s32 y, s32 z) {
Chunk::Chunk(s32 x, s32 y, s32 z, World &world) : m_world(world) {
m_x = x;
m_y = y;
m_z = z;
Expand Down Expand Up @@ -75,6 +74,8 @@ void Chunk::setBlock(int x, int y, int z, u16 type) {
m_lightmap.removeSunlight(x, y, z);
}

onBlockPlaced(x, y, z, block);

setBlockRaw(x, y, z, type);

// FIXME
Expand Down Expand Up @@ -112,16 +113,8 @@ void Chunk::setBlockRaw(int x, int y, int z, u16 type) {

if (m_data[x][y][z] == type) return;

gk::Vector3i pos{x, y, z};
gk::Vector3i absolutePos{x + m_x * CHUNK_WIDTH, y + m_y * CHUNK_HEIGHT, z + m_z * CHUNK_DEPTH};
if (type == BlockType::Workbench) {
m_blockData.emplace(pos, new BlockData{absolutePos, 3, 3});
}
else if (type == BlockType::Furnace)
m_blockData.emplace(pos, new BlockData{absolutePos, 3, 1});

if (m_data[x][y][z] == BlockType::Workbench || m_data[x][y][z] == BlockType::Furnace) {
auto it = m_blockData.find(pos);
if (type == 0) {
auto it = m_blockData.find(gk::Vector3i{x, y, z});
if (it != m_blockData.end())
m_blockData.erase(it);
}
Expand All @@ -139,9 +132,31 @@ BlockData *Chunk::getBlockData(int x, int y, int z) const {
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getBlockData(x, y, z + CHUNK_DEPTH) : 0;
if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getBlockData(x, y, z - CHUNK_DEPTH) : 0;

auto it = m_blockData.find(gk::Vector3i{x, y, z});
if (it == m_blockData.end())
gk::Vector3i pos{x, y, z};
auto it = m_blockData.find(pos);
if (it == m_blockData.end()) {
return nullptr;
}

return it->second.get();
}

BlockData *Chunk::addBlockData(int x, int y, int z, int inventoryWidth, int inventoryHeight) {
if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->addBlockData(x + CHUNK_WIDTH, y, z) : 0;
if(x >= CHUNK_WIDTH) return m_surroundingChunks[1] ? m_surroundingChunks[1]->addBlockData(x - CHUNK_WIDTH, y, z) : 0;
if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->addBlockData(x, y + CHUNK_HEIGHT, z) : 0;
if(y >= CHUNK_HEIGHT) return m_surroundingChunks[5] ? m_surroundingChunks[5]->addBlockData(x, y - CHUNK_HEIGHT, z) : 0;
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->addBlockData(x, y, z + CHUNK_DEPTH) : 0;
if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->addBlockData(x, y, z - CHUNK_DEPTH) : 0;

gk::Vector3i pos{x, y, z};
auto it = m_blockData.find(pos);
if (it == m_blockData.end()) {
gk::Vector3i absolutePos{x + m_x * CHUNK_WIDTH, y + m_y * CHUNK_HEIGHT, z + m_z * CHUNK_DEPTH};
BlockData *data = new BlockData{absolutePos, inventoryWidth, inventoryHeight};
m_blockData.emplace(pos, data);
return data;
}

return it->second.get();
}
Expand Down
8 changes: 8 additions & 0 deletions common/source/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ BlockData *World::getBlockData(int x, int y, int z) const {
return nullptr;
}

BlockData *World::addBlockData(int x, int y, int z, int inventoryWidth, int inventoryHeight) const {
Chunk *chunk = getChunkAtBlockPos(x, y, z);
if (chunk)
return chunk->addBlockData(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1), inventoryWidth, inventoryHeight);

return nullptr;
}

u16 World::getBlock(int x, int y, int z) const {
Chunk *chunk = getChunkAtBlockPos(x, y, z);
if (chunk)
Expand Down
4 changes: 4 additions & 0 deletions mods/default/furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ mod:block {
tiles = {"furnace_top.png", "furnace_top.png", "furnace_front.png", "furnace_side.png"},
alt_tiles = {"", "", "furnace_front_on.png", ""},

on_block_placed = function(pos, world)
world:add_block_data(pos.x, pos.y, pos.z, 3, 1)
end,

on_block_activated = function(pos, player, world, client)
local gui = LuaGUI.new()

Expand Down
7 changes: 4 additions & 3 deletions mods/default/workbench.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ mod:block {
name = "Workbench",
tiles = {"crafting_table_top.png", "crafting_table_top.png", "crafting_table_front.png", "crafting_table_side.png"},

on_block_placed = function(pos, world)
world:add_block_data(pos.x, pos.y, pos.z, 3, 3)
end,

on_block_activated = function(pos, player, world, client)
local gui = LuaGUI.new()

Expand Down Expand Up @@ -49,9 +53,6 @@ mod:block {
pos = {x = gui_pos.x, y = gui_pos.y},

block = {x = pos.x, y = pos.y, z = pos.z},

-- offset = 0,
-- size = 4
}

gui:image {
Expand Down
3 changes: 2 additions & 1 deletion server/include/world/ServerBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ class ServerBlock : public Block {

void onTick(const glm::ivec3 &, std::unordered_map<u16, ServerPlayer> &, Chunk &, World &, Server &) const;
bool onBlockActivated(const glm::ivec3 &pos, Player &player, World &world, Client &client) const;
void onBlockPlaced(const glm::ivec3 &pos, World &world);
void onBlockPlaced(const glm::ivec3 &pos, World &world) const;

bool canUpdate() const { return m_onTick.valid(); }

void setOnBlockActivated(const sol::function &function) { m_onBlockActivated = function; }
void setOnTick(const sol::function &function) { m_onTick = function; m_canUpdate = m_onTick.valid(); }
void setOnBlockPlaced(const sol::function &function) { m_onBlockPlaced = function; }

private:
sol::unsafe_function m_onBlockActivated;
Expand Down
5 changes: 3 additions & 2 deletions server/include/world/ServerChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

class ServerPlayer;
class Server;
class World;

class ServerChunk : public Chunk {
public:
ServerChunk(s32 x, s32 y, s32 z) : Chunk(x, y, z) {}
ServerChunk(s32 x, s32 y, s32 z, World &world) : Chunk(x, y, z, world) {}

void updateLights();

void onBlockPlaced(int x, int y, int z, const Block &block) const;
void tick(std::unordered_map<u16, ServerPlayer> &players, World &world, Server &server);

bool isSent() const { return m_isSent; }
Expand Down
1 change: 1 addition & 0 deletions server/source/lua/LuaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void LuaMod::registerBlock(const sol::table &table) {
block.setLightSource(table["is_light_source"].get_or(false));
block.setOnBlockActivated(onBlockActivated);
block.setOnTick(onTick);
block.setOnBlockPlaced(table["on_block_placed"]);

sol::optional<sol::table> boundingBox = table["bounding_box"];
if (boundingBox != sol::nullopt) {
Expand Down
6 changes: 4 additions & 2 deletions server/source/lua/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void ScriptEngine::initUsertypes() {
"get_block", &World::getBlock,
"get_data", &World::getData,
"set_data", &World::setData,
"get_block_data", &World::getBlockData
"get_block_data", &World::getBlockData,
"add_block_data", &World::addBlockData
);

m_lua.new_usertype<ServerWorld>("ServerWorld",
Expand All @@ -59,7 +60,8 @@ void ScriptEngine::initUsertypes() {
m_lua.new_usertype<Chunk>("Chunk",
"get_block", &Chunk::getBlock,
"get_data", &Chunk::getData,
"get_block_data", &Chunk::getBlockData
"get_block_data", &Chunk::getBlockData,
"add_block_data", &Chunk::addBlockData
);

m_lua.new_usertype<BlockData>("BlockData",
Expand Down
2 changes: 1 addition & 1 deletion server/source/world/ServerBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool ServerBlock::onBlockActivated(const glm::ivec3 &pos, Player &player, World
return false;
}

void ServerBlock::onBlockPlaced(const glm::ivec3 &pos, World &world) {
void ServerBlock::onBlockPlaced(const glm::ivec3 &pos, World &world) const {
try {
if (m_onBlockPlaced) {
m_onBlockPlaced(pos, world);
Expand Down
5 changes: 5 additions & 0 deletions server/source/world/ServerChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ void ServerChunk::updateLights() {
}
}

void ServerChunk::onBlockPlaced(int x, int y, int z, const Block &block) const {
const ServerBlock &serverBlock = (ServerBlock &)block;
serverBlock.onBlockPlaced(glm::ivec3{x, y, z}, m_world);
}

void ServerChunk::tick(std::unordered_map<u16, ServerPlayer> &players, World &world, Server &server) {
if (!m_tickingBlocks.empty()) {
for (auto &it : m_tickingBlocks) {
Expand Down
Loading

0 comments on commit a10c037

Please sign in to comment.