NetworkMessage.cpp 3.7 KB

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