NetworkResponse.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "NetworkResponse.h"
  2. #include "Chunk.h"
  3. #include "Entity.h"
  4. #include "Game.h"
  5. NetworkResponse::NetworkResponse()
  6. {
  7. adress = 0;
  8. adressLength = 0;
  9. broadcast = 0;
  10. message = 0;
  11. msgDelete = 0;
  12. msgLength = 0;
  13. useBackground = 0;
  14. }
  15. NetworkResponse::~NetworkResponse()
  16. {
  17. if( msgDelete )
  18. delete[] message;
  19. delete[] adress;
  20. }
  21. void NetworkResponse::adressChunck( Chunk* zChunk )
  22. {
  23. adressLength = 14;
  24. adress = new char[ adressLength ];
  25. adress[ 0 ] = 1; // world response
  26. *(int*)(adress + 1) = zChunk->getDimensionId();
  27. adress[ 5 ] = 1; // chunck
  28. Framework::Punkt center = zChunk->getCenter();
  29. *(int*)(adress + 6) = center.x;
  30. *(int*)(adress + 10) = center.y;
  31. minPosition = zChunk->getMin();
  32. maxPosition = zChunk->getMax();
  33. }
  34. void NetworkResponse::adressEntity( Entity* zEntity )
  35. {
  36. adressLength = 10;
  37. adress = new char[ adressLength ];
  38. adress[ 0 ] = 1; // world response
  39. *(int*)(adress + 1) = zEntity->getCurrentDimensionId();
  40. adress[ 5 ] = 2; // entity
  41. *(int*)(adress + 6) = zEntity->getId();
  42. minPosition = zEntity->getPosition();
  43. maxPosition = zEntity->getPosition();
  44. }
  45. void NetworkResponse::adressBlock( Block* zBlock )
  46. {
  47. adressLength = 18;
  48. adress = new char[ adressLength ];
  49. adress[ 0 ] = 1; // world response
  50. *(int*)(adress + 1) = zBlock->getDimensionId();
  51. adress[ 5 ] = 3; // block
  52. Framework::Vec3<int> pos = zBlock->getPos();
  53. *(int*)(adress + 6) = pos.x;
  54. *(int*)(adress + 10) = pos.y;
  55. *(int*)(adress + 14) = pos.z;
  56. minPosition = pos;
  57. maxPosition = pos;
  58. }
  59. void NetworkResponse::setMessage( char* msg, int length, bool deleteMsg )
  60. {
  61. message = msg;
  62. msgLength = length;
  63. msgDelete = deleteMsg;
  64. }
  65. void NetworkResponse::setUseBackground()
  66. {
  67. useBackground = 1;
  68. }
  69. void NetworkResponse::sendToAll()
  70. {
  71. broadcast = true;
  72. }
  73. bool NetworkResponse::isAreaAffected( Framework::Vec3<float> min, Framework::Vec3<float> max ) const
  74. {
  75. return minPosition.x <= max.x && maxPosition.x >= min.x &&
  76. minPosition.y <= max.y && maxPosition.y >= min.y &&
  77. minPosition.z <= max.z && maxPosition.z >= min.z;
  78. }
  79. void NetworkResponse::writeTo( Framework::StreamWriter* zWriter ) const
  80. {
  81. int total = msgLength + adressLength;
  82. if( total )
  83. {
  84. zWriter->schreibe( (char*)&total, 4 );
  85. zWriter->schreibe( adress, adressLength );
  86. zWriter->schreibe( message, msgLength );
  87. }
  88. }
  89. bool NetworkResponse::isBroadcast() const
  90. {
  91. return broadcast;
  92. }
  93. bool NetworkResponse::isEmpty() const
  94. {
  95. return msgLength + adressLength <= 0;
  96. }
  97. bool NetworkResponse::isUseBackground() const
  98. {
  99. return useBackground;
  100. }