#include "util.h" #include namespace Util { Transmission::map_t createUninitialisedMap() { Transmission::map_t map; map = new Transmission::field_t*[Constants::map_size.width]; for (unsigned int i = 0; i < Constants::map_size.width; ++i) map[i] = new Transmission::field_t[Constants::map_size.height]; return map; } Transmission::map_t createEmptyMap() { Transmission::map_t map = createUninitialisedMap(); for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { Transmission::field_t &cur = map[x][y]; cur = Transmission::none; } } return map; } void deleteMap(Transmission::map_t map) { for (unsigned int x = 0; x < Constants::map_size.width; ++x) delete[] map[x]; delete[] map; } // temporary Transmission::map_t createDemoMap() { Transmission::map_t map = createEmptyMap(); const char *tmpl[] = { " # # ", " #### ###### # #### # # ###### ### ", " # # ", " # ##### # # # # # ### # # # ", " # # # # # # # # # # ## # # ", " # # # # # # # # ### # # # # ", " # # # # # # # # # # # # ## # ", " # # ### ##### # ### # # # ", " ### # ", " # # ### #### #### #### ##### ", " #### # #..# #..# #..# # # ", " # # ### #..# #..# #### # # # # ", " # # # #..# #..# # # ", " # #### # #### #### # # ##### # ", " # # ", " #### ###### # ##### # ####### ### ", " # # " }; for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { Transmission::field_t &cur = map[x][y]; cur = Transmission::none; if (tmpl[y][x] == '#') cur |= Color::none | Transmission::block; /* this is a simple hack to create areas where no * autoplaced points will be placed (see below) */ else if (tmpl[y][x] == '.') cur |= Transmission::point; } } map[0][0] |= Transmission::bonuspoint; map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; //map[23][0] = Color::blue | Transmission::pacman | Transmission::direction_none; //map[24][0] = Color::green | Transmission::pacman | Transmission::direction_none; //map[25][0] = Color::yellow | Transmission::pacman | Transmission::direction_none; /* auto place normal points*/ for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { Transmission::field_t &cur = map[x][y]; if (cur == Transmission::none) cur |= Transmission::point; else if (cur == Transmission::point) cur = Transmission::none; } } return map; } Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def) { switch (mov) { case Actor::None: return Transmission::direction_none; break; case Actor::Left: return Transmission::direction_left; break; case Actor::Right: return Transmission::direction_right; break; case Actor::Up: return Transmission::direction_up; break; case Actor::Down: return Transmission::direction_down; break; default: return def; break; } return def; } Actor::Movement transmissionMovementToActor(Transmission::field_t field, Actor::Movement def) { switch (field) { case Transmission::direction_none: return Actor::None; break; case Transmission::direction_left: return Actor::Left; break; case Transmission::direction_right: return Actor::Right; break; case Transmission::direction_up: return Actor::Up; break; case Transmission::direction_down: return Actor::Down; break; default: return def; break; } return def; } QSharedPointer createPacket(const ::google::protobuf::MessageLite& packet) { qint64 packetlen = packet.ByteSize(); /* datalen = packet with length prepended */ qint64 datalen = sizeof(qint64) + packetlen; QSharedPointer data = QSharedPointer(new QByteArray); data->resize(datalen); /* use QDataStream for length to avoid endianess shit */ QDataStream out(data.data(), QIODevice::WriteOnly); out << packetlen; /* use protobuf.SerializeWithCachedSizesToArray() to avoid calling protobuf.ByteSize() again */ ::google::protobuf::uint8 *dataptr = reinterpret_cast(data->data()); packet.SerializeWithCachedSizesToArray(dataptr + sizeof(qint64)); return data; } bool sendPacket(QByteArray *data, QTcpSocket *socket) { int bytesWritten = socket->write(*data); if (bytesWritten != data->size()) { qWarning() << "[sendPacket] Not all data has been sent:" << "written=" << bytesWritten << ", length=" << data->size(); return false; } socket->flush(); return true; } bool sendPacket(const ::google::protobuf::MessageLite& packet, QTcpSocket *socket) { return sendPacket(createPacket(packet).data(), socket); } QSharedPointer receivePacket(QTcpSocket *socket) { QDataStream in(socket); qint64 datalen; in >> datalen; QSharedPointer data = QSharedPointer(new QByteArray); data->resize(datalen); socket->read(data->data(), data->size()); return data; } #if 0 void hexdump(void *pAddressIn, long lSize) { char szBuf[100]; long lIndent = 1; long lOutLen, lIndex, lIndex2, lOutLen2; long lRelPos; struct { char *pData; unsigned long lSize; } buf; unsigned char *pTmp,ucTmp; unsigned char *pAddress = (unsigned char *)pAddressIn; buf.pData = (char *)pAddress; buf.lSize = lSize; while (buf.lSize > 0) { pTmp = (unsigned char *)buf.pData; lOutLen = (int)buf.lSize; if (lOutLen > 16) lOutLen = 16; // create a 64-character formatted output line: sprintf(szBuf, " > " " " " %08lX", pTmp-pAddress); lOutLen2 = lOutLen; for(lIndex = 1+lIndent, lIndex2 = 53-15+lIndent, lRelPos = 0; lOutLen2; lOutLen2--, lIndex += 2, lIndex2++ ) { ucTmp = *pTmp++; sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp); if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char szBuf[lIndex2] = ucTmp; if (!(++lRelPos & 3)) // extra blank after 4 bytes { lIndex++; szBuf[lIndex+2] = ' '; } } if (!(lRelPos & 3)) lIndex--; szBuf[lIndex ] = '<'; szBuf[lIndex+1] = ' '; printf("%s\n", szBuf); buf.pData += lOutLen; buf.lSize -= lOutLen; } } #endif }