From b5d531aee369e35ea1b51120fc3bf4a1adeef8c3 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Alves Cruz Date: Fri, 27 Sep 2024 22:29:20 -0300 Subject: [PATCH] fix: sonar cloud warnings --- source/copybuffer.cpp | 7 +- source/editor.cpp | 25 +++---- source/iomap_otbm.cpp | 16 ++-- source/main_menubar.cpp | 10 +-- source/map_display.cpp | 161 ++++++++++++++++++---------------------- 5 files changed, 100 insertions(+), 119 deletions(-) diff --git a/source/copybuffer.cpp b/source/copybuffer.cpp index 9daea35a..2569ac34 100644 --- a/source/copybuffer.cpp +++ b/source/copybuffer.cpp @@ -85,11 +85,10 @@ void CopyBuffer::copy(Editor &editor, int floor) { // Monster const auto monstersSelection = tile->getSelectedMonsters(); - for (auto monsterIt = monstersSelection.begin(); monsterIt != monstersSelection.end(); ++monsterIt) { + std::ranges::for_each(monstersSelection, [&](const auto monster) { ++monsterCount; - // Copy items to copybuffer - copied_tile->addMonster((*monsterIt)->deepCopy()); - } + copied_tile->addMonster(monster->deepCopy()); + }); if (tile->spawnMonster && tile->spawnMonster->isSelected()) { copied_tile->spawnMonster = tile->spawnMonster->deepCopy(); diff --git a/source/editor.cpp b/source/editor.cpp index afd03c7c..2e319fa7 100644 --- a/source/editor.cpp +++ b/source/editor.cpp @@ -1101,19 +1101,9 @@ void Editor::moveSelection(const Position &offset) { } // Move monster const auto monstersSelection = new_tile->popSelectedMonsters(); - for (const auto monster : monstersSelection) { + std::ranges::for_each(monstersSelection, [&](const auto monster) { storage_tile->addMonster(monster); - } - /* - if (!new_tile->monsters.empty()) { - const auto view = std::ranges::remove_if(new_tile->monsters.begin(), new_tile->monsters.end(), [](const auto monster) { - return monster->isSelected(); - }); - - storage_tile->monsters = std::vector(view.begin(), view.end()); - new_tile->monsters.erase(view.begin(), view.end()); - } - */ + }); // Move npc if (new_tile->npc && new_tile->npc->isSelected()) { storage_tile->npc = new_tile->npc; @@ -1354,12 +1344,21 @@ void Editor::destroySelection() { delete *iit; } - const auto monstersSelection = newtile->popSelectedMonsters(); + auto monstersSelection = newtile->popSelectedMonsters(); + std::ranges::for_each(monstersSelection, [&](auto monster) { + ++monsterCount; + delete monster; + }); + // Clear the vector to avoid being used anywhere else in this block with nullptrs + monstersSelection.clear(); + + /* for (auto monsterIt = monstersSelection.begin(); monsterIt != monstersSelection.end(); ++monsterIt) { ++monsterCount; // Delete the monsters from the tile delete *monsterIt; } + */ if (newtile->spawnMonster && newtile->spawnMonster->isSelected()) { delete newtile->spawnMonster; diff --git a/source/iomap_otbm.cpp b/source/iomap_otbm.cpp index daac4c41..7a02b1e7 100644 --- a/source/iomap_otbm.cpp +++ b/source/iomap_otbm.cpp @@ -1058,9 +1058,9 @@ bool IOMapOTBM::loadSpawnsMonster(Map &map, pugi::xml_document &doc) { spawntime = g_settings.getInteger(Config::DEFAULT_SPAWN_MONSTER_TIME); } - uint8_t weight = monsterNode.attribute("weight").as_uint(); + auto weight = static_cast(monsterNode.attribute("weight").as_uint()); if (weight == 0) { - weight = g_settings.getInteger(Config::MONSTER_DEFAULT_WEIGHT); + weight = static_cast(g_settings.getInteger(Config::MONSTER_DEFAULT_WEIGHT)); } Direction direction = NORTH; @@ -1766,11 +1766,11 @@ bool IOMapOTBM::saveSpawns(Map &map, pugi::xml_document &doc) { int32_t radius = spawnMonster->getSize(); spawnNode.append_attribute("radius") = radius; - for (int32_t y = -radius; y <= radius; ++y) { - for (int32_t x = -radius; x <= radius; ++x) { - Tile* monster_tile = map.getTile(spawnPosition + Position(x, y, 0)); - if (monster_tile) { - for (const auto monster : monster_tile->monsters) { + for (auto y = -radius; y <= radius; ++y) { + for (auto x = -radius; x <= radius; ++x) { + const auto monsterTile = map.getTile(spawnPosition + Position(x, y, 0)); + if (monsterTile) { + for (const auto monster : monsterTile->monsters) { if (monster && !monster->isSaved()) { pugi::xml_node monsterNode = spawnNode.append_child("monster"); monsterNode.append_attribute("name") = monster->getName().c_str(); @@ -1787,7 +1787,7 @@ bool IOMapOTBM::saveSpawns(Map &map, pugi::xml_document &doc) { monsterNode.append_attribute("direction") = monster->getDirection(); } - if (monster_tile->monsters.size() > 1) { + if (monsterTile->monsters.size() > 1) { const auto weight = monster->getWeight(); monsterNode.append_attribute("weight") = weight > 0 ? weight : g_settings.getInteger(Config::MONSTER_DEFAULT_WEIGHT); } diff --git a/source/main_menubar.cpp b/source/main_menubar.cpp index c6a960b3..3938e52e 100644 --- a/source/main_menubar.cpp +++ b/source/main_menubar.cpp @@ -1551,11 +1551,11 @@ void MainMenuBar::OnMapRemoveEmptyMonsterSpawns(wxCommandEvent &WXUNUSED(event)) const int32_t radius = tile->spawnMonster->getSize(); bool empty = true; - for (int32_t y = -radius; y <= radius; ++y) { - for (int32_t x = -radius; x <= radius; ++x) { - Tile* creature_tile = map.getTile(spawnPosition + Position(x, y, 0)); - if (creature_tile) { - for (const auto monster : creature_tile->monsters) { + for (auto y = -radius; y <= radius; ++y) { + for (auto x = -radius; x <= radius; ++x) { + const auto creatureTile = map.getTile(spawnPosition + Position(x, y, 0)); + if (creatureTile) { + for (const auto monster : creatureTile->monsters) { if (empty) { empty = false; } diff --git a/source/map_display.cpp b/source/map_display.cpp index c35b1600..a1002bc5 100644 --- a/source/map_display.cpp +++ b/source/map_display.cpp @@ -393,65 +393,55 @@ Position MapCanvas::GetCursorPosition() const { } void MapCanvas::UpdatePositionStatus(int x, int y) { - if (x == -1) { - x = cursor_x; - } - if (y == -1) { - y = cursor_y; - } + + x == -1 ? cursor_x : x; + y == -1 ? cursor_y : y; int map_x, map_y; ScreenToMap(x, y, &map_x, &map_y); - wxString ss; - ss << "x: " << map_x << " y:" << map_y << " z:" << floor; - g_gui.root->SetStatusText(ss, 2); + g_gui.root->SetStatusText(fmt::format("x: {} y: {} z: {}", map_x, map_y, floor), 2); - ss = ""; - Tile* tile = editor.getMap().getTile(map_x, map_y, floor); - if (tile) { - if (tile->spawnMonster && g_settings.getInteger(Config::SHOW_SPAWNS_MONSTER)) { - ss << "Monster spawn radius: " << tile->spawnMonster->getSize(); - } else if (!tile->monsters.empty() && g_settings.getInteger(Config::SHOW_MONSTERS)) { - for (const auto monster : tile->monsters) { - ss << ("Monster"); - ss << " \"" << wxstr(monster->getName()) << "\" spawntime: " << monster->getSpawnMonsterTime(); - if (monster != tile->monsters.back()) { - ss << " - "; - } - } - } else if (tile->spawnNpc && g_settings.getInteger(Config::SHOW_SPAWNS_NPC)) { - ss << "Npc spawn radius: " << tile->spawnNpc->getSize(); - } else if (tile->npc && g_settings.getInteger(Config::SHOW_NPCS)) { - ss << ("NPC"); - ss << " \"" << wxstr(tile->npc->getName()) << "\" spawntime: " << tile->npc->getSpawnNpcTime(); - } else if (Item* item = tile->getTopItem()) { - ss << "Item \"" << wxstr(item->getName()) << "\""; - ss << " id:" << item->getID(); - ss << " cid:" << item->getClientID(); - if (item->getUniqueID()) { - ss << " uid:" << item->getUniqueID(); - } - if (item->getActionID()) { - ss << " aid:" << item->getActionID(); - } - if (item->hasWeight()) { - wxString s; - s.Printf("%.2f", item->getWeight()); - ss << " weight: " << s; - } - } else { - ss << "Nothing"; - } - } else { - ss << "Nothing"; - } + const auto tile = editor.getMap().getTile(map_x, map_y, floor); + + std::string description = "Nothing"; if (editor.IsLive()) { editor.GetLive().updateCursor(Position(map_x, map_y, floor)); } - g_gui.root->SetStatusText(ss, 1); + if (!tile) { + g_gui.root->SetStatusText(description, 1); + return; + } + + description.clear(); + if (tile->spawnMonster && g_settings.getInteger(Config::SHOW_SPAWNS_MONSTER)) { + description = fmt::format("Monster spawn radius: {}", tile->spawnMonster->getSize()); + } else if (!tile->monsters.empty() && g_settings.getInteger(Config::SHOW_MONSTERS)) { + std::vector texts; + for (const auto monster : tile->monsters) { + const auto monsterWeight = tile->monsters.size() > 1 ? std::to_string(monster->getWeight()) : "0"; + texts.emplace_back(fmt::format("Monster \"{}\", spawntime: {}, weight: {}", monster->getName(), monster->getSpawnMonsterTime(), monsterWeight)); + } + description = fmt::format("{}", fmt::join(texts, " - ")); + } else if (tile->spawnNpc && g_settings.getInteger(Config::SHOW_SPAWNS_NPC)) { + description = fmt::format("Npc spawn radius: {}", tile->spawnNpc->getSize()); + } else if (tile->npc && g_settings.getInteger(Config::SHOW_NPCS)) { + description = fmt::format("NPC \"{}\", spawntime: {}", tile->npc->getName(), tile->npc->getSpawnNpcTime()); + } else if (const auto item = tile->getTopItem()) { + description = fmt::format("Item \"{}\", id: {}, cid: {}", item->getName(), item->getID(), item->getClientID()); + + description = item->getUniqueID() ? fmt::format("{}, uid: {}", description, item->getUniqueID()) : description; + + description = item->getActionID() ? fmt::format("{}, aid: {}", description, item->getActionID()) : description; + + description = item->hasWeight() ? fmt::format("{}, weight: {:.2f}", description, item->getWeight()) : description; + } else { + description = "Nothing"; + } + + g_gui.root->SetStatusText(description, 1); } void MapCanvas::UpdateZoomStatus() { @@ -2371,65 +2361,58 @@ void MapCanvas::OnProperties(wxCommandEvent &WXUNUSED(event)) { return; } - Tile* tile = editor.getSelection().getSelectedTile(); + const auto tile = editor.getSelection().getSelectedTile(); if (!tile) { return; } ASSERT(tile->isSelected()); - Tile* new_tile = tile->deepCopy(editor.getMap()); + const auto newTile = tile->deepCopy(editor.getMap()); wxDialog* w = nullptr; - if (new_tile->spawnMonster && g_settings.getInteger(Config::SHOW_SPAWNS_MONSTER)) { - w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), new_tile, new_tile->spawnMonster); + if (newTile->spawnMonster && g_settings.getInteger(Config::SHOW_SPAWNS_MONSTER)) { + w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), newTile, newTile->spawnMonster); } else if (g_settings.getInteger(Config::SHOW_MONSTERS)) { - std::vector selectedMonsters = new_tile->getSelectedMonsters(); - - Monster* monster = nullptr; - int count = 0; - for (auto it = selectedMonsters.begin(); it != selectedMonsters.end(); ++it) { - ++count; - const auto itMonster = (*it); - if (itMonster->isSelected()) { - monster = itMonster; - } + std::vector selectedMonsters = newTile->getSelectedMonsters(); + + const auto it = std::ranges::find_if(selectedMonsters | std::views::reverse, [&](const auto itMonster) { + return itMonster->isSelected(); + }); + + if (it == selectedMonsters.rend()) { + return; } - w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), new_tile, monster); - } else if (new_tile->npc && g_settings.getInteger(Config::SHOW_NPCS)) { - w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), new_tile, new_tile->npc); - } else if (new_tile->spawnNpc && g_settings.getInteger(Config::SHOW_SPAWNS_NPC)) { - w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), new_tile, new_tile->spawnNpc); + + w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), newTile, *it); + } else if (newTile->npc && g_settings.getInteger(Config::SHOW_NPCS)) { + w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), newTile, newTile->npc); + } else if (newTile->spawnNpc && g_settings.getInteger(Config::SHOW_SPAWNS_NPC)) { + w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), newTile, newTile->spawnNpc); } else { - ItemVector selected_items = new_tile->getSelectedItems(); - - Item* item = nullptr; - int count = 0; - for (ItemVector::iterator it = selected_items.begin(); it != selected_items.end(); ++it) { - ++count; - if ((*it)->isSelected()) { - item = *it; - } + const auto selectedItems = newTile->getSelectedItems(); + + const auto it = std::ranges::find_if(selectedItems | std::views::reverse, [&](const auto itItem) { + return itItem->isSelected(); + }); + + if (it == selectedItems.rend()) { + return; } - if (item) { - if (editor.getMap().getVersion().otbm >= MAP_OTBM_4) { - w = newd PropertiesWindow(g_gui.root, &editor.getMap(), new_tile, item); - } else { - w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), new_tile, item); - } + if (editor.getMap().getVersion().otbm >= MAP_OTBM_4) { + w = newd PropertiesWindow(g_gui.root, &editor.getMap(), newTile, *it); } else { - return; + w = newd OldPropertiesWindow(g_gui.root, &editor.getMap(), newTile, *it); } } - int ret = w->ShowModal(); - if (ret != 0) { - Action* action = editor.createAction(ACTION_CHANGE_PROPERTIES); - action->addChange(newd Change(new_tile)); + if (w->ShowModal() != 0) { + const auto action = editor.createAction(ACTION_CHANGE_PROPERTIES); + action->addChange(newd Change(newTile)); editor.addAction(action); } else { // Cancel! - delete new_tile; + delete newTile; } w->Destroy(); }