NetworkMessage.cpp 3.6 KB

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