123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- #include "NetworkMessage.h"
- #include "Chunk.h"
- #include "Entity.h"
- #include "Game.h"
- NetworkMessage::NetworkMessage()
- : Framework::ReferenceCounter(),
- onAfterSend(0)
- {
- address = 0;
- addressLength = 0;
- broadcast = 0;
- message = 0;
- msgLength = 0;
- useBackground = 0;
- }
- NetworkMessage::~NetworkMessage()
- {
- delete[] message;
- delete[] address;
- }
- void NetworkMessage::setOnAfterSend(std::function<void()> onAfterSend)
- {
- if (this->onAfterSend)
- throw "Illegal State exception: onAfterSend was already set";
- this->onAfterSend = onAfterSend;
- }
- void NetworkMessage::addressChunck(const Chunk* zChunk)
- {
- delete[] address;
- addressLength = 14;
- address = new char[addressLength];
- address[0] = 1; // dimension response
- *(int*)(address + 1) = zChunk->getDimensionId();
- address[5] = 1; // chunck
- Framework::Punkt center = zChunk->getCenter();
- *(int*)(address + 6) = center.x;
- *(int*)(address + 10) = center.y;
- }
- void NetworkMessage::addressEntity(const Entity* zEntity)
- {
- delete[] address;
- addressLength = 10;
- address = new char[addressLength];
- address[0] = 1; // dimension response
- *(int*)(address + 1) = zEntity->getDimensionId();
- address[5] = 2; // entity
- *(int*)(address + 6) = zEntity->getId();
- }
- void NetworkMessage::addressDimension(const Dimension *zDim)
- {
- delete[] address;
- addressLength = 5;
- address = new char[5];
- address[0] = 1; // dimension response
- *(int*)(address + 1) = zDim->getDimensionId();
- }
- void NetworkMessage::addressBlock(const Block* zBlock)
- {
- delete[] address;
- addressLength = 18;
- address = new char[addressLength];
- address[0] = 1; // dimension response
- *(int*)(address + 1) = zBlock->getDimensionId();
- address[5] = 3; // block
- Framework::Vec3<int> pos = zBlock->getPos();
- *(int*)(address + 6) = pos.x;
- *(int*)(address + 10) = pos.y;
- *(int*)(address + 14) = pos.z;
- }
- void NetworkMessage::openDialog(Framework::Text dialogName)
- {
- delete[] address;
- addressLength = (char)(4 + dialogName.getLength());
- address = new char[addressLength];
- address[0] = 2; // gui message
- address[1] = 0; // open dialog
- *(short*)(address + 2) = (short)dialogName.getLength(); // block
- memcpy(address + 4, dialogName.getText(), dialogName.getLength());
- }
- void NetworkMessage::addressGui(Framework::Text elementId)
- {
- delete[] address;
- addressLength = (char)(4 + elementId.getLength());
- address = new char[addressLength];
- address[0] = 2; // gui message
- address[1] = 1; // element message
- *(short*)(address + 2) = (short)elementId.getLength(); // block
- memcpy(address + 4, elementId.getText(), elementId.getLength());
- }
- void NetworkMessage::setMessage(char* msg, int length)
- {
- delete[] message;
- message = msg;
- msgLength = length;
- }
- void NetworkMessage::sendChatMessage(ChatMessage* zMsg)
- {
- delete[] address;
- addressLength = 1;
- address = new char[1];
- address[0] = 4; // chat message
- InMemoryBuffer buffer;
- zMsg->writeTo(&buffer);
- char* msg = new char[buffer.getSize()];
- buffer.lese(msg, (int)buffer.getSize());
- setMessage(msg, (int)buffer.getSize());
- }
- void NetworkMessage::sendChatOptions(ChatObserver* zOptions)
- {
- delete[] address;
- addressLength = 1;
- address = new char[1];
- address[0] = 5; // chat options
- InMemoryBuffer buffer;
- zOptions->writeTo(&buffer);
- char* msg = new char[buffer.getSize()];
- buffer.lese(msg, (int)buffer.getSize());
- setMessage(msg, (int)buffer.getSize());
- }
- void NetworkMessage::sendMap(ChunkMap* zMap)
- {
- delete[] address;
- addressLength = 1;
- address = new char[1];
- address[0] = 6; // map data
- InMemoryBuffer buffer;
- zMap->writeTo(&buffer);
- char* msg = new char[buffer.getSize()];
- buffer.lese(msg, (int)buffer.getSize());
- setMessage(msg, (int)buffer.getSize());
- }
- void NetworkMessage::sendPlayerPositions(char* msg, int length) {
- delete[] address;
- addressLength = 1;
- address = new char[1];
- address[0] = 7; // map player positions
- setMessage(msg, length);
- }
- void NetworkMessage::setUseBackground()
- {
- useBackground = 1;
- }
- void NetworkMessage::sendToAll()
- {
- broadcast = true;
- }
- void NetworkMessage::animateBlockBone(int dimensionId,
- Framework::Punkt chunkCenter,
- int index,
- int boneId,
- double time,
- Framework::Vec3<float> pos,
- Framework::Vec3<float> rotation)
- {
- delete[] address;
- addressLength = 15;
- address = new char[addressLength];
- address[0] = 1; // dimension api
- *(int*)(address + 1) = dimensionId;
- address[5] = 1; // chunk api
- *(int*)(address + 6) = chunkCenter.x;
- *(int*)(address + 10) = chunkCenter.y;
- address[14] = 1; // animate block
- char* msg = new char[40];
- *(int*)(msg) = index;
- *(int*)(msg + 4) = boneId;
- *(double*)(msg + 8) = time;
- *(float*)(msg + 16) = pos.x;
- *(float*)(msg + 20) = pos.y;
- *(float*)(msg + 24) = pos.z;
- *(float*)(msg + 28) = rotation.x;
- *(float*)(msg + 32) = rotation.y;
- *(float*)(msg + 36) = rotation.z;
- setMessage(msg, 40);
- }
- void NetworkMessage::syncTime(
- double time, double nightLength, double transitionLength, double dayLength)
- {
- delete[] address;
- addressLength = 1;
- address = new char[1];
- address[0] = 8;
- delete[] message;
- msgLength = 8 * 4;
- message = new char[msgLength];
- *(double*)(message) = time;
- *(double*)(message + 8) = nightLength;
- *(double*)(message + 16) = transitionLength;
- *(double*)(message + 24) = dayLength;
- }
- void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const
- {
- int total = msgLength + addressLength;
- if (total)
- {
- zWriter->schreibe((char*)&GameClient::Message::API_MESSAGE, 1);
- zWriter->schreibe((char*)&total, 4);
- zWriter->schreibe(address, addressLength);
- zWriter->schreibe(message, msgLength);
- if (onAfterSend) onAfterSend();
- }
- }
- bool NetworkMessage::isBroadcast() const
- {
- return broadcast;
- }
- bool NetworkMessage::isEmpty() const
- {
- return msgLength + addressLength <= 0;
- }
- bool NetworkMessage::isUseBackground() const
- {
- return useBackground;
- }
- NetworkMessage* NetworkMessage::clone() const
- {
- NetworkMessage* result = new NetworkMessage();
- result->message = new char[(int)msgLength];
- memcpy(result->message, message, (int)msgLength);
- result->msgLength = msgLength;
- result->address = new char[(int)addressLength];
- memcpy(result->address, address, (int)addressLength);
- result->addressLength = addressLength;
- result->broadcast = broadcast;
- result->useBackground = useBackground;
- return result;
- }
|