NetworkMessage.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "NetworkMessage.h"
  2. #include "Chunk.h"
  3. #include "Entity.h"
  4. #include "Game.h"
  5. NetworkMessage::NetworkMessage()
  6. : Framework::ReferenceCounter()
  7. {
  8. address = 0;
  9. addressLength = 0;
  10. broadcast = 0;
  11. message = 0;
  12. msgLength = 0;
  13. useBackground = 0;
  14. }
  15. NetworkMessage::~NetworkMessage()
  16. {
  17. delete[] message;
  18. delete[] address;
  19. }
  20. void NetworkMessage::addressChunck(const Chunk* zChunk)
  21. {
  22. delete[] address;
  23. addressLength = 14;
  24. address = new char[addressLength];
  25. address[0] = 1; // dimension response
  26. *(int*)(address + 1) = zChunk->getDimensionId();
  27. address[5] = 1; // chunck
  28. Framework::Punkt center = zChunk->getCenter();
  29. *(int*)(address + 6) = center.x;
  30. *(int*)(address + 10) = center.y;
  31. }
  32. void NetworkMessage::addressEntity(const Entity* zEntity)
  33. {
  34. delete[] address;
  35. addressLength = 10;
  36. address = new char[addressLength];
  37. address[0] = 1; // dimension response
  38. *(int*)(address + 1) = zEntity->getDimensionId();
  39. address[5] = 2; // entity
  40. *(int*)(address + 6) = zEntity->getId();
  41. }
  42. void NetworkMessage::addressDimension(const Dimension *zDim)
  43. {
  44. delete[] address;
  45. addressLength = 5;
  46. address = new char[5];
  47. address[0] = 1; // dimension response
  48. *(int*)(address + 1) = zDim->getDimensionId();
  49. }
  50. void NetworkMessage::addressBlock(const Block* zBlock)
  51. {
  52. delete[] address;
  53. addressLength = 18;
  54. address = new char[addressLength];
  55. address[0] = 1; // dimension response
  56. *(int*)(address + 1) = zBlock->getDimensionId();
  57. address[5] = 3; // block
  58. Framework::Vec3<int> pos = zBlock->getPos();
  59. *(int*)(address + 6) = pos.x;
  60. *(int*)(address + 10) = pos.y;
  61. *(int*)(address + 14) = pos.z;
  62. }
  63. void NetworkMessage::openDialog(Framework::Text dialogName)
  64. {
  65. delete[] address;
  66. addressLength = (char)(4 + dialogName.getLength());
  67. address = new char[addressLength];
  68. address[0] = 2; // gui message
  69. address[1] = 0; // open dialog
  70. *(short*)(address + 2) = (short)dialogName.getLength(); // block
  71. memcpy(address + 4, dialogName.getText(), dialogName.getLength());
  72. }
  73. void NetworkMessage::addressGui(Framework::Text elementId)
  74. {
  75. delete[] address;
  76. addressLength = (char)(4 + elementId.getLength());
  77. address = new char[addressLength];
  78. address[0] = 2; // gui message
  79. address[1] = 1; // element message
  80. *(short*)(address + 2) = (short)elementId.getLength(); // block
  81. memcpy(address + 4, elementId.getText(), elementId.getLength());
  82. }
  83. void NetworkMessage::setMessage(char* msg, int length)
  84. {
  85. delete[] message;
  86. message = msg;
  87. msgLength = length;
  88. }
  89. void NetworkMessage::sendChatMessage(ChatMessage* zMsg)
  90. {
  91. delete[] address;
  92. addressLength = 1;
  93. address = new char[1];
  94. address[0] = 4; // chat message
  95. InMemoryBuffer buffer;
  96. zMsg->writeTo(&buffer);
  97. char* msg = new char[buffer.getSize()];
  98. buffer.lese(msg, (int)buffer.getSize());
  99. setMessage(msg, (int)buffer.getSize());
  100. }
  101. void NetworkMessage::sendChatOptions(ChatObserver* zOptions)
  102. {
  103. delete[] address;
  104. addressLength = 1;
  105. address = new char[1];
  106. address[0] = 5; // chat options
  107. InMemoryBuffer buffer;
  108. zOptions->writeTo(&buffer);
  109. char* msg = new char[buffer.getSize()];
  110. buffer.lese(msg, (int)buffer.getSize());
  111. setMessage(msg, (int)buffer.getSize());
  112. }
  113. void NetworkMessage::sendMap(ChunkMap* zMap)
  114. {
  115. delete[] address;
  116. addressLength = 1;
  117. address = new char[1];
  118. address[0] = 6; // map data
  119. InMemoryBuffer buffer;
  120. zMap->writeTo(&buffer);
  121. char* msg = new char[buffer.getSize()];
  122. buffer.lese(msg, (int)buffer.getSize());
  123. setMessage(msg, (int)buffer.getSize());
  124. }
  125. void NetworkMessage::sendPlayerPositions(char* msg, int length) {
  126. delete[] address;
  127. addressLength = 1;
  128. address = new char[1];
  129. address[0] = 7; // map player positions
  130. setMessage(msg, length);
  131. }
  132. void NetworkMessage::setUseBackground()
  133. {
  134. useBackground = 1;
  135. }
  136. void NetworkMessage::sendToAll()
  137. {
  138. broadcast = true;
  139. }
  140. void NetworkMessage::animateBlockBone(int dimensionId,
  141. Framework::Punkt chunkCenter,
  142. int index,
  143. int boneId,
  144. double time,
  145. Framework::Vec3<float> pos,
  146. Framework::Vec3<float> rotation)
  147. {
  148. delete[] address;
  149. addressLength = 15;
  150. address = new char[addressLength];
  151. address[0] = 1; // dimension api
  152. *(int*)(address + 1) = dimensionId;
  153. address[5] = 1; // chunk api
  154. *(int*)(address + 6) = chunkCenter.x;
  155. *(int*)(address + 10) = chunkCenter.y;
  156. address[14] = 1; // animate block
  157. char* msg = new char[40];
  158. *(int*)(msg) = index;
  159. *(int*)(msg + 4) = boneId;
  160. *(double*)(msg + 8) = time;
  161. *(float*)(msg + 16) = pos.x;
  162. *(float*)(msg + 20) = pos.y;
  163. *(float*)(msg + 24) = pos.z;
  164. *(float*)(msg + 28) = rotation.x;
  165. *(float*)(msg + 32) = rotation.y;
  166. *(float*)(msg + 36) = rotation.z;
  167. setMessage(msg, 40);
  168. }
  169. void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const
  170. {
  171. int total = msgLength + addressLength;
  172. if (total)
  173. {
  174. zWriter->schreibe((char*)&GameClient::Message::API_MESSAGE, 1);
  175. zWriter->schreibe((char*)&total, 4);
  176. zWriter->schreibe(address, addressLength);
  177. zWriter->schreibe(message, msgLength);
  178. }
  179. }
  180. bool NetworkMessage::isBroadcast() const
  181. {
  182. return broadcast;
  183. }
  184. bool NetworkMessage::isEmpty() const
  185. {
  186. return msgLength + addressLength <= 0;
  187. }
  188. bool NetworkMessage::isUseBackground() const
  189. {
  190. return useBackground;
  191. }
  192. NetworkMessage* NetworkMessage::clone() const
  193. {
  194. NetworkMessage* result = new NetworkMessage();
  195. result->message = new char[(int)msgLength];
  196. memcpy(result->message, message, (int)msgLength);
  197. result->msgLength = msgLength;
  198. result->address = new char[(int)addressLength];
  199. memcpy(result->address, address, (int)addressLength);
  200. result->addressLength = addressLength;
  201. result->broadcast = broadcast;
  202. result->useBackground = useBackground;
  203. return result;
  204. }