123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #include "NetworkMessage.h"
- #include "Chunk.h"
- #include "Entity.h"
- #include "Game.h"
- NetworkMessage::NetworkMessage()
- : Framework::ReferenceCounter()
- {
- address = 0;
- addressLength = 0;
- broadcast = 0;
- message = 0;
- msgLength = 0;
- useBackground = 0;
- }
- NetworkMessage::~NetworkMessage()
- {
- delete[] message;
- delete[] address;
- }
- void NetworkMessage::addressChunck(const Chunk* zChunk)
- {
- delete[] address;
- addressLength = 10;
- address = new char[addressLength];
- address[0] = 1; // dimension response
- address[1] = 1; // chunck
- Framework::Punkt center = zChunk->getCenter();
- *(int*)(address + 2) = center.x;
- *(int*)(address + 6) = center.y;
- }
- void NetworkMessage::addressEntity(const Entity* zEntity)
- {
- delete[] address;
- addressLength = 6;
- address = new char[addressLength];
- address[0] = 1; // dimension response
- address[1] = 2; // entity
- *(int*)(address + 2) = zEntity->getId();
- }
- void NetworkMessage::addressDimension()
- {
- delete[] address;
- addressLength = 1;
- address = new char[1];
- address[0] = 1; // dimension response
- }
- void NetworkMessage::addressBlock(const Block* zBlock)
- {
- delete[] address;
- addressLength = 14;
- address = new char[addressLength];
- address[0] = 1; // dimension response
- address[1] = 3; // block
- Framework::Vec3<int> pos = zBlock->getPos();
- *(int*)(address + 2) = pos.x;
- *(int*)(address + 6) = pos.y;
- *(int*)(address + 10) = 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::setUseBackground()
- {
- useBackground = 1;
- }
- void NetworkMessage::sendToAll()
- {
- broadcast = true;
- }
- 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);
- }
- }
- 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;
- }
|