Browse Source

logger update

Kajetan Johannes Hammerle 3 years ago
parent
commit
9a3c3161b4

+ 10 - 23
client/Game.cpp

@@ -1,7 +1,11 @@
 #include "client/Game.h"
+#include "client/packets/WorldPackets.h"
 #include "common/network/Packets.h"
+#include "utils/Logger.h"
 #include "utils/Utils.h"
 
+BlockRegistry Game::blockRegistry;
+
 Game::Game(TextInput*& textInput, const Controller& controller,
            const Clock& fps, const Clock& tps, RenderSettings& settings,
            const Size& size, Client& client)
@@ -10,7 +14,7 @@ Game::Game(TextInput*& textInput, const Controller& controller,
       renderState(&Game::renderConnectState),
       baseGUI(size, textInput, controller), startGUI(baseGUI),
       world(blockRegistry), worldRenderer(world) {
-    pos = Vector3(16.0f, 30.0f, -10.0f);
+    pos = Vector3(0.0f, 30.0f, 0.0f);
     rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
     rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
 }
@@ -93,31 +97,14 @@ void Game::onPacket(InPacket& in) {
         return;
     }
     switch(id) {
-        case ServerPacket::CHAT:
+        case S_CHAT:
             {
                 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;
+                puts(s);
                 break;
             }
+        case S_WORLD_SEGMENT: WorldPackets::receiveChunk(world, in); break;
     }
 }
 
@@ -126,9 +113,9 @@ void Game::tickConnectState() {
     StartGUI::Address a;
     if(startGUI.getAddress(a)) {
         if(client.connect(a, 11196, 3000)) {
-            std::cout << client.getError() << '\n';
+            LOG_INFO(client.getError());
         } else {
-            std::cout << "connected\n";
+            LOG_INFO("connected");
             tickState = &Game::tickConnectedState;
             renderState = &Game::renderConnectedState;
         }

+ 2 - 2
client/Game.h

@@ -34,12 +34,12 @@ class Game final {
     Quaternion lastRotation;
     Quaternion rotation;
 
-    BlockRegistry blockRegistry;
     World world;
-
     WorldRenderer worldRenderer;
 
 public:
+    static BlockRegistry blockRegistry;
+
     Game(TextInput*& textInput, const Controller& controller, const Clock& fps,
          const Clock& tps, RenderSettings& renderSettings, const Size& size,
          Client& client);

+ 4 - 3
client/Main.cpp

@@ -6,24 +6,25 @@
 #include "network/Client.h"
 #include "network/ENet.h"
 #include "rendering/Window.h"
+#include "utils/Logger.h"
 #include "wrapper/GL.h"
 
 int main() {
     ENet enet;
     if(enet.init()) {
-        std::cout << "cannot initialize enet\n";
+        LOG_ERROR("cannot initialize enet");
         return 0;
     }
     Client client;
     if(client.hasError()) {
-        std::cout << client.getError() << '\n';
+        LOG_ERROR(client.getError());
         return 0;
     }
     WindowOptions options(4, 0, {1024, 620}, false, "test");
     TextInput* textInput = nullptr;
     Window w(textInput, options);
     if(w.getError().has()) {
-        std::cout << w.getError().message << '\n';
+        LOG_ERROR(w.getError().message);
         return 0;
     }
 

+ 33 - 0
client/packets/WorldPackets.cpp

@@ -0,0 +1,33 @@
+#include "client/packets/WorldPackets.h"
+#include "client/Game.h"
+#include "common/network/Packets.h"
+#include "utils/Logger.h"
+
+void WorldPackets::receiveChunk(World& w, InPacket& in) {
+    LOG_DEBUG("received chunk from server");
+    int32 ox;
+    int32 oz;
+    if(in.readS32(ox) || in.readS32(oz)) {
+        LOG_WARNING("missing chunk offset in packet");
+        return;
+    }
+    uint16 counter = 0;
+    const Block* b = &Game::blockRegistry.getBlock("air");
+    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);
+                }
+                w.setBlock(ox + x, y, oz + z, *b);
+                counter--;
+            }
+        }
+    }
+    w.dirty = true;
+}

+ 11 - 0
client/packets/WorldPackets.h

@@ -0,0 +1,11 @@
+#ifndef WORLD_PACKETS_H
+#define WORLD_PACKETS_H
+
+#include "common/world/World.h"
+#include "network/Packet.h"
+
+namespace WorldPackets {
+    void receiveChunk(World& w, InPacket& in);
+}
+
+#endif

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

