NetworkMessage.cpp 2.9 KB

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