ServerPlayer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "server/entities/ServerPlayer.h"
  2. #include "common/network/toclient/EntityUpdatePacket.h"
  3. #include "server/Game.h"
  4. #include "server/GameServer.h"
  5. #include "utils/Logger.h"
  6. ServerPlayer::ServerPlayer(Server::Client& client) : client(client) {
  7. skip = true;
  8. }
  9. void ServerPlayer::tick() {
  10. if(!history.canRead()) {
  11. LOG_DEBUG("No data");
  12. return;
  13. }
  14. PlayerUpdatePacket p = history.read();
  15. Vector3 move = p.position - position;
  16. float xzDistance = Vector2(move[0], move[2]).squareLength();
  17. float maxXZDistance = getMaxXZVelocity();
  18. maxXZDistance *= maxXZDistance;
  19. maxXZDistance *= 1.01f;
  20. if(xzDistance > maxXZDistance) {
  21. setClientPosition();
  22. return;
  23. }
  24. if(move[1] > 0.4f) {
  25. jump();
  26. }
  27. Entity::tick();
  28. Vector3 actualClientMove = Game::world.limitMove(*this, move);
  29. Vector3 actualServerMove =
  30. Game::world.limitMove(*this, Vector3(move[0], velocity[1], move[2]));
  31. float distance = (actualClientMove - actualServerMove).squareLength();
  32. if(distance > 0.001f) {
  33. actualClientMove = actualServerMove;
  34. }
  35. this->move(actualClientMove);
  36. if((move - actualClientMove).squareLength() > 0.05f) {
  37. setClientPosition();
  38. }
  39. velocity[0] = 0.0f;
  40. velocity[2] = 0.0f;
  41. }
  42. void ServerPlayer::onChat(InPacket& in) {
  43. (void)in;
  44. }
  45. void ServerPlayer::send(OutPacket& out) {
  46. client.send(out);
  47. }
  48. void ServerPlayer::onUpdatePacket(const PlayerUpdatePacket& p) {
  49. if(history.write(p)) {
  50. LOG_WARNING("FULL QUEUE");
  51. }
  52. }
  53. void ServerPlayer::setClientPosition() {
  54. history.clear();
  55. EntityUpdatePacket e(*this);
  56. OutPacket out = OutPacket::sequenced(EntityUpdatePacket::getSize());
  57. e.write(out);
  58. GameServer::sendToAll(out);
  59. }