NetworkResponse.cpp 2.4 KB

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