WorldPackets.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "server/packets/WorldPackets.h"
  2. #include "common/network/Packets.h"
  3. void WorldPackets::sendChunk(ServerPlayer& p, World& w, int cx, int cz) {
  4. constexpr int size = BlockStorage::SEGMENT;
  5. cx *= size;
  6. cz *= size;
  7. OutPacket out = OutPacket::reliable(
  8. w.blocks.getHeight() * size * size * sizeof(Block::Id) * 2 + 10);
  9. out.writeU16(Packets::S_WORLD_SEGMENT);
  10. out.writeS32(cx);
  11. out.writeS32(cz);
  12. int endX = cx + size;
  13. int endZ = cz + size;
  14. Block::Id current = w.blocks.get(cx, 0, cz);
  15. uint16 counter = 0;
  16. for(int y = 0; y < w.blocks.getHeight(); y++) {
  17. for(int x = cx; x < endX; x++) {
  18. for(int z = cz; z < endZ; z++) {
  19. Block::Id id = w.blocks.get(x, y, z);
  20. if(current == id && counter < 65535) {
  21. counter++;
  22. } else {
  23. out.writeU16(current);
  24. out.writeU16(counter);
  25. current = id;
  26. counter = 1;
  27. }
  28. }
  29. }
  30. }
  31. out.writeU16(current);
  32. out.writeU16(counter);
  33. p.send(out);
  34. }