summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pacman-c++/mainwidget.cpp2
-rw-r--r--pacman-c++/sceneholder.cpp9
-rw-r--r--pacman-c++/sceneholder.h3
-rw-r--r--pacman-c++/server.cpp115
-rw-r--r--pacman-c++/server.h8
-rw-r--r--pacman-c++/util.cpp10
6 files changed, 120 insertions, 27 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp
index 42abd01..f2e1b31 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..2fcfb37 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 */
@@ -132,9 +133,11 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x
132 m_actors[color] = actor; 133 m_actors[color] = actor;
133 addItem(actor); 134 addItem(actor);
134 actor->setPos(mapPositionToCoord(x, y)); 135 actor->setPos(mapPositionToCoord(x, y));
136 qDebug() << endl << "create act " << endl;
135 } 137 }
136 else 138 else
137 { 139 {
140 qDebug() << endl << "act has pnts: " << actor->getRoundPoints() << actor->getGamePoints() << endl;
138 Actor::Movement direction = Util::transmissionMovementToActor( 141 Actor::Movement direction = Util::transmissionMovementToActor(
139 cur & Transmission::direction_mask); 142 cur & Transmission::direction_mask);
140 /* WARNING: do NOT add actor to visualMap as visualMap-items may 143 /* WARNING: do NOT add actor to visualMap as visualMap-items may
@@ -151,6 +154,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x
151 qDebug() << "[SceneUpdate] actor moves: color=" << color 154 qDebug() << "[SceneUpdate] actor moves: color=" << color
152 << "direction=" << direction << "newpos=" << QPoint(x, y); 155 << "direction=" << direction << "newpos=" << QPoint(x, y);
153 } 156 }
157 actor->setPos(mapPositionToCoord(x, y));
154 } 158 }
155 159
156 if (cur & Transmission::empty) 160 if (cur & Transmission::empty)
@@ -195,6 +199,9 @@ unsigned int SceneHolder::pointsLeft()
195void SceneHolder::decrementPoints() 199void SceneHolder::decrementPoints()
196{ 200{
197 --m_pointsLeft; 201 --m_pointsLeft;
202 if (m_pointsLeft == 0) {
203 emit allPointsRemoved();
204 }
198} 205}
199 206
200void SceneHolder::setEatingOrder(QList<Color::Color> &order) 207void 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 4051807..3cb422b 100644
--- a/pacman-c++/server.cpp
+++ b/pacman-c++/server.cpp
@@ -31,30 +31,20 @@ bool Server::run()
31 if (!waitForClientConnections()) 31 if (!waitForClientConnections())
32 return false; 32 return false;
33 33
34 qDebug() << "[Server] Creating map..."; 34 initRoundMap(true);
35 Transmission::map_t map = Util::createDemoMap();
36 Util::placeActors(map, m_maxplayers, Color::order);
37 Util::fillPoints(map);
38 35
39 /* save positions of blocks for later usage */ 36 m_tickTimer = new QTimer(this);
40 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 37 connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick()));
41 { 38 m_tickTimer->start(Constants::tick);
42 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
43 {
44 Transmission::field_t &cur = map[x][y];
45 if (cur & Transmission::block)
46 m_blocks.append(QPoint(x, y));
47 }
48 }
49 39
50 updateMap(map); 40 /* finish round every 3 sec
51 sendUpdate(map, true); 41 QTimer *timer2 = new QTimer(this);
52 Util::deleteMap(map); 42 connect(timer2, SIGNAL(timeout()), this, SLOT(onRoundFinished()));
53 map = NULL; 43 timer2->start(3000);
44 / */
45
46 connect(this, SIGNAL(allPointsRemoved()), SLOT(onRoundFinished()));
54 47
55 QTimer *timer = new QTimer(this);
56 connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
57 timer->start(Constants::tick);
58 return true; 48 return true;
59} 49}
60 50
@@ -109,7 +99,6 @@ invalid_direction:
109 99
110 // <t3h g4m3 10gic> 100 // <t3h g4m3 10gic>
111 // TODO: support actors eating each other 101 // TODO: support actors eating each other
112 // TODO: old item - REMOVE THAT?
113 GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; 102 GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()];
114 103
115 /* check if there's an item at new location of actor */ 104 /* check if there's an item at new location of actor */
@@ -501,6 +490,88 @@ void Server::keyPressUpdate()
501 } 490 }
502} 491}
503 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 qDebug() << endl;
511 qDebug() << endl;
512 qDebug() << "[Server] Creating map...";
513 qDebug() << endl;
514 qDebug() << endl;
515 qDebug() << endl;
516
517 disconnect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished()));
518 if (!firstPacket) {
519 // clear old map
520 for (unsigned int i=0; i<visualMap.size(); ++i) {
521 for (unsigned int j=0; j<visualMap[0].size(); ++j) {
522 GameEntity *e = visualMap[i][j];
523 if (e != NULL) {
524 if (qgraphicsitem_cast<Actor *>(e) == NULL) {
525 removeItem(e);
526 delete e;
527 }
528 }
529 visualMap[i][j] = 0;
530 }
531 }
532 }
533
534 // create new map
535 Transmission::map_t map = Util::createDemoMap();
536 // add content
537 Util::placeActors(map, m_maxplayers, Color::order);
538 /*
539 if (!firstPacket) {
540 for (unsigned int i=0; i<Constants::map_size.width; ++i) {
541 for (unsigned int j=0; j<Constants::map_size.height; ++j) {
542 if (map[i][j] & Transmission::pacman) {
543 Color::Color color = static_cast<Color::Color>(map[i][j] & Transmission::color_mask);
544 Actor *actor = m_actors.value(color, NULL);
545 qDebug() << "setting actor to " << i << j << mapPositionToCoord(i, j);
546 actor->setPos(mapPositionToCoord(i, j));
547 }
548 }
549 }
550 }
551 */
552
553 Util::fillPoints(map);
554
555 /* save positions of blocks for later usage */
556 m_blocks.clear();
557 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
558 {
559 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
560 {
561 Transmission::field_t &cur = map[x][y];
562 if (cur & Transmission::block)
563 m_blocks.append(QPoint(x, y));
564 }
565 }
566
567 updateMap(map);
568 sendUpdate(map, firstPacket);
569 Util::deleteMap(map);
570 map = NULL;
571
572 connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished()));
573}
574
504bool Server::parseCommandline() 575bool Server::parseCommandline()
505{ 576{
506 AnyOption opt; 577 AnyOption opt;
diff --git a/pacman-c++/server.h b/pacman-c++/server.h
index fd1b571..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;
@@ -61,6 +67,8 @@ protected:
61 67
62 unsigned int m_curRound; // current round starting with 0 68 unsigned int m_curRound; // current round starting with 0
63 69
70 QTimer *m_tickTimer;
71
64}; 72};
65 73
66#endif // SERVER_H 74#endif // SERVER_H
diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp
index 708d005..fcc1aff 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();
@@ -139,15 +139,19 @@ namespace Util
139 void fillPoints(Transmission::map_t map, Transmission::field_t type) 139 void fillPoints(Transmission::map_t map, Transmission::field_t type)
140 { 140 {
141 /* auto place normal points*/ 141 /* auto place normal points*/
142 int i =0;
142 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 143 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
143 { 144 {
144 for (unsigned int y = 0; y < Constants::map_size.height; ++y) 145 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
145 { 146 {
146 Transmission::field_t &cur = map[x][y]; 147 Transmission::field_t &cur = map[x][y];
147 if (cur == Transmission::none) 148 if (cur == Transmission::none) {
149 if (++i > 10 ) continue;
150
148 cur = type; 151 cur = type;
149 else if (cur == Transmission::point) 152 } else if (cur == Transmission::point) {
150 cur = Transmission::none; 153 cur = Transmission::none;
154 }
151 } 155 }
152 } 156 }
153 } 157 }