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++/common/util.cpp | 342 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 pacman-c++/common/util.cpp (limited to 'pacman-c++/common/util.cpp') diff --git a/pacman-c++/common/util.cpp b/pacman-c++/common/util.cpp new file mode 100644 index 0000000..a3426b6 --- /dev/null +++ b/pacman-c++/common/util.cpp @@ -0,0 +1,342 @@ +#include "util.h" + +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(); + QSharedPointer data = QSharedPointer(new QByteArray); + data->resize(packetlen); + + /* use protobuf.SerializeWithCachedSizesToArray() to avoid calling protobuf.ByteSize() again */ + ::google::protobuf::uint8 *dataptr = reinterpret_cast(data->data()); + packet.SerializeWithCachedSizesToArray(dataptr); + return data; + } + + bool sendPacket(QByteArray *data, ENetPeer *peer, ENetHost *host) + { + ENetPacket *packet = enet_packet_create(data->data(), data->length(), ENET_PACKET_FLAG_RELIABLE); + if (enet_peer_send(peer, 0, packet) < 0) + { + qDebug() << "[sendPacket] Error while sending packet"; + return false; + } + enet_host_flush(host); + return true; + } + + bool sendPacket(const ::google::protobuf::MessageLite& packet, ENetPeer *peer, ENetHost *host) + { + return sendPacket(createPacket(packet).data(), peer, host); + } + + QSharedPointer receivePacket(ENetPacket *packet) + { + const char *pdata = reinterpret_cast(packet->data); + QSharedPointer data = QSharedPointer(new QByteArray(pdata, packet->dataLength)); + 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