Browse Source

experimental world sending

Kajetan Johannes Hammerle 3 years ago
parent
commit
6c07bebf91

+ 24 - 4
client/Game.cpp

@@ -94,10 +94,30 @@ void Game::onPacket(InPacket& in) {
     }
     switch(id) {
         case ServerPacket::CHAT:
-            StringBuffer<256> s;
-            in.readString(s);
-            std::cout << s << '\n';
-            break;
+            {
+                StringBuffer<256> s;
+                in.readString(s);
+                std::cout << s << '\n';
+                break;
+            }
+        case ServerPacket::WORLD:
+            {
+                std::cout << "World data:\n";
+                for(int x = 0; x < world.getSize(); x++) {
+                    for(int y = 0; y < world.getHeight(); y++) {
+                        for(int z = 0; z < world.getSize(); z++) {
+                            BlockId id;
+                            if(in.readU16(id)) {
+                                std::cout << "WELL\n";
+                                return;
+                            }
+                            world.setBlock(x, y, z, blockRegistry.getBlock(id));
+                        }
+                    }
+                }
+                world.dirty = true;
+                break;
+            }
     }
 }
 

+ 14 - 10
client/rendering/renderer/WorldRenderer.cpp

@@ -2,20 +2,24 @@
 
 WorldRenderer::WorldRenderer(const World& world)
     : world(world), texture("resources/textures.png", 4) {
-    TypedBuffer<Triangle> buffer(100);
-    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) {
-                    addCube(buffer, x, y, z);
+}
+
+void WorldRenderer::render(float lag, ShaderMatrix& sm) {
+    if(world.dirty) {
+        TypedBuffer<Triangle> buffer(100);
+        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) {
+                        addCube(buffer, x, y, z);
+                    }
                 }
             }
         }
+        mesh.build(buffer);
+        world.dirty = false;
+        std::cout << "UPDATE\n";
     }
-    mesh.build(buffer);
-}
-
-void WorldRenderer::render(float lag, ShaderMatrix& sm) {
     (void)lag;
     (void)sm;
     texture.bindTo(0);

+ 4 - 5
client/rendering/renderer/WorldRenderer.h

@@ -7,17 +7,16 @@
 #include "rendering/FileTexture.h"
 
 class WorldRenderer {
+    const World& world;
+    Mesh mesh;
+    FileTexture texture;
+
 public:
     WorldRenderer(const World& world);
 
     void render(float lag, ShaderMatrix& sm);
     void addCube(TypedBuffer<Triangle>& buffer, float x, float y, float z);
     bool isAir(int x, int y, int z) const;
-
-private:
-    const World& world;
-    Mesh mesh;
-    FileTexture texture;
 };
 
 #endif

+ 3 - 3
common/block/BlockRegistry.h

@@ -7,6 +7,9 @@
 #include "utils/List.h"
 
 class BlockRegistry final {
+    List<Block> blocks;
+    HashMap<BlockName, BlockId> registryToId;
+
 public:
     BlockRegistry();
     void forEach(void (*f)(const Block&)) const;
@@ -15,9 +18,6 @@ public:
 
 private:
     void add(const char* registry);
-
-    List<Block> blocks;
-    HashMap<BlockName, BlockId> registryToId;
 };
 
 #endif

+ 1 - 1
common/network/Packets.h

@@ -1,6 +1,6 @@
 #ifndef PACKETS_H
 #define PACKETS_H
 
-enum ServerPacket { CHAT };
+enum ServerPacket { CHAT, WORLD };
 
 #endif

+ 1 - 1
common/world/World.cpp

@@ -3,7 +3,7 @@
 #include "utils/Random.h"
 
 World::World(const BlockRegistry& blockRegistry)
-    : blockRegistry(blockRegistry), blocks(7, 7) {
+    : blockRegistry(blockRegistry), blocks(7, 7), dirty(true) {
 }
 
 void World::setBlock(int x, int y, int z, const Block& block) {

+ 2 - 0
common/world/World.h

@@ -9,6 +9,8 @@ class World final {
     BlockStorage blocks;
 
 public:
+    mutable bool dirty;
+
     World(const BlockRegistry& blockRegistry);
 
     void setBlock(int x, int y, int z, const Block& block);

+ 1 - 0
meson.build

@@ -15,6 +15,7 @@ src_server = ['server/Main.cpp',
     'server/commands/CommandManager.cpp',
     'server/world/WorldGenerator.cpp',
     'server/snuviscript/Snuvi.cpp',
+    'server/PackageHandler.cpp',
 ]
 
 src_client = ['client/Main.cpp',

+ 16 - 1
server/GameServer.cpp

@@ -1,5 +1,6 @@
 #include <iostream>
 
+#include "common/network/Packets.h"
 #include "server/GameServer.h"
 #include "server/commands/CommandManager.h"
 #include "server/commands/CommandTypes.h"
@@ -8,6 +9,7 @@
 GameServer::GameServer(Server& server) : state(server), reader(0, "> ") {
     worlds.add(new World(blocks));
     WorldGenerator wg(blocks, *(worlds[0]));
+    wg.generate();
 }
 
 void GameServer::tick() {
@@ -28,8 +30,21 @@ void GameServer::handleCommands() {
 }
 
 void GameServer::onConnect(Server::Client& client) {
-    (void)client;
     std::cout << "connected\n";
+
+    World& w = *(worlds[0]);
+    OutPacket out = OutPacket::reliable(
+        w.getHeight() * w.getSize() * w.getSize() * sizeof(BlockId) + 2);
+    out.writeU16(ServerPacket::WORLD);
+    for(int x = 0; x < w.getSize(); x++) {
+        for(int y = 0; y < w.getHeight(); y++) {
+            for(int z = 0; z < w.getSize(); z++) {
+                out.writeU16(w.getBlock(x, y, z).getId());
+            }
+        }
+    }
+
+    client.send(out);
 }
 
 void GameServer::onDisconnect(Server::Client& client) {

+ 1 - 0
server/Main.cpp

@@ -3,6 +3,7 @@
 #include "network/ENet.h"
 #include "network/Server.h"
 #include "server/GameServer.h"
+#include "server/PackageHandler.h"
 #include "server/snuviscript/Snuvi.h"
 #include "utils/Clock.h"
 

+ 1 - 0
server/PackageHandler.cpp

@@ -0,0 +1 @@
+#include "server/PackageHandler.h"

+ 4 - 0
server/PackageHandler.h

@@ -0,0 +1,4 @@
+#ifndef PACKAGE_HANDLER_H
+#define PACKAGE_HANDLER_H
+
+#endif