| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2011 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include <QDir> |
| 17 | #include <QDomDocument> |
| 18 | #include <QDomElement> |
| 19 | #include <QFile> |
| 20 | #include <QStringList> |
| 21 | #include <QTextStream> |
| 22 | |
| 23 | #include "../common/config.hpp" |
| 24 | #include "../common/datakeywords.hpp" |
| 25 | |
| 26 | #include "database.hpp" |
| 27 | #include "layers.hpp" |
| 28 | #include "renderer.hpp" |
| 29 | #include "settings.hpp" |
| 30 | #include "track.hpp" |
| 31 | #include "trackdata.hpp" |
| 32 | #include "trackloader.hpp" |
| 33 | #include "trackobject.hpp" |
| 34 | #include "tracktile.hpp" |
| 35 | |
| 36 | #include <MCAssetManager> |
| 37 | #include <MCObjectFactory> |
| 38 | #include <MCShapeView> |
| 39 | |
| 40 | #include "simple_logger.hpp" |
| 41 | |
| 42 | #include <algorithm> |
| 43 | #include <cassert> |
| 44 | #include <memory> |
| 45 | |
| 46 | using std::dynamic_pointer_cast; |
| 47 | |
| 48 | static const int UNLOCK_LIMIT = 6; // Position required to unlock a new track |
| 49 | |
| 50 | TrackLoader * TrackLoader::m_instance = nullptr; |
| 51 | |
| 52 | TrackLoader::TrackLoader() |
| 53 | : m_assetManager( |
| 54 | Config::General::dataPath, |
| 55 | (std::string(Config::General::dataPath) + QDir::separator().toLatin1() + std::string("surfaces.conf")), |
| 56 | "", |
| 57 | (std::string(Config::General::dataPath) + QDir::separator().toLatin1() + std::string("meshes.conf"))) |
| 58 | , m_objectFactory(m_assetManager) |
| 59 | , m_trackObjectFactory(m_objectFactory) |
| 60 | { |
| 61 | assert(!TrackLoader::m_instance); |
| 62 | TrackLoader::m_instance = this; |
| 63 | } |
| 64 | |
| 65 | TrackLoader & TrackLoader::instance() |
| 66 | { |
| 67 | assert(TrackLoader::m_instance); |
| 68 | return *m_instance; |
| 69 | } |
| 70 | |
| 71 | void TrackLoader::addTrackSearchPath(QString path) |
| 72 | { |
| 73 | m_paths.push_back(path); |
| 74 | } |
| 75 | |
| 76 | void TrackLoader::loadAssets() |
| 77 | { |
| 78 | m_assetManager.load(); |
| 79 | } |
| 80 | |
| 81 | int TrackLoader::loadTracks(int lapCount, DifficultyProfile::Difficulty difficulty) |
| 82 | { |
| 83 | int numLoaded = 0; |
| 84 | for (auto && path : m_paths) |
| 85 | { |
| 86 | juzzlin::L().info() << "Loading race tracks from '" << path.toStdString() << "'.."; |
| 87 | auto trackPaths(QDir(path).entryList(QStringList("*.trk"))); |
| 88 | for (auto && trackPath : trackPaths) |
| 89 | { |
| 90 | trackPath = path + QDir::separator() + trackPath; |
| 91 | if (auto trackData = loadTrack(trackPath)) |
| 92 | { |
| 93 | juzzlin::L().info() << " Found '" << trackPath.toStdString() << "', index=" << trackData->index(); |
| 94 | m_tracks.push_back(std::make_shared<Track>(std::move(trackData))); |
| 95 | numLoaded++; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | juzzlin::L().error() << "Couldn't load '" << trackPath.toStdString() << "'.."; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (!trackPaths.size()) |
| 104 | { |
| 105 | juzzlin::L().info() << " No race tracks found."; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (numLoaded) |
| 110 | { |
| 111 | updateLockedTracks(lapCount, difficulty); |
| 112 | } |
| 113 | |
| 114 | return numLoaded; |
| 115 | } |
| 116 | |
| 117 | void TrackLoader::updateLockedTracks(int lapCount, DifficultyProfile::Difficulty difficulty) |
| 118 | { |
| 119 | sortTracks(); |
| 120 | |
| 121 | bool firstOfficialTrackUnlocked = false; |
| 122 | |
| 123 | // Check if the tracks are locked/unlocked. |
| 124 | for (auto && track : m_tracks) |
| 125 | { |
| 126 | if (!track->trackData().isUserTrack() && !Database::instance().loadTrackUnlockStatus(*track, lapCount, difficulty)) |
| 127 | { |
| 128 | #ifndef UNLOCK_ALL_TRACKS |
| 129 | track->trackData().setIsLocked(true); |
| 130 | #else |
| 131 | track->trackData().setIsLocked(false); |
| 132 | #endif |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | track->trackData().setIsLocked(false); |
| 137 | |
| 138 | // This is needed in the case new tracks are added to the game afterwards. |
| 139 | const auto bestPos = Database::instance().loadBestPos(*track, lapCount, difficulty); |
| 140 | if (bestPos.second && bestPos.first >= 1 && bestPos.first <= UNLOCK_LIMIT) |
| 141 | { |
| 142 | auto && next = track->next().lock(); |
| 143 | if (next) |
| 144 | { |
| 145 | next->trackData().setIsLocked(false); |
| 146 | Database::instance().saveTrackUnlockStatus(*next, lapCount, difficulty); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Always unlock the first official track |
| 152 | if (!track->trackData().isUserTrack() && !firstOfficialTrackUnlocked) |
| 153 | { |
| 154 | track->trackData().setIsLocked(false); |
| 155 | firstOfficialTrackUnlocked = true; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void TrackLoader::sortTracks() |
| 161 | { |
| 162 | // Sort tracks with respect to their indices. Move user tracks to the |
| 163 | // beginning of the track array. |
| 164 | std::stable_sort(m_tracks.begin(), m_tracks.end(), |
| 165 | [](auto lhs, auto rhs) -> bool { |
| 166 | const int left = lhs->trackData().isUserTrack() ? -1 : static_cast<int>(lhs->trackData().index()); |
| 167 | return left < static_cast<int>(rhs->trackData().index()); |
| 168 | }); |
| 169 | |
| 170 | // Cross-link the tracks |
| 171 | for (size_t i = 0; i + 1 < m_tracks.size(); i++) |
| 172 | { |
| 173 | m_tracks[i]->setNext(m_tracks[i + 1]); |
| 174 | m_tracks[i + 1]->setPrev(m_tracks[i]); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | std::unique_ptr<TrackData> TrackLoader::loadTrack(QString path) |
| 179 | { |
| 180 | QDomDocument doc; |
| 181 | |
| 182 | QFile file(path); |
| 183 | if (!file.open(QIODevice::ReadOnly)) |
| 184 | { |
| 185 | return nullptr; |
| 186 | } |
| 187 | |
| 188 | if (!doc.setContent(&file)) |
| 189 | { |
| 190 | file.close(); |
| 191 | return nullptr; |
| 192 | } |
| 193 | |
| 194 | file.close(); |
| 195 | |
| 196 | std::unique_ptr<TrackData> newData; |
| 197 | const auto root = doc.documentElement(); |
| 198 | if (root.nodeName() == "track") |
| 199 | { |
| 200 | const auto cols = root.attribute(DataKeywords::Header::cols, "0").toUInt(); |
| 201 | const auto rows = root.attribute(DataKeywords::Header::rows, "0").toUInt(); |
| 202 | |
| 203 | if (cols > 0 && rows > 0) |
| 204 | { |
| 205 | const auto name = root.attribute(DataKeywords::Header::name, "undefined"); |
| 206 | const bool isUserTrack = root.attribute(DataKeywords::Header::user, "0").toUInt(); |
| 207 | |
| 208 | newData = std::make_unique<TrackData>(name, isUserTrack, cols, rows); |
| 209 | newData->setFileName(path); |
| 210 | newData->setIndex(root.attribute(DataKeywords::Header::index, "999").toUInt()); |
| 211 | |
| 212 | // A temporary route vector. |
| 213 | std::vector<TargetNodeBasePtr> route; |
| 214 | |
| 215 | auto node = root.firstChild(); |
| 216 | while (!node.isNull()) |
| 217 | { |
| 218 | auto element = node.toElement(); |
| 219 | if (!element.isNull()) |
| 220 | { |
| 221 | // Read a tile element |
| 222 | if (element.nodeName() == DataKeywords::Track::tile) |
| 223 | { |
| 224 | readTile(element, *newData); |
| 225 | } |
| 226 | // Read an object element |
| 227 | else if (element.nodeName() == DataKeywords::Track::object) |
| 228 | { |
| 229 | readObject(element, *newData); |
| 230 | } |
| 231 | // Read a target node element |
| 232 | else if (element.nodeName() == DataKeywords::Track::node) |
| 233 | { |
| 234 | readTargetNode(element, *newData, route); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | node = node.nextSibling(); |
| 239 | } |
| 240 | |
| 241 | newData->route().buildFromVector(route); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return newData; |
| 246 | } |
| 247 | |
| 248 | void TrackLoader::readTile(QDomElement & element, TrackData & newData) |
| 249 | { |
| 250 | // X-coordinate in the tile matrix |
| 251 | const auto i = element.attribute(DataKeywords::Tile::i, "0").toUInt(); |
| 252 | |
| 253 | // Mirror the y-index, because game has the y-axis pointing up. |
| 254 | const auto j = newData.map().rows() - 1 - element.attribute(DataKeywords::Tile::j, "0").toUInt(); |
| 255 | |
| 256 | const auto tile = dynamic_pointer_cast<TrackTile>(newData.map().getTile(i, j)); |
| 257 | assert(tile); |
| 258 | |
| 259 | const auto type = element.attribute(DataKeywords::Tile::type, "clear").toStdString(); |
| 260 | tile->setTileType(type.c_str()); |
| 261 | tile->setTileTypeEnum(tileTypeEnumFromString(type.c_str())); |
| 262 | |
| 263 | // Mirror the angle, because game has the y-axis pointing up. |
| 264 | const int o = -element.attribute("o", "0").toInt(); |
| 265 | tile->setRotation(o); |
| 266 | tile->setComputerHint( |
| 267 | static_cast<TrackTileBase::ComputerHint>(element.attribute(DataKeywords::Tile::computerHint, "0").toUInt())); |
| 268 | tile->setExcludeFromMinimap(element.attribute(DataKeywords::Tile::excludeFromMinimap, "0").toInt()); |
| 269 | |
| 270 | // Associate with a surface object corresponging |
| 271 | // to the tile type. |
| 272 | // surface() throws if fails. Handled of higher level. |
| 273 | tile->setSurface(MCAssetManager::surfaceManager().surface(type)); |
| 274 | |
| 275 | // Set preview surface, if found. |
| 276 | try |
| 277 | { |
| 278 | tile->setPreviewSurface(MCAssetManager::surfaceManager().surface(type + "Preview")); |
| 279 | } catch (...) |
| 280 | { |
| 281 | // Don't care |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | TrackTile::TileType TrackLoader::tileTypeEnumFromString(std::string str) |
| 286 | { |
| 287 | static std::map<string, TrackTile::TileType> mappings = { |
| 288 | { "bridge", TrackTile::TileType::Bridge }, |
| 289 | { "corner90", TrackTile::TileType::Corner90 }, |
| 290 | { "corner45Left", TrackTile::TileType::Corner45Left }, |
| 291 | { "corner45Right", TrackTile::TileType::Corner45Right }, |
| 292 | { "straight", TrackTile::TileType::Straight }, |
| 293 | { "straight45Male", TrackTile::TileType::Straight45Male }, |
| 294 | { "straight45Female", TrackTile::TileType::Straight45Female }, |
| 295 | { "grass", TrackTile::TileType::Grass }, |
| 296 | { "sand", TrackTile::TileType::Sand }, |
| 297 | { "sandGrassStraight", TrackTile::TileType::SandGrassStraight }, |
| 298 | { "sandGrassCorner", TrackTile::TileType::SandGrassCorner }, |
| 299 | { "sandGrassCorner2", TrackTile::TileType::SandGrassCorner2 }, |
| 300 | { "finish", TrackTile::TileType::Finish }, |
| 301 | { "clear", TrackTile::TileType::None } |
| 302 | }; |
| 303 | |
| 304 | if (!mappings.count(str)) |
| 305 | { |
| 306 | juzzlin::L().error() << "No mapping for tile '" << str << "'.."; |
| 307 | return TrackTile::TileType::None; |
| 308 | } |
| 309 | |
| 310 | return mappings[str]; |
| 311 | } |
| 312 | |
| 313 | void TrackLoader::readObject(QDomElement & element, TrackData & newData) |
| 314 | { |
| 315 | // X-coordinate in the world |
| 316 | const int x = element.attribute(DataKeywords::Object::x, "0").toInt(); |
| 317 | |
| 318 | // Y-coordinate in the world |
| 319 | const int y = element.attribute(DataKeywords::Object::y, "0").toInt(); |
| 320 | |
| 321 | // Height of the map. |
| 322 | const int h = static_cast<int>(newData.map().rows() * TrackTile::height()); |
| 323 | |
| 324 | // The y-coordinates needs to be mirrored, because the y-axis is pointing |
| 325 | // down in the editor's coordinate system. |
| 326 | const MCVector2dF location(x, h - y); |
| 327 | |
| 328 | // Mirror the angle, because the y-axis is pointing |
| 329 | // down in the editor's coordinate system. |
| 330 | const int angle = -element.attribute(DataKeywords::Object::orientation, "0").toInt(); |
| 331 | |
| 332 | const auto category = element.attribute(DataKeywords::Object::category, ""); |
| 333 | const auto role = element.attribute(DataKeywords::Object::role, ""); |
| 334 | const bool forceStationary = element.attribute(DataKeywords::Object::forceStationary, "0").toUInt(); |
| 335 | |
| 336 | if (const auto object = m_trackObjectFactory.build(category, role, location, angle, forceStationary)) |
| 337 | { |
| 338 | newData.objects().add(std::shared_ptr<TrackObject>(object)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void TrackLoader::readTargetNode(QDomElement & element, TrackData & newData, std::vector<TargetNodeBasePtr> & route) |
| 343 | { |
| 344 | // Height of the map. The y-coordinates needs to be mirrored, because |
| 345 | // the coordinate system is y-wise mirrored in the editor. |
| 346 | const int mapHeight = static_cast<int>(newData.map().rows() * TrackTile::height()); |
| 347 | |
| 348 | const auto targetNode = std::make_shared<TargetNodeBase>(); |
| 349 | targetNode->setIndex(element.attribute(DataKeywords::Node::index, "0").toInt()); |
| 350 | |
| 351 | const int x = element.attribute(DataKeywords::Node::x, "0").toInt(); |
| 352 | const int y = element.attribute(DataKeywords::Node::y, "0").toInt(); |
| 353 | |
| 354 | targetNode->setLocation(QPointF(x, mapHeight - y)); |
| 355 | |
| 356 | const int w = element.attribute(DataKeywords::Node::width, "0").toInt(); |
| 357 | const int h = element.attribute(DataKeywords::Node::height, "0").toInt(); |
| 358 | |
| 359 | if (w > 0 && h > 0) |
| 360 | { |
| 361 | targetNode->setSize(QSizeF(w, h)); |
| 362 | } |
| 363 | |
| 364 | route.push_back(targetNode); |
| 365 | } |
| 366 | |
| 367 | size_t TrackLoader::tracks() const |
| 368 | { |
| 369 | return m_tracks.size(); |
| 370 | } |
| 371 | |
| 372 | std::shared_ptr<Track> TrackLoader::track(size_t index) const |
| 373 | { |
| 374 | if (index < tracks()) |
| 375 | { |
| 376 | return m_tracks.at(index); |
| 377 | } |
| 378 | |
| 379 | return nullptr; |
| 380 | } |