NetworkResponse.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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; // dimension 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; // dimension 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; // dimension 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::openDialog(Framework::Text dialogName)
  60. {
  61. adressLength = (char)(4 + dialogName.getLength());
  62. adress = new char[adressLength];
  63. adress[0] = 2; // gui message
  64. adress[1] = 0; // open dialog
  65. *(short*)(adress + 2) = (short)dialogName.getLength(); // block
  66. memcpy(adress + 4, dialogName.getText(), dialogName.getLength());
  67. minPosition.x = std::numeric_limits<float>::min();
  68. minPosition.y = std::numeric_limits<float>::min();
  69. minPosition.z = std::numeric_limits<float>::min();
  70. maxPosition.x = std::numeric_limits<float>::max();
  71. maxPosition.y = std::numeric_limits<float>::max();
  72. maxPosition.z = std::numeric_limits<float>::max();
  73. }
  74. void NetworkResponse::adressGui(Framework::Text elementId)
  75. {
  76. adressLength = (char)(4 + elementId.getLength());
  77. adress = new char[adressLength];
  78. adress[0] = 2; // gui message
  79. adress[1] = 1; // element message
  80. *(short*)(adress + 2) = (short)elementId.getLength(); // block
  81. memcpy(adress + 4, elementId.getText(), elementId.getLength());
  82. minPosition.x = std::numeric_limits<float>::min();
  83. minPosition.y = std::numeric_limits<float>::min();
  84. minPosition.z = std::numeric_limits<float>::min();
  85. maxPosition.x = std::numeric_limits<float>::max();
  86. maxPosition.y = std::numeric_limits<float>::max();
  87. maxPosition.z = std::numeric_limits<float>::max();
  88. }
  89. void NetworkResponse::setMessage(char* msg, int length, bool deleteMsg)
  90. {
  91. message = msg;
  92. msgLength = length;
  93. msgDelete = deleteMsg;
  94. }
  95. void NetworkResponse::setUseBackground()
  96. {
  97. useBackground = 1;
  98. }
  99. void NetworkResponse::sendToAll()
  100. {
  101. broadcast = true;
  102. }
  103. bool NetworkResponse::isAreaAffected(Framework::Vec3<float> min, Framework::Vec3<float> max) const
  104. {
  105. return minPosition.x <= max.x && maxPosition.x >= min.x &&
  106. minPosition.y <= max.y && maxPosition.y >= min.y &&
  107. minPosition.z <= max.z && maxPosition.z >= min.z;
  108. }
  109. void NetworkResponse::writeTo(Framework::StreamWriter* zWriter) const
  110. {
  111. int total = msgLength + adressLength;
  112. if (total)
  113. {
  114. zWriter->schreibe((char*)&total, 4);
  115. zWriter->schreibe(adress, adressLength);
  116. zWriter->schreibe(message, msgLength);
  117. }
  118. }
  119. bool NetworkResponse::isBroadcast() const
  120. {
  121. return broadcast;
  122. }
  123. bool NetworkResponse::isEmpty() const
  124. {
  125. return msgLength + adressLength <= 0;
  126. }
  127. bool NetworkResponse::isUseBackground() const
  128. {
  129. return useBackground;
  130. }