summaryrefslogtreecommitdiffstats
path: root/pacman-c++
diff options
context:
space:
mode:
Diffstat (limited to 'pacman-c++')
-rw-r--r--pacman-c++/mainwidget.cpp2
-rw-r--r--pacman-c++/sceneholder.cpp11
-rw-r--r--pacman-c++/sceneholder.h3
-rw-r--r--pacman-c++/server.cpp127
-rw-r--r--pacman-c++/server.h12
-rw-r--r--pacman-c++/util.cpp7
6 files changed, 134 insertions, 28 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp
index daba1ca..f81961f 100644
--- a/pacman-c++/mainwidget.cpp
+++ b/pacman-c++/mainwidget.cpp
@@ -110,7 +110,7 @@ void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet)
110 turnPointsLbl->setText(QString::number(packet.round_points(i))); 110 turnPointsLbl->setText(QString::number(packet.round_points(i)));
111 111
112 QLabel *allPointsLbl = dynamic_cast<QLabel *>(score->itemAtPosition(1, 1)->widget()); 112 QLabel *allPointsLbl = dynamic_cast<QLabel *>(score->itemAtPosition(1, 1)->widget());
113 allPointsLbl->setText(QString::number(packet.round_points(i))); 113 allPointsLbl->setText(QString::number(packet.game_points(i)));
114 } 114 }
115} 115}
116 116
diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp
index dace711..37db691 100644
--- a/pacman-c++/sceneholder.cpp
+++ b/pacman-c++/sceneholder.cpp
@@ -73,8 +73,9 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x
73 } 73 }
74 74
75 /* an item must be removed by an actor */ 75 /* an item must be removed by an actor */
76 if (actor == NULL) 76 if (actor == NULL) {
77 Q_ASSERT(false); 77 Q_ASSERT(false);
78 }
78 oldItem->onDie(actor); 79 oldItem->onDie(actor);
79 80
80 /* register item for removal in next update */ 81 /* register item for removal in next update */
@@ -151,6 +152,11 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x
151 qDebug() << "[SceneUpdate] actor moves: color=" << color 152 qDebug() << "[SceneUpdate] actor moves: color=" << color
152 << "direction=" << direction << "newpos=" << QPoint(x, y); 153 << "direction=" << direction << "newpos=" << QPoint(x, y);
153 } 154 }
155
156 QPoint distance = QPoint(x, y) - CoordToMapPosition(actor->pos().x(), actor->pos().y());
157 if (distance.manhattanLength() > 1) {
158 actor->setPos(mapPositionToCoord(x, y));
159 }
154 } 160 }
155 161
156 if (cur & Transmission::empty) 162 if (cur & Transmission::empty)
@@ -195,6 +201,9 @@ unsigned int SceneHolder::pointsLeft()
195void SceneHolder::decrementPoints() 201void SceneHolder::decrementPoints()
196{ 202{
197 --m_pointsLeft; 203 --m_pointsLeft;
204 if (m_pointsLeft == 0) {
205 emit allPointsRemoved();
206 }
198} 207}
199 208
200void SceneHolder::setEatingOrder(QList<Color::Color> &order) 209void SceneHolder::setEatingOrder(QList<Color::Color> &order)
diff --git a/pacman-c++/sceneholder.h b/pacman-c++/sceneholder.h
index 69ee598..26ba942 100644
--- a/pacman-c++/sceneholder.h
+++ b/pacman-c++/sceneholder.h
@@ -24,6 +24,9 @@ public:
24 void setEatingOrder(QList<Color::Color> &order); 24 void setEatingOrder(QList<Color::Color> &order);
25 QList<Color::Color> &eatingOrder(); 25 QList<Color::Color> &eatingOrder();
26 26
27signals:
28 void allPointsRemoved();
29
27private slots: 30private slots:
28 void decrementPoints(); 31 void decrementPoints();
29 32
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp
index 07e4c72..1c3caf9 100644
--- a/pacman-c++/server.cpp
+++ b/pacman-c++/server.cpp
@@ -11,7 +11,7 @@
11 11
12Server::Server(QWidget *parent) 12Server::Server(QWidget *parent)
13 : SceneHolder(parent), m_bindaddress(QHostAddress::Any), 13 : SceneHolder(parent), m_bindaddress(QHostAddress::Any),
14 m_port(Constants::Networking::port), m_numbots(0) 14 m_port(Constants::Networking::port), m_rounds(3), m_numbots(0), m_curRound(0)
15{ 15{
16 /* determine max players by using order array */ 16 /* determine max players by using order array */
17 for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); 17 for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers);
@@ -26,34 +26,25 @@ bool Server::run()
26 26
27 qDebug() << "[Server] Running server..."; 27 qDebug() << "[Server] Running server...";
28 qDebug() << "[Server] Max players:" << m_maxplayers; 28 qDebug() << "[Server] Max players:" << m_maxplayers;
29 qDebug() << "[Server] Number of rounds:" << m_rounds;
29 qDebug() << "[Server] Number of bots:" << m_numbots; 30 qDebug() << "[Server] Number of bots:" << m_numbots;
30 if (!waitForClientConnections()) 31 if (!waitForClientConnections())
31 return false; 32 return false;
32 33
33 qDebug() << "[Server] Creating map..."; 34 initRoundMap(true);
34 Transmission::map_t map = Util::createDemoMap();
35 Util::placeActors(map, m_maxplayers, Color::order);
36 Util::fillPoints(map);
37 35
38 /* save positions of blocks for later usage */ 36 m_tickTimer = new QTimer(this);
39 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 37 connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick()));
40 { 38 m_tickTimer->start(Constants::tick);
41 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
42 {
43 Transmission::field_t &cur = map[x][y];
44 if (cur & Transmission::block)
45 m_blocks.append(QPoint(x, y));
46 }
47 }
48 39
49 updateMap(map); 40 /* finish round every 3 sec
50 sendUpdate(map, true); 41 QTimer *timer2 = new QTimer(this);
51 Util::deleteMap(map); 42 connect(timer2, SIGNAL(timeout()), this, SLOT(onRoundFinished()));
52 map = NULL; 43 timer2->start(3000);
44 // */
45
46 connect(this, SIGNAL(allPointsRemoved()), SLOT(onRoundFinished()));
53 47
54 QTimer *timer = new QTimer(this);
55 connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
56 timer->start(Constants::tick);
57 return true; 48 return true;
58} 49}
59 50
@@ -108,7 +99,6 @@ invalid_direction:
108 99
109 // <t3h g4m3 10gic> 100 // <t3h g4m3 10gic>
110 // TODO: support actors eating each other 101 // TODO: support actors eating each other
111 // TODO: old item - REMOVE THAT?
112 GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; 102 GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()];
113 103
114 /* check if there's an item at new location of actor */ 104 /* check if there's an item at new location of actor */
@@ -500,6 +490,81 @@ void Server::keyPressUpdate()
500 } 490 }
501} 491}
502 492
493void Server::onRoundFinished()
494{
495 foreach(Actor *actor, m_actors) {
496 actor->finishRound();
497 }
498
499 initRoundMap();
500 ++m_curRound;
501
502 if(m_curRound >= m_rounds) {
503 // end of game
504 m_tickTimer->stop();
505 }
506}
507
508void Server::initRoundMap(bool firstPacket)
509{
510 disconnect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished()));
511 if (!firstPacket) {
512 // clear old map
513 for (unsigned int i=0; i<visualMap.size(); ++i) {
514 for (unsigned int j=0; j<visualMap[0].size(); ++j) {
515 GameEntity *e = visualMap[i][j];
516 if (e != NULL) {
517 if (qgraphicsitem_cast<Actor *>(e) == NULL) {
518 removeItem(e);
519 delete e;
520 }
521 }
522 visualMap[i][j] = 0;
523 }
524 }
525 }
526
527 // create new map
528 Transmission::map_t map = Util::createDemoMap();
529 // add content
530 Util::placeActors(map, m_maxplayers, Color::order);
531 /*
532 if (!firstPacket) {
533 for (unsigned int i=0; i<Constants::map_size.width; ++i) {
534 for (unsigned int j=0; j<Constants::map_size.height; ++j) {
535 if (map[i][j] & Transmission::pacman) {
536 Color::Color color = static_cast<Color::Color>(map[i][j] & Transmission::color_mask);
537 Actor *actor = m_actors.value(color, NULL);
538 qDebug() << "setting actor to " << i << j << mapPositionToCoord(i, j);
539 actor->setPos(mapPositionToCoord(i, j));
540 }
541 }
542 }
543 }
544 */
545
546 Util::fillPoints(map);
547
548 /* save positions of blocks for later usage */
549 m_blocks.clear();
550 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
551 {
552 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
553 {
554 Transmission::field_t &cur = map[x][y];
555 if (cur & Transmission::block)
556 m_blocks.append(QPoint(x, y));
557 }
558 }
559
560 updateMap(map);
561 sendUpdate(map, firstPacket);
562 Util::deleteMap(map);
563 map = NULL;
564
565 connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished()));
566}
567
503bool Server::parseCommandline() 568bool Server::parseCommandline()
504{ 569{
505 AnyOption opt; 570 AnyOption opt;
@@ -535,6 +600,10 @@ bool Server::parseCommandline()
535 out << " --nocolorblocks" << endl 600 out << " --nocolorblocks" << endl
536 << " Disable random colorized blocks" << endl 601 << " Disable random colorized blocks" << endl
537 << endl; 602 << endl;
603 opt.setOption("rounds", 'r');
604 out << " -r, --rounds [1..n]" << endl
605 << " Number of rounds to play" << endl
606 << endl;
538 opt.setFlag("nocolorblocks"); 607 opt.setFlag("nocolorblocks");
539 out << " -h, --help" << endl 608 out << " -h, --help" << endl
540 << " Prints this help message" << endl; 609 << " Prints this help message" << endl;
@@ -603,6 +672,18 @@ bool Server::parseCommandline()
603 m_numbots = numbots; 672 m_numbots = numbots;
604 } 673 }
605 674
675 if (opt.getValue("rounds") != NULL)
676 {
677 bool ok;
678 unsigned int rounds = QString(opt.getValue("rounds")).toUInt(&ok);
679 if (!ok || rounds == 0)
680 {
681 qCritical() << "Invalid number of rounds: " << opt.getValue("rounds") << endl;
682 return false;
683 }
684 m_rounds = rounds;
685 }
686
606 this->setProperty("coloredblocks", !opt.getFlag("nocolorblocks")); 687 this->setProperty("coloredblocks", !opt.getFlag("nocolorblocks"));
607 688
608 return true; 689 return true;
diff --git a/pacman-c++/server.h b/pacman-c++/server.h
index 826c701..dc8de88 100644
--- a/pacman-c++/server.h
+++ b/pacman-c++/server.h
@@ -38,6 +38,12 @@ protected:
38 void colorizeBlocks(Transmission::map_t map); 38 void colorizeBlocks(Transmission::map_t map);
39 void botCalculate(Actor *actor); 39 void botCalculate(Actor *actor);
40 40
41protected slots:
42 // TODO: call this when a pacman get's eaten
43 void onRoundFinished(); // called when a round is finished
44protected:
45 void initRoundMap(bool firstPacket = false); // creates new round map
46
41protected: 47protected:
42 QMap<Color::Color, QTcpSocket *> m_clientConnections; 48 QMap<Color::Color, QTcpSocket *> m_clientConnections;
43 QList<Color::Color> m_bots; 49 QList<Color::Color> m_bots;
@@ -56,7 +62,13 @@ protected:
56 QHostAddress m_bindaddress; 62 QHostAddress m_bindaddress;
57 unsigned int m_port; 63 unsigned int m_port;
58 unsigned int m_maxplayers; 64 unsigned int m_maxplayers;
65 unsigned int m_rounds; // number of rounds (>= 1)
59 unsigned int m_numbots; 66 unsigned int m_numbots;
67
68 unsigned int m_curRound; // current round starting with 0
69
70 QTimer *m_tickTimer;
71
60}; 72};
61 73
62#endif // SERVER_H 74#endif // SERVER_H
diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp
index 708d005..070c716 100644
--- a/pacman-c++/util.cpp
+++ b/pacman-c++/util.cpp
@@ -90,7 +90,7 @@ namespace Util
90 QList<QPoint> actors; 90 QList<QPoint> actors;
91 for(unsigned int i = 0; i < players; ++i) 91 for(unsigned int i = 0; i < players; ++i)
92 { 92 {
93 /* first remove formally placed actors from map */ 93 /* first remove formerly placed actors from map */
94 foreach(QPoint pos, actors) 94 foreach(QPoint pos, actors)
95 map[pos.x()][pos.y()] = Transmission::none; 95 map[pos.x()][pos.y()] = Transmission::none;
96 actors.clear(); 96 actors.clear();
@@ -144,10 +144,11 @@ namespace Util
144 for (unsigned int y = 0; y < Constants::map_size.height; ++y) 144 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
145 { 145 {
146 Transmission::field_t &cur = map[x][y]; 146 Transmission::field_t &cur = map[x][y];
147 if (cur == Transmission::none) 147 if (cur == Transmission::none) {
148 cur = type; 148 cur = type;
149 else if (cur == Transmission::point) 149 } else if (cur == Transmission::point) {
150 cur = Transmission::none; 150 cur = Transmission::none;
151 }
151 } 152 }
152 } 153 }
153 } 154 }