summaryrefslogtreecommitdiffstats
path: root/pacman-c++/server.cpp
diff options
context:
space:
mode:
authortotycro <totycro@unknown-horizons.org>2011-04-19 21:56:37 +0200
committertotycro <totycro@unknown-horizons.org>2011-04-19 21:56:37 +0200
commiteaf133fd08c9708fe718ef47451bed7ea803a46a (patch)
treeb3b9c2fca22bf4351aa854abd84e7a2f39ccbdb9 /pacman-c++/server.cpp
parentb6fec20dadaf71fc28961a4a6d720d1d665508e8 (diff)
downloadfoop-eaf133fd08c9708fe718ef47451bed7ea803a46a.tar.gz
foop-eaf133fd08c9708fe718ef47451bed7ea803a46a.tar.bz2
foop-eaf133fd08c9708fe718ef47451bed7ea803a46a.zip
Added rounds
rounds will end when all points are removed TODO: end round when a pacman gets eaten
Diffstat (limited to 'pacman-c++/server.cpp')
-rw-r--r--pacman-c++/server.cpp115
1 files changed, 93 insertions, 22 deletions
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;