std::string World::serialize(const std::vector<Vector2i> &chunkIds, const std::vector<Vector2i> &positions, const std::vector<Vector2i> &sizes) { ProtocolMessage msg; WorldZone *worldZone = new WorldZone; ChunkZone *chunkZone; VectorInt *id; VectorUint *position; VectorUint *size; std::string serialized; Chunk *chunk; unsigned int x; unsigned int y; unsigned int chunkNb; unsigned int nbChunks = chunkIds.size(); for (chunkNb = 0; chunkNb < nbChunks; ++chunkNb) { if ((chunk = getChunk(chunkIds[chunkNb])) != nullptr) { chunkZone = worldZone->add_chunkzone(); if (chunkZone == nullptr) continue ; for (y = 0; y < Chunk::height; ++y) { for (x = 0; x < Chunk::width; ++x) { chunkZone->add_fgtiles(static_cast<unsigned int>(chunk->getTile(x, y))); } } } else continue ; id = new VectorInt; position = new VectorUint; size = new VectorUint; id->set_x(chunkIds[chunkNb].x); id->set_y(chunkIds[chunkNb].y); position->set_x(positions[chunkNb].x); position->set_y(positions[chunkNb].y); size->set_x(sizes[chunkNb].x); size->set_y(sizes[chunkNb].y); chunkZone->set_allocated_id(id); chunkZone->set_allocated_position(position); chunkZone->set_allocated_size(size); } msg.set_content(ProtocolMessage::CHUNKZONE); msg.set_allocated_worldzone(worldZone); msg.SerializeToString(&serialized); return serialized; }
void Server::actDisplacement(Client *client, Action act) { ClientEntity &clEnt = client->getEntity(); Vector2f ratio = {CHUNKWIDTH / 256.f, CHUNKHEIGHT / 256.f}; // to remove, let me maintain the speed clEnt.move(Vector2f(act == Action::Left ? -0.01 / ratio.x : act == Action::Right ? 0.01 / ratio.x : 0, act == Action::Forward ? 0.01 / ratio.y : act == Action::Back ? -0.01 / ratio.y : 0)); // petite partie en dur :D const Vector2f &plPos = clEnt.getPosition(); const Vector2i &plChunk = clEnt.getChunkId(); ProtocolMessage msg; Displacement *displacement = new Displacement; VectorFloat *acceleration = new VectorFloat; VectorFloat *velocity = new VectorFloat; Position *position = new Position; VectorInt *chunkId = new VectorInt; VectorFloat *pos = new VectorFloat; std::string serialized; // quick hard coded value acceleration->set_x(0); acceleration->set_y(0); velocity->set_x(0); velocity->set_y(0); chunkId->set_x(plChunk.x); chunkId->set_y(plChunk.y); pos->set_x(plPos.x); pos->set_y(plPos.y); position->set_allocated_chunkid(chunkId); position->set_allocated_pos(pos); displacement->set_allocated_acceleration(acceleration); displacement->set_allocated_velocity(velocity); displacement->set_allocated_position(position); msg.set_content(ProtocolMessage::DISPLACEMENT); msg.set_allocated_displacement(displacement); msg.SerializeToString(&serialized); client->sendPacket(0, serialized); }
std::string World::serialize(const Vector2i &chunkId) { ProtocolMessage msg; ChunkData *chunkData = new ChunkData; VectorInt *vecInt; std::string serialized; Chunk *chunk; unsigned int fgcont = 0; unsigned int bgcont = 0; TileType fgtile; TileType bgtile; TileType oldfgtile; TileType oldbgtile; if ((chunk = getChunk(chunkId)) != nullptr) { for (unsigned int y = 0; y < Chunk::height; ++y) { for (unsigned int x = 0; x < Chunk::width; ++x) { fgtile = chunk->getTile(x, y); bgtile = chunk->getBgTile(x, y); if (x == 0 && y == 0) { oldfgtile = fgtile; oldbgtile = bgtile; } else { if (fgtile != oldfgtile) { chunkData->add_fgnumber(fgcont); chunkData->add_fgtilecode(static_cast<unsigned int>(oldfgtile)); fgcont = 0; oldfgtile = fgtile; } if (bgtile != oldbgtile) { chunkData->add_bgnumber(bgcont); chunkData->add_bgtilecode(static_cast<unsigned int>(oldbgtile)); bgcont = 0; oldbgtile = bgtile; } } ++fgcont; ++bgcont; } } } chunkData->add_bgnumber(bgcont); chunkData->add_bgtilecode(static_cast<unsigned int>(oldbgtile)); chunkData->add_fgnumber(fgcont); chunkData->add_fgtilecode(static_cast<unsigned int>(oldfgtile)); vecInt = new VectorInt; vecInt->set_x(chunkId.x); vecInt->set_y(chunkId.y); chunkData->set_allocated_id(vecInt); msg.set_content(ProtocolMessage::CHUNK); msg.set_allocated_chunkdata(chunkData); msg.SerializeToString(&serialized); return serialized; }