summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2011-04-14 20:37:53 +0200
committermanuel <manuel@mausz.at>2011-04-14 20:37:53 +0200
commit001f1d11e977d5586fe8496821b3a1e4bf76f10c (patch)
treed0e344db1bd6791500f8973aad3aea26c350cedd
parent197708500f3adaaa50bc1a5a94aec0db8ea621e5 (diff)
downloadfoop-001f1d11e977d5586fe8496821b3a1e4bf76f10c.tar.gz
foop-001f1d11e977d5586fe8496821b3a1e4bf76f10c.tar.bz2
foop-001f1d11e977d5586fe8496821b3a1e4bf76f10c.zip
place actors at random positions
-rw-r--r--pacman-c++/mainwidget.cpp46
-rw-r--r--pacman-c++/server.cpp4
-rw-r--r--pacman-c++/util.cpp60
-rw-r--r--pacman-c++/util.h2
4 files changed, 81 insertions, 31 deletions
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp
index a77daab..1fc7687 100644
--- a/pacman-c++/mainwidget.cpp
+++ b/pacman-c++/mainwidget.cpp
@@ -25,10 +25,12 @@ MainWidget::MainWidget(QWidget *parent)
25 25
26 /* call updateMap after m_color ist set! */ 26 /* call updateMap after m_color ist set! */
27 createGui(); 27 createGui();
28 Transmission::map_t map = Util::createDemoMap(); 28
29 m_scene->updateMap(map); 29 /* wait for the server to send the first map update (initial map)
30 Util::deleteMap(map); 30 * WARNING: this will block the gui
31 map = NULL; 31 */
32 m_socket->waitForReadyRead();
33 tick();
32 34
33 connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); 35 connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick()));
34 36
@@ -84,7 +86,7 @@ void MainWidget::createGui()
84 86
85 QGraphicsView *window = new QGraphicsView(m_scene, this); 87 QGraphicsView *window = new QGraphicsView(m_scene, this);
86 window->setFrameStyle(0); 88 window->setFrameStyle(0);
87 window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height); 89 window->setFixedSize(m_scene->sceneRect().size().toSize());
88 window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint); 90 window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint);
89 window->setFocusPolicy(Qt::NoFocus); 91 window->setFocusPolicy(Qt::NoFocus);
90 layout->addWidget(window, 0, Qt::AlignCenter); 92 layout->addWidget(window, 0, Qt::AlignCenter);
@@ -140,27 +142,27 @@ Transmission::field_t MainWidget::translateKey(int key)
140 142
141void MainWidget::tick() 143void MainWidget::tick()
142{ 144{
143 QSharedPointer<QByteArray> data = Util::receivePacket(m_socket); 145 while (m_socket->bytesAvailable() > (qint64)sizeof(qint64))
144 bool worked = m_updatepacket.ParseFromArray(data->data(), data->size());
145 Q_ASSERT(worked);
146 Q_UNUSED(worked);
147 Transmission::map_t map = Util::createUninitialisedMap();
148 Q_ASSERT(m_updatepacket.field_size() == (int) (Constants::map_size.width * Constants::map_size.height));
149 int i = 0;
150 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
151 { 146 {
152 for (unsigned int y = 0; y < Constants::map_size.height; ++y) 147 QSharedPointer<QByteArray> data = Util::receivePacket(m_socket);
148 bool worked = m_updatepacket.ParseFromArray(data->data(), data->size());
149 Q_ASSERT(worked);
150 Q_UNUSED(worked);
151 Transmission::map_t map = Util::createUninitialisedMap();
152 Q_ASSERT(m_updatepacket.field_size() == (int) (Constants::map_size.width * Constants::map_size.height));
153 int i = 0;
154 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
153 { 155 {
154 map[x][y] = m_updatepacket.field(i); 156 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
155 ++i; 157 {
158 map[x][y] = m_updatepacket.field(i);
159 ++i;
160 }
156 } 161 }
162 m_scene->updateMap(map);
163 Util::deleteMap(map);
164 updateScore(m_updatepacket);
157 } 165 }
158 m_scene->updateMap(map);
159 Util::deleteMap(map);
160 updateScore(m_updatepacket);
161
162 if (m_socket->bytesAvailable() > (qint64)sizeof(qint64))
163 tick();
164} 166}
165 167
166void MainWidget::keyPressEvent(QKeyEvent* event) 168void MainWidget::keyPressEvent(QKeyEvent* event)
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp
index e09d126..a045cad 100644
--- a/pacman-c++/server.cpp
+++ b/pacman-c++/server.cpp
@@ -23,8 +23,12 @@ bool Server::run()
23 if (!waitForClientConnections()) 23 if (!waitForClientConnections())
24 return false; 24 return false;
25 25
26 qDebug() << "[Server] Creating map...";
26 Transmission::map_t map = Util::createDemoMap(); 27 Transmission::map_t map = Util::createDemoMap();
28 Util::placeActors(map, m_maxplayers, Color::order);
29 Util::makePoints(map);
27 updateMap(map); 30 updateMap(map);
31 sendUpdate(map);
28 Util::deleteMap(map); 32 Util::deleteMap(map);
29 map = NULL; 33 map = NULL;
30 34
diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp
index eaf18e4..31d2e79 100644
--- a/pacman-c++/util.cpp
+++ b/pacman-c++/util.cpp
@@ -33,7 +33,6 @@ namespace Util
33 delete[] map; 33 delete[] map;
34 } 34 }
35 35
36 // temporary
37 Transmission::map_t createDemoMap() 36 Transmission::map_t createDemoMap()
38 { 37 {
39 Transmission::map_t map = createEmptyMap(); 38 Transmission::map_t map = createEmptyMap();
@@ -67,19 +66,64 @@ namespace Util
67 if (tmpl[y][x] == '#') 66 if (tmpl[y][x] == '#')
68 cur |= Color::none | Transmission::block; 67 cur |= Color::none | Transmission::block;
69 /* this is a simple hack to create areas where no 68 /* this is a simple hack to create areas where no
70 * autoplaced points will be placed (see below) 69 * autoplaced points/actors will be placed (see makePoints)
71 */ 70 */
72 else if (tmpl[y][x] == '.') 71 else if (tmpl[y][x] == '.')
73 cur |= Transmission::point; 72 cur |= Transmission::point;
74 } 73 }
75 } 74 }
75 return map;
76 }
77
78 void placeActors(Transmission::map_t map, unsigned int players, const Color::Color *colors)
79 {
80 int mindistance = 5;
81 /* this outer loop is used if there are no more valid places left
82 * so we can change mindistance
83 */
84 for(unsigned int i = 0; i < players; ++i)
85 {
86 /* get list of valid positions */
87 QList<QPoint> validpos;
88 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
89 {
90 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
91 {
92 Transmission::field_t &cur = map[x][y];
93 if (cur == Transmission::none)
94 validpos.append(QPoint(x, y));
95 }
96 }
97
98 /* place actors at map */
99 for(i = 0; i < players; ++i)
100 {
101 int rand = (int) (validpos.size() * (qrand() / (RAND_MAX + 1.0)));
102 QPoint newpos = validpos.at(rand);
103 map[newpos.x()][newpos.y()] = colors[i] | Transmission::pacman | Transmission::direction_none;
104 validpos.removeAt(rand);
76 105
77 map[0][0] |= Transmission::bonuspoint; 106 QMutableListIterator<QPoint> j(validpos);
78 map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; 107 while(j.hasNext())
79 //map[23][0] = Color::blue | Transmission::pacman | Transmission::direction_none; 108 {
80 //map[24][0] = Color::green | Transmission::pacman | Transmission::direction_none; 109 j.next();
81 //map[25][0] = Color::yellow | Transmission::pacman | Transmission::direction_none; 110 QPoint tmp = j.value() - newpos;
111 if (tmp.manhattanLength() < mindistance)
112 j.remove();
113 }
82 114
115 if (validpos.empty())
116 {
117 qWarning() << "There are no more valid positions for actors left on the map";
118 mindistance -= 2;
119 break;
120 }
121 }
122 }
123 }
124
125 void makePoints(Transmission::map_t map)
126 {
83 /* auto place normal points*/ 127 /* auto place normal points*/
84 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 128 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
85 { 129 {
@@ -92,8 +136,6 @@ namespace Util
92 cur = Transmission::none; 136 cur = Transmission::none;
93 } 137 }
94 } 138 }
95
96 return map;
97 } 139 }
98 140
99 Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def) 141 Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def)
diff --git a/pacman-c++/util.h b/pacman-c++/util.h
index 87dbd86..5ea2953 100644
--- a/pacman-c++/util.h
+++ b/pacman-c++/util.h
@@ -11,6 +11,8 @@ namespace Util
11{ 11{
12 Transmission::map_t createUninitialisedMap(); 12 Transmission::map_t createUninitialisedMap();
13 Transmission::map_t createDemoMap(); 13 Transmission::map_t createDemoMap();
14 void placeActors(Transmission::map_t map, unsigned int players, const Color::Color *colors);
15 void makePoints(Transmission::map_t map);
14 Transmission::map_t createEmptyMap(); 16 Transmission::map_t createEmptyMap();
15 void deleteMap(Transmission::map_t map); 17 void deleteMap(Transmission::map_t map);
16 18