#include #include "common/network/Packets.h" #include "server/GameServer.h" #include "server/commands/CommandManager.h" #include "server/commands/CommandTypes.h" #include "server/world/WorldGenerator.h" GameServer::GameServer(Server& server) : state(server), reader(0, "> ") { worlds.add(new World(blocks)); WorldGenerator wg(blocks, *(worlds[0])); wg.generate(); } void GameServer::tick() { tps.update(); handleCommands(); } void GameServer::handleCommands() { if(!reader.canRead()) { return; } const char* s = reader.readLine(); if(s == nullptr) { return; } RawCommand rawCommand(s); commandManager.execute(state, rawCommand); } void GameServer::onConnect(Server::Client& 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) { (void)client; std::cout << "disconnected\n"; } void GameServer::onPacket(Server::Client& client, InPacket& in) { (void)client; StringBuffer<256> s; int8 c; while(in.readS8(c)) { s.append(c); } std::cout << "Packet: " << s << "\n"; } bool GameServer::isRunning() const { return state.running; }