Exemplo n.º 1
0
/***************************************************************************
 ***************          Calculates waypoints for AI          *************
 **************************************************************************/
void DriverAI::calcWaypoints(SceneManager *sm, int idStart, int idEnd) {
    //Get list of tiles that will be crossed
    vector<int> tilePath = getTilePath(sm, idStart, idEnd);
    
    //Create waypoint at the edges where one tiles meets the next
    waypoints.clear();
    waypoints = convertTilePathToEdgePath(sm, tilePath);
}
Exemplo n.º 2
0
/**
 * @brief Gets a Tile where the image data can be stored. If the Tile isn't cached a new Tile is returned.
 *
 * @param ti A shared pointer to the TileIdentifier of the Tile.
 **/
shared_ptr<Tile> Cache::getTile(const shared_ptr<TileIdentifier>& ti)
{
    // TODO: Finer Synchronization
    GlobalCacheLock.lock();
    shared_ptr<CacheOfOneStylesheet> cache;
    const string& stylesheet = ti->getStylesheetPath();
    auto cacheIt = AllCaches.find(stylesheet);
    if (cacheIt != AllCaches.end()) {
        // Found cache for Stylesheet.
        cache = cacheIt->second;
    } else {
        // Creating a new cache for stylesheet.
        cache = boost::make_shared<CacheOfOneStylesheet>();
        AllCaches[stylesheet] = cache;
        boost::filesystem::path dir(Config->get<string>(opt::server::cache_path) + "/" + ti->getStylesheetPath());
        boost::filesystem::create_directories(dir);
        log << log4cpp::Priority::DEBUG << "Stylesheetcache " << stylesheet << " created.";
    }
    // Get tile from map
    shared_ptr<Tile> tile;
    auto tileIt = cache->find(*ti);
    if (tileIt != cache->end()) {
        // Cache hit
        tile = tileIt->second.first;
        RecentlyUsedList.erase(tileIt->second.second);
        RecentlyUsedList.push_front(tileIt->second.first);
        tileIt->second.second = RecentlyUsedList.begin();
    }  else {
        // Cache miss
        tile = boost::make_shared<Tile>(ti);
        if (ti->getZoom() <= Config->get<int>(opt::server::cache_keep_tile)) {
            // Try to load prerendered image data from file.
            boost::filesystem::path path = getTilePath(ti);
            Tile::ImageType image = boost::make_shared<Tile::ImageType::element_type>();
            try {
                readFile(image, path);
                tile->setImage(image);
            } catch (excp::FileNotFoundException) {
                log << log4cpp::Priority::DEBUG << "readFile: Not found: " << path.string();
            }
        }
        RecentlyUsedList.push_front(tile);
        cache->insert(std::make_pair(*ti, CacheElement(tile, RecentlyUsedList.begin())));
    }
    while (RecentlyUsedList.size() > Config->get<int>(opt::server::cache_size)) {
        shared_ptr<Tile> tileToDelete = RecentlyUsedList.back();
        // Evict a Tile when cache is full.
        if (tileToDelete->getIdentifier()->getZoom() <= Config->get<int>(opt::server::cache_keep_tile)) {
            // Evict to hard drive.
            shared_ptr<TileIdentifier> tiToDelete = tileToDelete->getIdentifier();
            boost::filesystem::path path = getTilePath(tiToDelete);
            try {
                writeFile(tileToDelete, path);
            } catch (excp::FileNotFoundException) {
                log << log4cpp::Priority::DEBUG << "WriteFile: Could not open file " << path.string();
                // Disk is full
            } catch (excp::InputFormatException) {
                log << log4cpp::Priority::DEBUG << "WriteFile: Image not yet rendered " << *tile->getIdentifier();
                RecentlyUsedList.push_front(tileToDelete);
                cacheIt = AllCaches.find(tileToDelete->getIdentifier()->getStylesheetPath());
                if (cacheIt != AllCaches.end()) {
                    cacheIt->second->erase(*tileToDelete->getIdentifier());
                    cacheIt->second->insert(std::make_pair(*tiToDelete, CacheElement(tileToDelete, RecentlyUsedList.begin())));
                }
                RecentlyUsedList.pop_back();
                break;
            }
        }
        // Delete tile
        log << log4cpp::Priority::DEBUG << "Deleting least recently used Tile." << *tileToDelete->getIdentifier();
        cacheIt = AllCaches.find(tileToDelete->getIdentifier()->getStylesheetPath());
        if (cacheIt != AllCaches.end()) {
            cacheIt->second->erase(*tileToDelete->getIdentifier());
        }
        RecentlyUsedList.pop_back();
    }
    GlobalCacheLock.unlock();
    return tile;
}