NetworkResponse.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. affectedDimension = -1;
  15. }
  16. NetworkResponse::~NetworkResponse()
  17. {
  18. if (msgDelete)
  19. delete[] message;
  20. delete[] adress;
  21. }
  22. void NetworkResponse::adressChunck(Chunk* zChunk)
  23. {
  24. adressLength = 10;
  25. adress = new char[adressLength];
  26. adress[0] = 1; // dimension response
  27. adress[1] = 1; // chunck
  28. Framework::Punkt center = zChunk->getCenter();
  29. *(int*)(adress + 2) = center.x;
  30. *(int*)(adress + 6) = center.y;
  31. minPosition = zChunk->getMin();
  32. maxPosition = zChunk->getMax();
  33. affectedDimension = zChunk->getDimensionId();
  34. }
  35. void NetworkResponse::adressEntity(Entity* zEntity)
  36. {
  37. adressLength = 6;
  38. adress = new char[adressLength];
  39. adress[0] = 1; // dimension response
  40. adress[1] = 2; // entity
  41. *(int*)(adress + 2) = zEntity->getId();
  42. minPosition = zEntity->getPosition();
  43. maxPosition = zEntity->getPosition();
  44. affectedDimension = zEntity->getCurrentDimensionId();
  45. }
  46. void NetworkResponse::adressBlock(Block* zBlock)
  47. {
  48. adressLength = 14;
  49. adress = new char[adressLength];
  50. adress[0] = 1; // dimension response
  51. adress[1] = 3; // block
  52. Framework::Vec3<int> pos = zBlock->getPos();
  53. *(int*)(adress + 2) = pos.x;
  54. *(int*)(adress + 6) = pos.y;
  55. *(int*)(adress + 10) = pos.z;
  56. minPosition = pos;
  57. maxPosition = pos;
  58. affectedDimension = zBlock->getDimensionId();
  59. }
  60. void NetworkResponse::openDialog(Framework::Text dialogName)
  61. {
  62. adressLength = (char)(4 + dialogName.getLength());
  63. adress = new char[adressLength];
  64. adress[0] = 2; // gui message
  65. adress[1] = 0; // open dialog
  66. *(short*)(adress + 2) = (short)dialogName.getLength(); // block
  67. memcpy(adress + 4, dialogName.getText(), dialogName.getLength());
  68. minPosition.x = std::numeric_limits<float>::min();
  69. minPosition.y = std::numeric_limits<float>::min();
  70. minPosition.z = std::numeric_limits<float>::min();
  71. maxPosition.x = std::numeric_limits<float>::max();
  72. maxPosition.y = std::numeric_limits<float>::max();
  73. maxPosition.z = std::numeric_limits<float>::max();
  74. }
  75. void NetworkResponse::adressGui(Framework::Text elementId)
  76. {
  77. adressLength = (char)(4 + elementId.getLength());
  78. adress = new char[adressLength];
  79. adress[0] = 2; // gui message
  80. adress[1] = 1; // element message
  81. *(short*)(adress + 2) = (short)elementId.getLength(); // block
  82. memcpy(adress + 4, elementId.getText(), elementId.getLength());
  83. minPosition.x = std::numeric_limits<float>::min();
  84. minPosition.y = std::numeric_limits<float>::min();
  85. minPosition.z = std::numeric_limits<float>::min();
  86. maxPosition.x = std::numeric_limits<float>::max();
  87. maxPosition.y = std::numeric_limits<float>::max();
  88. maxPosition.z = std::numeric_limits<float>::max();
  89. }
  90. void NetworkResponse::setMessage(char* msg, int length, bool deleteMsg)
  91. {
  92. message = msg;
  93. msgLength = length;
  94. msgDelete = deleteMsg;
  95. }
  96. void NetworkResponse::setUseBackground()
  97. {
  98. useBackground = 1;
  99. }
  100. void NetworkResponse::sendToAll()
  101. {
  102. broadcast = true;
  103. }
  104. bool NetworkResponse::isAreaAffected(Framework::Vec3<float> min, Framework::Vec3<float> max, int affectedDimension) const
  105. {
  106. return minPosition.x <= max.x && maxPosition.x >= min.x &&
  107. minPosition.y <= max.y && maxPosition.y >= min.y &&
  108. minPosition.z <= max.z && maxPosition.z >= min.z && (this->affectedDimension < 0 || this->affectedDimension == affectedDimension);
  109. }
  110. void NetworkResponse::writeTo(Framework::StreamWriter* zWriter) const
  111. {
  112. int total = msgLength + adressLength;
  113. if (total)
  114. {
  115. zWriter->schreibe((char*)&total, 4);
  116. zWriter->schreibe(adress, adressLength);
  117. zWriter->schreibe(message, msgLength);
  118. }
  119. }
  120. bool NetworkResponse::isBroadcast() const
  121. {
  122. return broadcast;
  123. }
  124. bool NetworkResponse::isEmpty() const
  125. {
  126. return msgLength + adressLength <= 0;
  127. }
  128. bool NetworkResponse::isUseBackground() const
  129. {
  130. return useBackground;
  131. }