@@ -1,4 +1,5 @@
 #include "client/rendering/renderer/WorldRenderer.h"
+#include "utils/Logger.h"
 
 WorldRenderer::WorldRenderer(const World& world)
     : world(world), texture("resources/textures.png", 4) {
@@ -18,10 +19,9 @@ void WorldRenderer::render(float lag, ShaderMatrix& sm) {
         }
         mesh.build(buffer);
         world.dirty = false;
-        std::cout << "UPDATE\n";
+        LOG_DEBUG("world render update");
     }
     (void)lag;
-    (void)sm;
     texture.bindTo(0);
     for(int x = -1; x <= 1; x++) {
         for(int z = -1; z <= 1; z++) {

+ 11 - 0
common/entities/Player.h

@@ -0,0 +1,11 @@
+#ifndef PLAYER_H
+#define PLAYER_H
+
+#include "math/Vector.h"
+
+struct Player {
+    Vector3 lastPosition;
+    Vector3 position;
+};
+
+#endif

+ 4 - 1
common/network/Packets.h

@@ -1,6 +1,9 @@
 #ifndef PACKETS_H
 #define PACKETS_H
 
-enum ServerPacket { CHAT, WORLD };
+// packets from the server to clients
+enum ServerPacket { S_CHAT, S_WORLD_SEGMENT };
+// packets from clients to the server
+enum ClientPacket { C_CHAT };
 
 #endif

+ 0 - 4
common/world/BlockStorage.cpp

@@ -1,9 +1,5 @@
 #include "common/world/BlockStorage.h"
 
-static constexpr int SEGMENT_BITS = 6;
-static constexpr int SEGMENT = 1 << SEGMENT_BITS;
-static constexpr int SEGMENT_MASK = (1 << SEGMENT_BITS) - 1;
-
 BlockStorage::BlockStorage(int sizeBits, int heightBits)
     : size(1 << sizeBits), height(1 << heightBits), sizeMask(size - 1) {
     maps.resize((size * size * height) / SEGMENT);

+ 5 - 0
common/world/BlockStorage.h

@@ -12,7 +12,12 @@ class BlockStorage final {
 
     List<UniquePointer<BlockMap>> maps;
 
+    static constexpr int SEGMENT_BITS = 4;
+    static constexpr int SEGMENT_MASK = (1 << SEGMENT_BITS) - 1;
+
 public:
+    static constexpr int SEGMENT = 1 << SEGMENT_BITS;
+
     BlockStorage(int sizeBits, int heightBits);
 
     BlockId get(int x, int y, int z) const;

+ 5 - 2
meson.build

@@ -17,6 +17,8 @@ src_server = ['server/Main.cpp',
     'server/world/WorldGenerator.cpp',
     'server/snuviscript/Snuvi.cpp',
     'server/GameServer.cpp',
+    'server/entities/ServerPlayer.cpp',
+    'server/packets/WorldPackets.cpp',
 ]
 
 src_client = ['client/Main.cpp',
@@ -34,12 +36,13 @@ src_client = ['client/Main.cpp',
     'client/rendering/Vertex.cpp',
     'client/rendering/Triangle.cpp',
     'client/gui/BaseGUI.cpp',
-    'client/gui/StartGUI.cpp'
+    'client/gui/StartGUI.cpp',
+    'client/packets/WorldPackets.cpp',
 ]
 
 sources_test = ['tests/Main.cpp']
 
-args = ['-Wall', '-Wextra', '-pedantic', '-Werror']
+args = ['-Wall', '-Wextra', '-pedantic', '-Werror', '-DLOG_LEVEL=4']
 
 liblonelytiger_proj = subproject('lonely-tiger')
 liblonelytiger_dep = liblonelytiger_proj.get_variable('liblonelytiger_dep')

+ 16 - 0
server/Game.cpp

@@ -2,8 +2,10 @@
 #include "commands/Commands.h"
 #include "memory/UniquePointer.h"
 #include "raw-terminal/RawReader.h"
+#include "server/packets/WorldPackets.h"
 #include "server/world/WorldGenerator.h"
 #include "utils/List.h"
+#include "utils/Logger.h"
 
 static bool running = true;
 static RawReader<256, 10> reader{0, "> "};
@@ -45,4 +47,18 @@ static void handleCommands() {
 void Game::tick() {
     ticksPerSecond.update();
     handleCommands();
+}
+
+void Game::addPlayer(ServerPlayer& p) {
+    LOG_INFO("player add");
+    for(int x = -1; x <= 1; x++) {
+        for(int z = -1; z <= 1; z++) {
+            WorldPackets::sendChunk(p, getTestWorld(), x, z);
+        }
+    }
+}
+
+void Game::removePlayer(ServerPlayer& p) {
+    LOG_INFO("player remove");
+    (void)p;
 }

+ 4 - 0
server/Game.h

@@ -2,6 +2,7 @@
 #define GAMESERVER_H
 
 #include "common/world/World.h"
+#include "server/entities/ServerPlayer.h"
 #include "utils/Clock.h"
 
 namespace Game {
@@ -14,6 +15,9 @@ namespace Game {
     void testWorld();
     World& getTestWorld();
     void tick();
+
+    void addPlayer(ServerPlayer& p);
+    void removePlayer(ServerPlayer& p);
 }
 
 #endif

+ 34 - 22
server/GameServer.cpp

@@ -1,41 +1,53 @@
 #include "server/GameServer.h"
 #include "common/network/Packets.h"
+#include "memory/UniquePointer.h"
 #include "server/Game.h"
+#include "utils/Logger.h"
 
 static Server* server = nullptr;
+static HashMap<int, UniquePointer<ServerPlayer>> players;
 
 struct Receiver {
     void onConnect(Server::Client& client) {
-        std::cout << "connected\n";
-
-        World& w = Game::getTestWorld();
-        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());
-                }
-            }
+        if(players.tryEmplace(client.getId(), new ServerPlayer(client))) {
+            LOG_WARNING(StringBuffer<50>("cannot emplace client id: ")
+                            .append(client.getId()));
+            server->disconnect(client);
+            return;
         }
-
-        client.send(out);
+        auto* p = players.search(client.getId());
+        if(p == nullptr) {
+            LOG_WARNING("cannot find added client");
+            return;
+        }
+        Game::addPlayer(**p);
     }
 
     void onDisconnect(Server::Client& client) {
-        (void)client;
-        std::cout << "disconnected\n";
+        auto* p = players.search(client.getId());
+        if(p == nullptr) {
+            LOG_WARNING("disconnecting missing player");
+            return;
+        }
+        Game::removePlayer(**p);
+        players.remove(client.getId());
     }
 
     void onPacket(Server::Client& client, InPacket& in) {
-        (void)client;
-        StringBuffer<256> s;
-        int8 c;
-        while(in.readS8(c)) {
-            s.append(c);
+        auto* p = players.search(client.getId());
+        if(p == nullptr) {
+            LOG_WARNING("packet from missing player");
+            return;
+        }
+        uint16 id;
+        if(in.readU16(id)) {
+            LOG_WARNING("invalid packet from client");
+            return;
+        }
+        switch(id) {
+            case C_CHAT: (*p)->onChat(in); break;
+            default: LOG_WARNING("invalid packet from client");
         }
-        std::cout << "Packet: " << s << "\n";
     }
 };
 

+ 3 - 2
server/Main.cpp

@@ -3,6 +3,7 @@
 #include "server/GameServer.h"
 #include "server/commands/Commands.h"
 #include "server/snuviscript/Snuvi.h"
+#include "utils/Logger.h"
 
 void loop() {
     Clock clock;
@@ -32,12 +33,12 @@ void init(Server* server) {
 int main() {
     ENet enet;
     if(enet.init()) {
-        std::cout << "cannot initialize enet\n";
+        LOG_ERROR("cannot initialize enet");
         return 0;
     }
     Server server(11196, 50);
     if(server.hasError()) {
-        std::cout << server.getError() << "\n";
+        LOG_ERROR(server.getError());
         return 0;
     }
     init(&server);

+ 4 - 1
server/commands/Commands.cpp

@@ -2,6 +2,7 @@
 #include "server/commands/DefaultCommands.h"
 #include "server/commands/SnuviCommands.h"
 #include "utils/HashMap.h"
+#include "utils/Logger.h"
 
 static HashMap<Commands::Name, Commands::Command> commands;
 
@@ -17,7 +18,9 @@ void Commands::add(const Name& name, Command command) {
 void Commands::execute(const Raw& raw) {
     Arguments args(raw);
     if(args.getLength() == 0) {
-        std::cout << "Invalid command syntax: '" << raw << "'\n";
+        LOG_INFO(StringBuffer<100>("invalid command syntax: '")
+                     .append(raw)
+                     .append("'"));
         return;
     }
     Name command(args[0]);

+ 2 - 2
server/commands/DefaultCommands.cpp

@@ -15,10 +15,10 @@ static void commandSay(const Commands::Arguments& args) {
         s.append(' ');
     }
     OutPacket out = OutPacket::reliable(260);
-    out.writeU16(ServerPacket::CHAT);
+    out.writeU16(S_CHAT);
     out.writeString(s);
     GameServer::sendToAll(out);
-    std::cout << s << '\n';
+    puts(s);
 }
 
 void DefaultCommands::init() {

+ 4 - 4
server/commands/SnuviCommands.cpp

@@ -4,8 +4,8 @@
 static Commands::Arguments commandArguments = Commands::Raw();
 
 static void helpScript() {
-    std::cout << "/script start <script>\n";
-    std::cout << "/script term\n";
+    puts("/script start <script>");
+    puts("/script term");
 }
 
 static void commandScript(const Commands::Arguments& args) {
@@ -13,13 +13,13 @@ static void commandScript(const Commands::Arguments& args) {
         helpScript();
     } else if(strcmp(args[1], "start") == 0) {
         if(args.getLength() < 3) {
-            std::cout << "/script start <script>\n";
+            puts("/script start <script>");
             return;
         }
         Snuvi::start(args[2]);
     } else if(strcmp(args[1], "term") == 0) {
         Snuvi::termAll();
-        std::cout << "all scripts were terminated\n";
+        puts("all scripts were terminated");
     } else {
         helpScript();
     }

+ 12 - 0
server/entities/ServerPlayer.cpp

@@ -0,0 +1,12 @@
+#include "server/entities/ServerPlayer.h"
+
+ServerPlayer::ServerPlayer(Server::Client& client) : client(client) {
+}
+
+void ServerPlayer::onChat(InPacket& in) {
+    (void)in;
+}
+
+void ServerPlayer::send(OutPacket& out) {
+    client.send(out);
+}

+ 18 - 0
server/entities/ServerPlayer.h

@@ -0,0 +1,18 @@
+#ifndef SERVER_PLAYER_H
+#define SERVER_PLAYER_H
+
+#include "common/entities/Player.h"
+#include "network/Server.h"
+
+class ServerPlayer : public Player {
+    Server::Client& client;
+
+public:
+    ServerPlayer(Server::Client& client);
+
+    void onChat(InPacket& in);
+    void sendChunk();
+    void send(OutPacket& out);
+};
+
+#endif

+ 38 - 0
server/packets/WorldPackets.cpp

@@ -0,0 +1,38 @@
+#include "server/packets/WorldPackets.h"
+#include "common/network/Packets.h"
+
+void WorldPackets::sendChunk(ServerPlayer& p, World& w, int cx, int cz) {
+    constexpr int size = BlockStorage::SEGMENT;
+    cx *= size;
+    cz *= size;
+    OutPacket out = OutPacket::reliable(
+        w.getHeight() * size * size * sizeof(BlockId) * 2 + 10);
+    out.writeU16(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();
+    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();
+                if(current == id && counter < 65535) {
+                    counter++;
+                } else {
+                    out.writeU16(current);
+                    out.writeU16(counter);
+                    current = id;
+                    counter = 1;
+                }
+            }
+        }
+    }
+    out.writeU16(current);
+    out.writeU16(counter);
+
+    p.send(out);
+}

+ 11 - 0
server/packets/WorldPackets.h

@@ -0,0 +1,11 @@
+#ifndef WORLD_PACKETS_H
+#define WORLD_PACKETS_H
+
+#include "common/world/World.h"
+#include "server/entities/ServerPlayer.h"
+
+namespace WorldPackets {
+    void sendChunk(ServerPlayer& p, World& w, int cx, int cz);
+}
+
+#endif

+ 1 - 1
subprojects/gaming-core

@@ -1 +1 @@
-Subproject commit f2184c96f9ed350792c4429aa3aaa530ba1b617a
+Subproject commit 35bbce6cd688713970efad4fad1412d404bc2e21