summaryrefslogtreecommitdiffstats
path: root/pacman-c++/server.cpp
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2011-04-15 03:20:17 +0200
committermanuel <manuel@mausz.at>2011-04-15 03:20:17 +0200
commit536ddd91ae7f162045226d4b358ba95d678f6deb (patch)
tree7311153ba9e54bacd5d319e21bf419a3acdb7c4c /pacman-c++/server.cpp
parent9a1787acd9fcd5dc0cce80cc0d6d007fa47b8bbe (diff)
downloadfoop-536ddd91ae7f162045226d4b358ba95d678f6deb.tar.gz
foop-536ddd91ae7f162045226d4b358ba95d678f6deb.tar.bz2
foop-536ddd91ae7f162045226d4b358ba95d678f6deb.zip
add support for random bonus points
Diffstat (limited to 'pacman-c++/server.cpp')
-rw-r--r--pacman-c++/server.cpp50
1 files changed, 46 insertions, 4 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp
index a045cad..01c74c4 100644
--- a/pacman-c++/server.cpp
+++ b/pacman-c++/server.cpp
@@ -3,6 +3,7 @@
3#include "pacman.pb.h" 3#include "pacman.pb.h"
4#include "block.h" 4#include "block.h"
5#include "anyoption.h" 5#include "anyoption.h"
6#include "bonuspoint.h"
6#include <QtNetwork/QTcpServer> 7#include <QtNetwork/QTcpServer>
7#include <QtNetwork/QTcpSocket> 8#include <QtNetwork/QTcpSocket>
8#include <QTextStream> 9#include <QTextStream>
@@ -26,7 +27,7 @@ bool Server::run()
26 qDebug() << "[Server] Creating map..."; 27 qDebug() << "[Server] Creating map...";
27 Transmission::map_t map = Util::createDemoMap(); 28 Transmission::map_t map = Util::createDemoMap();
28 Util::placeActors(map, m_maxplayers, Color::order); 29 Util::placeActors(map, m_maxplayers, Color::order);
29 Util::makePoints(map); 30 Util::fillPoints(map);
30 updateMap(map); 31 updateMap(map);
31 sendUpdate(map); 32 sendUpdate(map);
32 Util::deleteMap(map); 33 Util::deleteMap(map);
@@ -43,6 +44,12 @@ void Server::tick()
43 qDebug() << "[Tick] Doing server update"; 44 qDebug() << "[Tick] Doing server update";
44 Transmission::map_t map = calculateUpdates(); 45 Transmission::map_t map = calculateUpdates();
45 updateMap(map); 46 updateMap(map);
47
48 /* add a random bonus point */
49 QPoint pos = addRandomPoint(map, Transmission::bonuspoint);
50 if (!pos.isNull())
51 updateMap(map, pos.x(), pos.y());
52
46 sendUpdate(map); 53 sendUpdate(map);
47 Util::deleteMap(map); 54 Util::deleteMap(map);
48} 55}
@@ -161,6 +168,43 @@ invalid_direction:
161 return map; 168 return map;
162} 169}
163 170
171QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t type)
172{
173 int chance = Constants::Random::bouns_point_chance;
174 int rand = (int) (chance * (qrand() / (RAND_MAX + 1.0)));
175 if (rand != 0)
176 return QPoint();
177
178 /* get list of valid positions */
179 QList<QPoint> validpos;
180 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
181 {
182 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
183 {
184 if (visualMap[x][y] == NULL)
185 validpos.append(QPoint(x, y));
186 }
187 }
188
189 /* remove actors possitions from list
190 * performance would be better if actors would be listed in visualMap too
191 * but this isn't possible that easily. see comment in updateMap(map)
192 */
193 foreach (Actor *tmp, m_actors)
194 {
195 QPoint oldpos = CoordToMapPosition(tmp->pos().toPoint());
196 validpos.removeAll(oldpos);
197 }
198
199 if (validpos.empty())
200 return QPoint();
201
202 rand = (int) (validpos.size() * (qrand() / (RAND_MAX + 1.0)));
203 QPoint pos = validpos.at(rand);
204 map[pos.x()][pos.y()] = type;
205 return pos;
206}
207
164bool Server::waitForClientConnections() 208bool Server::waitForClientConnections()
165{ 209{
166 // server must stay alive as long as sockets (qt parent mem mechanism) 210 // server must stay alive as long as sockets (qt parent mem mechanism)
@@ -185,14 +229,12 @@ bool Server::waitForClientConnections()
185 QTcpSocket *socket = tcpSrv->nextPendingConnection(); 229 QTcpSocket *socket = tcpSrv->nextPendingConnection();
186 connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); 230 connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate()));
187 231
188 /* assign color */ 232 /* assign color and notify client */
189 Color::Color color = Color::order[i]; 233 Color::Color color = Color::order[i];
190 m_clientConnections[color] = socket; 234 m_clientConnections[color] = socket;
191 /* notify player of color */
192 packet.set_color(color); 235 packet.set_color(color);
193 packet.set_maxplayers(m_maxplayers); 236 packet.set_maxplayers(m_maxplayers);
194 Util::sendPacket(packet, socket); 237 Util::sendPacket(packet, socket);
195
196 qDebug() << "[Connect] New Player: color=" << color; 238 qDebug() << "[Connect] New Player: color=" << color;
197 } 239 }
198 240