* Gets a sorted list of the facilities(=iterators) NOT connected to the Access Lift. * @param remove Facility to ignore (in case of facility dismantling). * @return a sorted list of iterators pointing to elements in _facilities. */
| 1335 | * @return a sorted list of iterators pointing to elements in _facilities. |
| 1336 | */ |
| 1337 | std::list<std::vector<BaseFacility*>::iterator> Base::getDisconnectedFacilities(BaseFacility *remove) |
| 1338 | { |
| 1339 | std::list<std::vector<BaseFacility*>::iterator> result; |
| 1340 | |
| 1341 | if (remove != 0 && remove->getRules()->isLift()) |
| 1342 | { // Theoretically this is impossible, but sanity check is good :) |
| 1343 | for (std::vector<BaseFacility*>::iterator i = _facilities.begin(); i != _facilities.end(); ++i) |
| 1344 | { |
| 1345 | if ((*i) != remove) result.push_back(i); |
| 1346 | } |
| 1347 | return result; |
| 1348 | } |
| 1349 | |
| 1350 | std::vector<std::pair<std::vector<BaseFacility*>::iterator, bool>*> facilitiesConnStates; |
| 1351 | std::pair<std::vector<BaseFacility*>::iterator, bool> *grid[BASE_SIZE][BASE_SIZE]; |
| 1352 | BaseFacility *lift = 0; |
| 1353 | |
| 1354 | for (int x = 0; x < BASE_SIZE; ++x) |
| 1355 | { |
| 1356 | for (int y = 0; y < BASE_SIZE; ++y) |
| 1357 | { |
| 1358 | grid[x][y] = 0; |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | // Ok, fill up the grid(+facilitiesConnStates), and search the lift |
| 1363 | for (std::vector<BaseFacility*>::iterator i = _facilities.begin(); i != _facilities.end(); ++i) |
| 1364 | { |
| 1365 | if ((*i) != remove) |
| 1366 | { |
| 1367 | if ((*i)->getRules()->isLift()) lift = (*i); |
| 1368 | for (int x = 0; x != (*i)->getRules()->getSize(); ++x) |
| 1369 | { |
| 1370 | for (int y = 0; y != (*i)->getRules()->getSize(); ++y) |
| 1371 | { |
| 1372 | std::pair<std::vector<BaseFacility*>::iterator, bool> *p = new std::pair<std::vector<BaseFacility*>::iterator, bool>(i,false); |
| 1373 | facilitiesConnStates.push_back(p); |
| 1374 | grid[(*i)->getX() + x][(*i)->getY() + y] = p; |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | // we're in real trouble if this happens... |
| 1381 | if (lift == 0) |
| 1382 | { |
| 1383 | //TODO: something clever. |
| 1384 | return result; |
| 1385 | } |
| 1386 | |
| 1387 | // Now make the recursion manually using a stack |
| 1388 | std::stack<std::pair<int, int> > stack; |
| 1389 | stack.push(std::make_pair(lift->getX(),lift->getY())); |
| 1390 | while (!stack.empty()) |
| 1391 | { |
| 1392 | int x = stack.top().first, y = stack.top().second; |
| 1393 | stack.pop(); |
| 1394 | if (x >= 0 && x < BASE_SIZE && y >= 0 && y < BASE_SIZE && grid[x][y] != 0 && !grid[x][y]->second) |