Skip to content

Commit

Permalink
fix: sonar cloud warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed Sep 28, 2024
1 parent 079410f commit b5d531a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 119 deletions.
7 changes: 3 additions & 4 deletions source/copybuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 12 additions & 13 deletions source/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Monster*>(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;
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions source/iomap_otbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(monsterNode.attribute("weight").as_uint());
if (weight == 0) {
weight = g_settings.getInteger(Config::MONSTER_DEFAULT_WEIGHT);
weight = static_cast<uint8_t>(g_settings.getInteger(Config::MONSTER_DEFAULT_WEIGHT));
}

Direction direction = NORTH;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions source/main_menubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
161 changes: 72 additions & 89 deletions source/map_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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() {
Expand Down Expand Up @@ -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<Monster*> 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<Monster*> 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();
}
Expand Down

0 comments on commit b5d531a

Please sign in to comment.