From cd17c9fb9fa8d2a73102c4ce1cc65cf3b9be1394 Mon Sep 17 00:00:00 2001 From: T1ti <40864460+T1ti@users.noreply.github.com> Date: Tue, 29 Oct 2024 03:08:17 +0100 Subject: [PATCH] only load world once in mapcreationwizard instead of loading two copies in NoggitWindows+mapcreationwizard --- .../Ui/MapCreationWizard.cpp | 86 +++++++++++++------ .../Ui/MapCreationWizard.hpp | 8 +- .../ui/windows/noggitWindow/NoggitWindow.cpp | 71 ++++++++------- .../ui/windows/noggitWindow/NoggitWindow.hpp | 10 +-- .../noggitWindow/widgets/MapListItem.cpp | 53 +++++------- 5 files changed, 134 insertions(+), 94 deletions(-) diff --git a/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.cpp b/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.cpp index b5e63154..10d7fcf5 100755 --- a/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.cpp +++ b/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.cpp @@ -562,35 +562,58 @@ void MapCreationWizard::populateNameSet(WMOInstance& instance) std::string MapCreationWizard::getDifficultyString() { - if (_instance_type->itemData(_instance_type->currentIndex()).toInt() == 1 && _difficulty_max_players->value() == 5) // dungeon + int instance_type = _instance_type->itemData(_instance_type->currentIndex()).toInt(); + int difficulty_type = _difficulty_type->currentIndex(); + assert(instance_type == 1 || instance_type == 2); + +/* +| Name | Entry Condition | Difficulty Entry 1 | Difficulty Entry 2 | Difficulty Entry 3 | +|--------------------|----------------------|-------------------------|-------------------------|-------------------------| +| Normal Creature | Different than 0 | 0 | 0 | 0 | +| Dungeon Creature | Normal Dungeon | Heroic Dungeon | 0 | 0 | +| Raid Creature | 10man Normal Raid | 25man Normal Raid | 10man Heroic Raid | 25man Heroic Raid | +| Battleground | 51-59 | 60-69 | 70-79 | 80 | +*/ + + if (instance_type == 1 && _difficulty_max_players->value() == 5) // dungeon { - if (_difficulty_type->currentIndex() == 0) - return "DUNGEON_DIFFICULTY_5PLAYER"; - else - return "DUNGEON_DIFFICULTY_5PLAYER_HEROIC"; + if (difficulty_type == 0) + return "DUNGEON_DIFFICULTY_5PLAYER"; + else if (difficulty_type == 1) + return "DUNGEON_DIFFICULTY_5PLAYER_HEROIC"; + else + return "Unsupported difficulty for 5 men dungeon"; } - else if (_instance_type->itemData(_instance_type->currentIndex()).toInt() == 2) + else if (instance_type == 2) // raid { switch (_difficulty_max_players->value()) { case 10: - if (_difficulty_type->currentIndex() == 0) + if (difficulty_type == 0) return "RAID_DIFFICULTY_10PLAYER"; - else + else if (difficulty_type == 2) return "RAID_DIFFICULTY_10PLAYER_HEROIC"; + break; case 20: - if (_difficulty_type->currentIndex() == 0) + if (difficulty_type == 0) return "RAID_DIFFICULTY_20PLAYER"; + break; case 25: // in BC 25men was difficulty 0, after the 10men mode in wrath it is difficulty 1 - if (_difficulty_type->currentIndex() == (0 || 1)) // maybe instead check if a difficulty 25 already exists + if (difficulty_type == 0 || difficulty_type == 1) // maybe instead check if a difficulty 25 already exists return "RAID_DIFFICULTY_25PLAYER"; - else + else if (difficulty_type == 3) return "RAID_DIFFICULTY_25PLAYER_HEROIC"; + break; case 40: + if (difficulty_type == 0) return "RAID_DIFFICULTY_40PLAYER"; + break; + default: + return "invalid player count"; } } + assert(false); return ""; } @@ -598,6 +621,8 @@ void MapCreationWizard::selectMap(int map_id) { _is_new_record = false; + // int map_id = world->getMapID(); + auto table = _project->ClientDatabase->LoadTable("Map", readFileAsIMemStream); auto record = table.Record(map_id); @@ -605,9 +630,14 @@ void MapCreationWizard::selectMap(int map_id) if (_world) { - delete _world; + // delete _world; + _world.reset(); } + // auto noggitWindow = reinterpret_cast(parent()); + // _world = world; + + auto directoryName = record.Columns["Directory"].Value; auto instanceType = record.Columns["InstanceType"].Value; @@ -622,7 +652,8 @@ void MapCreationWizard::selectMap(int map_id) // auto timeOffset = record.Columns["TimeOffset"].Value; auto raidOffset = record.Columns["RaidOffset"].Value; - _world = new World(directoryName, map_id, Noggit::NoggitRenderContext::MAP_VIEW); + // _world = new World(directoryName, map_id, Noggit::NoggitRenderContext::MAP_VIEW); + _world = std::make_unique(directoryName, map_id, Noggit::NoggitRenderContext::MAP_VIEW); // check if map has a wdl and prompt to create a new one std::stringstream filename; @@ -637,13 +668,13 @@ void MapCreationWizard::selectMap(int map_id) bool answer = prompt.exec() == QMessageBox::StandardButton::Yes; if (answer) { - _world->horizon.save_wdl(_world, true); + _world->horizon.save_wdl(_world.get(), true); _world->horizon.set_minimap(&_world->mapIndex); // _world = new World(directoryName, map_id, Noggit::NoggitRenderContext::MAP_VIEW); // refresh minimap } } - _minimap_widget->world(_world); + _minimap_widget->world(_world.get()); _directory->setText(QString::fromStdString(directoryName)); _directory->setEnabled(false); @@ -837,7 +868,7 @@ void MapCreationWizard::saveCurrentEntry() { _world->mapIndex.removeGlobalWmo(); } - _world->mapIndex.saveChanged(_world, true); + _world->mapIndex.saveChanged(_world.get(), true); _world->mapIndex.save(); // save wdt file if (_is_new_record) @@ -939,7 +970,7 @@ void MapCreationWizard::discardChanges() MapCreationWizard::~MapCreationWizard() { - delete _world; + // delete _world; disconnect(_connection); } @@ -950,7 +981,8 @@ void MapCreationWizard::addNewMap() if (_world) { - delete _world; + // delete _world; + _world.reset(); } // default to a new internal map name that isn't already used, or default world will load existing files @@ -965,11 +997,13 @@ void MapCreationWizard::addNewMap() suffix++; } - _world = new World(internal_map_name, _cur_map_id, Noggit::NoggitRenderContext::MAP_VIEW, true); + // _world = new World(internal_map_name, _cur_map_id, Noggit::NoggitRenderContext::MAP_VIEW, true); + _world = std::make_unique(internal_map_name, _cur_map_id, Noggit::NoggitRenderContext::MAP_VIEW, true); + // hack to reset the minimap if there is an existing WDL with the same path(happens when removing a map from map.dbc but not the files _world->horizon.set_minimap(&_world->mapIndex, true); - _minimap_widget->world(_world); + _minimap_widget->world(_world.get()); _directory->setText(internal_map_name.c_str()); _directory->setEnabled(true); @@ -1167,14 +1201,14 @@ void LocaleDBCEntry::toRecord(DBCFile::Record &record, size_t field) void LocaleDBCEntry::setDefaultLocValue(const std::string& text) { - // set the default locale's widget text and select it, but don't write data. + // set the default locale's widget text and select it, but don't write data. - int locale_id = Noggit::Application::NoggitApplication::instance()->clientData()->getLocaleId(); - _current_locale->setCurrentIndex(locale_id); - setCurrentLocale(_locale_names[locale_id]); + int locale_id = Noggit::Application::NoggitApplication::instance()->clientData()->getLocaleId(); + _current_locale->setCurrentIndex(locale_id); + setCurrentLocale(_locale_names[locale_id]); - // fill default locale's line edit - setValue(text, locale_id); + // fill default locale's line edit + setValue(text, locale_id); } void LocaleDBCEntry::clear() diff --git a/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.hpp b/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.hpp index 7630dbfd..1dad92e7 100755 --- a/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.hpp +++ b/src/noggit/ui/tools/MapCreationWizard/Ui/MapCreationWizard.hpp @@ -96,8 +96,12 @@ namespace Noggit ~MapCreationWizard(); void wheelEvent(QWheelEvent *event) override; - void destroyFakeWorld() { if(_world) delete _world; _world = nullptr; _minimap_widget->world (nullptr); }; + // void destroyFakeWorld() { if(_world) _world.reset(); _world = nullptr; _minimap_widget->world (nullptr); }; void addNewMap(); + + World* getWorld() { return _world.get(); }; + std::unique_ptr _world; + signals: void map_dbc_updated(int new_map = 0); @@ -160,8 +164,6 @@ namespace Noggit WmoEntryTab _wmoEntryTab; - World* _world = nullptr; - bool _is_new_record = false; int _cur_map_id = -1; diff --git a/src/noggit/ui/windows/noggitWindow/NoggitWindow.cpp b/src/noggit/ui/windows/noggitWindow/NoggitWindow.cpp index 1ffdd2c8..4cc05d64 100755 --- a/src/noggit/ui/windows/noggitWindow/NoggitWindow.cpp +++ b/src/noggit/ui/windows/noggitWindow/NoggitWindow.cpp @@ -149,6 +149,10 @@ namespace Noggit::Ui::Windows ) { QSettings settings; + assert(getWorld()); + + unsigned int world_map_id = getWorld()->getMapID(); + #ifdef USE_MYSQL_UID_STORAGE bool use_mysql = settings.value("project/mysql/enabled", false).toBool(); @@ -158,23 +162,23 @@ namespace Noggit::Ui::Windows valid_conn = mysql::testConnection(true); } - if ((valid_conn && mysql::hasMaxUIDStoredDB(_world->getMapID())) - || uid_storage::hasMaxUIDStored(_world->getMapID()) + if ((valid_conn && mysql::hasMaxUIDStoredDB(world_map_id)) + || uid_storage::hasMaxUIDStored(world_map_id) ) { - _world->mapIndex.loadMaxUID(); + getWorld()->mapIndex.loadMaxUID(); enterMapAt(pos, camera_pitch, camera_yaw, uid_fix_mode::none, from_bookmark); } #else - if (uid_storage::hasMaxUIDStored(_world->getMapID())) + if (uid_storage::hasMaxUIDStored(world_map_id)) { if (settings.value("uid_startup_check", true).toBool()) { enterMapAt(pos, camera_pitch, camera_yaw, uid_fix_mode::max_uid, from_bookmark); } else { - _world->mapIndex.loadMaxUID(); + getWorld()->mapIndex.loadMaxUID(); enterMapAt(pos, camera_pitch, camera_yaw, uid_fix_mode::none, from_bookmark); } } @@ -199,30 +203,32 @@ namespace Noggit::Ui::Windows bool from_bookmark ) { - if (_world->mapIndex.hasAGlobalWMO()) - { - // enter at mdoel's position - // pos = glm::vec3(_world->mWmoEntry[0], _world->mWmoEntry.pos[1], _world->mWmoEntry.pos[2]); + World* world = getWorld(); - // better, enter at model's max extent, facing toward min extent - auto min_extent = glm::vec3(_world->mWmoEntry.extents[0][0], _world->mWmoEntry.extents[0][1], _world->mWmoEntry.extents[0][2]); - auto max_extent = glm::vec3(_world->mWmoEntry.extents[1][0], _world->mWmoEntry.extents[1][1] * 2, _world->mWmoEntry.extents[1][2]); - float dx = min_extent.x - max_extent.x; - float dy = min_extent.z - max_extent.z; // flipping z and y works better for some reason - float dz = min_extent.y - max_extent.y; + if (world->mapIndex.hasAGlobalWMO()) + { + // enter at mdoel's position + // pos = glm::vec3(_world->mWmoEntry[0], _world->mWmoEntry.pos[1], _world->mWmoEntry.pos[2]); - pos = { _world->mWmoEntry.pos[0], _world->mWmoEntry.pos[1], _world->mWmoEntry.pos[2] }; + // better, enter at model's max extent, facing toward min extent + auto min_extent = glm::vec3(world->mWmoEntry.extents[0][0], world->mWmoEntry.extents[0][1], world->mWmoEntry.extents[0][2]); + auto max_extent = glm::vec3(world->mWmoEntry.extents[1][0], world->mWmoEntry.extents[1][1] * 2, world->mWmoEntry.extents[1][2]); + float dx = min_extent.x - max_extent.x; + float dy = min_extent.z - max_extent.z; // flipping z and y works better for some reason + float dz = min_extent.y - max_extent.y; - camera_yaw = math::degrees(math::radians(std::atan2(dx, dy))); + pos = { world->mWmoEntry.pos[0], world->mWmoEntry.pos[1], world->mWmoEntry.pos[2] }; - float distance = std::sqrt(dx * dx + dy * dy + dz * dz); - camera_pitch = -math::degrees(math::radians(std::asin(dz / distance))); + camera_yaw = math::degrees(math::radians(std::atan2(dx, dy))); - } + float distance = std::sqrt(dx * dx + dy * dy + dz * dz); + camera_pitch = -math::degrees(math::radians(std::asin(dz / distance))); + + } - _map_creation_wizard->destroyFakeWorld(); - _map_view = (new MapView(camera_yaw, camera_pitch, pos, this, _project, std::move(_world), uid_fix, from_bookmark)); + // _map_creation_wizard->destroyFakeWorld(); + _map_view = (new MapView(camera_yaw, camera_pitch, pos, this, _project, std::move(_map_creation_wizard->_world), uid_fix, from_bookmark)); connect(_map_view, &MapView::uid_fix_failed, [this]() { promptUidFixFailure(); }); connect(_settings, &settings::saved, [this]() @@ -277,20 +283,24 @@ namespace Noggit::Ui::Windows void NoggitWindow::loadMap(int map_id) { - _minimap->world(nullptr); + // _minimap->world(nullptr); + // World is now created only here in + // void MapCreationWizard::selectMap(int map_id) + emit mapSelected(map_id); + + /* _world.reset(); auto table = _project->ClientDatabase->LoadTable("Map", readFileAsIMemStream); auto record = table.Record(map_id); _world = std::make_unique(record.Columns["Directory"].Value, map_id, Noggit::NoggitRenderContext::MAP_VIEW); - _minimap->world(_world.get()); + */ + + _minimap->world(getWorld()); _project->ClientDatabase->UnloadTable("Map"); - - emit mapSelected(map_id); - } void NoggitWindow::buildMenu() @@ -434,13 +444,14 @@ namespace Noggit::Ui::Windows auto& entry(_project->Bookmarks.at(item->data(Qt::UserRole).toInt())); - _world.reset(); + _map_creation_wizard->_world.reset(); for (DBCFile::Iterator it = gMapDB.begin(); it != gMapDB.end(); ++it) { if (it->getInt(MapDB::MapID) == entry.map_id) { - _world = std::make_unique(it->getString(MapDB::InternalName), + // emit mapSelected(map_id); to update UI + _map_creation_wizard->_world = std::make_unique(it->getString(MapDB::InternalName), entry.map_id, Noggit::NoggitRenderContext::MAP_VIEW); check_uid_then_enter_map(entry.position, math::degrees(entry.camera_pitch), math::degrees(entry.camera_yaw), true @@ -458,7 +469,7 @@ namespace Noggit::Ui::Windows QObject::connect(_minimap, &minimap_widget::map_clicked, [this](::glm::vec3 const& pos) { - if (_world->mapIndex.hasAGlobalWMO()) // skip uid check + if (getWorld()->mapIndex.hasAGlobalWMO()) // skip uid check enterMapAt(pos, math::degrees(30.f), math::degrees(90.f), uid_fix_mode::none, false); else check_uid_then_enter_map(pos, math::degrees(30.f), math::degrees(90.f)); diff --git a/src/noggit/ui/windows/noggitWindow/NoggitWindow.hpp b/src/noggit/ui/windows/noggitWindow/NoggitWindow.hpp index fd18e012..ff4902c9 100755 --- a/src/noggit/ui/windows/noggitWindow/NoggitWindow.hpp +++ b/src/noggit/ui/windows/noggitWindow/NoggitWindow.hpp @@ -50,22 +50,24 @@ namespace Noggit::Ui::Windows QToolBar* _app_toolbar; + // std::unique_ptr _world; + std::unordered_set displayed_widgets; void buildMenu(); signals: void exitPromptOpened(); void mapSelected(int map_id); - private: std::unique_ptr _buildMapListComponent; std::shared_ptr _applicationConfiguration; std::shared_ptr _project; - void handleEventMapListContextMenuPinMap(int mapId, std::string MapName); - void handleEventMapListContextMenuUnpinMap(int mapId); + void handleEventMapListContextMenuPinMap(int mapId, std::string MapName); + void handleEventMapListContextMenuUnpinMap(int mapId); + World* getWorld() { return _map_creation_wizard->getWorld(); }; void loadMap (int map_id); @@ -98,8 +100,6 @@ namespace Noggit::Ui::Windows void applyFilterSearch(const QString& name, int type, int expansion, bool wmo_maps); - std::unique_ptr _world; - bool map_loaded = false; bool exit_to_project_selection = false; diff --git a/src/noggit/ui/windows/noggitWindow/widgets/MapListItem.cpp b/src/noggit/ui/windows/noggitWindow/widgets/MapListItem.cpp index 4ba04cb6..e90ae3fc 100755 --- a/src/noggit/ui/windows/noggitWindow/widgets/MapListItem.cpp +++ b/src/noggit/ui/windows/noggitWindow/widgets/MapListItem.cpp @@ -11,24 +11,19 @@ namespace Noggit::Ui::Widget auto layout = QGridLayout(); QIcon icon; - if (_map_data.expansion_id == 0) - icon = QIcon(":/icon-classic"); - if (_map_data.expansion_id == 1) - icon = QIcon(":/icon-burning"); - if (_map_data.expansion_id == 2) - icon = QIcon(":/icon-wrath"); - if (_map_data.expansion_id == 3) - icon = QIcon(":/icon-cata"); - if (_map_data.expansion_id == 4) - icon = QIcon(":/icon-panda"); - if (_map_data.expansion_id == 5) - icon = QIcon(":/icon-warlords"); - if (_map_data.expansion_id == 6) - icon = QIcon(":/icon-legion"); - if (_map_data.expansion_id == 7) - icon = QIcon(":/icon-battle"); - if (_map_data.expansion_id == 8) - icon = QIcon(":/icon-shadow"); + switch (_map_data.expansion_id) + { + case 0: icon = QIcon(":/icon-classic"); break; + case 1: icon = QIcon(":/icon-burning"); break; + case 2: icon = QIcon(":/icon-wrath"); break; + case 3: icon = QIcon(":/icon-cata"); break; + case 4: icon = QIcon(":/icon-panda"); break; + case 5: icon = QIcon(":/icon-warlords"); break; + case 6: icon = QIcon(":/icon-legion"); break; + case 7: icon = QIcon(":/icon-battle"); break; + case 8: icon = QIcon(":/icon-shadow"); break; + default: break; + } _map_icon = new QLabel("", parent); _map_icon->setPixmap(icon.pixmap(QSize(32, 32))); @@ -54,18 +49,16 @@ namespace Noggit::Ui::Widget _map_id->setAutoFillBackground(true); auto instance_type = QString("Unknown"); - if (_map_data.map_type_id == 0) - instance_type = QString("Continent"); - if (_map_data.map_type_id == 1) - instance_type = QString("Dungeon"); - if (_map_data.map_type_id == 2) - instance_type = QString("Raid"); - if (_map_data.map_type_id == 3) - instance_type = QString("Battleground"); - if (_map_data.map_type_id == 4) - instance_type = QString("Arena"); - if (_map_data.map_type_id == 5) - instance_type = QString("Scenario"); + switch (_map_data.map_type_id) + { + case 0: instance_type = "Continent"; break; + case 1: instance_type = "Dungeon"; break; + case 2: instance_type = "Raid"; break; + case 3: instance_type = "Battleground"; break; + case 4: instance_type = "Arena"; break; + case 5: instance_type = "Scenario"; break; + default: instance_type = "Unknown"; break; + } _map_instance_type = new QLabel(instance_type, this); _map_instance_type->setGeometry(150, 15, 125, 20);