123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #include "NetworkResponse.h"
- #include "Chunk.h"
- #include "Entity.h"
- #include "Game.h"
- NetworkResponse::NetworkResponse()
- {
- adress = 0;
- adressLength = 0;
- broadcast = 0;
- message = 0;
- msgDelete = 0;
- msgLength = 0;
- useBackground = 0;
- affectedDimension = -1;
- }
- NetworkResponse::~NetworkResponse()
- {
- if (msgDelete)
- delete[] message;
- delete[] adress;
- }
- void NetworkResponse::adressChunck(Chunk* zChunk)
- {
- adressLength = 10;
- adress = new char[adressLength];
- adress[0] = 1; // dimension response
- adress[1] = 1; // chunck
- Framework::Punkt center = zChunk->getCenter();
- *(int*)(adress + 2) = center.x;
- *(int*)(adress + 6) = center.y;
- minPosition = zChunk->getMin();
- maxPosition = zChunk->getMax();
- affectedDimension = zChunk->getDimensionId();
- }
- void NetworkResponse::adressEntity(Entity* zEntity)
- {
- adressLength = 6;
- adress = new char[adressLength];
- adress[0] = 1; // dimension response
- adress[1] = 2; // entity
- *(int*)(adress + 2) = zEntity->getId();
- minPosition = zEntity->getPosition();
- maxPosition = zEntity->getPosition();
- affectedDimension = zEntity->getCurrentDimensionId();
- }
- void NetworkResponse::adressBlock(Block* zBlock)
- {
- adressLength = 14;
- adress = new char[adressLength];
- adress[0] = 1; // dimension response
- adress[1] = 3; // block
- Framework::Vec3<int> pos = zBlock->getPos();
- *(int*)(adress + 2) = pos.x;
- *(int*)(adress + 6) = pos.y;
- *(int*)(adress + 10) = pos.z;
- minPosition = pos;
- maxPosition = pos;
- affectedDimension = zBlock->getDimensionId();
- }
- void NetworkResponse::openDialog(Framework::Text dialogName)
- {
- adressLength = (char)(4 + dialogName.getLength());
- adress = new char[adressLength];
- adress[0] = 2; // gui message
- adress[1] = 0; // open dialog
- *(short*)(adress + 2) = (short)dialogName.getLength(); // block
- memcpy(adress + 4, dialogName.getText(), dialogName.getLength());
- minPosition.x = std::numeric_limits<float>::min();
- minPosition.y = std::numeric_limits<float>::min();
- minPosition.z = std::numeric_limits<float>::min();
- maxPosition.x = std::numeric_limits<float>::max();
- maxPosition.y = std::numeric_limits<float>::max();
- maxPosition.z = std::numeric_limits<float>::max();
- }
- void NetworkResponse::adressGui(Framework::Text elementId)
- {
- adressLength = (char)(4 + elementId.getLength());
- adress = new char[adressLength];
- adress[0] = 2; // gui message
- adress[1] = 1; // element message
- *(short*)(adress + 2) = (short)elementId.getLength(); // block
- memcpy(adress + 4, elementId.getText(), elementId.getLength());
- minPosition.x = std::numeric_limits<float>::min();
- minPosition.y = std::numeric_limits<float>::min();
- minPosition.z = std::numeric_limits<float>::min();
- maxPosition.x = std::numeric_limits<float>::max();
- maxPosition.y = std::numeric_limits<float>::max();
- maxPosition.z = std::numeric_limits<float>::max();
- }
- void NetworkResponse::setMessage(char* msg, int length, bool deleteMsg)
- {
- message = msg;
- msgLength = length;
- msgDelete = deleteMsg;
- }
- void NetworkResponse::setUseBackground()
- {
- useBackground = 1;
- }
- void NetworkResponse::sendToAll()
- {
- broadcast = true;
- }
- bool NetworkResponse::isAreaAffected(Framework::Vec3<float> min, Framework::Vec3<float> max, int affectedDimension) const
- {
- return minPosition.x <= max.x && maxPosition.x >= min.x &&
- minPosition.y <= max.y && maxPosition.y >= min.y &&
- minPosition.z <= max.z && maxPosition.z >= min.z && (this->affectedDimension < 0 || this->affectedDimension == affectedDimension);
- }
- void NetworkResponse::writeTo(Framework::StreamWriter* zWriter) const
- {
- int total = msgLength + adressLength;
- if (total)
- {
- zWriter->schreibe((char*)&total, 4);
- zWriter->schreibe(adress, adressLength);
- zWriter->schreibe(message, msgLength);
- }
- }
- bool NetworkResponse::isBroadcast() const
- {
- return broadcast;
- }
- bool NetworkResponse::isEmpty() const
- {
- return msgLength + adressLength <= 0;
- }
- bool NetworkResponse::isUseBackground() const
- {
- return useBackground;
- }
|