From 001f1d11e977d5586fe8496821b3a1e4bf76f10c Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 14 Apr 2011 20:37:53 +0200 Subject: place actors at random positions --- pacman-c++/util.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 9 deletions(-) (limited to 'pacman-c++/util.cpp') 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 delete[] map; } - // temporary Transmission::map_t createDemoMap() { Transmission::map_t map = createEmptyMap(); @@ -67,19 +66,64 @@ namespace Util if (tmpl[y][x] == '#') cur |= Color::none | Transmission::block; /* this is a simple hack to create areas where no - * autoplaced points will be placed (see below) + * autoplaced points/actors will be placed (see makePoints) */ else if (tmpl[y][x] == '.') cur |= Transmission::point; } } + return map; + } + + void placeActors(Transmission::map_t map, unsigned int players, const Color::Color *colors) + { + int mindistance = 5; + /* this outer loop is used if there are no more valid places left + * so we can change mindistance + */ + for(unsigned int i = 0; i < players; ++i) + { + /* get list of valid positions */ + QList validpos; + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + { + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + Transmission::field_t &cur = map[x][y]; + if (cur == Transmission::none) + validpos.append(QPoint(x, y)); + } + } + + /* place actors at map */ + for(i = 0; i < players; ++i) + { + int rand = (int) (validpos.size() * (qrand() / (RAND_MAX + 1.0))); + QPoint newpos = validpos.at(rand); + map[newpos.x()][newpos.y()] = colors[i] | Transmission::pacman | Transmission::direction_none; + validpos.removeAt(rand); - map[0][0] |= Transmission::bonuspoint; - map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right; - //map[23][0] = Color::blue | Transmission::pacman | Transmission::direction_none; - //map[24][0] = Color::green | Transmission::pacman | Transmission::direction_none; - //map[25][0] = Color::yellow | Transmission::pacman | Transmission::direction_none; + QMutableListIterator j(validpos); + while(j.hasNext()) + { + j.next(); + QPoint tmp = j.value() - newpos; + if (tmp.manhattanLength() < mindistance) + j.remove(); + } + if (validpos.empty()) + { + qWarning() << "There are no more valid positions for actors left on the map"; + mindistance -= 2; + break; + } + } + } + } + + void makePoints(Transmission::map_t map) + { /* auto place normal points*/ for (unsigned int x = 0; x < Constants::map_size.width; ++x) { @@ -92,8 +136,6 @@ namespace Util cur = Transmission::none; } } - - return map; } Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def) -- cgit v1.2.3