Browse Source

blocks are a global namespace

Kajetan Johannes Hammerle 2 years ago
parent
commit
86ebd10fe2

+ 6 - 23
client/Game.cpp

@@ -6,11 +6,9 @@
 #include "common/network/toserver/PlayerUpdatePacket.h"
 #include "rendering/renderer/WorldRenderer.h"
 #include "utils/Logger.h"
-#include "utils/Random.h"
 #include "utils/Utils.h"
 
-BlockRegistry Game::blockRegistry;
-World Game::world{blockRegistry};
+World Game::world;
 static WorldRenderer worldRenderer{Game::world};
 Controller Game::controller;
 Entity Game::player;
@@ -22,9 +20,6 @@ static State renderState;
 static BaseGUI baseGUI;
 static StartGUI startGUI;
 
-static List<PlayerUpdatePacket> delay;
-static Random rng;
-
 static void tickConnectedState() {
     Game::player.skip = false;
     Game::world.tick();
@@ -59,7 +54,6 @@ static void tickConnectedState() {
 
     if(Game::controller.jump.isDown() && Game::player.isOnGround()) {
         Game::player.jump();
-        // Game::player.acceleration[1] += 0.4;
     }
     if(Game::controller.camLeft.isDown()) {
         Game::player.addLengthAngle(-rotationSpeed);
@@ -74,21 +68,10 @@ static void tickConnectedState() {
         Game::player.addWidthAngle(rotationSpeed * 0.5f);
     }
 
-    delay.add(PlayerUpdatePacket(Game::player));
-    if(rng.nextFloat() < 0.5f) {
-        OutPacket out = OutPacket::reliable(PlayerUpdatePacket::getSize());
-        delay[0].write(out);
-        GameClient::send(out);
-        delay.remove(0);
-
-        if(delay.getLength() > 0) {
-            OutPacket out = OutPacket::reliable(PlayerUpdatePacket::getSize());
-            delay[0].write(out);
-            GameClient::send(out);
-            delay.remove(0);
-        }
-    }
-    LOG_DEBUG(delay.getLength());
+    PlayerUpdatePacket p(Game::player);
+    OutPacket out = OutPacket::reliable(PlayerUpdatePacket::getSize());
+    p.write(out);
+    GameClient::send(out);
 }
 
 static void renderConnectedState() {
@@ -114,6 +97,7 @@ static void renderConnectState() {
 }
 
 bool Game::init() {
+    Block::init();
     if(worldRenderer.init()) {
         return true;
     }
@@ -155,5 +139,4 @@ void Game::onEntityUpdate(EntityUpdatePacket& p) {
     LOG_DEBUG("set");
     player.setPosition(p.position);
     player.velocity = p.velocity;
-    delay.clear();
 }

+ 0 - 2
client/Game.h

@@ -2,13 +2,11 @@
 #define GAME_H
 
 #include "client/input/Controller.h"
-#include "common/block/BlockRegistry.h"
 #include "common/entities/Entity.h"
 #include "common/network/toclient/EntityUpdatePacket.h"
 #include "common/world/World.h"
 
 namespace Game {
-    extern BlockRegistry blockRegistry;
     extern World world;
     extern Controller controller;
     extern Entity player;

+ 5 - 9
client/packets/WorldPackets.cpp

@@ -12,19 +12,15 @@ void WorldPackets::receiveChunk(World& w, InPacket& in) {
         return;
     }
     uint16 counter = 0;
-    const Block* b = &Game::blockRegistry.getBlock("air");
+    Block::Id b = 0;
     for(int y = 0; y < w.getHeight(); y++) {
         for(int x = 0; x < BlockStorage::SEGMENT; x++) {
             for(int z = 0; z < BlockStorage::SEGMENT; z++) {
-                if(counter == 0) {
-                    BlockId id;
-                    if(in.readU16(id) || in.readU16(counter)) {
-                        LOG_WARNING("missing block data in packet");
-                        return;
-                    }
-                    b = &Game::blockRegistry.getBlock(id);
+                if(counter == 0 && (in.readU16(b) || in.readU16(counter))) {
+                    LOG_WARNING("missing block data in packet");
+                    return;
                 }
-                w.setBlock(ox + x, y, oz + z, *b);
+                w.setBlock(ox + x, y, oz + z, b);
                 counter--;
             }
         }

+ 2 - 2
client/rendering/renderer/WorldRenderer.cpp

@@ -20,7 +20,7 @@ void WorldRenderer::render() {
         for(int x = 0; x < world.getSize(); x++) {
             for(int y = 0; y < world.getHeight(); y++) {
                 for(int z = 0; z < world.getSize(); z++) {
-                    if(world.getBlock(x, y, z).getId() != 0) {
+                    if(world.getBlock(x, y, z) != 0) {
                         addCube(buffer, x, y, z);
                     }
                 }
@@ -42,7 +42,7 @@ void WorldRenderer::render() {
 }
 
 bool WorldRenderer::isAir(int x, int y, int z) const {
-    return world.getBlock(x, y, z).getId() == 0;
+    return world.getBlock(x, y, z) == 0;
 }
 
 void WorldRenderer::addCube(TypedBuffer<Triangle>& buffer, float x, float y,

+ 17 - 0
common/Block.cpp

@@ -0,0 +1,17 @@
+#include "common/Block.h"
+#include "utils/Array.h"
+#include "utils/ArrayList.h"
+
+static Array<ArrayList<CollisionBox, 1>, Block::MAX> collisionBoxes;
+
+void Block::init() {
+    for(int i = 1; i < collisionBoxes.getLength(); i++) {
+        collisionBoxes[i].add(Vector3(1.0f, 1.0f, 1.0f));
+    }
+}
+
+void Block::addBoxes(Id block, List<CollisionBox>& list, const Vector3& pos) {
+    for(const CollisionBox& box : collisionBoxes[block]) {
+        list.add(box.offset(pos));
+    }
+}

+ 16 - 0
common/Block.h

@@ -0,0 +1,16 @@
+#ifndef BLOCK_H
+#define BLOCK_H
+
+#include "common/utils/CollisionBox.h"
+#include "utils/List.h"
+#include "utils/Types.h"
+
+namespace Block {
+    static constexpr int MAX = 256;
+    typedef uint16 Id;
+
+    void init();
+    void addBoxes(Id block, List<CollisionBox>& list, const Vector3& pos);
+}
+
+#endif

+ 0 - 22
common/block/Block.cpp

@@ -1,22 +0,0 @@
-#include "common/block/Block.h"
-
-Block::Block(BlockId id, const BlockName& registry)
-    : id(id), registry(registry) {
-    if(id > 0) {
-        boxes.add(Vector3(1.0f, 1.0f, 1.0f));
-    }
-}
-
-BlockId Block::getId() const {
-    return id;
-}
-
-const BlockName& Block::getRegistry() const {
-    return registry;
-}
-
-void Block::addBoxes(List<CollisionBox>& list, const Vector3& pos) const {
-    for(const CollisionBox& box : boxes) {
-        list.add(box.offset(pos));
-    }
-}

+ 0 - 23
common/block/Block.h

@@ -1,23 +0,0 @@
-#ifndef BLOCK_H
-#define BLOCK_H
-
-#include "common/block/BlockTypes.h"
-#include "common/utils/CollisionBox.h"
-#include "utils/ArrayList.h"
-#include "utils/List.h"
-
-class Block final {
-    BlockId id;
-    BlockName registry;
-    ArrayList<CollisionBox, 1> boxes;
-
-public:
-    Block(BlockId id, const BlockName& registry);
-
-    BlockId getId() const;
-    const BlockName& getRegistry() const;
-
-    void addBoxes(List<CollisionBox>& list, const Vector3& pos) const;
-};
-
-#endif

+ 0 - 29
common/block/BlockRegistry.cpp

@@ -1,29 +0,0 @@
-#include "common/block/BlockRegistry.h"
-
-BlockRegistry::BlockRegistry() {
-    add("air");
-    add("stone");
-    add("dirt");
-    add("grass");
-}
-
-void BlockRegistry::forEach(void (*f)(const Block&)) const {
-    for(const Block& b : blocks) {
-        f(b);
-    }
-}
-
-void BlockRegistry::add(const char* registry) {
-    BlockId id = blocks.getLength();
-    blocks.add(id, registry);
-    registryToId.add(registry, id);
-}
-
-const Block& BlockRegistry::getBlock(const BlockName& registry) const {
-    const BlockId* id = registryToId.search(registry);
-    return id == nullptr ? blocks[0] : blocks[*id];
-}
-
-const Block& BlockRegistry::getBlock(BlockId id) const {
-    return id < blocks.getLength() ? blocks[id] : blocks[0];
-}

+ 0 - 23
common/block/BlockRegistry.h

@@ -1,23 +0,0 @@
-#ifndef BLOCKREGISTRY_H
-#define BLOCKREGISTRY_H
-
-#include "common/block/Block.h"
-#include "common/block/BlockTypes.h"
-#include "utils/HashMap.h"
-#include "utils/List.h"
-
-class BlockRegistry final {
-    List<Block> blocks;
-    HashMap<BlockName, BlockId> registryToId;
-
-public:
-    BlockRegistry();
-    void forEach(void (*f)(const Block&)) const;
-    const Block& getBlock(const BlockName& registry) const;
-    const Block& getBlock(BlockId id) const;
-
-private:
-    void add(const char* registry);
-};
-
-#endif

+ 0 - 10
common/block/BlockTypes.h

@@ -1,10 +0,0 @@
-#ifndef BLOCKTYPES_H
-#define BLOCKTYPES_H
-
-#include "utils/StringBuffer.h"
-#include "utils/Types.h"
-
-typedef StringBuffer<32> BlockName;
-typedef uint16 BlockId;
-
-#endif

+ 4 - 4
common/world/BlockMap.cpp

@@ -1,19 +1,19 @@
 #include "common/world/BlockMap.h"
 
-static constexpr BlockId EMPTY_BLOCK = 65535;
+static constexpr Block::Id EMPTY_BLOCK = 65535;
 
-BlockMap::BlockMap(int length, BlockId id) : blocks(length, 1) {
+BlockMap::BlockMap(int length, Block::Id id) : blocks(length, 1) {
     blocks.fill(0);
     map.reserve(2);
     map.add(id);
     map.add(EMPTY_BLOCK);
 }
 
-BlockId BlockMap::get(int index) const {
+Block::Id BlockMap::get(int index) const {
     return map[blocks.get(index)];
 }
 
-void BlockMap::set(int index, BlockId id) {
+void BlockMap::set(int index, Block::Id id) {
     for(int i = 0; i < map.getLength(); i++) {
         if(map[i] == id) {
             blocks.set(index, i);

+ 5 - 5
common/world/BlockMap.h

@@ -1,19 +1,19 @@
 #ifndef BLOCKMAP_H
 #define BLOCKMAP_H
 
-#include "common/block/BlockTypes.h"
+#include "common/Block.h"
 #include "utils/BitArray.h"
 #include "utils/List.h"
 
 class BlockMap {
     BitArray blocks;
-    List<BlockId> map;
+    List<Block::Id> map;
 
 public:
-    BlockMap(int length, BlockId id);
+    BlockMap(int length, Block::Id id);
 
-    BlockId get(int index) const;
-    void set(int index, BlockId id);
+    Block::Id get(int index) const;
+    void set(int index, Block::Id id);
 };
 
 #endif

+ 2 - 2
common/world/BlockStorage.cpp

@@ -5,7 +5,7 @@ BlockStorage::BlockStorage(int sizeBits, int heightBits)
     maps.resize((size * size * height) / SEGMENT);
 }
 
-BlockId BlockStorage::get(int x, int y, int z) const {
+Block::Id BlockStorage::get(int x, int y, int z) const {
     if(y < 0 || y >= height) {
         return y < 0;
     }
@@ -21,7 +21,7 @@ BlockId BlockStorage::get(int x, int y, int z) const {
     return maps[segmentIndex]->get(index & SEGMENT_MASK);
 }
 
-void BlockStorage::set(int x, int y, int z, BlockId id) {
+void BlockStorage::set(int x, int y, int z, Block::Id id) {
     if(y < 0 || y >= height) {
         return;
     }

+ 2 - 2
common/world/BlockStorage.h

@@ -20,8 +20,8 @@ public:
 
     BlockStorage(int sizeBits, int heightBits);
 
-    BlockId get(int x, int y, int z) const;
-    void set(int x, int y, int z, BlockId id);
+    Block::Id get(int x, int y, int z) const;
+    void set(int x, int y, int z, Block::Id id);
 
     int getSize() const;
     int getHeight() const;

+ 9 - 9
common/world/World.cpp

@@ -5,16 +5,15 @@
 #include "utils/Logger.h"
 #include "utils/Random.h"
 
-World::World(const BlockRegistry& blockRegistry)
-    : blockRegistry(blockRegistry), blocks(7, 7), dirty(true) {
+World::World() : blocks(7, 7), dirty(true) {
 }
 
-void World::setBlock(int x, int y, int z, const Block& block) {
-    blocks.set(x, y, z, block.getId());
+void World::setBlock(int x, int y, int z, Block::Id block) {
+    blocks.set(x, y, z, block);
 }
 
-const Block& World::getBlock(int x, int y, int z) const {
-    return blockRegistry.getBlock(blocks.get(x, y, z));
+Block::Id World::getBlock(int x, int y, int z) const {
+    return blocks.get(x, y, z);
 }
 
 int World::getSize() const {
@@ -50,9 +49,10 @@ List<CollisionBox> World::getBoxes(const CollisionBox& box) const {
     for(int x = minX; x <= maxX; x++) {
         for(int y = minY; y <= maxY; y++) {
             for(int z = minZ; z <= maxZ; z++) {
-                getBlock(x, y, z).addBoxes(
-                    boxes, Vector3(static_cast<float>(x), static_cast<float>(y),
-                                   static_cast<float>(z)));
+                Block::addBoxes(getBlock(x, y, z), boxes,
+                                Vector3(static_cast<float>(x),
+                                        static_cast<float>(y),
+                                        static_cast<float>(z)));
             }
         }
     }

+ 3 - 5
common/world/World.h

@@ -1,23 +1,21 @@
 #ifndef WORLD_H
 #define WORLD_H
 
-#include "common/block/BlockRegistry.h"
 #include "common/entities/Entity.h"
 #include "common/world/BlockStorage.h"
 #include "utils/List.h"
 
 class World final {
-    const BlockRegistry& blockRegistry;
     BlockStorage blocks;
     List<Entity*> entities;
 
 public:
     mutable bool dirty;
 
-    World(const BlockRegistry& blockRegistry);
+    World();
 
-    void setBlock(int x, int y, int z, const Block& block);
-    const Block& getBlock(int x, int y, int z) const;
+    void setBlock(int x, int y, int z, Block::Id block);
+    Block::Id getBlock(int x, int y, int z) const;
 
     int getSize() const;
     int getHeight() const;

+ 1 - 2
meson.build

@@ -1,8 +1,7 @@
 project('cubes plus plus', 'cpp')
 
 src_common = [
-    'common/block/Block.cpp', 
-    'common/block/BlockRegistry.cpp', 
+    'common/Block.cpp', 
     'common/world/BlockMap.cpp',
     'common/world/BlockStorage.cpp',
     'common/world/World.cpp', 

+ 2 - 2
server/Game.cpp

@@ -13,8 +13,7 @@ static bool running = true;
 static RawReader<256, 10> reader{0, "> "};
 
 Clock Game::ticksPerSecond;
-BlockRegistry Game::blocks;
-World Game::world{blocks};
+World Game::world;
 
 void Game::stop() {
     running = false;
@@ -25,6 +24,7 @@ bool Game::isRunning() {
 }
 
 void Game::init() {
+    Block::init();
     WorldGenerator::generate(world);
 }
 

+ 0 - 1
server/Game.h

@@ -7,7 +7,6 @@
 
 namespace Game {
     extern Clock ticksPerSecond;
-    extern BlockRegistry blocks;
     extern World world;
 
     void stop();

+ 2 - 2
server/GameServer.cpp

@@ -47,7 +47,7 @@ struct Receiver {
         }
         uint16 id;
         if(in.readU16(id)) {
-            LOG_WARNING("invalid packet from client");
+            LOG_WARNING("invalid packet from client without id");
             return;
         }
         switch(id) {
@@ -55,7 +55,7 @@ struct Receiver {
             case Packets::C_PLAYER_UPDATE:
                 handleControllerPacket(**p, in);
                 break;
-            default: LOG_WARNING("invalid packet from client");
+            default: LOG_WARNING("invalid packet from client with unknown id");
         }
     }
 };

+ 3 - 3
server/packets/WorldPackets.cpp

@@ -6,20 +6,20 @@ void WorldPackets::sendChunk(ServerPlayer& p, World& w, int cx, int cz) {
     cx *= size;
     cz *= size;
     OutPacket out = OutPacket::reliable(
-        w.getHeight() * size * size * sizeof(BlockId) * 2 + 10);
+        w.getHeight() * size * size * sizeof(Block::Id) * 2 + 10);
     out.writeU16(Packets::S_WORLD_SEGMENT);
     out.writeS32(cx);
     out.writeS32(cz);
     int endX = cx + size;
     int endZ = cz + size;
 
-    BlockId current = w.getBlock(cx, 0, cz).getId();
+    Block::Id current = w.getBlock(cx, 0, cz);
     uint16 counter = 0;
 
     for(int y = 0; y < w.getHeight(); y++) {
         for(int x = cx; x < endX; x++) {
             for(int z = cz; z < endZ; z++) {
-                BlockId id = w.getBlock(x, y, z).getId();
+                Block::Id id = w.getBlock(x, y, z);
                 if(current == id && counter < 65535) {
                     counter++;
                 } else {

+ 1 - 3
server/world/WorldGenerator.cpp

@@ -1,15 +1,13 @@
 #include "server/world/WorldGenerator.h"
 #include "common/world/HighMap.h"
-#include "server/Game.h"
 
 void WorldGenerator::generate(World& w) {
-    const Block& stone = Game::blocks.getBlock("stone");
     HighMap map(w.getSize(), w.getHeight());
     for(int x = 0; x < w.getSize(); x++) {
         for(int z = 0; z < w.getSize(); z++) {
             int height = map.getHeight(x, z);
             for(int y = 0; y < height; y++) {
-                w.setBlock(x, y, z, stone);
+                w.setBlock(x, y, z, 1);
             }
         }
     }