From ce48af53646cd9e7ec762fc1ac176b3aa620b11d Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 5 May 2011 00:57:07 +0200 Subject: - refactorized the whole project and made a few subprojects - replaced tcp with enet - added connect dialog - some smaller bugfixes --- pacman-c++/util.cpp | 357 ---------------------------------------------------- 1 file changed, 357 deletions(-) delete mode 100644 pacman-c++/util.cpp (limited to 'pacman-c++/util.cpp') diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp deleted file mode 100644 index 09b2be1..0000000 --- a/pacman-c++/util.cpp +++ /dev/null @@ -1,357 +0,0 @@ -#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; - } - - 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/actors will be placed (see makePoints) - */ - else if (tmpl[y][x] == '.') - cur |= Transmission::point; - } - } - return map; - } - - void placeActors(Transmission::map_t map, unsigned int players, const Color::Color *colors) - { -#if 0 - for(unsigned int i = 0; i < players; ++i) - map[i][0] = colors[i] | Transmission::pacman; - return; -#endif - - int mindistance = Constants::Game::player_minimum_distance; - /* this outer loop is used if there are no more valid places left - * so we can change mindistance - */ - QList actors; - for(unsigned int i = 0; i < players; ++i) - { - /* first remove formerly placed actors from map */ - foreach(QPoint pos, actors) - map[pos.x()][pos.y()] = Transmission::none; - actors.clear(); - - /* get list of valid positions */ - QList validpos; - 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) - validpos.append(QPoint(x, y)); - } - } - - /* place actors at map */ - for(i = 0; i < players; ++i) - { - int rand = (int) (validpos.size() * (qrand() / (RAND_MAX + 1.0))); - QPoint newpos = validpos.at(rand); - map[newpos.x()][newpos.y()] = colors[i] | Transmission::pacman; - qDebug() << "[Place] Actor" << i << "at" << newpos; - actors.append(newpos); - validpos.removeAt(rand); - - QMutableListIterator j(validpos); - while(j.hasNext()) - { - j.next(); - QPoint tmp = j.value() - newpos; - if (tmp.manhattanLength() < mindistance) - j.remove(); - } - - if (validpos.empty()) - { - qWarning() << "There are no more valid positions for actors left on the map"; - mindistance -= Constants::Game::player_distance_decr; - break; - } - } - } - } - - void fillPoints(Transmission::map_t map, Transmission::field_t type) - { - /* 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) - { -#if 0 - /* use for endround testing */ - if (x > 0) - continue; -#endif - cur = type; - } - else if (cur == Transmission::point) - cur = Transmission::none; - } - } - } - - 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; - } - - const QString colorToString(Color::Color color) - { - switch(color) - { - case Color::none: - return "none"; - break; - case Color::red: - return "red"; - break; - case Color::blue: - return "blue"; - break; - case Color::green: - return "green"; - break; - case Color::yellow: - return "yellow"; - break; - default: - return "unknown"; - break; - } - } - - 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()) - { - qDebug() << "[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; - } - - int floorLog2(unsigned int n) - { - if (n == 0) - return -1; - - int pos = 0; - if (n >= 1<<16) { n >>= 16; pos += 16; } - if (n >= 1<< 8) { n >>= 8; pos += 8; } - if (n >= 1<< 4) { n >>= 4; pos += 4; } - if (n >= 1<< 2) { n >>= 2; pos += 2; } - if (n >= 1<< 1) { pos += 1; } - return pos; - } - -#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 -} -- cgit v1.2.3