summaryrefslogtreecommitdiffstats
path: root/pacman-c++/server.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pacman-c++/server.cpp')
-rw-r--r--pacman-c++/server.cpp122
1 files changed, 105 insertions, 17 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp
index 01c74c4..628faa2 100644
--- a/pacman-c++/server.cpp
+++ b/pacman-c++/server.cpp
@@ -28,6 +28,18 @@ bool Server::run()
28 Transmission::map_t map = Util::createDemoMap(); 28 Transmission::map_t map = Util::createDemoMap();
29 Util::placeActors(map, m_maxplayers, Color::order); 29 Util::placeActors(map, m_maxplayers, Color::order);
30 Util::fillPoints(map); 30 Util::fillPoints(map);
31
32 /* save positions of blocks for later usage */
33 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
34 {
35 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
36 {
37 Transmission::field_t &cur = map[x][y];
38 if (cur & Transmission::block)
39 m_blocks.append(QPoint(x, y));
40 }
41 }
42
31 updateMap(map); 43 updateMap(map);
32 sendUpdate(map); 44 sendUpdate(map);
33 Util::deleteMap(map); 45 Util::deleteMap(map);
@@ -41,7 +53,7 @@ bool Server::run()
41 53
42void Server::tick() 54void Server::tick()
43{ 55{
44 qDebug() << "[Tick] Doing server update"; 56 //qDebug() << "[Tick] Doing server update";
45 Transmission::map_t map = calculateUpdates(); 57 Transmission::map_t map = calculateUpdates();
46 updateMap(map); 58 updateMap(map);
47 59
@@ -50,6 +62,10 @@ void Server::tick()
50 if (!pos.isNull()) 62 if (!pos.isNull())
51 updateMap(map, pos.x(), pos.y()); 63 updateMap(map, pos.x(), pos.y());
52 64
65 /* add/remove random colorized block */
66 if (this->property("coloredblocks").toBool())
67 colorizeBlocks(map);
68
53 sendUpdate(map); 69 sendUpdate(map);
54 Util::deleteMap(map); 70 Util::deleteMap(map);
55} 71}
@@ -66,17 +82,18 @@ Transmission::map_t Server::calculateUpdates()
66 while (i.hasNext()) 82 while (i.hasNext())
67 { 83 {
68 i.next(); 84 i.next();
85 Actor *actor = m_actors.value(i.key());
86 QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint());
87 Actor::Movement direction = i.value();
69 int turn = 0; 88 int turn = 0;
70 89
71invalid_direction: 90invalid_direction:
72 ++turn; 91 ++turn;
73 Actor *actor = m_actors.value(i.key());
74 QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint());
75 qDebug() << "[Calc] Actor wants to move: color=" << i.key() 92 qDebug() << "[Calc] Actor wants to move: color=" << i.key()
76 << "pos=" << mapPosition << "direction=" << i.value(); 93 << "pos=" << mapPosition << "direction=" << direction;
77 94
78 QPoint newMapPosition = mapPosition; 95 QPoint newMapPosition = mapPosition;
79 switch (i.value()) 96 switch (direction)
80 { 97 {
81 case Actor::Up: 98 case Actor::Up:
82 newMapPosition += QPoint(0, -1); 99 newMapPosition += QPoint(0, -1);
@@ -136,29 +153,29 @@ invalid_direction:
136 /* movement didn't work - e.g. was blocked */ 153 /* movement didn't work - e.g. was blocked */
137 if (mapPosition == newMapPosition) 154 if (mapPosition == newMapPosition)
138 { 155 {
139 /* */ 156 /* check turn */
140 if (turn == 1 && i.value() != actor->direction()) 157 if (turn == 1 && direction != actor->direction())
141 { 158 {
142 /* set direction back to last known direction and try again */ 159 /* set direction back to last known direction and try again */
143 qDebug() << "[Calc] Movement was blocked. Trying last known actor direction"; 160 qDebug() << "[Calc] Movement was blocked. Trying last known actor direction";
144 m_actorMovements[i.key()] = actor->direction(); 161 direction = actor->direction();
145 goto invalid_direction; 162 goto invalid_direction;
146 } 163 }
147 else 164 else
148 { 165 {
149 /* second turn didn't work too -> stop movement */ 166 /* second turn didn't work too -> stop movement */
150 m_actorMovements[i.key()] = Actor::None; 167 direction = Actor::None;
151 qDebug() << "[Calc] No good direction known. Movement stopped"; 168 qDebug() << "[Calc] No good direction known. Movement stopped";
152 } 169 }
153 } 170 }
154 171
155 map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | 172 map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman |
156 i.key() | Util::actorMovementToTransmission(i.value()); 173 i.key() | Util::actorMovementToTransmission(direction);
157 174
158 /* DEBUG: uncomments to disable auto-movement */ 175 /* DEBUG: uncomments to disable auto-movement */
159 //m_actorMovements[i.key()] = Actor::None; 176 //direction = Actor::None;
160 177
161 if (i.value() == Actor::None) 178 if (direction == Actor::None)
162 { 179 {
163 /* set actor to non-moving */ 180 /* set actor to non-moving */
164 m_actorMovements[i.key()] = Actor::None; 181 m_actorMovements[i.key()] = Actor::None;
@@ -170,7 +187,8 @@ invalid_direction:
170 187
171QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t type) 188QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t type)
172{ 189{
173 int chance = Constants::Random::bouns_point_chance; 190 int chance = Constants::Game::bouns_point_chance
191 - Constants::Game::bouns_point_chance_playerfactor * (m_maxplayers - 1);
174 int rand = (int) (chance * (qrand() / (RAND_MAX + 1.0))); 192 int rand = (int) (chance * (qrand() / (RAND_MAX + 1.0)));
175 if (rand != 0) 193 if (rand != 0)
176 return QPoint(); 194 return QPoint();
@@ -190,10 +208,10 @@ QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t typ
190 * performance would be better if actors would be listed in visualMap too 208 * 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) 209 * but this isn't possible that easily. see comment in updateMap(map)
192 */ 210 */
193 foreach (Actor *tmp, m_actors) 211 foreach (Actor *actor, m_actors)
194 { 212 {
195 QPoint oldpos = CoordToMapPosition(tmp->pos().toPoint()); 213 QPoint actorpos = CoordToMapPosition(actor->pos().toPoint());
196 validpos.removeAll(oldpos); 214 validpos.removeAll(actorpos);
197 } 215 }
198 216
199 if (validpos.empty()) 217 if (validpos.empty())
@@ -205,6 +223,60 @@ QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t typ
205 return pos; 223 return pos;
206} 224}
207 225
226void Server::colorizeBlocks(Transmission::map_t map)
227{
228 /* decrement tickcount of colored blocks */
229 if (!m_coloredBlocks.empty())
230 {
231 QMutableMapIterator<QPoint, unsigned int> i(m_coloredBlocks);
232 while(i.hasNext())
233 {
234 i.next();
235 unsigned val = i.value();
236 if (val > 0)
237 i.setValue(--val);
238 else
239 {
240 /* check for actor collision */
241 bool skip = false;
242 foreach(Actor *actor, m_actors)
243 {
244 if (CoordToMapPosition(actor->pos().toPoint()) == i.key())
245 skip = true;
246 if (skip)
247 break;
248 }
249 if (skip)
250 continue;
251
252 QPoint block = i.key();
253 map[block.x()][block.y()] |= Transmission::block | Color::none;
254 updateMap(map, block.x(), block.y());
255 m_blocks.append(block);
256 i.remove();
257 }
258 }
259 }
260
261 /* colorize a random block */
262 int rand = (int) (Constants::Game::colorize_block_chance * (qrand() / (RAND_MAX + 1.0)));
263 if (rand == 0)
264 {
265 rand = (int) (m_blocks.size() * (qrand() / (RAND_MAX + 1.0)));
266 QPoint block = m_blocks.at(rand);
267 m_blocks.removeAt(rand);
268
269 unsigned int min = Constants::Game::colorize_block_tickcount_min;
270 unsigned int max = Constants::Game::colorize_block_tickcount_max;
271 int tickcount = min + (int) ((max - min + 1) * (qrand() / (RAND_MAX + 1.0)));
272 m_coloredBlocks.insert(block, tickcount);
273
274 unsigned int color = (int) ((m_maxplayers - 1) * (qrand() / (RAND_MAX + 1.0)));
275 map[block.x()][block.y()] |= Transmission::block | Color::order[color];
276 updateMap(map, block.x(), block.y());
277 }
278}
279
208bool Server::waitForClientConnections() 280bool Server::waitForClientConnections()
209{ 281{
210 // server must stay alive as long as sockets (qt parent mem mechanism) 282 // server must stay alive as long as sockets (qt parent mem mechanism)
@@ -324,6 +396,10 @@ bool Server::parseCommandline()
324 << " Default: " << m_numbots << endl 396 << " Default: " << m_numbots << endl
325 << endl; 397 << endl;
326 opt.setOption("bots"); 398 opt.setOption("bots");
399 out << " --nocolorblocks" << endl
400 << " Disable random colorized blocks" << endl
401 << endl;
402 opt.setFlag("nocolorblocks");
327 out << " -h, --help" << endl 403 out << " -h, --help" << endl
328 << " Prints this help message" << endl; 404 << " Prints this help message" << endl;
329 opt.setFlag("help", 'h'); 405 opt.setFlag("help", 'h');
@@ -386,12 +462,24 @@ bool Server::parseCommandline()
386 m_numbots = numbots; 462 m_numbots = numbots;
387 } 463 }
388 464
465 this->setProperty("coloredblocks", !opt.getFlag("nocolorblocks"));
466
389 return true; 467 return true;
390} 468}
391 469
470bool operator<(const QPoint& lhs, const QPoint& rhs)
471{
472 if (lhs.x() < rhs.x())
473 return true;
474 else if (lhs.x() == rhs.x())
475 return lhs.y() < rhs.y();
476 else
477 return false;
478}
479
392bool Constants::server = true; 480bool Constants::server = true;
393 481
394int main(int argc, char ** argv) 482int main(int argc, char **argv)
395{ 483{
396 /* Verify that the version of the library that we linked against is 484 /* Verify that the version of the library that we linked against is
397 * compatible with the version of the headers we compiled against. 485 * compatible with the version of the headers we compiled against.