summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pacman-c++/constants.h30
-rw-r--r--pacman-c++/mainwidget.cpp41
-rw-r--r--pacman-c++/mainwidget.h1
-rw-r--r--pacman-c++/pacman.proto12
-rw-r--r--pacman-c++/server.cpp18
-rw-r--r--pacman-c++/util.cpp7
6 files changed, 60 insertions, 49 deletions
diff --git a/pacman-c++/constants.h b/pacman-c++/constants.h
index 15eba86..7ce26ad 100644
--- a/pacman-c++/constants.h
+++ b/pacman-c++/constants.h
@@ -34,16 +34,16 @@ namespace Color
34{ 34{
35 enum Color 35 enum Color
36 { 36 {
37 none = 0, 37 none = 0,
38 red = (1 << 0), 38 red = (1 << 0),
39 blue = (1 << 1), 39 blue = (1 << 1),
40 green = (1 << 2), 40 green = (1 << 2),
41 max = (1 << 2), 41 yellow = (1 << 3)
42 }; 42 };
43 43
44 /* colororder used in protocol and gui */ 44 /* colororder used in protocol and gui */
45 const Color order[] = 45 const Color order[] =
46 { Color::red, Color::blue, Color::green, Color::none }; 46 { Color::red, Color::blue, Color::green, Color::yellow, Color::none };
47} 47}
48 48
49// constants for data transmission to client 49// constants for data transmission to client
@@ -53,17 +53,17 @@ namespace Transmission
53 typedef unsigned int mask_t; 53 typedef unsigned int mask_t;
54 54
55 const field_t none = 0; 55 const field_t none = 0;
56 const field_t block = (1 << 3); 56 const field_t block = (1 << 4);
57 const field_t point = (1 << 4); 57 const field_t point = (1 << 5);
58 const field_t bonuspoint = (1 << 5); 58 const field_t bonuspoint = (1 << 6);
59 const field_t pacman = (1 << 6); 59 const field_t pacman = (1 << 7);
60 const field_t empty = (1 << 7); // explicit empty for update 60 const field_t empty = (1 << 8); // explicit empty for update
61 61
62 const field_t direction_none = 0; 62 const field_t direction_none = 0;
63 const field_t direction_left = (1 << 8); 63 const field_t direction_left = (1 << 9);
64 const field_t direction_right = (1 << 9); 64 const field_t direction_right = (1 << 10);
65 const field_t direction_up = (1 << 10); 65 const field_t direction_up = (1 << 11);
66 const field_t direction_down = (1 << 11); 66 const field_t direction_down = (1 << 12);
67 67
68 const mask_t color_mask = Color::none | Color::red | Color::blue | Color::green; 68 const mask_t color_mask = Color::none | Color::red | Color::blue | Color::green;
69 const mask_t type_mask = block | bonuspoint; 69 const mask_t type_mask = block | bonuspoint;
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp
index 6930b1a..a77daab 100644
--- a/pacman-c++/mainwidget.cpp
+++ b/pacman-c++/mainwidget.cpp
@@ -6,7 +6,7 @@
6#include "pacman.pb.h" 6#include "pacman.pb.h"
7 7
8MainWidget::MainWidget(QWidget *parent) 8MainWidget::MainWidget(QWidget *parent)
9 : QWidget(parent), m_currentKey(Transmission::none), m_running(false) 9 : QWidget(parent), m_currentKey(Transmission::none), m_running(false), m_maxplayers(0)
10{ 10{
11 /* create audio player */ 11 /* create audio player */
12 m_ambientPlayer = new GaplessAudioPlayer(Sound::Ambient, 100, this); 12 m_ambientPlayer = new GaplessAudioPlayer(Sound::Ambient, 100, this);
@@ -54,51 +54,55 @@ void MainWidget::createGui()
54 setFocusPolicy(Qt::StrongFocus); 54 setFocusPolicy(Qt::StrongFocus);
55 55
56 /* first one is always the own score */ 56 /* first one is always the own score */
57 QVBoxLayout *layout = new QVBoxLayout(this);
58 layout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
57 QHBoxLayout *scoreLayout = new QHBoxLayout(); 59 QHBoxLayout *scoreLayout = new QHBoxLayout();
58 for (unsigned int i = 0; Color::order[i] != Color::none ; ++i) 60 for (unsigned int i = 0; Color::order[i] != Color::none; ++i)
59 { 61 {
60 QGroupBox *scoreBox = new QGroupBox(QString("Spieler %1").arg(i + 1), this); 62 QGridLayout *playerLayout = new QGridLayout();
63 playerLayout->addWidget(new QLabel("Current:", this), 0, 0);
64 playerLayout->addWidget(new QLabel("Total:", this), 1, 0);
65 playerLayout->addWidget(new QLabel("0", this), 0, 1);
66 playerLayout->addWidget(new QLabel("0", this), 1, 1);
67
68 QGroupBox *scoreBox = new QGroupBox(QString("Player %1").arg(i + 1), this);
61 scoreBox->setObjectName(QString("actor%1").arg(i + 1)); 69 scoreBox->setObjectName(QString("actor%1").arg(i + 1));
62 scoreBox->setCheckable(true); 70 scoreBox->setCheckable(true);
63 connect(scoreBox, SIGNAL(clicked()), this, SLOT(playerScoreClicked())); 71 connect(scoreBox, SIGNAL(clicked()), this, SLOT(playerScoreClicked()));
64
65 QGridLayout *playerLayout = new QGridLayout();
66 scoreBox->setLayout(playerLayout); 72 scoreBox->setLayout(playerLayout);
67 73 scoreBox->setDisabled(i >= m_maxplayers);
68 playerLayout->addWidget(new QLabel("Rundenpunkte:", this), 0, 0); 74 m_playerScoreLayouts.append(playerLayout);
69 playerLayout->addWidget(new QLabel("Gesamtpunkte:", this), 1, 0);
70
71 playerLayout->addWidget(new QLabel("", this), 0, 1);
72 playerLayout->addWidget(new QLabel("", this), 1, 1);
73 75
74 if (Color::order[i] == m_scene->color()) 76 if (Color::order[i] == m_scene->color())
75 scoreLayout->insertWidget(0, scoreBox); 77 scoreLayout->insertWidget(0, scoreBox);
76 else 78 else
77 scoreLayout->addWidget(scoreBox); 79 scoreLayout->addWidget(scoreBox);
78 m_playerScoreLayouts.append(playerLayout);
79 } 80 }
80
81 QVBoxLayout *layout = new QVBoxLayout(this);
82 layout->addLayout(scoreLayout); 81 layout->addLayout(scoreLayout);
82 /* add some margin to scorebox hopefully won't resize the window */
83 //setMinimumWidth(scoreLayout->minimumSize().width() + 50);
83 84
84 QGraphicsView *window = new QGraphicsView(m_scene, this); 85 QGraphicsView *window = new QGraphicsView(m_scene, this);
85 window->setFrameStyle(0); 86 window->setFrameStyle(0);
86 window->setAlignment(Qt::AlignLeft | Qt::AlignTop);
87 window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); 87 window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height);
88 window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint); 88 window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint);
89 window->setFocusPolicy(Qt::NoFocus); 89 window->setFocusPolicy(Qt::NoFocus);
90 layout->addWidget(window); 90 layout->addWidget(window, 0, Qt::AlignCenter);
91 91
92 QFile css(":/stylesheet"); 92 QFile css(":/stylesheet");
93 css.open(QFile::ReadOnly); 93 css.open(QFile::ReadOnly);
94 qApp->setStyleSheet(QLatin1String(css.readAll())); 94 qApp->setStyleSheet(QLatin1String(css.readAll()));
95 95
96 /* add dummy layout at the end which gets streched when resizing */
97 QHBoxLayout *spacer = new QHBoxLayout();
98 layout->addLayout(spacer, 10);
99
96 setLayout(layout); 100 setLayout(layout);
97} 101}
98 102
99void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet) 103void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet)
100{ 104{
101 for(unsigned i = 0; Color::order[i] != Color::none; ++i) 105 for(unsigned i = 0; i < m_maxplayers; ++i)
102 { 106 {
103 QGridLayout *score = m_playerScoreLayouts.at(i); 107 QGridLayout *score = m_playerScoreLayouts.at(i);
104 QLabel *turnPointsLbl = dynamic_cast<QLabel *>(score->itemAtPosition(0, 1)->widget()); 108 QLabel *turnPointsLbl = dynamic_cast<QLabel *>(score->itemAtPosition(0, 1)->widget());
@@ -233,10 +237,11 @@ Color::Color MainWidget::connectToServer()
233 { 237 {
234 /* receive color */ 238 /* receive color */
235 QSharedPointer<QByteArray> data = Util::receivePacket(m_socket); 239 QSharedPointer<QByteArray> data = Util::receivePacket(m_socket);
236 ProtoBuf::WhoAmI packet; 240 ProtoBuf::Init packet;
237 bool worked = packet.ParseFromArray(data->data(), data->size()); 241 bool worked = packet.ParseFromArray(data->data(), data->size());
238 Q_ASSERT(worked); 242 Q_ASSERT(worked);
239 Q_UNUSED(worked); 243 Q_UNUSED(worked);
244 m_maxplayers = packet.maxplayers();
240 return static_cast<Color::Color>(packet.color() & Transmission::color_mask); 245 return static_cast<Color::Color>(packet.color() & Transmission::color_mask);
241 } 246 }
242 } 247 }
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h
index 6ecd812..ef282c1 100644
--- a/pacman-c++/mainwidget.h
+++ b/pacman-c++/mainwidget.h
@@ -54,6 +54,7 @@ private:
54 54
55 QTcpSocket *m_socket; 55 QTcpSocket *m_socket;
56 SceneHolder *m_scene; 56 SceneHolder *m_scene;
57 unsigned int m_maxplayers;
57 58
58 /* allocate as member variable as this packet is large and used often */ 59 /* allocate as member variable as this packet is large and used often */
59 ProtoBuf::MapUpdate m_updatepacket; 60 ProtoBuf::MapUpdate m_updatepacket;
diff --git a/pacman-c++/pacman.proto b/pacman-c++/pacman.proto
index 98478a0..51bb239 100644
--- a/pacman-c++/pacman.proto
+++ b/pacman-c++/pacman.proto
@@ -4,12 +4,18 @@ message KeyPressUpdate {
4 required uint32 newKey = 1; 4 required uint32 newKey = 1;
5} 5}
6 6
7message Init {
8 required uint32 color = 1;
9 required uint32 maxplayers = 2;
10}
11
12message MapInit {
13 repeated uint32 field = 1 [packed=true];
14}
15
7message MapUpdate { 16message MapUpdate {
8 repeated uint32 field = 1 [packed=true]; 17 repeated uint32 field = 1 [packed=true];
9 repeated uint32 round_points = 2; 18 repeated uint32 round_points = 2;
10 repeated uint32 game_points = 3; 19 repeated uint32 game_points = 3;
11} 20}
12 21
13message WhoAmI {
14 required uint32 color = 1;
15}
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp
index 50ee3b7..e09d126 100644
--- a/pacman-c++/server.cpp
+++ b/pacman-c++/server.cpp
@@ -17,6 +17,9 @@ Server::Server(QWidget *parent)
17 17
18bool Server::run() 18bool Server::run()
19{ 19{
20 qDebug() << "[Server] Running server...";
21 qDebug() << "[Server] Max players:" << m_maxplayers;
22 qDebug() << "[Server] Number of bots:" << m_numbots;
20 if (!waitForClientConnections()) 23 if (!waitForClientConnections())
21 return false; 24 return false;
22 25
@@ -164,20 +167,14 @@ bool Server::waitForClientConnections()
164 qCritical() << "Error while creating socket:" << qPrintable(tcpSrv->errorString()); 167 qCritical() << "Error while creating socket:" << qPrintable(tcpSrv->errorString());
165 return false; 168 return false;
166 } 169 }
167
168 qDebug() << "[Server] Listening on:" 170 qDebug() << "[Server] Listening on:"
169 << qPrintable(QString("%1:%2").arg(tcpSrv->serverAddress().toString()) 171 << qPrintable(QString("%1:%2").arg(tcpSrv->serverAddress().toString())
170 .arg(tcpSrv->serverPort())); 172 .arg(tcpSrv->serverPort()));
173
171 qDebug() << "[Server] Waiting for clients"; 174 qDebug() << "[Server] Waiting for clients";
172 ProtoBuf::WhoAmI packet; 175 ProtoBuf::Init packet;
173#define SINGLE 176 for (unsigned int i = 0; i < m_maxplayers; ++i)
174#ifdef SINGLE
175 for (unsigned int i = 0; i < 1; ++i)
176 {
177#else
178 for (unsigned int i = 0; Color::order[i] != Color::none; ++i)
179 { 177 {
180#endif
181 bool connectionAvailable = tcpSrv->waitForNewConnection(-1); 178 bool connectionAvailable = tcpSrv->waitForNewConnection(-1);
182 Q_ASSERT(connectionAvailable); 179 Q_ASSERT(connectionAvailable);
183 Q_UNUSED(connectionAvailable); 180 Q_UNUSED(connectionAvailable);
@@ -189,6 +186,7 @@ bool Server::waitForClientConnections()
189 m_clientConnections[color] = socket; 186 m_clientConnections[color] = socket;
190 /* notify player of color */ 187 /* notify player of color */
191 packet.set_color(color); 188 packet.set_color(color);
189 packet.set_maxplayers(m_maxplayers);
192 Util::sendPacket(packet, socket); 190 Util::sendPacket(packet, socket);
193 191
194 qDebug() << "[Connect] New Player: color=" << color; 192 qDebug() << "[Connect] New Player: color=" << color;
@@ -208,7 +206,7 @@ void Server::sendUpdate(Transmission::map_t map)
208 m_updatepacket.add_field(map[x][y]); 206 m_updatepacket.add_field(map[x][y]);
209 } 207 }
210 208
211 for(unsigned i = 0; Color::order[i] != Color::none; ++i) 209 for(unsigned i = 0; i < m_maxplayers; ++i)
212 { 210 {
213 m_updatepacket.add_round_points(m_actors.value(Color::order[i])->getRoundPoints()); 211 m_updatepacket.add_round_points(m_actors.value(Color::order[i])->getRoundPoints());
214 m_updatepacket.add_game_points(m_actors.value(Color::order[i])->getGamePoints()); 212 m_updatepacket.add_game_points(m_actors.value(Color::order[i])->getGamePoints());
diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp
index 2fb9f69..eaf18e4 100644
--- a/pacman-c++/util.cpp
+++ b/pacman-c++/util.cpp
@@ -75,9 +75,10 @@ namespace Util
75 } 75 }
76 76
77 map[0][0] |= Transmission::bonuspoint; 77 map[0][0] |= Transmission::bonuspoint;
78 map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; 78 map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right;
79 map[23][0] = Color::blue | Transmission::pacman | Transmission::direction_none; 79 //map[23][0] = Color::blue | Transmission::pacman | Transmission::direction_none;
80 map[24][0] = Color::green | Transmission::pacman | Transmission::direction_none; 80 //map[24][0] = Color::green | Transmission::pacman | Transmission::direction_none;
81 //map[25][0] = Color::yellow | Transmission::pacman | Transmission::direction_none;
81 82
82 /* auto place normal points*/ 83 /* auto place normal points*/
83 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 84 for (unsigned int x = 0; x < Constants::map_size.width; ++x)